001    /**
002     * Copyright (c) 2000-2012 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.dao.orm.hibernate.region.LiferayEhcacheRegionFactory;
018    import com.liferay.portal.dao.orm.hibernate.region.SingletonLiferayEhcacheRegionFactory;
019    import com.liferay.portal.kernel.bean.PortalBeanLocatorUtil;
020    import com.liferay.portal.kernel.cache.PortalCacheManager;
021    import com.liferay.portal.kernel.configuration.Configuration;
022    import com.liferay.portal.kernel.configuration.ConfigurationFactoryUtil;
023    import com.liferay.portal.kernel.deploy.hot.BaseHotDeployListener;
024    import com.liferay.portal.kernel.deploy.hot.HotDeployEvent;
025    import com.liferay.portal.kernel.deploy.hot.HotDeployException;
026    import com.liferay.portal.kernel.log.Log;
027    import com.liferay.portal.kernel.log.LogFactoryUtil;
028    import com.liferay.portal.kernel.plugin.PluginPackage;
029    import com.liferay.portal.kernel.servlet.PortletServlet;
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.portal.service.ServiceComponentLocalServiceUtil;
036    
037    import java.net.URL;
038    
039    import java.util.Properties;
040    
041    import javax.servlet.ServletContext;
042    
043    /**
044     * @author Jorge Ferrer
045     */
046    public class PluginPackageHotDeployListener extends BaseHotDeployListener {
047    
048            public static final String SERVICE_BUILDER_PROPERTIES =
049                    "SERVICE_BUILDER_PROPERTIES";
050    
051            public void invokeDeploy(HotDeployEvent hotDeployEvent)
052                    throws HotDeployException {
053    
054                    try {
055                            doInvokeDeploy(hotDeployEvent);
056                    }
057                    catch (Throwable t) {
058                            throwHotDeployException(
059                                    hotDeployEvent, "Error registering plugins for ", t);
060                    }
061            }
062    
063            public void invokeUndeploy(HotDeployEvent hotDeployEvent)
064                    throws HotDeployException {
065    
066                    try {
067                            doInvokeUndeploy(hotDeployEvent);
068                    }
069                    catch (Throwable t) {
070                            throwHotDeployException(
071                                    hotDeployEvent, "Error unregistering plugins for ", t);
072                    }
073            }
074    
075            protected void destroyServiceComponent(
076                            ServletContext servletContext, ClassLoader classLoader)
077                    throws Exception {
078    
079                    ServiceComponentLocalServiceUtil.destroyServiceComponent(
080                            servletContext, classLoader);
081            }
082    
083            protected void doInvokeDeploy(HotDeployEvent hotDeployEvent)
084                    throws Exception {
085    
086                    ServletContext servletContext = hotDeployEvent.getServletContext();
087    
088                    String servletContextName = servletContext.getServletContextName();
089    
090                    if (_log.isDebugEnabled()) {
091                            _log.debug("Invoking deploy for " + servletContextName);
092                    }
093    
094                    if (servletContext.getResource(
095                                    "/WEB-INF/liferay-theme-loader.xml") != null) {
096    
097                            return;
098                    }
099    
100                    PluginPackage pluginPackage =
101                            PluginPackageUtil.readPluginPackageServletContext(servletContext);
102    
103                    if (pluginPackage == null) {
104                            return;
105                    }
106    
107                    pluginPackage.setContext(servletContextName);
108    
109                    hotDeployEvent.setPluginPackage(pluginPackage);
110    
111                    PluginPackageUtil.registerInstalledPluginPackage(pluginPackage);
112    
113                    ClassLoader portletClassLoader = hotDeployEvent.getContextClassLoader();
114    
115                    servletContext.setAttribute(
116                            PortletServlet.PORTLET_CLASS_LOADER, portletClassLoader);
117    
118                    ServletContextPool.put(servletContextName, servletContext);
119    
120                    initServiceComponent(servletContext, portletClassLoader);
121    
122                    registerClpMessageListeners(servletContext, portletClassLoader);
123    
124                    reconfigureCaches(portletClassLoader);
125    
126                    if (_log.isInfoEnabled()) {
127                            _log.info(
128                                    "Plugin package " + pluginPackage.getModuleId() +
129                                            " registered successfully. It's now ready to be used.");
130                    }
131            }
132    
133            protected void doInvokeUndeploy(HotDeployEvent hotDeployEvent)
134                    throws Exception {
135    
136                    ServletContext servletContext = hotDeployEvent.getServletContext();
137    
138                    String servletContextName = servletContext.getServletContextName();
139    
140                    if (_log.isDebugEnabled()) {
141                            _log.debug("Invoking deploy for " + servletContextName);
142                    }
143    
144                    PluginPackage pluginPackage =
145                            PluginPackageUtil.readPluginPackageServletContext(servletContext);
146    
147                    if (pluginPackage == null) {
148                            return;
149                    }
150    
151                    hotDeployEvent.setPluginPackage(pluginPackage);
152    
153                    PluginPackageUtil.unregisterInstalledPluginPackage(pluginPackage);
154    
155                    ServletContextPool.remove(servletContextName);
156    
157                    destroyServiceComponent(
158                            servletContext, hotDeployEvent.getContextClassLoader());
159    
160                    unregisterClpMessageListeners(servletContext);
161    
162                    if (_log.isInfoEnabled()) {
163                            _log.info(
164                                    "Plugin package " + pluginPackage.getModuleId() +
165                                            " unregistered successfully");
166                    }
167            }
168    
169            protected void initServiceComponent(
170                            ServletContext servletContext, ClassLoader classLoader)
171                    throws Exception {
172    
173                    Configuration serviceBuilderPropertiesConfiguration = null;
174    
175                    try {
176                            serviceBuilderPropertiesConfiguration =
177                                    ConfigurationFactoryUtil.getConfiguration(
178                                            classLoader, "service");
179                    }
180                    catch (Exception e) {
181                            if (_log.isDebugEnabled()) {
182                                    _log.debug("Unable to read service.properties");
183                            }
184    
185                            return;
186                    }
187    
188                    Properties serviceBuilderProperties =
189                            serviceBuilderPropertiesConfiguration.getProperties();
190    
191                    if (serviceBuilderProperties.size() == 0) {
192                            return;
193                    }
194    
195                    servletContext.setAttribute(
196                            SERVICE_BUILDER_PROPERTIES, serviceBuilderProperties);
197    
198                    String buildNamespace = GetterUtil.getString(
199                            serviceBuilderProperties.getProperty("build.namespace"));
200                    long buildNumber = GetterUtil.getLong(
201                            serviceBuilderProperties.getProperty("build.number"));
202                    long buildDate = GetterUtil.getLong(
203                            serviceBuilderProperties.getProperty("build.date"));
204                    boolean buildAutoUpgrade = GetterUtil.getBoolean(
205                            serviceBuilderProperties.getProperty("build.auto.upgrade"), true);
206    
207                    if (_log.isDebugEnabled()) {
208                            _log.debug("Build namespace " + buildNamespace);
209                            _log.debug("Build number " + buildNumber);
210                            _log.debug("Build date " + buildDate);
211                            _log.debug("Build auto upgrade " + buildAutoUpgrade);
212                    }
213    
214                    if (Validator.isNull(buildNamespace)) {
215                            return;
216                    }
217    
218                    ServiceComponentLocalServiceUtil.initServiceComponent(
219                            servletContext, classLoader, buildNamespace, buildNumber, buildDate,
220                            buildAutoUpgrade);
221            }
222    
223            protected void reconfigureCaches(ClassLoader classLoader) throws Exception {
224                    Configuration portletPropertiesConfiguration = null;
225    
226                    try {
227                            portletPropertiesConfiguration =
228                                    ConfigurationFactoryUtil.getConfiguration(
229                                            classLoader, "portlet");
230                    }
231                    catch (Exception e) {
232                            if (_log.isDebugEnabled()) {
233                                    _log.debug("Unable to read portlet.properties");
234                            }
235    
236                            return;
237                    }
238    
239                    String cacheConfigurationLocation = portletPropertiesConfiguration.get(
240                            PropsKeys.EHCACHE_SINGLE_VM_CONFIG_LOCATION);
241    
242                    reconfigureCaches(
243                            classLoader, cacheConfigurationLocation,
244                            _SINGLE_VM_PORTAL_CACHE_MANAGER_BEAN_NAME);
245    
246                    String clusterCacheConfigurationLocation =
247                            portletPropertiesConfiguration.get(
248                                    PropsKeys.EHCACHE_MULTI_VM_CONFIG_LOCATION);
249    
250                    reconfigureCaches(
251                            classLoader, clusterCacheConfigurationLocation,
252                            _MULTI_VM_PORTAL_CACHE_MANAGER_BEAN_NAME);
253    
254                    String hibernateCacheConfigurationPath =
255                            portletPropertiesConfiguration.get(
256                                    PropsKeys.NET_SF_EHCACHE_CONFIGURATION_RESOURCE_NAME);
257    
258                    reconfigureHibernateCache(classLoader, hibernateCacheConfigurationPath);
259            }
260    
261            protected void reconfigureCaches(
262                            ClassLoader classLoader, String cacheConfigurationPath,
263                            String portalCacheManagerBeanId)
264                    throws Exception {
265    
266                    if (Validator.isNull(cacheConfigurationPath)) {
267                            return;
268                    }
269    
270                    URL cacheConfigurationURL = classLoader.getResource(
271                            cacheConfigurationPath);
272    
273                    if (cacheConfigurationURL != null) {
274                            PortalCacheManager portalCacheManager =
275                                    (PortalCacheManager)PortalBeanLocatorUtil.locate(
276                                            portalCacheManagerBeanId);
277    
278                            if (_log.isInfoEnabled()) {
279                                    _log.info(
280                                            "Reconfiguring caches in cache manager " +
281                                                    portalCacheManagerBeanId + " using " +
282                                                            cacheConfigurationURL);
283                            }
284    
285                            portalCacheManager.reconfigureCaches(cacheConfigurationURL);
286                    }
287            }
288    
289            protected void reconfigureHibernateCache(
290                    ClassLoader classLoader, String hibernateCacheConfigurationPath) {
291    
292                    if (Validator.isNull(hibernateCacheConfigurationPath)) {
293                            return;
294                    }
295    
296                    LiferayEhcacheRegionFactory liferayEhcacheRegionFactory =
297                            SingletonLiferayEhcacheRegionFactory.getInstance();
298    
299                    URL configurationFile = classLoader.getResource(
300                            hibernateCacheConfigurationPath);
301    
302                    if (Validator.isNotNull(configurationFile)) {
303                            if (_log.isInfoEnabled()) {
304                                    _log.info(
305                                            "Reconfiguring Hibernate caches using " +
306                                                    configurationFile);
307                            }
308    
309                            liferayEhcacheRegionFactory.reconfigureCaches(configurationFile);
310                    }
311            }
312    
313            private static final String _MULTI_VM_PORTAL_CACHE_MANAGER_BEAN_NAME =
314                    "com.liferay.portal.kernel.cache.MultiVMPortalCacheManager";
315    
316            private static final String _SINGLE_VM_PORTAL_CACHE_MANAGER_BEAN_NAME =
317                    "com.liferay.portal.kernel.cache.SingleVMPortalCacheManager";
318    
319            private static Log _log = LogFactoryUtil.getLog(
320                    PluginPackageHotDeployListener.class);
321    
322    }