001    /**
002     * Copyright (c) 2000-present 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.portal.deploy.hot;
016    
017    import com.liferay.portal.kernel.cache.PortalCacheManagerNames;
018    import com.liferay.portal.kernel.cache.configurator.PortalCacheConfiguratorSettings;
019    import com.liferay.portal.kernel.configuration.Configuration;
020    import com.liferay.portal.kernel.configuration.ConfigurationFactoryUtil;
021    import com.liferay.portal.kernel.deploy.hot.BaseHotDeployListener;
022    import com.liferay.portal.kernel.deploy.hot.HotDeployEvent;
023    import com.liferay.portal.kernel.deploy.hot.HotDeployException;
024    import com.liferay.portal.kernel.log.Log;
025    import com.liferay.portal.kernel.log.LogFactoryUtil;
026    import com.liferay.portal.kernel.plugin.PluginPackage;
027    import com.liferay.portal.kernel.servlet.ServletContextPool;
028    import com.liferay.portal.kernel.util.GetterUtil;
029    import com.liferay.portal.kernel.util.PropsKeys;
030    import com.liferay.portal.kernel.util.Validator;
031    import com.liferay.portal.plugin.PluginPackageUtil;
032    import com.liferay.portal.service.ServiceComponentLocalServiceUtil;
033    import com.liferay.portal.service.configuration.ServiceComponentConfiguration;
034    import com.liferay.portal.service.configuration.servlet.ServletServiceContextComponentConfiguration;
035    import com.liferay.registry.Registry;
036    import com.liferay.registry.RegistryUtil;
037    import com.liferay.registry.ServiceRegistrar;
038    import com.liferay.util.log4j.Log4JUtil;
039    import com.liferay.util.portlet.PortletProps;
040    
041    import java.lang.reflect.Method;
042    
043    import java.net.URL;
044    
045    import java.util.HashMap;
046    import java.util.Map;
047    import java.util.Properties;
048    
049    import javax.servlet.ServletContext;
050    
051    /**
052     * @author Jorge Ferrer
053     */
054    public class PluginPackageHotDeployListener extends BaseHotDeployListener {
055    
056            public static final String SERVICE_BUILDER_PROPERTIES =
057                    "SERVICE_BUILDER_PROPERTIES";
058    
059            @Override
060            public void invokeDeploy(HotDeployEvent hotDeployEvent)
061                    throws HotDeployException {
062    
063                    try {
064                            doInvokeDeploy(hotDeployEvent);
065                    }
066                    catch (Throwable t) {
067                            throwHotDeployException(
068                                    hotDeployEvent, "Error registering plugins for ", t);
069                    }
070            }
071    
072            @Override
073            public void invokeUndeploy(HotDeployEvent hotDeployEvent)
074                    throws HotDeployException {
075    
076                    try {
077                            doInvokeUndeploy(hotDeployEvent);
078                    }
079                    catch (Throwable t) {
080                            throwHotDeployException(
081                                    hotDeployEvent, "Error unregistering plugins for ", t);
082                    }
083            }
084    
085            protected void destroyServiceComponent(
086                            ServiceComponentConfiguration serviceComponentConfiguration,
087                            ClassLoader classLoader)
088                    throws Exception {
089    
090                    ServiceComponentLocalServiceUtil.destroyServiceComponent(
091                            serviceComponentConfiguration, classLoader);
092            }
093    
094            protected void doInvokeDeploy(HotDeployEvent hotDeployEvent)
095                    throws Exception {
096    
097                    ServletContext servletContext = hotDeployEvent.getServletContext();
098    
099                    String servletContextName = servletContext.getServletContextName();
100    
101                    if (_log.isDebugEnabled()) {
102                            _log.debug("Invoking deploy for " + servletContextName);
103                    }
104    
105                    PluginPackage pluginPackage =
106                            PluginPackageUtil.readPluginPackageServletContext(servletContext);
107    
108                    if (pluginPackage == null) {
109                            return;
110                    }
111    
112                    if (servletContext.getResource(
113                                    "/WEB-INF/liferay-theme-loader.xml") != null) {
114    
115                            PluginPackageUtil.registerInstalledPluginPackage(pluginPackage);
116    
117                            return;
118                    }
119    
120                    pluginPackage.setContext(servletContextName);
121    
122                    hotDeployEvent.setPluginPackage(pluginPackage);
123    
124                    PluginPackageUtil.registerInstalledPluginPackage(pluginPackage);
125    
126                    ClassLoader classLoader = hotDeployEvent.getContextClassLoader();
127    
128                    initLogger(classLoader);
129                    initPortletProps(classLoader);
130                    initServiceComponent(servletContext, classLoader);
131    
132                    registerClpMessageListeners(servletContext, classLoader);
133    
134                    reconfigureCaches(servletContext, classLoader);
135    
136                    if (_log.isInfoEnabled()) {
137                            _log.info(
138                                    "Plugin package " + pluginPackage.getModuleId() +
139                                            " registered successfully. It's now ready to be used.");
140                    }
141            }
142    
143            protected void doInvokeUndeploy(HotDeployEvent hotDeployEvent)
144                    throws Exception {
145    
146                    ServletContext servletContext = hotDeployEvent.getServletContext();
147    
148                    String servletContextName = servletContext.getServletContextName();
149    
150                    if (_log.isDebugEnabled()) {
151                            _log.debug("Invoking undeploy for " + servletContextName);
152                    }
153    
154                    PluginPackage pluginPackage =
155                            PluginPackageUtil.readPluginPackageServletContext(servletContext);
156    
157                    if (pluginPackage == null) {
158                            return;
159                    }
160    
161                    hotDeployEvent.setPluginPackage(pluginPackage);
162    
163                    PluginPackageUtil.unregisterInstalledPluginPackage(pluginPackage);
164    
165                    ServletContextPool.remove(servletContextName);
166    
167                    destroyServiceComponent(
168                            new ServletServiceContextComponentConfiguration(servletContext),
169                            hotDeployEvent.getContextClassLoader());
170    
171                    unregisterClpMessageListeners(servletContext);
172    
173                    ServiceRegistrar<PortalCacheConfiguratorSettings> serviceRegistrar =
174                            (ServiceRegistrar<PortalCacheConfiguratorSettings>)
175                                    servletContext.getAttribute(
176                                            _PORTAL_CACHE_CONFIGURATOR_SETTINGS_SERVICE_REGISTAR);
177    
178                    if (serviceRegistrar != null) {
179                            serviceRegistrar.destroy();
180                    }
181    
182                    if (_log.isInfoEnabled()) {
183                            _log.info(
184                                    "Plugin package " + pluginPackage.getModuleId() +
185                                            " unregistered successfully");
186                    }
187            }
188    
189            protected URL getPortalCacheConfigurationURL(
190                    Configuration configuration, ClassLoader classLoader,
191                    String configLocation) {
192    
193                    String cacheConfigurationLocation = configuration.get(configLocation);
194    
195                    if (Validator.isNull(cacheConfigurationLocation)) {
196                            return null;
197                    }
198    
199                    return classLoader.getResource(cacheConfigurationLocation);
200            }
201    
202            protected void initLogger(ClassLoader classLoader) {
203                    Log4JUtil.configureLog4J(
204                            classLoader.getResource("META-INF/portal-log4j.xml"));
205            }
206    
207            protected void initPortletProps(ClassLoader classLoader) throws Exception {
208                    if (classLoader.getResourceAsStream("portlet.properties") == null) {
209                            return;
210                    }
211    
212                    Class<?> clazz = classLoader.loadClass(PortletProps.class.getName());
213    
214                    Method method = clazz.getMethod("get", String.class);
215    
216                    method.invoke(null, "init");
217            }
218    
219            protected void initServiceComponent(
220                            ServletContext servletContext, ClassLoader classLoader)
221                    throws Exception {
222    
223                    Configuration serviceBuilderPropertiesConfiguration = null;
224    
225                    try {
226                            serviceBuilderPropertiesConfiguration =
227                                    ConfigurationFactoryUtil.getConfiguration(
228                                            classLoader, "service");
229                    }
230                    catch (Exception e) {
231                            if (_log.isDebugEnabled()) {
232                                    _log.debug("Unable to read service.properties");
233                            }
234    
235                            return;
236                    }
237    
238                    Properties serviceBuilderProperties =
239                            serviceBuilderPropertiesConfiguration.getProperties();
240    
241                    if (serviceBuilderProperties.isEmpty()) {
242                            return;
243                    }
244    
245                    servletContext.setAttribute(
246                            SERVICE_BUILDER_PROPERTIES, serviceBuilderProperties);
247    
248                    String buildNamespace = GetterUtil.getString(
249                            serviceBuilderProperties.getProperty("build.namespace"));
250                    long buildNumber = GetterUtil.getLong(
251                            serviceBuilderProperties.getProperty("build.number"));
252                    long buildDate = GetterUtil.getLong(
253                            serviceBuilderProperties.getProperty("build.date"));
254                    boolean buildAutoUpgrade = GetterUtil.getBoolean(
255                            serviceBuilderProperties.getProperty("build.auto.upgrade"), true);
256    
257                    if (_log.isDebugEnabled()) {
258                            _log.debug("Build namespace " + buildNamespace);
259                            _log.debug("Build number " + buildNumber);
260                            _log.debug("Build date " + buildDate);
261                            _log.debug("Build auto upgrade " + buildAutoUpgrade);
262                    }
263    
264                    if (Validator.isNull(buildNamespace)) {
265                            return;
266                    }
267    
268                    ServiceComponentLocalServiceUtil.initServiceComponent(
269                            new ServletServiceContextComponentConfiguration(servletContext),
270                            classLoader, buildNamespace, buildNumber, buildDate,
271                            buildAutoUpgrade);
272            }
273    
274            protected void reconfigureCaches(
275                            ServletContext servletContext, ClassLoader classLoader)
276                    throws Exception {
277    
278                    Configuration portletPropertiesConfiguration = null;
279    
280                    try {
281                            portletPropertiesConfiguration =
282                                    ConfigurationFactoryUtil.getConfiguration(
283                                            classLoader, "portlet");
284                    }
285                    catch (Exception e) {
286                            if (_log.isDebugEnabled()) {
287                                    _log.debug("Unable to read portlet.properties");
288                            }
289    
290                            return;
291                    }
292    
293                    String singleVMConfigurationLocation =
294                            portletPropertiesConfiguration.get(
295                                    PropsKeys.EHCACHE_SINGLE_VM_CONFIG_LOCATION);
296                    String multiVMConfigurationLocation =
297                            portletPropertiesConfiguration.get(
298                                    PropsKeys.EHCACHE_MULTI_VM_CONFIG_LOCATION);
299    
300                    if (Validator.isNull(singleVMConfigurationLocation) &&
301                            Validator.isNull(multiVMConfigurationLocation)) {
302    
303                            return;
304                    }
305    
306                    Registry registry = RegistryUtil.getRegistry();
307    
308                    ServiceRegistrar<PortalCacheConfiguratorSettings> serviceRegistrar =
309                            registry.getServiceRegistrar(PortalCacheConfiguratorSettings.class);
310    
311                    if (Validator.isNotNull(singleVMConfigurationLocation)) {
312                            Map<String, Object> properties = new HashMap<>();
313    
314                            properties.put(
315                                    "portal.cache.manager.name", PortalCacheManagerNames.SINGLE_VM);
316    
317                            serviceRegistrar.registerService(
318                                    PortalCacheConfiguratorSettings.class,
319                                    new PortalCacheConfiguratorSettings(
320                                            classLoader, singleVMConfigurationLocation),
321                                    properties);
322                    }
323    
324                    if (Validator.isNotNull(multiVMConfigurationLocation)) {
325                            Map<String, Object> properties = new HashMap<>();
326    
327                            properties.put(
328                                    "portal.cache.manager.name", PortalCacheManagerNames.MULTI_VM);
329    
330                            serviceRegistrar.registerService(
331                                    PortalCacheConfiguratorSettings.class,
332                                    new PortalCacheConfiguratorSettings(
333                                            classLoader, multiVMConfigurationLocation),
334                                    properties);
335                    }
336    
337                    servletContext.setAttribute(
338                            _PORTAL_CACHE_CONFIGURATOR_SETTINGS_SERVICE_REGISTAR,
339                            serviceRegistrar);
340            }
341    
342            private static final String
343                    _PORTAL_CACHE_CONFIGURATOR_SETTINGS_SERVICE_REGISTAR =
344                            "PORTAL_CACHE_CONFIGURATOR_SETTINGS_SERVICE_REGISTAR";
345    
346            private static final Log _log = LogFactoryUtil.getLog(
347                    PluginPackageHotDeployListener.class);
348    
349    }