001    /**
002     * Copyright (c) 2000-2012 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.tools.deploy;
016    
017    import com.liferay.portal.deploy.DeployUtil;
018    import com.liferay.portal.deploy.auto.AutoDeployer;
019    import com.liferay.portal.kernel.deploy.Deployer;
020    import com.liferay.portal.kernel.deploy.auto.AutoDeployException;
021    import com.liferay.portal.kernel.deploy.auto.context.AutoDeploymentContext;
022    import com.liferay.portal.kernel.log.Log;
023    import com.liferay.portal.kernel.log.LogFactoryUtil;
024    import com.liferay.portal.kernel.plugin.License;
025    import com.liferay.portal.kernel.plugin.PluginPackage;
026    import com.liferay.portal.kernel.servlet.PluginContextListener;
027    import com.liferay.portal.kernel.servlet.PortalClassLoaderServlet;
028    import com.liferay.portal.kernel.servlet.PortletServlet;
029    import com.liferay.portal.kernel.servlet.SecurePluginContextListener;
030    import com.liferay.portal.kernel.servlet.SecureServlet;
031    import com.liferay.portal.kernel.servlet.SerializableSessionAttributeListener;
032    import com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilter;
033    import com.liferay.portal.kernel.util.CharPool;
034    import com.liferay.portal.kernel.util.FileUtil;
035    import com.liferay.portal.kernel.util.GetterUtil;
036    import com.liferay.portal.kernel.util.HttpUtil;
037    import com.liferay.portal.kernel.util.PropertiesUtil;
038    import com.liferay.portal.kernel.util.PropsKeys;
039    import com.liferay.portal.kernel.util.ServerDetector;
040    import com.liferay.portal.kernel.util.StreamUtil;
041    import com.liferay.portal.kernel.util.StringBundler;
042    import com.liferay.portal.kernel.util.StringPool;
043    import com.liferay.portal.kernel.util.StringUtil;
044    import com.liferay.portal.kernel.util.SystemProperties;
045    import com.liferay.portal.kernel.util.TextFormatter;
046    import com.liferay.portal.kernel.util.Time;
047    import com.liferay.portal.kernel.util.Validator;
048    import com.liferay.portal.kernel.xml.Document;
049    import com.liferay.portal.kernel.xml.Element;
050    import com.liferay.portal.kernel.xml.SAXReaderUtil;
051    import com.liferay.portal.plugin.PluginPackageUtil;
052    import com.liferay.portal.tools.WebXMLBuilder;
053    import com.liferay.portal.util.ExtRegistry;
054    import com.liferay.portal.util.InitUtil;
055    import com.liferay.portal.util.PortalUtil;
056    import com.liferay.portal.util.PrefsPropsUtil;
057    import com.liferay.portal.util.PropsUtil;
058    import com.liferay.portal.util.PropsValues;
059    import com.liferay.portal.webserver.DynamicResourceServlet;
060    import com.liferay.util.ant.CopyTask;
061    import com.liferay.util.ant.DeleteTask;
062    import com.liferay.util.ant.ExpandTask;
063    import com.liferay.util.ant.UpToDateTask;
064    import com.liferay.util.ant.WarTask;
065    import com.liferay.util.xml.DocUtil;
066    import com.liferay.util.xml.XMLFormatter;
067    
068    import java.io.File;
069    import java.io.FileInputStream;
070    import java.io.IOException;
071    import java.io.InputStream;
072    
073    import java.util.ArrayList;
074    import java.util.HashMap;
075    import java.util.List;
076    import java.util.Map;
077    import java.util.Properties;
078    import java.util.Set;
079    import java.util.zip.ZipEntry;
080    import java.util.zip.ZipFile;
081    
082    import org.apache.oro.io.GlobFilenameFilter;
083    
084    /**
085     * @author Brian Wing Shun Chan
086     * @author Sandeep Soni
087     */
088    public class BaseDeployer implements AutoDeployer, Deployer {
089    
090            public static final String DEPLOY_TO_PREFIX = "DEPLOY_TO__";
091    
092            public static void main(String[] args) {
093                    InitUtil.initWithSpring();
094    
095                    List<String> wars = new ArrayList<String>();
096                    List<String> jars = new ArrayList<String>();
097    
098                    for (String arg : args) {
099                            String fileName = arg.toLowerCase();
100    
101                            if (fileName.endsWith(".war")) {
102                                    wars.add(arg);
103                            }
104                            else if (fileName.endsWith(".jar")) {
105                                    jars.add(arg);
106                            }
107                    }
108    
109                    new BaseDeployer(wars, jars);
110            }
111    
112            public BaseDeployer() {
113            }
114    
115            public BaseDeployer(List<String> wars, List<String> jars) {
116                    baseDir = System.getProperty("deployer.base.dir");
117                    destDir = System.getProperty("deployer.dest.dir");
118                    appServerType = System.getProperty("deployer.app.server.type");
119                    auiTaglibDTD = System.getProperty("deployer.aui.taglib.dtd");
120                    portletTaglibDTD = System.getProperty("deployer.portlet.taglib.dtd");
121                    portletExtTaglibDTD = System.getProperty(
122                            "deployer.portlet.ext.taglib.dtd");
123                    securityTaglibDTD = System.getProperty("deployer.security.taglib.dtd");
124                    themeTaglibDTD = System.getProperty("deployer.theme.taglib.dtd");
125                    uiTaglibDTD = System.getProperty("deployer.ui.taglib.dtd");
126                    utilTaglibDTD = System.getProperty("deployer.util.taglib.dtd");
127                    unpackWar = GetterUtil.getBoolean(
128                            System.getProperty("deployer.unpack.war"), true);
129                    filePattern = System.getProperty("deployer.file.pattern");
130                    jbossPrefix = GetterUtil.getString(
131                            System.getProperty("deployer.jboss.prefix"));
132                    tomcatLibDir = System.getProperty("deployer.tomcat.lib.dir");
133                    this.wars = wars;
134                    this.jars = jars;
135    
136                    checkArguments();
137    
138                    String context = System.getProperty("deployer.context");
139    
140                    try {
141                            deploy(context);
142                    }
143                    catch (Exception e) {
144                            e.printStackTrace();
145                    }
146            }
147    
148            public void addExtJar(List<String> jars, String resource) throws Exception {
149                    Set<String> servletContextNames = ExtRegistry.getServletContextNames();
150    
151                    for (String servletContextName : servletContextNames) {
152                            String extResource =
153                                    "ext-" + servletContextName + resource.substring(3);
154    
155                            String path = DeployUtil.getResourcePath(extResource);
156    
157                            if (_log.isDebugEnabled()) {
158                                    if (path == null) {
159                                            _log.debug("Resource " + extResource + " is not available");
160                                    }
161                                    else {
162                                            _log.debug(
163                                                    "Resource " + extResource + " is available at " + path);
164                                    }
165                            }
166    
167                            if (path != null) {
168                                    jars.add(path);
169                            }
170                    }
171            }
172    
173            public void addRequiredJar(List<String> jars, String resource)
174                    throws Exception {
175    
176                    String path = DeployUtil.getResourcePath(resource);
177    
178                    if (path == null) {
179                            throw new RuntimeException(
180                                    "Resource " + resource + " does not exist");
181                    }
182    
183                    if (_log.isDebugEnabled()) {
184                            _log.debug("Resource " + resource + " is available at " + path);
185                    }
186    
187                    jars.add(path);
188            }
189    
190            public int autoDeploy(AutoDeploymentContext autoDeploymentContext)
191                    throws AutoDeployException {
192    
193                    List<String> wars = new ArrayList<String>();
194    
195                    File file = autoDeploymentContext.getFile();
196    
197                    wars.add(file.getName());
198    
199                    this.wars = wars;
200    
201                    try {
202                            return deployFile(autoDeploymentContext);
203                    }
204                    catch (Exception e) {
205                            throw new AutoDeployException(e);
206                    }
207            }
208    
209            public void checkArguments() {
210                    if (Validator.isNull(baseDir)) {
211                            throw new IllegalArgumentException(
212                                    "The system property deployer.base.dir is not set");
213                    }
214    
215                    if (Validator.isNull(destDir)) {
216                            throw new IllegalArgumentException(
217                                    "The system property deployer.dest.dir is not set");
218                    }
219    
220                    if (Validator.isNull(appServerType)) {
221                            throw new IllegalArgumentException(
222                                    "The system property deployer.app.server.type is not set");
223                    }
224    
225                    if (!appServerType.equals(ServerDetector.GERONIMO_ID) &&
226                            !appServerType.equals(ServerDetector.GLASSFISH_ID) &&
227                            !appServerType.equals(ServerDetector.JBOSS_ID) &&
228                            !appServerType.equals(ServerDetector.JONAS_ID) &&
229                            !appServerType.equals(ServerDetector.JETTY_ID) &&
230                            !appServerType.equals(ServerDetector.OC4J_ID) &&
231                            !appServerType.equals(ServerDetector.RESIN_ID) &&
232                            !appServerType.equals(ServerDetector.TOMCAT_ID) &&
233                            !appServerType.equals(ServerDetector.WEBLOGIC_ID) &&
234                            !appServerType.equals(ServerDetector.WEBSPHERE_ID)) {
235    
236                            throw new IllegalArgumentException(
237                                    appServerType + " is not a valid application server type");
238                    }
239    
240                    if (appServerType.equals(ServerDetector.GLASSFISH_ID) ||
241                            appServerType.equals(ServerDetector.WEBSPHERE_ID)) {
242    
243                            unpackWar = false;
244                    }
245    
246                    if (Validator.isNotNull(jbossPrefix) &&
247                            !Validator.isNumber(jbossPrefix)) {
248    
249                            jbossPrefix = "1";
250                    }
251            }
252    
253            public void copyDependencyXml(String fileName, String targetDir)
254                    throws Exception {
255    
256                    copyDependencyXml(fileName, targetDir, null);
257            }
258    
259            public void copyDependencyXml(
260                            String fileName, String targetDir, Map<String, String> filterMap)
261                    throws Exception {
262    
263                    copyDependencyXml(fileName, targetDir, filterMap, false);
264            }
265    
266            public void copyDependencyXml(
267                            String fileName, String targetDir, Map<String, String> filterMap,
268                            boolean overwrite)
269                    throws Exception {
270    
271                    DeployUtil.copyDependencyXml(
272                            fileName, targetDir, fileName, filterMap, overwrite);
273            }
274    
275            public void copyDtds(File srcFile, PluginPackage pluginPackage)
276                    throws Exception {
277    
278                    File portalLog4jXml = new File(
279                            srcFile.getAbsolutePath() +
280                                    "/WEB-INF/classes/META-INF/portal-log4j.xml");
281    
282                    if (portalLog4jXml.exists()) {
283                            InputStream is = null;
284    
285                            try {
286                                    Class<?> clazz = getClass();
287    
288                                    ClassLoader classLoader = clazz.getClassLoader();
289    
290                                    is = classLoader.getResourceAsStream("META-INF/log4j.dtd");
291    
292                                    File file = new File(
293                                            srcFile.getAbsolutePath() +
294                                                    "/WEB-INF/classes/META-INF/log4j.dtd");
295    
296                                    FileUtil.write(file, is);
297                            }
298                            finally {
299                                    StreamUtil.cleanUp(is);
300                            }
301                    }
302            }
303    
304            public void copyJars(File srcFile, PluginPackage pluginPackage)
305                    throws Exception {
306    
307                    for (int i = 0; i < jars.size(); i++) {
308                            String jarFullName = jars.get(i);
309    
310                            String jarName = jarFullName.substring(
311                                    jarFullName.lastIndexOf("/") + 1);
312    
313                            if (!appServerType.equals(ServerDetector.TOMCAT_ID) ||
314                                    (appServerType.equals(ServerDetector.TOMCAT_ID) &&
315                                     !jarFullName.equals("util-java.jar"))) {
316    
317                                    FileUtil.copyFile(
318                                            jarFullName, srcFile + "/WEB-INF/lib/" + jarName, true);
319                            }
320                    }
321    
322                    FileUtil.delete(srcFile + "/WEB-INF/lib/util-jsf.jar");
323            }
324    
325            public void copyPortalDependencies(File srcFile) throws Exception {
326                    Properties properties = getPluginPackageProperties(srcFile);
327    
328                    if (properties == null) {
329                            return;
330                    }
331    
332                    // jars
333    
334                    String[] portalJars = StringUtil.split(
335                            properties.getProperty(
336                                    "portal-dependency-jars",
337                                    properties.getProperty("portal.dependency.jars")));
338    
339                    for (String portalJar : portalJars) {
340                            portalJar = portalJar.trim();
341    
342                            portalJar = fixPortalDependencyJar(portalJar);
343    
344                            if (_log.isDebugEnabled()) {
345                                    _log.debug("Copy portal JAR " + portalJar);
346                            }
347    
348                            try {
349                                    String portalJarPath = PortalUtil.getPortalLibDir() + portalJar;
350    
351                                    FileUtil.copyFile(
352                                            portalJarPath, srcFile + "/WEB-INF/lib/" + portalJar, true);
353                            }
354                            catch (Exception e) {
355                                    _log.error("Unable to copy portal JAR " + portalJar, e);
356                            }
357                    }
358    
359                    // tlds
360    
361                    String[] portalTlds = StringUtil.split(
362                            properties.getProperty(
363                                    "portal-dependency-tlds",
364                                    properties.getProperty("portal.dependency.tlds")));
365    
366                    for (String portalTld : portalTlds) {
367                            portalTld = portalTld.trim();
368    
369                            if (_log.isDebugEnabled()) {
370                                    _log.debug("Copy portal TLD " + portalTld);
371                            }
372    
373                            try {
374                                    String portalTldPath = DeployUtil.getResourcePath(portalTld);
375    
376                                    FileUtil.copyFile(
377                                            portalTldPath, srcFile + "/WEB-INF/tld/" + portalTld, true);
378                            }
379                            catch (Exception e) {
380                                    _log.error("Unable to copy portal TLD " + portalTld, e);
381                            }
382                    }
383    
384                    // commons-logging*.jar
385    
386                    File pluginLibDir = new File(srcFile + "/WEB-INF/lib/");
387    
388                    if (PropsValues.AUTO_DEPLOY_COPY_COMMONS_LOGGING) {
389                            String[] commonsLoggingJars = pluginLibDir.list(
390                                    new GlobFilenameFilter("commons-logging*.jar"));
391    
392                            if ((commonsLoggingJars == null) ||
393                                    (commonsLoggingJars.length == 0)) {
394    
395                                    String portalJarPath =
396                                            PortalUtil.getPortalLibDir() + "commons-logging.jar";
397    
398                                    FileUtil.copyFile(
399                                            portalJarPath, srcFile + "/WEB-INF/lib/commons-logging.jar",
400                                            true);
401                            }
402                    }
403    
404                    // log4j*.jar
405    
406                    if (PropsValues.AUTO_DEPLOY_COPY_LOG4J) {
407                            String[] log4jJars = pluginLibDir.list(
408                                    new GlobFilenameFilter("log4j*.jar"));
409    
410                            if ((log4jJars == null) || (log4jJars.length == 0)) {
411                                    String portalJarPath =
412                                            PortalUtil.getPortalLibDir() + "log4j.jar";
413    
414                                    FileUtil.copyFile(
415                                            portalJarPath, srcFile + "/WEB-INF/lib/log4j.jar", true);
416                            }
417                    }
418            }
419    
420            public void copyProperties(File srcFile, PluginPackage pluginPackage)
421                    throws Exception {
422    
423                    if (PropsValues.AUTO_DEPLOY_COPY_COMMONS_LOGGING) {
424                            copyDependencyXml(
425                                    "logging.properties", srcFile + "/WEB-INF/classes");
426                    }
427    
428                    if (PropsValues.AUTO_DEPLOY_COPY_LOG4J) {
429                            copyDependencyXml("log4j.properties", srcFile + "/WEB-INF/classes");
430                    }
431    
432                    File servicePropertiesFile = new File(
433                            srcFile.getAbsolutePath() + "/WEB-INF/classes/service.properties");
434    
435                    if (servicePropertiesFile.exists()) {
436                            File portletPropertiesFile = new File(
437                                    srcFile.getAbsolutePath() +
438                                            "/WEB-INF/classes/portlet.properties");
439    
440                            if (!portletPropertiesFile.exists()) {
441                                    String pluginPackageName = null;
442    
443                                    if (pluginPackage != null) {
444                                            pluginPackageName = pluginPackage.getName();
445                                    }
446                                    else {
447                                            pluginPackageName = srcFile.getName();
448                                    }
449    
450                                    FileUtil.write(
451                                            portletPropertiesFile,
452                                            "plugin.package.name=" + pluginPackageName);
453                            }
454                    }
455            }
456    
457            public void copyTlds(File srcFile, PluginPackage pluginPackage)
458                    throws Exception {
459    
460                    if (Validator.isNotNull(auiTaglibDTD)) {
461                            FileUtil.copyFile(
462                                    auiTaglibDTD, srcFile + "/WEB-INF/tld/aui.tld", true);
463                    }
464    
465                    if (Validator.isNotNull(portletTaglibDTD)) {
466                            FileUtil.copyFile(
467                                    portletTaglibDTD, srcFile + "/WEB-INF/tld/liferay-portlet.tld",
468                                    true);
469                    }
470    
471                    if (Validator.isNotNull(portletExtTaglibDTD)) {
472                            FileUtil.copyFile(
473                                    portletExtTaglibDTD,
474                                    srcFile + "/WEB-INF/tld/liferay-portlet-ext.tld", true);
475                    }
476    
477                    if (Validator.isNotNull(securityTaglibDTD)) {
478                            FileUtil.copyFile(
479                                    securityTaglibDTD,
480                                    srcFile + "/WEB-INF/tld/liferay-security.tld", true);
481                    }
482    
483                    if (Validator.isNotNull(themeTaglibDTD)) {
484                            FileUtil.copyFile(
485                                    themeTaglibDTD, srcFile + "/WEB-INF/tld/liferay-theme.tld",
486                                    true);
487                    }
488    
489                    if (Validator.isNotNull(uiTaglibDTD)) {
490                            FileUtil.copyFile(
491                                    uiTaglibDTD, srcFile + "/WEB-INF/tld/liferay-ui.tld", true);
492                    }
493    
494                    if (Validator.isNotNull(utilTaglibDTD)) {
495                            FileUtil.copyFile(
496                                    utilTaglibDTD, srcFile + "/WEB-INF/tld/liferay-util.tld", true);
497                    }
498            }
499    
500            public void copyTomcatContextXml(File targetDir) throws Exception {
501                    if (!appServerType.equals(ServerDetector.TOMCAT_ID)) {
502                            return;
503                    }
504    
505                    File targetFile = new File(targetDir, "META-INF/context.xml");
506    
507                    if (targetFile.exists()) {
508                            return;
509                    }
510    
511                    String contextPath = DeployUtil.getResourcePath("context.xml");
512    
513                    String content = FileUtil.read(contextPath);
514    
515                    if (!PropsValues.AUTO_DEPLOY_UNPACK_WAR) {
516                            content = StringUtil.replace(
517                                    content, "antiResourceLocking=\"true\"", StringPool.BLANK);
518                    }
519    
520                    FileUtil.write(targetFile, content);
521            }
522    
523            public void copyXmls(
524                            File srcFile, String displayName, PluginPackage pluginPackage)
525                    throws Exception {
526    
527                    if (appServerType.equals(ServerDetector.GERONIMO_ID)) {
528                            copyDependencyXml("geronimo-web.xml", srcFile + "/WEB-INF");
529                    }
530                    else if (appServerType.equals(ServerDetector.JBOSS_ID)) {
531                            if (ServerDetector.isJBoss5()) {
532                                    copyDependencyXml("jboss-web.xml", srcFile + "/WEB-INF");
533                            }
534                            else {
535                                    copyDependencyXml(
536                                            "jboss-deployment-structure.xml", srcFile + "/WEB-INF");
537                            }
538                    }
539                    else if (appServerType.equals(ServerDetector.WEBLOGIC_ID)) {
540                            copyDependencyXml("weblogic.xml", srcFile + "/WEB-INF");
541                    }
542                    else if (appServerType.equals(ServerDetector.WEBSPHERE_ID)) {
543                            copyDependencyXml("ibm-web-ext.xmi", srcFile + "/WEB-INF");
544                    }
545    
546                    copyDependencyXml("web.xml", srcFile + "/WEB-INF");
547            }
548    
549            public void deploy(String context) throws Exception {
550                    try {
551                            File baseDirFile = new File(baseDir);
552    
553                            File[] files = baseDirFile.listFiles();
554    
555                            if (files == null) {
556                                    return;
557                            }
558    
559                            files = FileUtil.sortFiles(files);
560    
561                            for (File srcFile : files) {
562                                    String fileName = srcFile.getName().toLowerCase();
563    
564                                    boolean deploy = false;
565    
566                                    if (fileName.endsWith(".war") || fileName.endsWith(".zip")) {
567                                            deploy = true;
568    
569                                            if (wars.size() > 0) {
570                                                    if (!wars.contains(srcFile.getName())) {
571                                                            deploy = false;
572                                                    }
573                                            }
574                                            else if (Validator.isNotNull(filePattern)) {
575                                                    if (!StringUtil.matchesIgnoreCase(
576                                                                    fileName, filePattern)) {
577    
578                                                            deploy = false;
579                                                    }
580                                            }
581                                    }
582    
583                                    if (deploy) {
584                                            AutoDeploymentContext autoDeploymentContext =
585                                                    new AutoDeploymentContext();
586    
587                                            autoDeploymentContext.setContext(context);
588                                            autoDeploymentContext.setFile(srcFile);
589    
590                                            deployFile(autoDeploymentContext);
591                                    }
592                            }
593                    }
594                    catch (Exception e) {
595                            e.printStackTrace();
596                    }
597            }
598    
599            public void deployDirectory(
600                            File srcFile, File mergeDir, File deployDir, String displayName,
601                            boolean overwrite, PluginPackage pluginPackage)
602                    throws Exception {
603    
604                    rewriteFiles(srcFile);
605    
606                    mergeDirectory(mergeDir, srcFile);
607    
608                    processPluginPackageProperties(srcFile, displayName, pluginPackage);
609    
610                    copyDtds(srcFile, pluginPackage);
611                    copyJars(srcFile, pluginPackage);
612                    copyProperties(srcFile, pluginPackage);
613                    copyTlds(srcFile, pluginPackage);
614                    copyXmls(srcFile, displayName, pluginPackage);
615                    copyPortalDependencies(srcFile);
616    
617                    updateGeronimoWebXml(srcFile, displayName, pluginPackage);
618    
619                    File webXml = new File(srcFile + "/WEB-INF/web.xml");
620    
621                    updateWebXml(webXml, srcFile, displayName, pluginPackage);
622    
623                    File extLibGlobalDir = new File(
624                            srcFile.getAbsolutePath() + "/WEB-INF/ext-lib/global");
625    
626                    if (extLibGlobalDir.exists()) {
627                            File globalLibDir = new File(PortalUtil.getGlobalLibDir());
628    
629                            CopyTask.copyDirectory(
630                                    extLibGlobalDir, globalLibDir, "*.jar", StringPool.BLANK,
631                                    overwrite, true);
632                    }
633    
634                    File extLibPortalDir = new File(
635                            srcFile.getAbsolutePath() + "/WEB-INF/ext-lib/portal");
636    
637                    if (extLibPortalDir.exists()) {
638                            File portalLibDir = new File(PortalUtil.getPortalLibDir());
639    
640                            CopyTask.copyDirectory(
641                                    extLibPortalDir, portalLibDir, "*.jar", StringPool.BLANK,
642                                    overwrite, true);
643                    }
644    
645                    if ((deployDir == null) || baseDir.equals(destDir)) {
646                            return;
647                    }
648    
649                    updateDeployDirectory(srcFile);
650    
651                    String excludes = StringPool.BLANK;
652    
653                    if (appServerType.equals(ServerDetector.JBOSS_ID)) {
654                            excludes += "**/WEB-INF/lib/log4j.jar,";
655                    }
656                    else if (appServerType.equals(ServerDetector.TOMCAT_ID)) {
657                            String[] libs = FileUtil.listFiles(tomcatLibDir);
658    
659                            for (String lib : libs) {
660                                    excludes += "**/WEB-INF/lib/" + lib + ",";
661                            }
662    
663                            File contextXml = new File(srcFile + "/META-INF/context.xml");
664    
665                            if (contextXml.exists()) {
666                                    String content = FileUtil.read(contextXml);
667    
668                                    if (content.contains(_PORTAL_CLASS_LOADER)) {
669                                            excludes += "**/WEB-INF/lib/util-bridges.jar,";
670                                            excludes += "**/WEB-INF/lib/util-java.jar,";
671                                            excludes += "**/WEB-INF/lib/util-taglib.jar,";
672                                    }
673                            }
674    
675                            try {
676    
677                                    // LEP-2990
678    
679                                    Class.forName("javax.el.ELContext");
680    
681                                    excludes += "**/WEB-INF/lib/el-api.jar,";
682                            }
683                            catch (ClassNotFoundException cnfe) {
684                            }
685                    }
686    
687                    // LPS-11268
688    
689                    Properties properties = getPluginPackageProperties(srcFile);
690    
691                    if (properties != null) {
692                            String deployExcludes = properties.getProperty("deploy-excludes");
693    
694                            if (deployExcludes != null) {
695                                    excludes += deployExcludes.trim();
696    
697                                    if (!excludes.endsWith(",")) {
698                                            excludes += ",";
699                                    }
700                            }
701    
702                            deployExcludes = properties.getProperty(
703                                    "deploy-excludes-" + appServerType);
704    
705                            if (deployExcludes != null) {
706                                    excludes += deployExcludes.trim();
707    
708                                    if (!excludes.endsWith(",")) {
709                                            excludes += ",";
710                                    }
711                            }
712                    }
713    
714                    if (_log.isDebugEnabled()) {
715                            _log.debug("Excludes " + excludes);
716                    }
717    
718                    if (!unpackWar) {
719                            File tempDir = new File(
720                                    SystemProperties.get(SystemProperties.TMP_DIR) +
721                                            File.separator + Time.getTimestamp());
722    
723                            excludes += "**/WEB-INF/web.xml";
724    
725                            WarTask.war(srcFile, tempDir, excludes, webXml);
726    
727                            if (isJEEDeploymentEnabled()) {
728                                    File tempWarDir = new File(
729                                            tempDir.getParent(), deployDir.getName());
730    
731                                    if (tempWarDir.exists()) {
732                                            tempWarDir.delete();
733                                    }
734    
735                                    if (!tempDir.renameTo(tempWarDir)) {
736                                            tempWarDir = tempDir;
737                                    }
738    
739                                    DeploymentHandler deploymentHandler = getDeploymentHandler();
740    
741                                    deploymentHandler.deploy(tempWarDir, displayName);
742    
743                                    deploymentHandler.releaseDeploymentManager();
744    
745                                    DeleteTask.deleteDirectory(tempWarDir);
746                            }
747                            else {
748                                    if (!tempDir.renameTo(deployDir)) {
749                                            WarTask.war(srcFile, deployDir, excludes, webXml);
750                                    }
751    
752                                    DeleteTask.deleteDirectory(tempDir);
753                            }
754                    }
755                    else {
756    
757                            // The deployer might only copy files that have been modified.
758                            // However, the deployer always copies and overwrites web.xml after
759                            // the other files have been copied because application servers
760                            // usually detect that a WAR has been modified based on the web.xml
761                            // timestamp.
762    
763                            excludes += "**/WEB-INF/web.xml";
764    
765                            CopyTask.copyDirectory(
766                                    srcFile, deployDir, StringPool.BLANK, excludes, overwrite,
767                                    true);
768    
769                            CopyTask.copyDirectory(
770                                    srcFile, deployDir, "**/WEB-INF/web.xml", StringPool.BLANK,
771                                    true, false);
772    
773                            if (appServerType.equals(ServerDetector.TOMCAT_ID)) {
774    
775                                    // See org.apache.catalina.startup.HostConfig to see how Tomcat
776                                    // checks to make sure that web.xml was modified 5 seconds after
777                                    // WEB-INF
778    
779                                    File deployWebXml = new File(deployDir + "/WEB-INF/web.xml");
780    
781                                    deployWebXml.setLastModified(
782                                            System.currentTimeMillis() + (Time.SECOND * 6));
783                            }
784                    }
785    
786                    if (appServerType.equals(ServerDetector.JETTY_ID)) {
787                            DeployUtil.redeployJetty(displayName);
788                    }
789            }
790    
791            public void deployDirectory(
792                            File srcFile, String displayName, boolean override,
793                            PluginPackage pluginPackage)
794                    throws Exception {
795    
796                    deployDirectory(
797                            srcFile, null, null, displayName, override, pluginPackage);
798            }
799    
800            public int deployFile(AutoDeploymentContext autoDeploymentContext)
801                    throws Exception {
802    
803                    File srcFile = autoDeploymentContext.getFile();
804    
805                    PluginPackage pluginPackage = readPluginPackage(srcFile);
806    
807                    if (_log.isInfoEnabled()) {
808                            _log.info("Deploying " + srcFile.getName());
809                    }
810    
811                    String specifiedContext = autoDeploymentContext.getContext();
812    
813                    String displayName = specifiedContext;
814                    boolean overwrite = false;
815                    String preliminaryContext = specifiedContext;
816    
817                    // The order of priority of the context is: 1.) the specified context,
818                    // 2.) if the file name starts with DEPLOY_TO_PREFIX, use the file name
819                    // after the prefix, or 3.) the recommended deployment context as
820                    // specified in liferay-plugin-package.properties, or 4.) the file name.
821    
822                    if (Validator.isNull(specifiedContext) &&
823                            srcFile.getName().startsWith(DEPLOY_TO_PREFIX)) {
824    
825                            displayName = srcFile.getName().substring(
826                                    DEPLOY_TO_PREFIX.length(), srcFile.getName().length() - 4);
827    
828                            overwrite = true;
829                            preliminaryContext = displayName;
830                    }
831    
832                    if (preliminaryContext == null) {
833                            preliminaryContext = getDisplayName(srcFile);
834                    }
835    
836                    if (pluginPackage != null) {
837                            if (!PluginPackageUtil.isCurrentVersionSupported(
838                                            pluginPackage.getLiferayVersions())) {
839    
840                                    throw new AutoDeployException(
841                                            srcFile.getName() +
842                                                    " does not support this version of Liferay");
843                            }
844    
845                            if (displayName == null) {
846                                    displayName = pluginPackage.getRecommendedDeploymentContext();
847                            }
848    
849                            if (Validator.isNull(displayName)) {
850                                    displayName = getDisplayName(srcFile);
851                            }
852    
853                            pluginPackage.setContext(displayName);
854    
855                            PluginPackageUtil.updateInstallingPluginPackage(
856                                    preliminaryContext, pluginPackage);
857                    }
858    
859                    String deployDir = null;
860    
861                    if (Validator.isNotNull(displayName)) {
862                            deployDir = displayName + ".war";
863                    }
864                    else {
865                            deployDir = srcFile.getName();
866                            displayName = getDisplayName(srcFile);
867                    }
868    
869                    if (appServerType.equals(ServerDetector.JBOSS_ID)) {
870                            deployDir = jbossPrefix + deployDir;
871                    }
872                    else if (appServerType.equals(ServerDetector.GERONIMO_ID) ||
873                                     appServerType.equals(ServerDetector.GLASSFISH_ID) ||
874                                     appServerType.equals(ServerDetector.JETTY_ID) ||
875                                     appServerType.equals(ServerDetector.JONAS_ID) ||
876                                     appServerType.equals(ServerDetector.OC4J_ID) ||
877                                     appServerType.equals(ServerDetector.RESIN_ID) ||
878                                     appServerType.equals(ServerDetector.TOMCAT_ID) ||
879                                     appServerType.equals(ServerDetector.WEBLOGIC_ID)) {
880    
881                            if (unpackWar) {
882                                    deployDir = deployDir.substring(0, deployDir.length() - 4);
883                            }
884                    }
885    
886                    String destDir = this.destDir;
887    
888                    if (autoDeploymentContext.getDestDir() != null) {
889                            destDir = autoDeploymentContext.getDestDir();
890                    }
891    
892                    File deployDirFile = new File(destDir + "/" + deployDir);
893    
894                    try {
895                            PluginPackage previousPluginPackage = readPluginPackage(
896                                    deployDirFile);
897    
898                            if ((pluginPackage != null) && (previousPluginPackage != null)) {
899                                    String name = pluginPackage.getName();
900                                    String previousVersion = previousPluginPackage.getVersion();
901                                    String version = pluginPackage.getVersion();
902    
903                                    if (_log.isInfoEnabled()) {
904                                            _log.info(
905                                                    "Updating " + name + " from version " +
906                                                            previousVersion + " to version " + version);
907                                    }
908    
909                                    if (pluginPackage.isPreviousVersionThan(
910                                                    previousPluginPackage)) {
911    
912                                            if (_log.isInfoEnabled()) {
913                                                    _log.info(
914                                                            "Not updating " + name + " because version " +
915                                                                    previousVersion + " is newer than version " +
916                                                                            version);
917                                            }
918    
919                                            return AutoDeployer.CODE_SKIP_NEWER_VERSION;
920                                    }
921    
922                                    overwrite = true;
923                            }
924    
925                            File mergeDirFile = new File(
926                                    srcFile.getParent() + "/merge/" + srcFile.getName());
927    
928                            if (srcFile.isDirectory()) {
929                                    deployDirectory(
930                                            srcFile, mergeDirFile, deployDirFile, displayName,
931                                            overwrite, pluginPackage);
932                            }
933                            else {
934                                    boolean deployed = deployFile(
935                                            srcFile, mergeDirFile, deployDirFile, displayName,
936                                            overwrite, pluginPackage);
937    
938                                    if (!deployed) {
939                                            String context = preliminaryContext;
940    
941                                            if (pluginPackage != null) {
942                                                    context = pluginPackage.getContext();
943                                            }
944    
945                                            PluginPackageUtil.endPluginPackageInstallation(context);
946                                    }
947                                    else {
948                                            postDeploy(destDir, deployDir);
949                                    }
950                            }
951    
952                            return AutoDeployer.CODE_DEFAULT;
953                    }
954                    catch (Exception e) {
955                            if (pluginPackage != null) {
956                                    PluginPackageUtil.endPluginPackageInstallation(
957                                            pluginPackage.getContext());
958                            }
959    
960                            throw e;
961                    }
962            }
963    
964            public boolean deployFile(
965                            File srcFile, File mergeDir, File deployDir, String displayName,
966                            boolean overwrite, PluginPackage pluginPackage)
967                    throws Exception {
968    
969                    boolean undeployOnRedeploy = false;
970    
971                    try {
972                            undeployOnRedeploy = PrefsPropsUtil.getBoolean(
973                                    PropsKeys.HOT_UNDEPLOY_ON_REDEPLOY,
974                                    PropsValues.HOT_UNDEPLOY_ON_REDEPLOY);
975                    }
976                    catch (Exception e) {
977    
978                            // This will only happen when running the deploy tool in Ant in the
979                            // classical way where the WAR file is actually massaged and
980                            // packaged.
981    
982                    }
983    
984                    if (undeployOnRedeploy) {
985                            DeployUtil.undeploy(appServerType, deployDir);
986                    }
987    
988                    if (!overwrite && UpToDateTask.isUpToDate(srcFile, deployDir)) {
989                            if (_log.isInfoEnabled()) {
990                                    _log.info(deployDir + " is already up to date");
991                            }
992    
993                            return false;
994                    }
995    
996                    File tempDir = new File(
997                            SystemProperties.get(SystemProperties.TMP_DIR) + File.separator +
998                                    Time.getTimestamp());
999    
1000                    ExpandTask.expand(srcFile, tempDir);
1001    
1002                    deployDirectory(
1003                            tempDir, mergeDir, deployDir, displayName, overwrite,
1004                            pluginPackage);
1005    
1006                    DeleteTask.deleteDirectory(tempDir);
1007    
1008                    return true;
1009            }
1010    
1011            public String downloadJar(String jar) throws Exception {
1012                    String tmpDir = SystemProperties.get(SystemProperties.TMP_DIR);
1013    
1014                    File file = new File(
1015                            tmpDir + "/liferay/com/liferay/portal/deploy/dependencies/" + jar);
1016    
1017                    if (!file.exists()) {
1018                            synchronized (this) {
1019                                    String url = PropsUtil.get(
1020                                            PropsKeys.LIBRARY_DOWNLOAD_URL + jar);
1021    
1022                                    if (_log.isInfoEnabled()) {
1023                                            _log.info("Downloading library from " + url);
1024                                    }
1025    
1026                                    byte[] bytes = HttpUtil.URLtoByteArray(url);
1027    
1028                                    FileUtil.write(file, bytes);
1029                            }
1030                    }
1031    
1032                    return FileUtil.getAbsolutePath(file);
1033            }
1034    
1035            public String fixPortalDependencyJar(String portalJar) {
1036                    if (portalJar.equals("antlr.jar")) {
1037                            portalJar = "antlr2.jar";
1038                    }
1039    
1040                    return portalJar;
1041            }
1042    
1043            public DeploymentHandler getDeploymentHandler() {
1044                    String prefix = "auto.deploy." + ServerDetector.getServerId() + ".jee.";
1045    
1046                    String dmId = PropsUtil.get(prefix + "dm.id");
1047                    String dmUser = PropsUtil.get(prefix + "dm.user");
1048                    String dmPassword = PropsUtil.get(prefix + "dm.passwd");
1049                    String dfClassName = PropsUtil.get(prefix + "df.classname");
1050    
1051                    return new DeploymentHandler(dmId, dmUser, dmPassword, dfClassName);
1052            }
1053    
1054            public String getDisplayName(File srcFile) {
1055                    String displayName = srcFile.getName();
1056    
1057                    if (StringUtil.endsWith(displayName, ".war") ||
1058                            StringUtil.endsWith(displayName, ".xml")) {
1059    
1060                            displayName = displayName.substring(0, displayName.length() - 4);
1061                    }
1062    
1063                    if (appServerType.equals(ServerDetector.JBOSS_ID) &&
1064                            Validator.isNotNull(jbossPrefix) &&
1065                            displayName.startsWith(jbossPrefix)) {
1066    
1067                            displayName = displayName.substring(1);
1068                    }
1069    
1070                    return displayName;
1071            }
1072    
1073            public String getDynamicResourceServletContent() {
1074                    StringBundler sb = new StringBundler();
1075    
1076                    sb.append("<servlet>");
1077                    sb.append("<servlet-name>");
1078                    sb.append("Dynamic Resource Servlet");
1079                    sb.append("</servlet-name>");
1080                    sb.append("<servlet-class>");
1081                    sb.append(PortalClassLoaderServlet.class.getName());
1082                    sb.append("</servlet-class>");
1083                    sb.append("<init-param>");
1084                    sb.append("<param-name>");
1085                    sb.append("servlet-class");
1086                    sb.append("</param-name>");
1087                    sb.append("<param-value>");
1088                    sb.append(DynamicResourceServlet.class.getName());
1089                    sb.append("</param-value>");
1090                    sb.append("</init-param>");
1091                    sb.append("<load-on-startup>1</load-on-startup>");
1092                    sb.append("</servlet>");
1093    
1094                    for (String allowedPath :
1095                                    PropsValues.DYNAMIC_RESOURCE_SERVLET_ALLOWED_PATHS) {
1096    
1097                            sb.append("<servlet-mapping>");
1098                            sb.append("<servlet-name>");
1099                            sb.append("Dynamic Resource Servlet");
1100                            sb.append("</servlet-name>");
1101                            sb.append("<url-pattern>");
1102                            sb.append(allowedPath);
1103    
1104                            if (!allowedPath.endsWith(StringPool.SLASH)) {
1105                                    sb.append(StringPool.SLASH);
1106                            }
1107    
1108                            sb.append(StringPool.STAR);
1109                            sb.append("</url-pattern>");
1110                            sb.append("</servlet-mapping>");
1111                    }
1112    
1113                    return sb.toString();
1114            }
1115    
1116            public String getExtraContent(
1117                            double webXmlVersion, File srcFile, String displayName)
1118                    throws Exception {
1119    
1120                    StringBundler sb = new StringBundler();
1121    
1122                    sb.append("<display-name>");
1123                    sb.append(displayName);
1124                    sb.append("</display-name>");
1125    
1126                    if (webXmlVersion < 2.4) {
1127                            sb.append("<context-param>");
1128                            sb.append("<param-name>liferay-invoker-enabled</param-name>");
1129                            sb.append("<param-value>false</param-value>");
1130                            sb.append("</context-param>");
1131                    }
1132    
1133                    sb.append("<listener>");
1134                    sb.append("<listener-class>");
1135                    sb.append(SerializableSessionAttributeListener.class.getName());
1136                    sb.append("</listener-class>");
1137                    sb.append("</listener>");
1138    
1139                    sb.append(getDynamicResourceServletContent());
1140    
1141                    File serverConfigWsdd = new File(
1142                            srcFile + "/WEB-INF/server-config.wsdd");
1143    
1144                    if (serverConfigWsdd.exists()) {
1145                            File webXml = new File(srcFile + "/WEB-INF/web.xml");
1146    
1147                            String content = FileUtil.read(webXml);
1148    
1149                            if (!content.contains("axis.servicesPath")) {
1150                                    String remotingContent = FileUtil.read(
1151                                            DeployUtil.getResourcePath("remoting-web.xml"));
1152    
1153                                    sb.append(remotingContent);
1154                            }
1155                    }
1156    
1157                    boolean hasTaglib = false;
1158    
1159                    if (Validator.isNotNull(auiTaglibDTD) ||
1160                            Validator.isNotNull(portletTaglibDTD) ||
1161                            Validator.isNotNull(portletExtTaglibDTD) ||
1162                            Validator.isNotNull(securityTaglibDTD) ||
1163                            Validator.isNotNull(themeTaglibDTD) ||
1164                            Validator.isNotNull(uiTaglibDTD) ||
1165                            Validator.isNotNull(utilTaglibDTD)) {
1166    
1167                            hasTaglib = true;
1168                    }
1169    
1170                    if (hasTaglib && (webXmlVersion > 2.3)) {
1171                            sb.append("<jsp-config>");
1172                    }
1173    
1174                    if (Validator.isNotNull(auiTaglibDTD)) {
1175                            sb.append("<taglib>");
1176                            sb.append("<taglib-uri>http://liferay.com/tld/aui</taglib-uri>");
1177                            sb.append("<taglib-location>");
1178                            sb.append("/WEB-INF/tld/aui.tld");
1179                            sb.append("</taglib-location>");
1180                            sb.append("</taglib>");
1181                    }
1182    
1183                    if (Validator.isNotNull(portletTaglibDTD)) {
1184                            sb.append("<taglib>");
1185                            sb.append(
1186                                    "<taglib-uri>http://java.sun.com/portlet_2_0</taglib-uri>");
1187                            sb.append("<taglib-location>");
1188                            sb.append("/WEB-INF/tld/liferay-portlet.tld");
1189                            sb.append("</taglib-location>");
1190                            sb.append("</taglib>");
1191                    }
1192    
1193                    if (Validator.isNotNull(portletExtTaglibDTD)) {
1194                            sb.append("<taglib>");
1195                            sb.append("<taglib-uri>");
1196                            sb.append("http://liferay.com/tld/portlet");
1197                            sb.append("</taglib-uri>");
1198                            sb.append("<taglib-location>");
1199                            sb.append("/WEB-INF/tld/liferay-portlet-ext.tld");
1200                            sb.append("</taglib-location>");
1201                            sb.append("</taglib>");
1202                    }
1203    
1204                    if (Validator.isNotNull(securityTaglibDTD)) {
1205                            sb.append("<taglib>");
1206                            sb.append("<taglib-uri>");
1207                            sb.append("http://liferay.com/tld/security");
1208                            sb.append("</taglib-uri>");
1209                            sb.append("<taglib-location>");
1210                            sb.append("/WEB-INF/tld/liferay-security.tld");
1211                            sb.append("</taglib-location>");
1212                            sb.append("</taglib>");
1213                    }
1214    
1215                    if (Validator.isNotNull(themeTaglibDTD)) {
1216                            sb.append("<taglib>");
1217                            sb.append("<taglib-uri>http://liferay.com/tld/theme</taglib-uri>");
1218                            sb.append("<taglib-location>");
1219                            sb.append("/WEB-INF/tld/liferay-theme.tld");
1220                            sb.append("</taglib-location>");
1221                            sb.append("</taglib>");
1222                    }
1223    
1224                    if (Validator.isNotNull(uiTaglibDTD)) {
1225                            sb.append("<taglib>");
1226                            sb.append("<taglib-uri>http://liferay.com/tld/ui</taglib-uri>");
1227                            sb.append("<taglib-location>");
1228                            sb.append("/WEB-INF/tld/liferay-ui.tld");
1229                            sb.append("</taglib-location>");
1230                            sb.append("</taglib>");
1231                    }
1232    
1233                    if (Validator.isNotNull(utilTaglibDTD)) {
1234                            sb.append("<taglib>");
1235                            sb.append("<taglib-uri>http://liferay.com/tld/util</taglib-uri>");
1236                            sb.append("<taglib-location>");
1237                            sb.append("/WEB-INF/tld/liferay-util.tld");
1238                            sb.append("</taglib-location>");
1239                            sb.append("</taglib>");
1240                    }
1241    
1242                    if (hasTaglib && (webXmlVersion > 2.3)) {
1243                            sb.append("</jsp-config>");
1244                    }
1245    
1246                    return sb.toString();
1247            }
1248    
1249            public String getExtraFiltersContent(double webXmlVersion, File srcFile)
1250                    throws Exception {
1251    
1252                    return getSessionFiltersContent();
1253            }
1254    
1255            public String getIgnoreFiltersContent(File srcFile) throws Exception {
1256                    boolean ignoreFiltersEnabled = true;
1257    
1258                    Properties properties = getPluginPackageProperties(srcFile);
1259    
1260                    if (properties != null) {
1261                            ignoreFiltersEnabled = GetterUtil.getBoolean(
1262                                    properties.getProperty("ignore-filters-enabled"), true);
1263                    }
1264    
1265                    if (ignoreFiltersEnabled) {
1266                            String ignoreFiltersContent = FileUtil.read(
1267                                    DeployUtil.getResourcePath("ignore-filters-web.xml"));
1268    
1269                            return ignoreFiltersContent;
1270                    }
1271                    else {
1272                            return StringPool.BLANK;
1273                    }
1274            }
1275    
1276            public String getInvokerFilterContent() {
1277                    StringBundler sb = new StringBundler(4);
1278    
1279                    sb.append(getInvokerFilterContent("ERROR"));
1280                    sb.append(getInvokerFilterContent("FORWARD"));
1281                    sb.append(getInvokerFilterContent("INCLUDE"));
1282                    sb.append(getInvokerFilterContent("REQUEST"));
1283    
1284                    return sb.toString();
1285            }
1286    
1287            public String getInvokerFilterContent(String dispatcher) {
1288                    StringBundler sb = new StringBundler(23);
1289    
1290                    sb.append("<filter>");
1291                    sb.append("<filter-name>Invoker Filter - ");
1292                    sb.append(dispatcher);
1293                    sb.append("</filter-name>");
1294                    sb.append("<filter-class>");
1295                    sb.append(InvokerFilter.class.getName());
1296                    sb.append("</filter-class>");
1297                    sb.append("<init-param>");
1298                    sb.append("<param-name>dispatcher</param-name>");
1299                    sb.append("<param-value>");
1300                    sb.append(dispatcher);
1301                    sb.append("</param-value>");
1302                    sb.append("</init-param>");
1303                    sb.append("</filter>");
1304    
1305                    sb.append("<filter-mapping>");
1306                    sb.append("<filter-name>Invoker Filter - ");
1307                    sb.append(dispatcher);
1308                    sb.append("</filter-name>");
1309                    sb.append("<url-pattern>/*</url-pattern>");
1310                    sb.append("<dispatcher>");
1311                    sb.append(dispatcher);
1312                    sb.append("</dispatcher>");
1313                    sb.append("</filter-mapping>");
1314    
1315                    return sb.toString();
1316            }
1317    
1318            public String getPluginPackageLicensesXml(List<License> licenses) {
1319                    if (licenses.isEmpty()) {
1320                            return StringPool.BLANK;
1321                    }
1322    
1323                    StringBundler sb = new StringBundler(5 * licenses.size() + 2);
1324    
1325                    for (int i = 0; i < licenses.size(); i++) {
1326                            License license = licenses.get(i);
1327    
1328                            if (i == 0) {
1329                                    sb.append("\r\n");
1330                            }
1331    
1332                            sb.append("\t\t<license osi-approved=\"");
1333                            sb.append(license.isOsiApproved());
1334                            sb.append("\">");
1335                            sb.append(license.getName());
1336                            sb.append("</license>\r\n");
1337    
1338                            if ((i + 1) == licenses.size()) {
1339                                    sb.append("\t");
1340                            }
1341                    }
1342    
1343                    return sb.toString();
1344            }
1345    
1346            public String getPluginPackageLiferayVersionsXml(
1347                    List<String> liferayVersions) {
1348    
1349                    if (liferayVersions.isEmpty()) {
1350                            return StringPool.BLANK;
1351                    }
1352    
1353                    StringBundler sb = new StringBundler(liferayVersions.size() * 3 + 2);
1354    
1355                    for (int i = 0; i < liferayVersions.size(); i++) {
1356                            String liferayVersion = liferayVersions.get(i);
1357    
1358                            if (i == 0) {
1359                                    sb.append("\r\n");
1360                            }
1361    
1362                            sb.append("\t\t<liferay-version>");
1363                            sb.append(liferayVersion);
1364                            sb.append("</liferay-version>\r\n");
1365    
1366                            if ((i + 1) == liferayVersions.size()) {
1367                                    sb.append("\t");
1368                            }
1369                    }
1370    
1371                    return sb.toString();
1372            }
1373    
1374            public Properties getPluginPackageProperties(File srcFile)
1375                    throws Exception {
1376    
1377                    File propertiesFile = new File(
1378                            srcFile + "/WEB-INF/liferay-plugin-package.properties");
1379    
1380                    if (!propertiesFile.exists()) {
1381                            return null;
1382                    }
1383    
1384                    String propertiesString = FileUtil.read(propertiesFile);
1385    
1386                    return PropertiesUtil.load(propertiesString);
1387            }
1388    
1389            public String getPluginPackageRequiredDeploymentContextsXml(
1390                    List<String> requiredDeploymentContexts) {
1391    
1392                    if (requiredDeploymentContexts.isEmpty()) {
1393                            return StringPool.BLANK;
1394                    }
1395    
1396                    StringBundler sb = new StringBundler(
1397                            requiredDeploymentContexts.size() * 3 + 2);
1398    
1399                    for (int i = 0; i < requiredDeploymentContexts.size(); i++) {
1400                            String requiredDeploymentContext = requiredDeploymentContexts.get(
1401                                    i);
1402    
1403                            if (i == 0) {
1404                                    sb.append("\r\n");
1405                            }
1406    
1407                            sb.append("\t\t<required-deployment-context>");
1408                            sb.append(requiredDeploymentContext);
1409                            sb.append("</required-deployment-context>\r\n");
1410    
1411                            if ((i + 1) == requiredDeploymentContexts.size()) {
1412                                    sb.append("\t");
1413                            }
1414                    }
1415    
1416                    return sb.toString();
1417            }
1418    
1419            public String getPluginPackageTagsXml(List<String> tags) {
1420                    if (tags.isEmpty()) {
1421                            return StringPool.BLANK;
1422                    }
1423    
1424                    StringBundler sb = new StringBundler(tags.size() * 3 + 2);
1425    
1426                    for (int i = 0; i < tags.size(); i++) {
1427                            String tag = tags.get(i);
1428    
1429                            if (i == 0) {
1430                                    sb.append("\r\n");
1431                            }
1432    
1433                            sb.append("\t\t<tag>");
1434                            sb.append(tag);
1435                            sb.append("</tag>\r\n");
1436    
1437                            if ((i + 1) == tags.size()) {
1438                                    sb.append("\t");
1439                            }
1440                    }
1441    
1442                    return sb.toString();
1443            }
1444    
1445            public Map<String, String> getPluginPackageXmlFilterMap(
1446                    PluginPackage pluginPackage) {
1447    
1448                    List<String> pluginTypes = pluginPackage.getTypes();
1449    
1450                    String pluginType = pluginTypes.get(0);
1451    
1452                    if (!pluginType.equals(getPluginType())) {
1453                            return null;
1454                    }
1455    
1456                    Map<String, String> filterMap = new HashMap<String, String>();
1457    
1458                    filterMap.put("author", pluginPackage.getAuthor());
1459                    filterMap.put("change_log", pluginPackage.getChangeLog());
1460                    filterMap.put(
1461                            "licenses",
1462                            getPluginPackageLicensesXml(pluginPackage.getLicenses()));
1463                    filterMap.put(
1464                            "liferay_versions",
1465                            getPluginPackageLiferayVersionsXml(
1466                                    pluginPackage.getLiferayVersions()));
1467                    filterMap.put("long_description", pluginPackage.getLongDescription());
1468                    filterMap.put("module_artifact_id", pluginPackage.getArtifactId());
1469                    filterMap.put("module_group_id", pluginPackage.getGroupId());
1470                    filterMap.put("module_version", pluginPackage.getVersion());
1471                    filterMap.put("page_url", pluginPackage.getPageURL());
1472                    filterMap.put("plugin_name", pluginPackage.getName());
1473                    filterMap.put("plugin_type", pluginType);
1474                    filterMap.put(
1475                            "plugin_type_name",
1476                            TextFormatter.format(pluginType, TextFormatter.J));
1477                    filterMap.put(
1478                            "recommended_deployment_context",
1479                            pluginPackage.getRecommendedDeploymentContext());
1480                    filterMap.put(
1481                            "required_deployment_contexts",
1482                            getPluginPackageRequiredDeploymentContextsXml(
1483                                    pluginPackage.getRequiredDeploymentContexts()));
1484                    filterMap.put("short_description", pluginPackage.getShortDescription());
1485                    filterMap.put("tags", getPluginPackageTagsXml(pluginPackage.getTags()));
1486    
1487                    return filterMap;
1488            }
1489    
1490            public String getPluginType() {
1491                    return null;
1492            }
1493    
1494            public String getServletContextIncludeFiltersContent(
1495                            double webXmlVersion, File srcFile)
1496                    throws Exception {
1497    
1498                    if (webXmlVersion < 2.4) {
1499                            return StringPool.BLANK;
1500                    }
1501    
1502                    Properties properties = getPluginPackageProperties(srcFile);
1503    
1504                    if (properties == null) {
1505                            return StringPool.BLANK;
1506                    }
1507    
1508                    if (!GetterUtil.getBoolean(
1509                                    properties.getProperty(
1510                                            "servlet-context-include-filters-enabled"), true)) {
1511    
1512                            return StringPool.BLANK;
1513                    }
1514    
1515                    return FileUtil.read(
1516                            DeployUtil.getResourcePath(
1517                                    "servlet-context-include-filters-web.xml"));
1518            }
1519    
1520            public String getSessionFiltersContent() throws Exception {
1521                    String sessionFiltersContent = FileUtil.read(
1522                            DeployUtil.getResourcePath("session-filters-web.xml"));
1523    
1524                    return sessionFiltersContent;
1525            }
1526    
1527            public String getSpeedFiltersContent(File srcFile) throws Exception {
1528                    boolean speedFiltersEnabled = true;
1529    
1530                    Properties properties = getPluginPackageProperties(srcFile);
1531    
1532                    if (properties != null) {
1533                            speedFiltersEnabled = GetterUtil.getBoolean(
1534                                    properties.getProperty("speed-filters-enabled"), true);
1535                    }
1536    
1537                    if (speedFiltersEnabled) {
1538                            String speedFiltersContent = FileUtil.read(
1539                                    DeployUtil.getResourcePath("speed-filters-web.xml"));
1540    
1541                            return speedFiltersContent;
1542                    }
1543                    else {
1544                            return StringPool.BLANK;
1545                    }
1546            }
1547    
1548            public boolean isJEEDeploymentEnabled() {
1549                    return GetterUtil.getBoolean(
1550                            PropsUtil.get(
1551                                    "auto.deploy." + ServerDetector.getServerId() +
1552                                            ".jee.deployment.enabled"));
1553            }
1554    
1555            public void mergeDirectory(File mergeDir, File targetDir) {
1556                    if ((mergeDir == null) || !mergeDir.exists()) {
1557                            return;
1558                    }
1559    
1560                    CopyTask.copyDirectory(mergeDir, targetDir, null, null, true, false);
1561            }
1562    
1563            public void postDeploy(String destDir, String deployDir) throws Exception {
1564                    if (appServerType.equals(ServerDetector.GLASSFISH_ID)) {
1565                            postDeployGlassfish(destDir, deployDir);
1566                    }
1567                    else if (appServerType.equals(ServerDetector.JBOSS_ID)) {
1568                            postDeployJBoss(destDir, deployDir);
1569                    }
1570                    else if (appServerType.equals(ServerDetector.WEBSPHERE_ID)) {
1571                            postDeployWebSphere(destDir, deployDir);
1572                    }
1573            }
1574    
1575            public void postDeployGlassfish(String destDir, String deployDir)
1576                    throws Exception {
1577    
1578                    FileUtil.delete(destDir + "/.autodeploystatus/" + deployDir);
1579            }
1580    
1581            public void postDeployJBoss(String destDir, String deployDir)
1582                    throws Exception {
1583    
1584                    FileUtil.write(
1585                            destDir + "/" + deployDir + ".dodeploy", StringPool.BLANK);
1586            }
1587    
1588            public void postDeployWebSphere(String destDir, String deployDir)
1589                    throws Exception {
1590    
1591                    if (Validator.isNull(
1592                                    PropsValues.AUTO_DEPLOY_WEBSPHERE_WSADMIN_APP_MANAGER_QUERY)) {
1593    
1594                            if (_log.isInfoEnabled()) {
1595                                    StringBundler sb = new StringBundler();
1596    
1597                                    sb.append("Do not install the plugin with wsadmin since the ");
1598                                    sb.append("property \"");
1599                                    sb.append(
1600                                            PropsKeys.AUTO_DEPLOY_WEBSPHERE_WSADMIN_APP_MANAGER_QUERY);
1601                                    sb.append("\"is not configured");
1602    
1603                                    _log.info(sb.toString());
1604                            }
1605    
1606                            return;
1607                    }
1608    
1609                    String wsadminContent = FileUtil.read(
1610                            DeployUtil.getResourcePath("wsadmin.py"));
1611    
1612                    String pluginServletContextName = deployDir.substring(
1613                            0, deployDir.length() - 4);
1614    
1615                    wsadminContent = StringUtil.replace(
1616                            wsadminContent,
1617                            new String[] {
1618                                    "${auto.deploy.dest.dir}",
1619                                    "${auto.deploy.websphere.wsadmin.app.manager.query}",
1620                                    "${plugin.servlet.context.name}"
1621                            },
1622                            new String[] {
1623                                    destDir,
1624                                    PropsValues.AUTO_DEPLOY_WEBSPHERE_WSADMIN_APP_MANAGER_QUERY,
1625                                    pluginServletContextName
1626                            });
1627    
1628                    String wsadminFileName = FileUtil.createTempFileName("py");
1629    
1630                    FileUtil.write(wsadminFileName, wsadminContent);
1631    
1632                    String webSphereHome = System.getenv("WAS_HOME");
1633    
1634                    if (_log.isInfoEnabled()) {
1635                            _log.info(
1636                                    "Installing plugin by executing " + webSphereHome +
1637                                            "\\bin\\wsadmin.bat -f " + wsadminFileName);
1638                    }
1639    
1640                    ProcessBuilder processBuilder = new ProcessBuilder(
1641                            webSphereHome + "\\bin\\wsadmin.bat", "-f", wsadminFileName);
1642    
1643                    processBuilder.redirectErrorStream(true);
1644    
1645                    Process process = processBuilder.start();
1646    
1647                    if (_log.isInfoEnabled()) {
1648                            InputStream inputStream = process.getInputStream();
1649    
1650                            String output = StringUtil.read(inputStream);
1651    
1652                            for (String line : StringUtil.split(output, CharPool.NEW_LINE)) {
1653                                    _log.info("Process output: " + line);
1654                            }
1655    
1656                            inputStream.close();
1657    
1658                            int exitValue = process.exitValue();
1659    
1660                            if (exitValue == 0) {
1661                                    _log.info(
1662                                            "Successfully executed command with an exit value of " +
1663                                                    exitValue);
1664                            }
1665                            else {
1666                                    _log.info(
1667                                            "Unsuccessfully executed command with an exit value of " +
1668                                                    exitValue);
1669                            }
1670                    }
1671    
1672                    FileUtil.delete(wsadminFileName);
1673            }
1674    
1675            public Map<String, String> processPluginPackageProperties(
1676                            File srcFile, String displayName, PluginPackage pluginPackage)
1677                    throws Exception {
1678    
1679                    if (pluginPackage == null) {
1680                            return null;
1681                    }
1682    
1683                    Properties properties = getPluginPackageProperties(srcFile);
1684    
1685                    if ((properties == null) || (properties.size() == 0)) {
1686                            return null;
1687                    }
1688    
1689                    Map<String, String> filterMap = getPluginPackageXmlFilterMap(
1690                            pluginPackage);
1691    
1692                    if (filterMap == null) {
1693                            return null;
1694                    }
1695    
1696                    copyDependencyXml(
1697                            "liferay-plugin-package.xml", srcFile + "/WEB-INF", filterMap,
1698                            true);
1699    
1700                    return filterMap;
1701            }
1702    
1703            /**
1704             * @see {@link PluginPackageUtil#_readPluginPackageServletContext(
1705             *      javax.servlet.ServletContext)}
1706             */
1707            public PluginPackage readPluginPackage(File file) {
1708                    if (!file.exists()) {
1709                            return null;
1710                    }
1711    
1712                    InputStream is = null;
1713                    ZipFile zipFile = null;
1714    
1715                    try {
1716                            boolean parseProps = false;
1717    
1718                            if (file.isDirectory()) {
1719                                    String path = file.getPath();
1720    
1721                                    File pluginPackageXmlFile = new File(
1722                                            file.getParent() + "/merge/" + file.getName() +
1723                                                    "/WEB-INF/liferay-plugin-package.xml");
1724    
1725                                    if (pluginPackageXmlFile.exists()) {
1726                                            is = new FileInputStream(pluginPackageXmlFile);
1727                                    }
1728                                    else {
1729                                            pluginPackageXmlFile = new File(
1730                                                    path + "/WEB-INF/liferay-plugin-package.xml");
1731    
1732                                            if (pluginPackageXmlFile.exists()) {
1733                                                    is = new FileInputStream(pluginPackageXmlFile);
1734                                            }
1735                                    }
1736    
1737                                    File pluginPackagePropertiesFile = new File(
1738                                            file.getParent() + "/merge/" + file.getName() +
1739                                                    "/WEB-INF/liferay-plugin-package.properties");
1740    
1741                                    if ((is == null) && pluginPackagePropertiesFile.exists()) {
1742                                            is = new FileInputStream(pluginPackagePropertiesFile);
1743    
1744                                            parseProps = true;
1745                                    }
1746                                    else {
1747                                            pluginPackagePropertiesFile = new File(
1748                                                    path + "/WEB-INF/liferay-plugin-package.properties");
1749    
1750                                            if ((is == null) && pluginPackagePropertiesFile.exists()) {
1751                                                    is = new FileInputStream(pluginPackagePropertiesFile);
1752    
1753                                                    parseProps = true;
1754                                            }
1755                                    }
1756                            }
1757                            else {
1758                                    zipFile = new ZipFile(file);
1759    
1760                                    File pluginPackageXmlFile = new File(
1761                                            file.getParent() + "/merge/" + file.getName() +
1762                                                    "/WEB-INF/liferay-plugin-package.xml");
1763    
1764                                    if (pluginPackageXmlFile.exists()) {
1765                                            is = new FileInputStream(pluginPackageXmlFile);
1766                                    }
1767                                    else {
1768                                            ZipEntry zipEntry = zipFile.getEntry(
1769                                                    "WEB-INF/liferay-plugin-package.xml");
1770    
1771                                            if (zipEntry != null) {
1772                                                    is = zipFile.getInputStream(zipEntry);
1773                                            }
1774                                    }
1775    
1776                                    File pluginPackagePropertiesFile = new File(
1777                                            file.getParent() + "/merge/" + file.getName() +
1778                                                    "/WEB-INF/liferay-plugin-package.properties");
1779    
1780                                    if ((is == null) && pluginPackagePropertiesFile.exists()) {
1781                                            is = new FileInputStream(pluginPackagePropertiesFile);
1782    
1783                                            parseProps = true;
1784                                    }
1785                                    else {
1786                                            ZipEntry zipEntry = zipFile.getEntry(
1787                                                    "WEB-INF/liferay-plugin-package.properties");
1788    
1789                                            if ((is == null) && (zipEntry != null)) {
1790                                                    is = zipFile.getInputStream(zipEntry);
1791    
1792                                                    parseProps = true;
1793                                            }
1794                                    }
1795                            }
1796    
1797                            if (is == null) {
1798                                    if (_log.isInfoEnabled()) {
1799                                            _log.info(
1800                                                    file.getPath() + " does not have a " +
1801                                                            "WEB-INF/liferay-plugin-package.xml or " +
1802                                                                    "WEB-INF/liferay-plugin-package.properties");
1803                                    }
1804    
1805                                    return null;
1806                            }
1807    
1808                            if (parseProps) {
1809                                    String displayName = getDisplayName(file);
1810    
1811                                    String propertiesString = StringUtil.read(is);
1812    
1813                                    Properties properties = PropertiesUtil.load(propertiesString);
1814    
1815                                    return PluginPackageUtil.readPluginPackageProperties(
1816                                            displayName, properties);
1817                            }
1818                            else {
1819                                    String xml = StringUtil.read(is);
1820    
1821                                    xml = XMLFormatter.fixProlog(xml);
1822    
1823                                    return PluginPackageUtil.readPluginPackageXml(xml);
1824                            }
1825                    }
1826                    catch (Exception e) {
1827                            _log.error(file.getPath() + ": " + e.toString());
1828                    }
1829                    finally {
1830                            if (is != null) {
1831                                    try {
1832                                            is.close();
1833                                    }
1834                                    catch (IOException ioe) {
1835                                    }
1836                            }
1837    
1838                            if (zipFile != null) {
1839                                    try {
1840                                            zipFile.close();
1841                                    }
1842                                    catch (IOException ioe) {
1843                                    }
1844                            }
1845                    }
1846    
1847                    return null;
1848            }
1849    
1850            public void rewriteFiles(File srcDir) throws Exception {
1851                    String[] fileNames = FileUtil.listFiles(srcDir + "/WEB-INF/");
1852    
1853                    for (String fileName : fileNames) {
1854                            String shortFileName = GetterUtil.getString(
1855                                    FileUtil.getShortFileName(fileName));
1856    
1857                            // LEP-6415
1858    
1859                            if (shortFileName.equalsIgnoreCase("mule-config.xml")) {
1860                                    continue;
1861                            }
1862    
1863                            String ext = GetterUtil.getString(FileUtil.getExtension(fileName));
1864    
1865                            if (!ext.equalsIgnoreCase("xml")) {
1866                                    continue;
1867                            }
1868    
1869                            // Make sure to rewrite any XML files to include external entities
1870                            // into same file. See LEP-3142.
1871    
1872                            File file = new File(srcDir + "/WEB-INF/" + fileName);
1873    
1874                            try {
1875                                    Document doc = SAXReaderUtil.read(file);
1876    
1877                                    String content = doc.formattedString(StringPool.TAB, true);
1878    
1879                                    FileUtil.write(file, content);
1880                            }
1881                            catch (Exception e) {
1882                                    if (_log.isWarnEnabled()) {
1883                                            _log.warn(
1884                                                    "Unable to format " + file + ": " + e.getMessage());
1885                                    }
1886                            }
1887                    }
1888            }
1889    
1890            public String secureWebXml(
1891                            String content, boolean hasCustomServletListener,
1892                            boolean securityManagerEnabled)
1893                    throws Exception {
1894    
1895                    if (!hasCustomServletListener && !securityManagerEnabled) {
1896                            return content;
1897                    }
1898    
1899                    Document document = SAXReaderUtil.read(content);
1900    
1901                    Element rootElement = document.getRootElement();
1902    
1903                    List<String> listenerClasses = new ArrayList<String>();
1904    
1905                    List<Element> listenerElements = rootElement.elements("listener");
1906    
1907                    for (Element listenerElement : listenerElements) {
1908                            String listenerClass = GetterUtil.getString(
1909                                    listenerElement.elementText("listener-class"));
1910    
1911                            if (listenerClass.equals(
1912                                            SecurePluginContextListener.class.getName())) {
1913    
1914                                    continue;
1915                            }
1916    
1917                            listenerClasses.add(listenerClass);
1918    
1919                            listenerElement.detach();
1920                    }
1921    
1922                    Element contextParamElement = rootElement.addElement("context-param");
1923    
1924                    DocUtil.add(contextParamElement, "param-name", "portalListenerClasses");
1925                    DocUtil.add(
1926                            contextParamElement, "param-value",
1927                            StringUtil.merge(listenerClasses));
1928    
1929                    if (securityManagerEnabled) {
1930                            List<Element> servletElements = rootElement.elements("servlet");
1931    
1932                            for (Element servletElement : servletElements) {
1933                                    Element servletClassElement = servletElement.element(
1934                                            "servlet-class");
1935    
1936                                    String servletClass = GetterUtil.getString(
1937                                            servletClassElement.getText());
1938    
1939                                    if (servletClass.equals(
1940                                                    PortalClassLoaderServlet.class.getName()) ||
1941                                            servletClass.equals(PortletServlet.class.getName())) {
1942    
1943                                            continue;
1944                                    }
1945    
1946                                    servletClassElement.setText(SecureServlet.class.getName());
1947    
1948                                    Element initParamElement = servletElement.addElement(
1949                                            "init-param");
1950    
1951                                    DocUtil.add(initParamElement, "param-name", "servlet-class");
1952                                    DocUtil.add(initParamElement, "param-value", servletClass);
1953                            }
1954                    }
1955    
1956                    return document.compactString();
1957            }
1958    
1959            public void setAppServerType(String appServerType) {
1960                    this.appServerType = appServerType;
1961            }
1962    
1963            public void setAuiTaglibDTD(String auiTaglibDTD) {
1964                    this.auiTaglibDTD = auiTaglibDTD;
1965            }
1966    
1967            public void setBaseDir(String baseDir) {
1968                    this.baseDir = baseDir;
1969            }
1970    
1971            public void setDestDir(String destDir) {
1972                    this.destDir = destDir;
1973            }
1974    
1975            public void setFilePattern(String filePattern) {
1976                    this.filePattern = filePattern;
1977            }
1978    
1979            public void setJars(List<String> jars) {
1980                    this.jars = jars;
1981            }
1982    
1983            public void setJbossPrefix(String jbossPrefix) {
1984                    this.jbossPrefix = jbossPrefix;
1985            }
1986    
1987            public void setPortletExtTaglibDTD(String portletExtTaglibDTD) {
1988                    this.portletExtTaglibDTD = portletExtTaglibDTD;
1989            }
1990    
1991            public void setPortletTaglibDTD(String portletTaglibDTD) {
1992                    this.portletTaglibDTD = portletTaglibDTD;
1993            }
1994    
1995            public void setSecurityTaglibDTD(String securityTaglibDTD) {
1996                    this.securityTaglibDTD = securityTaglibDTD;
1997            }
1998    
1999            public void setThemeTaglibDTD(String themeTaglibDTD) {
2000                    this.themeTaglibDTD = themeTaglibDTD;
2001            }
2002    
2003            public void setTomcatLibDir(String tomcatLibDir) {
2004                    this.tomcatLibDir = tomcatLibDir;
2005            }
2006    
2007            public void setUiTaglibDTD(String uiTaglibDTD) {
2008                    this.uiTaglibDTD = uiTaglibDTD;
2009            }
2010    
2011            public void setUnpackWar(boolean unpackWar) {
2012                    this.unpackWar = unpackWar;
2013            }
2014    
2015            public void setUtilTaglibDTD(String utilTaglibDTD) {
2016                    this.utilTaglibDTD = utilTaglibDTD;
2017            }
2018    
2019            public void setWars(List<String> wars) {
2020                    this.wars = wars;
2021            }
2022    
2023            public void updateDeployDirectory(File srcFile) throws Exception {
2024            }
2025    
2026            public void updateGeronimoWebXml(
2027                            File srcFile, String displayName, PluginPackage pluginPackage)
2028                    throws Exception {
2029    
2030                    if (!appServerType.equals(ServerDetector.GERONIMO_ID)) {
2031                            return;
2032                    }
2033    
2034                    File geronimoWebXml = new File(srcFile + "/WEB-INF/geronimo-web.xml");
2035    
2036                    Document document = SAXReaderUtil.read(geronimoWebXml);
2037    
2038                    Element rootElement = document.getRootElement();
2039    
2040                    Element environmentElement = rootElement.element("environment");
2041    
2042                    Element moduleIdElement = environmentElement.element("moduleId");
2043    
2044                    Element artifactIdElement = moduleIdElement.element("artifactId");
2045    
2046                    artifactIdElement.setText(displayName);
2047    
2048                    Element versionElement = moduleIdElement.element("version");
2049    
2050                    versionElement.setText(pluginPackage.getVersion());
2051    
2052                    String content = document.formattedString();
2053    
2054                    FileUtil.write(geronimoWebXml, content);
2055    
2056                    if (_log.isInfoEnabled()) {
2057                            _log.info("Modifying Geronimo " + geronimoWebXml);
2058                    }
2059            }
2060    
2061            public String updateLiferayWebXml(
2062                            double webXmlVersion, File srcFile, String webXmlContent)
2063                    throws Exception {
2064    
2065                    boolean liferayWebXmlEnabled = true;
2066    
2067                    Properties properties = getPluginPackageProperties(srcFile);
2068    
2069                    if (properties != null) {
2070                            liferayWebXmlEnabled = GetterUtil.getBoolean(
2071                                    properties.getProperty("liferay-web-xml-enabled"), true);
2072                    }
2073    
2074                    webXmlContent = WebXMLBuilder.organizeWebXML(webXmlContent);
2075    
2076                    int x = webXmlContent.indexOf("<filter>");
2077                    int y = webXmlContent.lastIndexOf("</filter-mapping>");
2078    
2079                    String webXmlFiltersContent = StringPool.BLANK;
2080    
2081                    if ((x == -1) || (y == -1)) {
2082                            x = webXmlContent.lastIndexOf("</display-name>") + 15;
2083                            y = x;
2084                    }
2085                    else {
2086                            if (liferayWebXmlEnabled && (webXmlVersion > 2.3)) {
2087                                    webXmlFiltersContent = webXmlContent.substring(x, y + 17);
2088    
2089                                    y = y + 17;
2090                            }
2091                            else {
2092                                    x = y + 17;
2093                                    y = y + 17;
2094                            }
2095                    }
2096    
2097                    if (webXmlVersion < 2.4) {
2098                            webXmlContent =
2099                                    webXmlContent.substring(0, x) +
2100                                            getExtraFiltersContent(webXmlVersion, srcFile) +
2101                                                    webXmlContent.substring(y);
2102    
2103                            return webXmlContent;
2104                    }
2105    
2106                    String filtersContent =
2107                            webXmlFiltersContent +
2108                                    getExtraFiltersContent(webXmlVersion, srcFile);
2109    
2110                    String liferayWebXmlContent = FileUtil.read(
2111                            DeployUtil.getResourcePath("web.xml"));
2112    
2113                    int z = liferayWebXmlContent.indexOf("</web-app>");
2114    
2115                    liferayWebXmlContent =
2116                            liferayWebXmlContent.substring(0, z) + filtersContent +
2117                                    liferayWebXmlContent.substring(z);
2118    
2119                    liferayWebXmlContent = WebXMLBuilder.organizeWebXML(
2120                            liferayWebXmlContent);
2121    
2122                    FileUtil.write(
2123                            srcFile + "/WEB-INF/liferay-web.xml", liferayWebXmlContent);
2124    
2125                    webXmlContent =
2126                            webXmlContent.substring(0, x) + getInvokerFilterContent() +
2127                                    webXmlContent.substring(y);
2128    
2129                    return webXmlContent;
2130            }
2131    
2132            public void updateWebXml(
2133                            File webXml, File srcFile, String displayName,
2134                            PluginPackage pluginPackage)
2135                    throws Exception {
2136    
2137                    // Check version
2138    
2139                    String content = FileUtil.read(webXml);
2140    
2141                    content = WebXMLBuilder.organizeWebXML(content);
2142    
2143                    int x = content.indexOf("<display-name>");
2144    
2145                    if (x != -1) {
2146                            int y = content.indexOf("</display-name>", x);
2147    
2148                            y = content.indexOf(">", y) + 1;
2149    
2150                            content = content.substring(0, x) + content.substring(y);
2151                    }
2152    
2153                    Document document = SAXReaderUtil.read(content);
2154    
2155                    Element rootElement = document.getRootElement();
2156    
2157                    double webXmlVersion = GetterUtil.getDouble(
2158                            rootElement.attributeValue("version"), 2.3);
2159    
2160                    if (!PropsValues.TCK_URL && (webXmlVersion <= 2.3)) {
2161                            throw new AutoDeployException(
2162                                    webXml.getName() +
2163                                            " must be updated to the Servlet 2.4 specification");
2164                    }
2165    
2166                    // Plugin context listener
2167    
2168                    StringBundler sb = new StringBundler(5);
2169    
2170                    sb.append("<listener>");
2171                    sb.append("<listener-class>");
2172    
2173                    boolean hasCustomServletListener = false;
2174    
2175                    List<Element> listenerElements = rootElement.elements("listener");
2176    
2177                    for (Element listenerElement : listenerElements) {
2178                            String listenerClass = GetterUtil.getString(
2179                                    listenerElement.elementText("listener-class"));
2180    
2181                            if (listenerClass.equals(
2182                                            SerializableSessionAttributeListener.class.getName()) ||
2183                                    listenerClass.equals(
2184                                            SecurePluginContextListener.class.getName())) {
2185    
2186                                    continue;
2187                            }
2188    
2189                            hasCustomServletListener = true;
2190    
2191                            break;
2192                    }
2193    
2194                    boolean securityManagerEnabled = false;
2195    
2196                    Properties properties = getPluginPackageProperties(srcFile);
2197    
2198                    if (properties != null) {
2199                            securityManagerEnabled = GetterUtil.getBoolean(
2200                                    properties.getProperty("security-manager-enabled"));
2201                    }
2202    
2203                    if (hasCustomServletListener || securityManagerEnabled) {
2204                            sb.append(SecurePluginContextListener.class.getName());
2205                    }
2206                    else {
2207                            sb.append(PluginContextListener.class.getName());
2208                    }
2209    
2210                    sb.append("</listener-class>");
2211                    sb.append("</listener>");
2212    
2213                    String pluginContextListenerContent = sb.toString();
2214    
2215                    // Merge content
2216    
2217                    String extraContent = getExtraContent(
2218                            webXmlVersion, srcFile, displayName);
2219    
2220                    int pos = content.indexOf("<listener>");
2221    
2222                    if (pos == -1) {
2223                            pos = content.indexOf("</web-app>");
2224                    }
2225    
2226                    String newContent =
2227                            content.substring(0, pos) + pluginContextListenerContent +
2228                                    extraContent + content.substring(pos);
2229    
2230                    // Update liferay-web.xml
2231    
2232                    newContent = updateLiferayWebXml(webXmlVersion, srcFile, newContent);
2233    
2234                    // Update web.xml
2235    
2236                    newContent = secureWebXml(
2237                            newContent, hasCustomServletListener, securityManagerEnabled);
2238    
2239                    newContent = WebXMLBuilder.organizeWebXML(newContent);
2240    
2241                    FileUtil.write(webXml, newContent, true);
2242    
2243                    if (_log.isInfoEnabled()) {
2244                            _log.info("Modifying Servlet " + webXmlVersion + " " + webXml);
2245                    }
2246            }
2247    
2248            protected String appServerType;
2249            protected String auiTaglibDTD;
2250            protected String baseDir;
2251            protected String destDir;
2252            protected String filePattern;
2253            protected List<String> jars;
2254            protected String jbossPrefix;
2255            protected String portletExtTaglibDTD;
2256            protected String portletTaglibDTD;
2257            protected String securityTaglibDTD;
2258            protected String themeTaglibDTD;
2259            protected String tomcatLibDir;
2260            protected String uiTaglibDTD;
2261            protected boolean unpackWar;
2262            protected String utilTaglibDTD;
2263            protected List<String> wars;
2264    
2265            private static final String _PORTAL_CLASS_LOADER =
2266                    "com.liferay.support.tomcat.loader.PortalClassLoader";
2267    
2268            private static Log _log = LogFactoryUtil.getLog(BaseDeployer.class);
2269    
2270    }