001
014
015 package com.liferay.portal.deploy.hot;
016
017 import com.liferay.portal.kernel.deploy.hot.BaseHotDeployListener;
018 import com.liferay.portal.kernel.deploy.hot.HotDeployEvent;
019 import com.liferay.portal.kernel.deploy.hot.HotDeployException;
020 import com.liferay.portal.kernel.log.Log;
021 import com.liferay.portal.kernel.log.LogFactoryUtil;
022 import com.liferay.portal.kernel.servlet.WebDirDetector;
023 import com.liferay.portal.kernel.servlet.taglib.FileAvailabilityUtil;
024 import com.liferay.portal.kernel.util.FileUtil;
025 import com.liferay.portal.kernel.util.HttpUtil;
026 import com.liferay.portal.kernel.util.StreamUtil;
027 import com.liferay.portal.kernel.util.StringBundler;
028 import com.liferay.portal.kernel.util.StringPool;
029 import com.liferay.portal.kernel.util.SystemProperties;
030 import com.liferay.portal.kernel.util.Time;
031 import com.liferay.portal.tools.WebXMLBuilder;
032 import com.liferay.portal.util.ExtRegistry;
033 import com.liferay.portal.util.PortalUtil;
034 import com.liferay.util.ant.CopyTask;
035
036 import java.io.File;
037 import java.io.FileOutputStream;
038 import java.io.InputStream;
039
040 import java.util.Iterator;
041 import java.util.Map;
042 import java.util.Set;
043
044 import javax.servlet.ServletContext;
045
046
049 public class ExtHotDeployListener extends BaseHotDeployListener {
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 extension environment for ",
060 t);
061 }
062 }
063
064 public void invokeUndeploy(HotDeployEvent hotDeployEvent)
065 throws HotDeployException {
066
067 try {
068 doInvokeUndeploy(hotDeployEvent);
069 }
070 catch (Throwable t) {
071 throwHotDeployException(
072 hotDeployEvent,
073 "Error unregistering extension environment for ", t);
074 }
075 }
076
077 protected void copyJar(
078 ServletContext servletContext, String dir, String jarName)
079 throws Exception {
080
081 String servletContextName = servletContext.getServletContextName();
082
083 String jarFullName = "/WEB-INF/" + jarName + "/" + jarName + ".jar";
084
085 InputStream is = servletContext.getResourceAsStream(jarFullName);
086
087 if (is == null) {
088 throw new HotDeployException(jarFullName + " does not exist");
089 }
090
091 String newJarFullName =
092 dir + "ext-" + servletContextName + jarName.substring(3) + ".jar";
093
094 StreamUtil.transfer(is, new FileOutputStream(new File(newJarFullName)));
095 }
096
097 protected void doInvokeDeploy(HotDeployEvent hotDeployEvent)
098 throws Exception {
099
100 ServletContext servletContext = hotDeployEvent.getServletContext();
101
102 String servletContextName = servletContext.getServletContextName();
103
104 if (_log.isDebugEnabled()) {
105 _log.debug("Invoking deploy for " + servletContextName);
106 }
107
108 String xml = HttpUtil.URLtoString(
109 servletContext.getResource(
110 "/WEB-INF/ext-" + servletContextName + ".xml"));
111
112 if (xml == null) {
113 return;
114 }
115
116 if (_log.isInfoEnabled()) {
117 _log.info(
118 "Registering extension environment for " + servletContextName);
119 }
120
121 if (ExtRegistry.isRegistered(servletContextName)) {
122 if (_log.isInfoEnabled()) {
123 _log.info(
124 "Extension environment for " + servletContextName +
125 " has been applied.");
126 }
127
128 return;
129 }
130
131 Map<String, Set<String>> conflicts = ExtRegistry.getConflicts(
132 servletContext);
133
134 if (!conflicts.isEmpty()) {
135 StringBundler sb = new StringBundler();
136
137 sb.append(
138 "Extension environment for " + servletContextName +
139 " cannot be applied because of detected conflicts:");
140
141 Iterator<Map.Entry<String, Set<String>>> itr =
142 conflicts.entrySet().iterator();
143
144 while (itr.hasNext()) {
145 Map.Entry<String, Set<String>> entry = itr.next();
146
147 String conflictServletContextName = entry.getKey();
148 Set<String> conflictFiles = entry.getValue();
149
150 sb.append("\n\t");
151 sb.append(conflictServletContextName);
152 sb.append(":");
153
154 for (String conflictFile : conflictFiles) {
155 sb.append("\n\t\t");
156 sb.append(conflictFile);
157 }
158 }
159
160 _log.error(sb.toString());
161
162 return;
163 }
164
165 installExt(servletContext, hotDeployEvent.getContextClassLoader());
166
167 FileAvailabilityUtil.reset();
168
169 if (_log.isInfoEnabled()) {
170 _log.info(
171 "Extension environment for " + servletContextName +
172 " has been applied. You must reboot the server and " +
173 "redeploy all other plugins.");
174 }
175 }
176
177 protected void doInvokeUndeploy(HotDeployEvent hotDeployEvent)
178 throws Exception {
179
180 ServletContext servletContext = hotDeployEvent.getServletContext();
181
182 String servletContextName = servletContext.getServletContextName();
183
184 if (_log.isDebugEnabled()) {
185 _log.debug("Invoking undeploy for " + servletContextName);
186 }
187
188 String xml = HttpUtil.URLtoString(
189 servletContext.getResource(
190 "/WEB-INF/ext-" + servletContextName + ".xml"));
191
192 if (xml == null) {
193 return;
194 }
195
196 if (_log.isInfoEnabled()) {
197 _log.info(
198 "Extension environment for " +
199 servletContextName + " will not be undeployed");
200 }
201 }
202
203 protected void installExt(
204 ServletContext servletContext, ClassLoader portletClassLoader)
205 throws Exception {
206
207 String servletContextName = servletContext.getServletContextName();
208
209 String globalLibDir = PortalUtil.getGlobalLibDir();
210 String portalWebDir = PortalUtil.getPortalWebDir();
211 String portalLibDir = PortalUtil.getPortalLibDir();
212 String pluginWebDir = WebDirDetector.getRootDir(portletClassLoader);
213
214 copyJar(servletContext, globalLibDir, "ext-service");
215 copyJar(servletContext, portalLibDir, "ext-impl");
216 copyJar(servletContext, portalLibDir, "ext-util-bridges");
217 copyJar(servletContext, portalLibDir, "ext-util-java");
218 copyJar(servletContext, portalLibDir, "ext-util-taglib");
219
220 mergeWebXml(portalWebDir, pluginWebDir);
221
222 CopyTask.copyDirectory(
223 pluginWebDir + "WEB-INF/ext-web/docroot", portalWebDir,
224 StringPool.BLANK, "**/WEB-INF/web.xml", true, false);
225
226 FileUtil.copyFile(
227 pluginWebDir + "WEB-INF/ext-" + servletContextName + ".xml",
228 portalWebDir + "WEB-INF/ext-" + servletContextName + ".xml");
229
230 ExtRegistry.registerExt(servletContext);
231 }
232
233 protected void mergeWebXml(String portalWebDir, String pluginWebDir) {
234 if (!FileUtil.exists(
235 pluginWebDir + "WEB-INF/ext-web/docroot/WEB-INF/web.xml")) {
236
237 return;
238 }
239
240 String tmpDir =
241 SystemProperties.get(SystemProperties.TMP_DIR) + StringPool.SLASH +
242 Time.getTimestamp();
243
244 WebXMLBuilder.main(
245 new String[] {
246 portalWebDir + "WEB-INF/web.xml",
247 pluginWebDir + "WEB-INF/ext-web/docroot/WEB-INF/web.xml",
248 tmpDir + "/web.xml"
249 });
250
251 File portalWebXml = new File(portalWebDir + "WEB-INF/web.xml");
252 File tmpWebXml = new File(tmpDir + "/web.xml");
253
254 tmpWebXml.setLastModified(portalWebXml.lastModified());
255
256 CopyTask.copyFile(
257 tmpWebXml, new File(portalWebDir + "WEB-INF"), true, true);
258
259 FileUtil.deltree(tmpDir);
260 }
261
262 private static Log _log = LogFactoryUtil.getLog(ExtHotDeployListener.class);
263
264 }