001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.util.bridges.alloy;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.messaging.MessageBusUtil;
020    import com.liferay.portal.kernel.messaging.MessageListener;
021    import com.liferay.portal.kernel.portlet.FriendlyURLMapper;
022    import com.liferay.portal.kernel.portlet.LiferayPortletConfig;
023    import com.liferay.portal.kernel.portlet.Router;
024    import com.liferay.portal.kernel.scheduler.SchedulerEngineHelperUtil;
025    import com.liferay.portal.kernel.scheduler.StorageType;
026    import com.liferay.portal.kernel.search.Indexer;
027    import com.liferay.portal.kernel.search.IndexerRegistryUtil;
028    import com.liferay.portal.kernel.servlet.HttpMethods;
029    import com.liferay.portal.kernel.util.JavaConstants;
030    import com.liferay.portal.kernel.util.ParamUtil;
031    import com.liferay.portal.kernel.util.StringBundler;
032    import com.liferay.portal.kernel.util.Validator;
033    import com.liferay.portal.model.Portlet;
034    
035    import java.io.IOException;
036    
037    import java.util.HashMap;
038    import java.util.HashSet;
039    import java.util.Map;
040    import java.util.Set;
041    
042    import javax.portlet.ActionRequest;
043    import javax.portlet.ActionResponse;
044    import javax.portlet.GenericPortlet;
045    import javax.portlet.PortletConfig;
046    import javax.portlet.PortletContext;
047    import javax.portlet.PortletException;
048    import javax.portlet.PortletRequest;
049    import javax.portlet.PortletRequestDispatcher;
050    import javax.portlet.PortletResponse;
051    import javax.portlet.RenderRequest;
052    import javax.portlet.RenderResponse;
053    import javax.portlet.ResourceRequest;
054    import javax.portlet.ResourceResponse;
055    
056    /**
057     * @author Brian Wing Shun Chan
058     */
059    public class AlloyPortlet extends GenericPortlet {
060    
061            @Override
062            public void destroy() {
063                    for (AlloyController alloyController : _alloyControllers) {
064                            BaseAlloyControllerImpl baseAlloyControllerImpl =
065                                    (BaseAlloyControllerImpl)alloyController;
066    
067                            Indexer indexer = baseAlloyControllerImpl.indexer;
068    
069                            if (indexer != null) {
070                                    IndexerRegistryUtil.unregister(indexer);
071                            }
072    
073                            MessageListener controllerMessageListener =
074                                    baseAlloyControllerImpl.controllerMessageListener;
075    
076                            if (controllerMessageListener != null) {
077                                    MessageBusUtil.removeDestination(
078                                            baseAlloyControllerImpl.getControllerDestinationName());
079                            }
080    
081                            MessageListener schedulerMessageListener =
082                                    baseAlloyControllerImpl.schedulerMessageListener;
083    
084                            if (schedulerMessageListener != null) {
085                                    try {
086                                            SchedulerEngineHelperUtil.unschedule(
087                                                    baseAlloyControllerImpl.getSchedulerJobName(),
088                                                    baseAlloyControllerImpl.getMessageListenerGroupName(),
089                                                    StorageType.MEMORY_CLUSTERED);
090    
091                                            MessageBusUtil.removeDestination(
092                                                    baseAlloyControllerImpl.getSchedulerDestinationName());
093                                    }
094                                    catch (Exception e) {
095                                            _log.error(e, e);
096                                    }
097                            }
098                    }
099            }
100    
101            @Override
102            public void init(PortletConfig portletConfig) throws PortletException {
103                    super.init(portletConfig);
104    
105                    LiferayPortletConfig liferayPortletConfig =
106                            (LiferayPortletConfig)portletConfig;
107    
108                    Portlet portlet = liferayPortletConfig.getPortlet();
109    
110                    FriendlyURLMapper friendlyURLMapper =
111                            portlet.getFriendlyURLMapperInstance();
112    
113                    Router router = friendlyURLMapper.getRouter();
114    
115                    router.urlToParameters(HttpMethods.GET, _defaultRouteParameters);
116            }
117    
118            @Override
119            public void processAction(
120                            ActionRequest actionRequest, ActionResponse actionResponse)
121                    throws IOException, PortletException {
122    
123                    String path = getPath(actionRequest);
124    
125                    include(path, actionRequest, actionResponse);
126            }
127    
128            @Override
129            public void render(
130                            RenderRequest renderRequest, RenderResponse renderResponse)
131                    throws IOException, PortletException {
132    
133                    String path = getPath(renderRequest);
134    
135                    include(path, renderRequest, renderResponse);
136            }
137    
138            @Override
139            public void serveResource(
140                            ResourceRequest resourceRequest, ResourceResponse resourceResponse)
141                    throws IOException, PortletException {
142    
143                    String path = getPath(resourceRequest);
144    
145                    include(path, resourceRequest, resourceResponse);
146            }
147    
148            protected Map<String, String> getDefaultRouteParameters() {
149                    /*Map<String, String> defaultRouteParameters =
150                            new HashMap<String, String[]>();
151    
152                    defaultRouteParameters.put("controller", new String[] {"assets"});
153                    defaultRouteParameters.put("action", new String[] {"index"});
154    
155                    return defaultRouteParameters;*/
156    
157                    return _defaultRouteParameters;
158            }
159    
160            protected String getPath(PortletRequest portletRequest) {
161                    LiferayPortletConfig liferayPortletConfig =
162                            (LiferayPortletConfig)portletRequest.getAttribute(
163                                    JavaConstants.JAVAX_PORTLET_CONFIG);
164    
165                    Portlet portlet = liferayPortletConfig.getPortlet();
166    
167                    String controllerPath = ParamUtil.getString(
168                            portletRequest, "controller");
169    
170                    if (Validator.isNull(controllerPath)) {
171                            Map<String, String> defaultRouteParameters =
172                                    getDefaultRouteParameters();
173    
174                            controllerPath = defaultRouteParameters.get("controller");
175                    }
176    
177                    StringBundler sb = new StringBundler(5);
178    
179                    sb.append("/WEB-INF/jsp/");
180                    sb.append(portlet.getFriendlyURLMapping());
181                    sb.append("/controllers/");
182                    sb.append(controllerPath);
183                    sb.append("_controller.jsp");
184    
185                    return sb.toString();
186            }
187    
188            protected void include(
189                            String path, PortletRequest portletRequest,
190                            PortletResponse portletResponse)
191                    throws IOException, PortletException {
192    
193                    PortletContext portletContext = getPortletContext();
194    
195                    PortletRequestDispatcher portletRequestDispatcher =
196                            portletContext.getRequestDispatcher(path);
197    
198                    if (portletRequestDispatcher == null) {
199                            _log.error(path + " is not a valid include");
200                    }
201                    else {
202                            portletRequestDispatcher.include(portletRequest, portletResponse);
203                    }
204            }
205    
206            protected void registerAlloyController(AlloyController alloyController) {
207                    if (!_alloyControllers.contains(alloyController)) {
208                            _alloyControllers.add(alloyController);
209                    }
210            }
211    
212            private static Log _log = LogFactoryUtil.getLog(AlloyPortlet.class);
213    
214            private Set<AlloyController> _alloyControllers =
215                    new HashSet<AlloyController>();
216            private Map<String, String> _defaultRouteParameters =
217                    new HashMap<String, String>();
218    
219    }