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