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