001
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
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,
220 buildDate, 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(
259 classLoader, hibernateCacheConfigurationPath);
260 }
261
262 protected void reconfigureCaches(
263 ClassLoader classLoader, String cacheConfigurationPath,
264 String portalCacheManagerBeanId)
265 throws Exception {
266
267 if (Validator.isNull(cacheConfigurationPath)) {
268 return;
269 }
270
271 URL cacheConfigurationURL = classLoader.getResource(
272 cacheConfigurationPath);
273
274 if (cacheConfigurationURL != null) {
275 PortalCacheManager portalCacheManager =
276 (PortalCacheManager)PortalBeanLocatorUtil.locate(
277 portalCacheManagerBeanId);
278
279 if (_log.isInfoEnabled()) {
280 _log.info(
281 "Reconfiguring caches in cache manager " +
282 portalCacheManagerBeanId + " using " +
283 cacheConfigurationURL);
284 }
285
286 portalCacheManager.reconfigureCaches(cacheConfigurationURL);
287 }
288 }
289
290 protected void reconfigureHibernateCache(
291 ClassLoader classLoader, String hibernateCacheConfigurationPath) {
292
293 if (Validator.isNull(hibernateCacheConfigurationPath)) {
294 return;
295 }
296
297 LiferayEhcacheRegionFactory liferayEhcacheRegionFactory =
298 SingletonLiferayEhcacheRegionFactory.getInstance();
299
300 URL configurationFile = classLoader.getResource(
301 hibernateCacheConfigurationPath);
302
303 if (Validator.isNotNull(configurationFile)) {
304 if (_log.isInfoEnabled()) {
305 _log.info(
306 "Reconfiguring Hibernate caches using " +
307 configurationFile);
308 }
309
310 liferayEhcacheRegionFactory.reconfigureCaches(configurationFile);
311 }
312 }
313
314 private static final String _MULTI_VM_PORTAL_CACHE_MANAGER_BEAN_NAME =
315 "com.liferay.portal.kernel.cache.MultiVMPortalCacheManager";
316
317 private static final String _SINGLE_VM_PORTAL_CACHE_MANAGER_BEAN_NAME =
318 "com.liferay.portal.kernel.cache.SingleVMPortalCacheManager";
319
320 private static Log _log = LogFactoryUtil.getLog(
321 PluginPackageHotDeployListener.class);
322
323 }