001    /**
002     * Copyright (c) 2000-present 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.util.FileUtil;
024    import com.liferay.portal.kernel.util.HttpUtil;
025    import com.liferay.portal.kernel.util.StreamUtil;
026    import com.liferay.portal.kernel.util.StringBundler;
027    import com.liferay.portal.kernel.util.StringPool;
028    import com.liferay.portal.kernel.util.SystemProperties;
029    import com.liferay.portal.kernel.util.Time;
030    import com.liferay.portal.tools.WebXMLBuilder;
031    import com.liferay.portal.util.ExtRegistry;
032    import com.liferay.portal.util.PortalUtil;
033    import com.liferay.util.ant.CopyTask;
034    
035    import java.io.File;
036    import java.io.FileOutputStream;
037    import java.io.InputStream;
038    
039    import java.util.Map;
040    import java.util.Set;
041    
042    import javax.servlet.ServletContext;
043    
044    /**
045     * @author Brian Wing Shun Chan
046     */
047    public class ExtHotDeployListener extends BaseHotDeployListener {
048    
049            @Override
050            public void invokeDeploy(HotDeployEvent hotDeployEvent)
051                    throws HotDeployException {
052    
053                    try {
054                            doInvokeDeploy(hotDeployEvent);
055                    }
056                    catch (Throwable t) {
057                            throwHotDeployException(
058                                    hotDeployEvent, "Error registering extension environment for ",
059                                    t);
060                    }
061            }
062    
063            @Override
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                            for (Map.Entry<String, Set<String>> entry : conflicts.entrySet()) {
142                                    String conflictServletContextName = entry.getKey();
143                                    Set<String> conflictFiles = entry.getValue();
144    
145                                    sb.append("\n\t");
146                                    sb.append(conflictServletContextName);
147                                    sb.append(":");
148    
149                                    for (String conflictFile : conflictFiles) {
150                                            sb.append("\n\t\t");
151                                            sb.append(conflictFile);
152                                    }
153                            }
154    
155                            _log.error(sb.toString());
156    
157                            return;
158                    }
159    
160                    installExt(servletContext, hotDeployEvent.getContextClassLoader());
161    
162                    if (_log.isInfoEnabled()) {
163                            _log.info(
164                                    "Extension environment for " + servletContextName +
165                                            " has been applied. You must reboot the server and " +
166                                                    "redeploy all other plugins.");
167                    }
168            }
169    
170            protected void doInvokeUndeploy(HotDeployEvent hotDeployEvent)
171                    throws Exception {
172    
173                    ServletContext servletContext = hotDeployEvent.getServletContext();
174    
175                    String servletContextName = servletContext.getServletContextName();
176    
177                    if (_log.isDebugEnabled()) {
178                            _log.debug("Invoking undeploy for " + servletContextName);
179                    }
180    
181                    String xml = HttpUtil.URLtoString(
182                            servletContext.getResource(
183                                    "/WEB-INF/ext-" + servletContextName + ".xml"));
184    
185                    if (xml == null) {
186                            return;
187                    }
188    
189                    if (_log.isInfoEnabled()) {
190                            _log.info(
191                                    "Extension environment for " +
192                                            servletContextName + " will not be undeployed");
193                    }
194            }
195    
196            protected void installExt(
197                            ServletContext servletContext, ClassLoader portletClassLoader)
198                    throws Exception {
199    
200                    String servletContextName = servletContext.getServletContextName();
201    
202                    String globalLibDir = PortalUtil.getGlobalLibDir();
203                    String portalWebDir = PortalUtil.getPortalWebDir();
204                    String portalLibDir = PortalUtil.getPortalLibDir();
205                    String pluginWebDir = WebDirDetector.getRootDir(portletClassLoader);
206    
207                    copyJar(servletContext, globalLibDir, "ext-service");
208                    copyJar(servletContext, portalLibDir, "ext-impl");
209                    copyJar(servletContext, portalLibDir, "ext-util-bridges");
210                    copyJar(servletContext, portalLibDir, "ext-util-java");
211                    copyJar(servletContext, portalLibDir, "ext-util-taglib");
212    
213                    mergeWebXml(portalWebDir, pluginWebDir);
214    
215                    CopyTask.copyDirectory(
216                            pluginWebDir + "WEB-INF/ext-web/docroot", portalWebDir,
217                            StringPool.BLANK, "**/WEB-INF/web.xml", true, false);
218    
219                    FileUtil.copyFile(
220                            pluginWebDir + "WEB-INF/ext-" + servletContextName + ".xml",
221                            portalWebDir + "WEB-INF/ext-" + servletContextName + ".xml");
222    
223                    ExtRegistry.registerExt(servletContext);
224            }
225    
226            protected void mergeWebXml(String portalWebDir, String pluginWebDir) {
227                    if (!FileUtil.exists(
228                                    pluginWebDir + "WEB-INF/ext-web/docroot/WEB-INF/web.xml")) {
229    
230                            return;
231                    }
232    
233                    String tmpDir =
234                            SystemProperties.get(SystemProperties.TMP_DIR) + StringPool.SLASH +
235                                    Time.getTimestamp();
236    
237                    WebXMLBuilder.main(
238                            new String[] {
239                                    portalWebDir + "WEB-INF/web.xml",
240                                    pluginWebDir + "WEB-INF/ext-web/docroot/WEB-INF/web.xml",
241                                    tmpDir + "/web.xml"
242                            });
243    
244                    File portalWebXml = new File(portalWebDir + "WEB-INF/web.xml");
245                    File tmpWebXml = new File(tmpDir + "/web.xml");
246    
247                    tmpWebXml.setLastModified(portalWebXml.lastModified());
248    
249                    CopyTask.copyFile(
250                            tmpWebXml, new File(portalWebDir + "WEB-INF"), true, true);
251    
252                    FileUtil.deltree(tmpDir);
253            }
254    
255            private static final Log _log = LogFactoryUtil.getLog(
256                    ExtHotDeployListener.class);
257    
258    }