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