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.service.ServiceComponentLocalServiceUtil;
028    import com.liferay.portal.kernel.service.configuration.ServiceComponentConfiguration;
029    import com.liferay.portal.kernel.service.configuration.servlet.ServletServiceContextComponentConfiguration;
030    import com.liferay.portal.kernel.servlet.ServletContextPool;
031    import com.liferay.portal.kernel.util.GetterUtil;
032    import com.liferay.portal.kernel.util.PropsKeys;
033    import com.liferay.portal.kernel.util.Validator;
034    import com.liferay.portal.plugin.PluginPackageUtil;
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                    reconfigureCaches(servletContext, classLoader);
133    
134                    if (_log.isInfoEnabled()) {
135                            _log.info(
136                                    "Plugin package " + pluginPackage.getModuleId() +
137                                            " registered successfully. It's now ready to be used.");
138                    }
139            }
140    
141            protected void doInvokeUndeploy(HotDeployEvent hotDeployEvent)
142                    throws Exception {
143    
144                    ServletContext servletContext = hotDeployEvent.getServletContext();
145    
146                    String servletContextName = servletContext.getServletContextName();
147    
148                    if (_log.isDebugEnabled()) {
149                            _log.debug("Invoking undeploy for " + servletContextName);
150                    }
151    
152                    PluginPackage pluginPackage =
153                            PluginPackageUtil.readPluginPackageServletContext(servletContext);
154    
155                    if (pluginPackage == null) {
156                            return;
157                    }
158    
159                    hotDeployEvent.setPluginPackage(pluginPackage);
160    
161                    PluginPackageUtil.unregisterInstalledPluginPackage(pluginPackage);
162    
163                    ServletContextPool.remove(servletContextName);
164    
165                    destroyServiceComponent(
166                            new ServletServiceContextComponentConfiguration(servletContext),
167                            hotDeployEvent.getContextClassLoader());
168    
169                    ServiceRegistrar<PortalCacheConfiguratorSettings> serviceRegistrar =
170                            (ServiceRegistrar<PortalCacheConfiguratorSettings>)
171                                    servletContext.getAttribute(
172                                            _PORTAL_CACHE_CONFIGURATOR_SETTINGS_SERVICE_REGISTAR);
173    
174                    if (serviceRegistrar != null) {
175                            serviceRegistrar.destroy();
176                    }
177    
178                    if (_log.isInfoEnabled()) {
179                            _log.info(
180                                    "Plugin package " + pluginPackage.getModuleId() +
181                                            " unregistered successfully");
182                    }
183            }
184    
185            protected URL getPortalCacheConfigurationURL(
186                    Configuration configuration, ClassLoader classLoader,
187                    String configLocation) {
188    
189                    String cacheConfigurationLocation = configuration.get(configLocation);
190    
191                    if (Validator.isNull(cacheConfigurationLocation)) {
192                            return null;
193                    }
194    
195                    return classLoader.getResource(cacheConfigurationLocation);
196            }
197    
198            protected void initLogger(ClassLoader classLoader) {
199                    Log4JUtil.configureLog4J(
200                            classLoader.getResource("META-INF/portal-log4j.xml"));
201            }
202    
203            protected void initPortletProps(ClassLoader classLoader) throws Exception {
204                    if (classLoader.getResourceAsStream("portlet.properties") == null) {
205                            return;
206                    }
207    
208                    Class<?> clazz = classLoader.loadClass(PortletProps.class.getName());
209    
210                    Method method = clazz.getMethod("get", String.class);
211    
212                    method.invoke(null, "init");
213            }
214    
215            protected void initServiceComponent(
216                            ServletContext servletContext, ClassLoader classLoader)
217                    throws Exception {
218    
219                    Configuration serviceBuilderPropertiesConfiguration = null;
220    
221                    try {
222                            serviceBuilderPropertiesConfiguration =
223                                    ConfigurationFactoryUtil.getConfiguration(
224                                            classLoader, "service");
225                    }
226                    catch (Exception e) {
227                            if (_log.isDebugEnabled()) {
228                                    _log.debug("Unable to read service.properties");
229                            }
230    
231                            return;
232                    }
233    
234                    Properties serviceBuilderProperties =
235                            serviceBuilderPropertiesConfiguration.getProperties();
236    
237                    if (serviceBuilderProperties.isEmpty()) {
238                            return;
239                    }
240    
241                    servletContext.setAttribute(
242                            SERVICE_BUILDER_PROPERTIES, serviceBuilderProperties);
243    
244                    String buildNamespace = GetterUtil.getString(
245                            serviceBuilderProperties.getProperty("build.namespace"));
246                    long buildNumber = GetterUtil.getLong(
247                            serviceBuilderProperties.getProperty("build.number"));
248                    long buildDate = GetterUtil.getLong(
249                            serviceBuilderProperties.getProperty("build.date"));
250                    boolean buildAutoUpgrade = GetterUtil.getBoolean(
251                            serviceBuilderProperties.getProperty("build.auto.upgrade"), true);
252    
253                    if (_log.isDebugEnabled()) {
254                            _log.debug("Build namespace " + buildNamespace);
255                            _log.debug("Build number " + buildNumber);
256                            _log.debug("Build date " + buildDate);
257                            _log.debug("Build auto upgrade " + buildAutoUpgrade);
258                    }
259    
260                    if (Validator.isNull(buildNamespace)) {
261                            return;
262                    }
263    
264                    ServiceComponentLocalServiceUtil.initServiceComponent(
265                            new ServletServiceContextComponentConfiguration(servletContext),
266                            classLoader, buildNamespace, buildNumber, buildDate,
267                            buildAutoUpgrade);
268            }
269    
270            protected void reconfigureCaches(
271                            ServletContext servletContext, ClassLoader classLoader)
272                    throws Exception {
273    
274                    Configuration portletPropertiesConfiguration = null;
275    
276                    try {
277                            portletPropertiesConfiguration =
278                                    ConfigurationFactoryUtil.getConfiguration(
279                                            classLoader, "portlet");
280                    }
281                    catch (Exception e) {
282                            if (_log.isDebugEnabled()) {
283                                    _log.debug("Unable to read portlet.properties");
284                            }
285    
286                            return;
287                    }
288    
289                    String singleVMConfigurationLocation =
290                            portletPropertiesConfiguration.get(
291                                    PropsKeys.EHCACHE_SINGLE_VM_CONFIG_LOCATION);
292                    String multiVMConfigurationLocation =
293                            portletPropertiesConfiguration.get(
294                                    PropsKeys.EHCACHE_MULTI_VM_CONFIG_LOCATION);
295    
296                    if (Validator.isNull(singleVMConfigurationLocation) &&
297                            Validator.isNull(multiVMConfigurationLocation)) {
298    
299                            return;
300                    }
301    
302                    Registry registry = RegistryUtil.getRegistry();
303    
304                    ServiceRegistrar<PortalCacheConfiguratorSettings> serviceRegistrar =
305                            registry.getServiceRegistrar(PortalCacheConfiguratorSettings.class);
306    
307                    if (Validator.isNotNull(singleVMConfigurationLocation)) {
308                            Map<String, Object> properties = new HashMap<>();
309    
310                            properties.put(
311                                    "portal.cache.manager.name", PortalCacheManagerNames.SINGLE_VM);
312    
313                            serviceRegistrar.registerService(
314                                    PortalCacheConfiguratorSettings.class,
315                                    new PortalCacheConfiguratorSettings(
316                                            classLoader, singleVMConfigurationLocation),
317                                    properties);
318                    }
319    
320                    if (Validator.isNotNull(multiVMConfigurationLocation)) {
321                            Map<String, Object> properties = new HashMap<>();
322    
323                            properties.put(
324                                    "portal.cache.manager.name", PortalCacheManagerNames.MULTI_VM);
325    
326                            serviceRegistrar.registerService(
327                                    PortalCacheConfiguratorSettings.class,
328                                    new PortalCacheConfiguratorSettings(
329                                            classLoader, multiVMConfigurationLocation),
330                                    properties);
331                    }
332    
333                    servletContext.setAttribute(
334                            _PORTAL_CACHE_CONFIGURATOR_SETTINGS_SERVICE_REGISTAR,
335                            serviceRegistrar);
336            }
337    
338            private static final String
339                    _PORTAL_CACHE_CONFIGURATOR_SETTINGS_SERVICE_REGISTAR =
340                            "PORTAL_CACHE_CONFIGURATOR_SETTINGS_SERVICE_REGISTAR";
341    
342            private static final Log _log = LogFactoryUtil.getLog(
343                    PluginPackageHotDeployListener.class);
344    
345    }