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