1
22
23 package com.liferay.portal.deploy.hot;
24
25 import com.liferay.portal.kernel.deploy.hot.HotDeployEvent;
26 import com.liferay.portal.kernel.deploy.hot.HotDeployException;
27 import com.liferay.portal.kernel.util.HttpUtil;
28 import com.liferay.portal.kernel.util.PortalClassLoaderUtil;
29 import com.liferay.portal.lastmodified.LastModifiedCSS;
30 import com.liferay.portal.lastmodified.LastModifiedJavaScript;
31 import com.liferay.portal.service.ThemeLocalServiceUtil;
32 import com.liferay.portal.velocity.LiferayResourceCacheUtil;
33 import com.liferay.portal.velocity.VelocityContextPool;
34
35 import java.util.HashMap;
36 import java.util.List;
37 import java.util.Map;
38
39 import javax.servlet.ServletContext;
40
41 import org.apache.commons.logging.Log;
42 import org.apache.commons.logging.LogFactory;
43
44
52 public class ThemeHotDeployListener extends BaseHotDeployListener {
53
54 public void invokeDeploy(HotDeployEvent event) throws HotDeployException {
55 try {
56 doInvokeDeploy(event);
57 }
58 catch (Exception e) {
59 throwHotDeployException(event, "Error registering themes for ", e);
60 }
61 }
62
63 public void invokeUndeploy(HotDeployEvent event) throws HotDeployException {
64 try {
65 doInvokeUndeploy(event);
66 }
67 catch (Exception e) {
68 throwHotDeployException(
69 event, "Error unregistering themes for ", e);
70 }
71 }
72
73 protected void doInvokeDeploy(HotDeployEvent event) throws Exception {
74 ServletContext servletContext = event.getServletContext();
75
76 String servletContextName = servletContext.getServletContextName();
77
78 if (_log.isDebugEnabled()) {
79 _log.debug("Invoking deploy for " + servletContextName);
80 }
81
82 String[] xmls = new String[] {
83 HttpUtil.URLtoString(servletContext.getResource(
84 "/WEB-INF/liferay-look-and-feel.xml"))
85 };
86
87 if (xmls[0] == null) {
88 return;
89 }
90
91 if (_log.isInfoEnabled()) {
92 _log.info("Registering themes for " + servletContextName);
93 }
94
95 List<String> themeIds = ThemeLocalServiceUtil.init(
96 servletContextName, servletContext, null, true, xmls,
97 event.getPluginPackage());
98
99 VelocityContextPool.put(servletContextName, servletContext);
100
101 _vars.put(servletContextName, themeIds);
102
103 if (_log.isInfoEnabled()) {
104 _log.info(
105 "Themes for " + servletContextName +
106 " registered successfully");
107 }
108 }
109
110 protected void doInvokeUndeploy(HotDeployEvent event) throws Exception {
111 ServletContext servletContext = event.getServletContext();
112
113 String servletContextName = servletContext.getServletContextName();
114
115 if (_log.isDebugEnabled()) {
116 _log.debug("Invoking undeploy for " + servletContextName);
117 }
118
119 List<String> themeIds = _vars.remove(servletContextName);
120
121 if (themeIds != null) {
122 if (_log.isInfoEnabled()) {
123 _log.info("Unregistering themes for " + servletContextName);
124 }
125
126 try {
127 ThemeLocalServiceUtil.uninstallThemes(themeIds);
128 }
129 catch (Exception e) {
130 _log.error(e, e);
131 }
132 }
133 else {
134 return;
135 }
136
137
139 ClassLoader contextClassLoader =
140 Thread.currentThread().getContextClassLoader();
141
142 try {
143 Thread.currentThread().setContextClassLoader(
144 PortalClassLoaderUtil.getClassLoader());
145
146 VelocityContextPool.remove(servletContextName);
147
148 LiferayResourceCacheUtil.clear();
149
150 LastModifiedCSS.clear();
151 LastModifiedJavaScript.clear();
152 }
153 finally {
154 Thread.currentThread().setContextClassLoader(contextClassLoader);
155 }
156
157 if (_log.isInfoEnabled()) {
158 _log.info(
159 "Themes for " + servletContextName +
160 " unregistered successfully");
161 }
162 }
163
164 private static Log _log = LogFactory.getLog(ThemeHotDeployListener.class);
165
166 private static Map<String, List<String>> _vars =
167 new HashMap<String, List<String>>();
168
169 }