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