001    /**
002     * Copyright (c) 2000-2011 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.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    /**
047     * @author Brian Wing Shun Chan
048     */
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 installExt(
098                            ServletContext servletContext, ClassLoader portletClassLoader)
099                    throws Exception {
100    
101                    String servletContextName = servletContext.getServletContextName();
102    
103                    String globalLibDir = PortalUtil.getGlobalLibDir();
104                    String portalWebDir = PortalUtil.getPortalWebDir();
105                    String portalLibDir = PortalUtil.getPortalLibDir();
106                    String pluginWebDir = WebDirDetector.getRootDir(portletClassLoader);
107    
108                    copyJar(servletContext, globalLibDir, "ext-service");
109                    copyJar(servletContext, portalLibDir, "ext-impl");
110                    copyJar(servletContext, portalLibDir, "ext-util-bridges");
111                    copyJar(servletContext, portalLibDir, "ext-util-java");
112                    copyJar(servletContext, portalLibDir, "ext-util-taglib");
113    
114                    mergeWebXml(portalWebDir, pluginWebDir);
115    
116                    CopyTask.copyDirectory(
117                            pluginWebDir + "WEB-INF/ext-web/docroot", portalWebDir,
118                            StringPool.BLANK, "**/WEB-INF/web.xml", true, false);
119    
120                    FileUtil.copyFile(
121                            pluginWebDir + "WEB-INF/ext-" + servletContextName + ".xml",
122                            portalWebDir + "WEB-INF/ext-" + servletContextName + ".xml");
123    
124                    ExtRegistry.registerExt(servletContext);
125            }
126    
127            protected void doInvokeDeploy(HotDeployEvent hotDeployEvent)
128                    throws Exception {
129    
130                    ServletContext servletContext = hotDeployEvent.getServletContext();
131    
132                    String servletContextName = servletContext.getServletContextName();
133    
134                    if (_log.isDebugEnabled()) {
135                            _log.debug("Invoking deploy for " + servletContextName);
136                    }
137    
138                    String xml = HttpUtil.URLtoString(
139                            servletContext.getResource(
140                                    "/WEB-INF/ext-" + servletContextName + ".xml"));
141    
142                    if (xml == null) {
143                            return;
144                    }
145    
146                    logRegistration(servletContextName);
147    
148                    if (ExtRegistry.isRegistered(servletContextName)) {
149                            if (_log.isInfoEnabled()) {
150                                    _log.info(
151                                            "Extension environment for " + servletContextName +
152                                                    " has been applied.");
153                            }
154    
155                            return;
156                    }
157    
158                    Map<String, Set<String>> conflicts = ExtRegistry.getConflicts(
159                            servletContext);
160    
161                    if (!conflicts.isEmpty()) {
162                            StringBundler sb = new StringBundler();
163    
164                            sb.append(
165                                    "Extension environment for " + servletContextName +
166                                            " cannot be applied because of detected conflicts:");
167    
168                            Iterator<Map.Entry<String, Set<String>>> itr =
169                                    conflicts.entrySet().iterator();
170    
171                            while (itr.hasNext()) {
172                                    Map.Entry<String, Set<String>> entry = itr.next();
173    
174                                    String conflictServletContextName = entry.getKey();
175                                    Set<String> conflictFiles = entry.getValue();
176    
177                                    sb.append("\n\t");
178                                    sb.append(conflictServletContextName);
179                                    sb.append(":");
180    
181                                    for (String conflictFile : conflictFiles) {
182                                            sb.append("\n\t\t");
183                                            sb.append(conflictFile);
184                                    }
185                            }
186    
187                            _log.error(sb.toString());
188    
189                            return;
190                    }
191    
192                    installExt(servletContext, hotDeployEvent.getContextClassLoader());
193    
194                    FileAvailabilityUtil.reset();
195    
196                    if (_log.isInfoEnabled()) {
197                            _log.info(
198                                    "Extension environment for " + servletContextName +
199                                            " has been applied. You must reboot the server and " +
200                                                    "redeploy all other plugins.");
201                    }
202            }
203    
204            protected void doInvokeUndeploy(HotDeployEvent hotDeployEvent)
205                    throws Exception {
206    
207                    ServletContext servletContext = hotDeployEvent.getServletContext();
208    
209                    String servletContextName = servletContext.getServletContextName();
210    
211                    if (_log.isDebugEnabled()) {
212                            _log.debug("Invoking undeploy for " + servletContextName);
213                    }
214    
215                    String xml = HttpUtil.URLtoString(
216                            servletContext.getResource(
217                                    "/WEB-INF/ext-" + servletContextName + ".xml"));
218    
219                    if (xml == null) {
220                            return;
221                    }
222    
223                    if (_log.isInfoEnabled()) {
224                            _log.info(
225                                    "Extension environment for " +
226                                            servletContextName + " will not be undeployed");
227                    }
228            }
229    
230            protected void logRegistration(String servletContextName) {
231                    if (_log.isInfoEnabled()) {
232                            _log.info(
233                                    "Registering extension environment for " + servletContextName);
234                    }
235            }
236    
237            protected void mergeWebXml(String portalWebDir, String pluginWebDir) {
238                    if (!FileUtil.exists(
239                                    pluginWebDir + "WEB-INF/ext-web/docroot/WEB-INF/web.xml")) {
240    
241                            return;
242                    }
243    
244                    String tmpDir =
245                            SystemProperties.get(SystemProperties.TMP_DIR) + StringPool.SLASH +
246                                    Time.getTimestamp();
247    
248                    WebXMLBuilder.main(
249                            new String[] {
250                                    portalWebDir + "WEB-INF/web.xml",
251                                    pluginWebDir + "WEB-INF/ext-web/docroot/WEB-INF/web.xml",
252                                    tmpDir + "/web.xml"
253                            });
254    
255                    File portalWebXml = new File(portalWebDir + "WEB-INF/web.xml");
256                    File tmpWebXml = new File(tmpDir + "/web.xml");
257    
258                    tmpWebXml.setLastModified(portalWebXml.lastModified());
259    
260                    CopyTask.copyFile(
261                            tmpWebXml, new File(portalWebDir + "WEB-INF"), true, true);
262    
263                    FileUtil.deltree(tmpDir);
264            }
265    
266            private static Log _log = LogFactoryUtil.getLog(ExtHotDeployListener.class);
267    
268    }