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