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