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.kernel.deploy.Deployer;
019    import com.liferay.portal.kernel.deploy.auto.AutoDeployException;
020    import com.liferay.portal.kernel.log.Log;
021    import com.liferay.portal.kernel.log.LogFactoryUtil;
022    import com.liferay.portal.kernel.plugin.License;
023    import com.liferay.portal.kernel.plugin.PluginPackage;
024    import com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilter;
025    import com.liferay.portal.kernel.util.FileUtil;
026    import com.liferay.portal.kernel.util.GetterUtil;
027    import com.liferay.portal.kernel.util.HttpUtil;
028    import com.liferay.portal.kernel.util.PropertiesUtil;
029    import com.liferay.portal.kernel.util.PropsKeys;
030    import com.liferay.portal.kernel.util.ServerDetector;
031    import com.liferay.portal.kernel.util.StringBundler;
032    import com.liferay.portal.kernel.util.StringPool;
033    import com.liferay.portal.kernel.util.StringUtil;
034    import com.liferay.portal.kernel.util.SystemProperties;
035    import com.liferay.portal.kernel.util.TextFormatter;
036    import com.liferay.portal.kernel.util.Time;
037    import com.liferay.portal.kernel.util.Validator;
038    import com.liferay.portal.kernel.xml.Document;
039    import com.liferay.portal.kernel.xml.Element;
040    import com.liferay.portal.kernel.xml.SAXReaderUtil;
041    import com.liferay.portal.plugin.PluginPackageUtil;
042    import com.liferay.portal.tools.WebXMLBuilder;
043    import com.liferay.portal.util.ExtRegistry;
044    import com.liferay.portal.util.InitUtil;
045    import com.liferay.portal.util.PortalUtil;
046    import com.liferay.portal.util.PrefsPropsUtil;
047    import com.liferay.portal.util.PropsUtil;
048    import com.liferay.portal.util.PropsValues;
049    import com.liferay.util.ant.CopyTask;
050    import com.liferay.util.ant.DeleteTask;
051    import com.liferay.util.ant.ExpandTask;
052    import com.liferay.util.ant.UpToDateTask;
053    import com.liferay.util.ant.WarTask;
054    import com.liferay.util.xml.XMLFormatter;
055    
056    import java.io.File;
057    import java.io.FileInputStream;
058    import java.io.IOException;
059    import java.io.InputStream;
060    
061    import java.util.ArrayList;
062    import java.util.HashMap;
063    import java.util.List;
064    import java.util.Map;
065    import java.util.Properties;
066    import java.util.Set;
067    import java.util.zip.ZipEntry;
068    import java.util.zip.ZipFile;
069    
070    import org.apache.oro.io.GlobFilenameFilter;
071    
072    /**
073     * @author Brian Wing Shun Chan
074     * @author Sandeep Soni
075     */
076    public class BaseDeployer implements Deployer {
077    
078            public static final String DEPLOY_TO_PREFIX = "DEPLOY_TO__";
079    
080            public static void main(String[] args) {
081                    InitUtil.initWithSpring();
082    
083                    List<String> wars = new ArrayList<String>();
084                    List<String> jars = new ArrayList<String>();
085    
086                    for (String arg : args) {
087                            String fileName = arg.toLowerCase();
088    
089                            if (fileName.endsWith(".war")) {
090                                    wars.add(arg);
091                            }
092                            else if (fileName.endsWith(".jar")) {
093                                    jars.add(arg);
094                            }
095                    }
096    
097                    new BaseDeployer(wars, jars);
098            }
099    
100            public BaseDeployer() {
101            }
102    
103            public BaseDeployer(List<String> wars, List<String> jars) {
104                    baseDir = System.getProperty("deployer.base.dir");
105                    destDir = System.getProperty("deployer.dest.dir");
106                    appServerType = System.getProperty("deployer.app.server.type");
107                    auiTaglibDTD = System.getProperty("deployer.aui.taglib.dtd");
108                    portletTaglibDTD = System.getProperty("deployer.portlet.taglib.dtd");
109                    portletExtTaglibDTD = System.getProperty(
110                            "deployer.portlet.ext.taglib.dtd");
111                    securityTaglibDTD = System.getProperty("deployer.security.taglib.dtd");
112                    themeTaglibDTD = System.getProperty("deployer.theme.taglib.dtd");
113                    uiTaglibDTD = System.getProperty("deployer.ui.taglib.dtd");
114                    utilTaglibDTD = System.getProperty("deployer.util.taglib.dtd");
115                    unpackWar = GetterUtil.getBoolean(
116                            System.getProperty("deployer.unpack.war"), true);
117                    filePattern = System.getProperty("deployer.file.pattern");
118                    jbossPrefix = GetterUtil.getString(
119                            System.getProperty("deployer.jboss.prefix"));
120                    tomcatLibDir = System.getProperty("deployer.tomcat.lib.dir");
121                    this.wars = wars;
122                    this.jars = jars;
123    
124                    checkArguments();
125    
126                    String context = System.getProperty("deployer.context");
127    
128                    try {
129                            deploy(context);
130                    }
131                    catch (Exception e) {
132                            e.printStackTrace();
133                    }
134            }
135    
136            public void addExtJar(List<String> jars, String resource)
137                    throws Exception {
138    
139                    Set<String> servletContextNames = ExtRegistry.getServletContextNames();
140    
141                    for (String servletContextName : servletContextNames) {
142                            String extResource =
143                                    "ext-" + servletContextName + resource.substring(3);
144    
145                            String path = DeployUtil.getResourcePath(extResource);
146    
147                            if (_log.isDebugEnabled()) {
148                                    if (path == null) {
149                                            _log.debug("Resource " + extResource + " is not available");
150                                    }
151                                    else {
152                                            _log.debug(
153                                                    "Resource " + extResource + " is available at " + path);
154                                    }
155                            }
156    
157                            if (path != null) {
158                                    jars.add(path);
159                            }
160                    }
161            }
162    
163            public void addRequiredJar(List<String> jars, String resource)
164                    throws Exception {
165    
166                    String path = DeployUtil.getResourcePath(resource);
167    
168                    if (path == null) {
169                            throw new RuntimeException(
170                                    "Resource " + resource + " does not exist");
171                    }
172    
173                    if (_log.isDebugEnabled()) {
174                            _log.debug("Resource " + resource + " is available at " + path);
175                    }
176    
177                    jars.add(path);
178            }
179    
180            public void checkArguments() {
181                    if (Validator.isNull(baseDir)) {
182                            throw new IllegalArgumentException(
183                                    "The system property deployer.base.dir is not set");
184                    }
185    
186                    if (Validator.isNull(destDir)) {
187                            throw new IllegalArgumentException(
188                                    "The system property deployer.dest.dir is not set");
189                    }
190    
191                    if (Validator.isNull(appServerType)) {
192                            throw new IllegalArgumentException(
193                                    "The system property deployer.app.server.type is not set");
194                    }
195    
196                    if (!appServerType.equals(ServerDetector.GERONIMO_ID) &&
197                            !appServerType.equals(ServerDetector.GLASSFISH_ID) &&
198                            !appServerType.equals(ServerDetector.JBOSS_ID) &&
199                            !appServerType.equals(ServerDetector.JONAS_ID) &&
200                            !appServerType.equals(ServerDetector.JETTY_ID) &&
201                            !appServerType.equals(ServerDetector.OC4J_ID) &&
202                            !appServerType.equals(ServerDetector.RESIN_ID) &&
203                            !appServerType.equals(ServerDetector.TOMCAT_ID) &&
204                            !appServerType.equals(ServerDetector.WEBLOGIC_ID) &&
205                            !appServerType.equals(ServerDetector.WEBSPHERE_ID)) {
206    
207                            throw new IllegalArgumentException(
208                                    appServerType + " is not a valid application server type");
209                    }
210    
211                    if (appServerType.equals(ServerDetector.GLASSFISH_ID) ||
212                            appServerType.equals(ServerDetector.WEBLOGIC_ID)) {
213    
214                            unpackWar = false;
215                    }
216    
217                    if (Validator.isNotNull(jbossPrefix) &&
218                            !Validator.isNumber(jbossPrefix)) {
219    
220                            jbossPrefix = "1";
221                    }
222            }
223    
224            public void copyDependencyXml(String fileName, String targetDir)
225                    throws Exception {
226    
227                    copyDependencyXml(fileName, targetDir, null);
228            }
229    
230            public void copyDependencyXml(
231                            String fileName, String targetDir, Map<String, String> filterMap)
232                    throws Exception {
233    
234                    copyDependencyXml(fileName, targetDir, filterMap, false);
235            }
236    
237            public void copyDependencyXml(
238                            String fileName, String targetDir, Map<String, String> filterMap,
239                            boolean overwrite)
240                    throws Exception {
241    
242                    DeployUtil.copyDependencyXml(
243                            fileName, targetDir, fileName, filterMap, overwrite);
244            }
245    
246            public void copyJars(File srcFile, PluginPackage pluginPackage)
247                    throws Exception {
248    
249                    for (int i = 0; i < jars.size(); i++) {
250                            String jarFullName = jars.get(i);
251    
252                            String jarName = jarFullName.substring(
253                                    jarFullName.lastIndexOf("/") + 1, jarFullName.length());
254    
255                            if ((!appServerType.equals(ServerDetector.TOMCAT_ID)) ||
256                                    (appServerType.equals(ServerDetector.TOMCAT_ID) &&
257                                     !jarFullName.equals("util-java.jar"))) {
258    
259                                    FileUtil.copyFile(
260                                            jarFullName, srcFile + "/WEB-INF/lib/" + jarName, true);
261                            }
262                    }
263    
264                    FileUtil.delete(srcFile + "/WEB-INF/lib/util-jsf.jar");
265            }
266    
267            public void copyPortalDependencies(File srcFile) throws Exception {
268                    Properties properties = getPluginPackageProperties(srcFile);
269    
270                    if (properties == null) {
271                            return;
272                    }
273    
274                    // jars
275    
276                    String[] portalJars = StringUtil.split(
277                            properties.getProperty(
278                                    "portal-dependency-jars",
279                                    properties.getProperty("portal.dependency.jars")));
280    
281                    for (int i = 0; i < portalJars.length; i++) {
282                            String portalJar = portalJars[i].trim();
283    
284                            portalJar = fixPortalDependencyJar(portalJar);
285    
286                            if (_log.isDebugEnabled()) {
287                                    _log.debug("Copy portal JAR " + portalJar);
288                            }
289    
290                            try {
291                                    String portalJarPath = PortalUtil.getPortalLibDir() + portalJar;
292    
293                                    FileUtil.copyFile(
294                                            portalJarPath, srcFile + "/WEB-INF/lib/" + portalJar, true);
295                            }
296                            catch (Exception e) {
297                                    _log.error("Unable to copy portal JAR " + portalJar, e);
298                            }
299                    }
300    
301                    // tlds
302    
303                    String[] portalTlds = StringUtil.split(
304                            properties.getProperty(
305                                    "portal-dependency-tlds",
306                                    properties.getProperty("portal.dependency.tlds")));
307    
308                    for (int i = 0; i < portalTlds.length; i++) {
309                            String portalTld = portalTlds[i].trim();
310    
311                            if (_log.isDebugEnabled()) {
312                                    _log.debug("Copy portal TLD " + portalTld);
313                            }
314    
315                            try {
316                                    String portalTldPath = DeployUtil.getResourcePath(portalTld);
317    
318                                    FileUtil.copyFile(
319                                            portalTldPath, srcFile + "/WEB-INF/tld/" + portalTld, true);
320                            }
321                            catch (Exception e) {
322                                    _log.error("Unable to copy portal TLD " + portalTld, e);
323                            }
324                    }
325    
326                    // commons-logging*.jar
327    
328                    File pluginLibDir = new File(srcFile + "/WEB-INF/lib/");
329    
330                    if (PropsValues.AUTO_DEPLOY_COPY_COMMONS_LOGGING) {
331                            String[] commonsLoggingJars = pluginLibDir.list(
332                                    new GlobFilenameFilter("commons-logging*.jar"));
333    
334                            if ((commonsLoggingJars == null) ||
335                                    (commonsLoggingJars.length == 0)) {
336    
337                                    String portalJarPath =
338                                            PortalUtil.getPortalLibDir() + "commons-logging.jar";
339    
340                                    FileUtil.copyFile(
341                                            portalJarPath, srcFile + "/WEB-INF/lib/commons-logging.jar",
342                                            true);
343                            }
344                    }
345    
346                    // log4j*.jar
347    
348                    if (PropsValues.AUTO_DEPLOY_COPY_LOG4J) {
349                            String[] log4jJars = pluginLibDir.list(
350                                    new GlobFilenameFilter("log4j*.jar"));
351    
352                            if ((log4jJars == null) || (log4jJars.length == 0)) {
353                                    String portalJarPath =
354                                            PortalUtil.getPortalLibDir() + "log4j.jar";
355    
356                                    FileUtil.copyFile(
357                                            portalJarPath, srcFile + "/WEB-INF/lib/log4j.jar", true);
358                            }
359                    }
360            }
361    
362            public void copyProperties(File srcFile, PluginPackage pluginPackage)
363                    throws Exception {
364    
365                    if (PropsValues.AUTO_DEPLOY_COPY_COMMONS_LOGGING) {
366                            copyDependencyXml(
367                                    "logging.properties", srcFile + "/WEB-INF/classes");
368                    }
369    
370                    if (PropsValues.AUTO_DEPLOY_COPY_LOG4J) {
371                            copyDependencyXml("log4j.properties", srcFile + "/WEB-INF/classes");
372                    }
373    
374                    File servicePropertiesFile = new File(
375                            srcFile.getAbsolutePath() + "/WEB-INF/classes/service.properties");
376    
377                    if (servicePropertiesFile.exists()) {
378                            File portletPropertiesFile = new File(
379                                    srcFile.getAbsolutePath() +
380                                            "/WEB-INF/classes/portlet.properties");
381    
382                            if (!portletPropertiesFile.exists()) {
383                                    String pluginPackageName = null;
384    
385                                    if (pluginPackage != null) {
386                                            pluginPackageName = pluginPackage.getName();
387                                    }
388                                    else {
389                                            pluginPackageName = srcFile.getName();
390                                    }
391    
392                                    FileUtil.write(
393                                            portletPropertiesFile,
394                                            "plugin.package.name=" + pluginPackageName);
395                            }
396                    }
397            }
398    
399            public void copyTlds(File srcFile, PluginPackage pluginPackage)
400                    throws Exception {
401    
402                    if (Validator.isNotNull(auiTaglibDTD)) {
403                            FileUtil.copyFile(
404                                    auiTaglibDTD, srcFile + "/WEB-INF/tld/aui.tld", true);
405                    }
406    
407                    if (Validator.isNotNull(portletTaglibDTD)) {
408                            FileUtil.copyFile(
409                                    portletTaglibDTD, srcFile + "/WEB-INF/tld/liferay-portlet.tld",
410                                    true);
411                    }
412    
413                    if (Validator.isNotNull(portletExtTaglibDTD)) {
414                            FileUtil.copyFile(
415                                    portletExtTaglibDTD,
416                                    srcFile + "/WEB-INF/tld/liferay-portlet-ext.tld", true);
417                    }
418    
419                    if (Validator.isNotNull(securityTaglibDTD)) {
420                            FileUtil.copyFile(
421                                    securityTaglibDTD,
422                                    srcFile + "/WEB-INF/tld/liferay-security.tld", true);
423                    }
424    
425                    if (Validator.isNotNull(themeTaglibDTD)) {
426                            FileUtil.copyFile(
427                                    themeTaglibDTD, srcFile + "/WEB-INF/tld/liferay-theme.tld",
428                                    true);
429                    }
430    
431                    if (Validator.isNotNull(uiTaglibDTD)) {
432                            FileUtil.copyFile(
433                                    uiTaglibDTD, srcFile + "/WEB-INF/tld/liferay-ui.tld", true);
434                    }
435    
436                    if (Validator.isNotNull(utilTaglibDTD)) {
437                            FileUtil.copyFile(
438                                    utilTaglibDTD, srcFile + "/WEB-INF/tld/liferay-util.tld", true);
439                    }
440            }
441    
442            public void copyXmls(
443                            File srcFile, String displayName, PluginPackage pluginPackage)
444                    throws Exception {
445    
446                    if (appServerType.equals(ServerDetector.GERONIMO_ID)) {
447                            copyDependencyXml("geronimo-web.xml", srcFile + "/WEB-INF");
448                    }
449                    else if (appServerType.equals(ServerDetector.JBOSS_ID)) {
450                            copyDependencyXml(
451                                    "jboss-deployment-structure.xml", srcFile + "/WEB-INF");
452                    }
453                    else if (appServerType.equals(ServerDetector.WEBLOGIC_ID)) {
454                            copyDependencyXml("weblogic.xml", srcFile + "/WEB-INF");
455                    }
456                    else if (appServerType.equals(ServerDetector.WEBSPHERE_ID)) {
457                            copyDependencyXml("ibm-web-ext.xmi", srcFile + "/WEB-INF");
458                    }
459    
460                    copyDependencyXml("web.xml", srcFile + "/WEB-INF");
461            }
462    
463            public void deploy(String context) throws Exception {
464                    try {
465                            File baseDirFile = new File(baseDir);
466    
467                            File[] files = baseDirFile.listFiles();
468    
469                            if (files == null) {
470                                    return;
471                            }
472    
473                            files = FileUtil.sortFiles(files);
474    
475                            for (int i = 0; i < files.length; i++) {
476                                    File srcFile = files[i];
477    
478                                    String fileName = srcFile.getName().toLowerCase();
479    
480                                    boolean deploy = false;
481    
482                                    if (fileName.endsWith(".war") || fileName.endsWith(".zip")) {
483                                            deploy = true;
484    
485                                            if (wars.size() > 0) {
486                                                    if (!wars.contains(srcFile.getName())) {
487                                                            deploy = false;
488                                                    }
489                                            }
490                                            else if (Validator.isNotNull(filePattern)) {
491                                                    if (!StringUtil.matchesIgnoreCase(
492                                                                    fileName, filePattern)) {
493    
494                                                            deploy = false;
495                                                    }
496                                            }
497                                    }
498    
499                                    if (deploy) {
500                                            deployFile(srcFile, context);
501                                    }
502                            }
503                    }
504                    catch (Exception e) {
505                            e.printStackTrace();
506                    }
507            }
508    
509            public void deployDirectory(
510                            File srcFile, File mergeDir, File deployDir, String displayName,
511                            boolean overwrite, PluginPackage pluginPackage)
512                    throws Exception {
513    
514                    rewriteFiles(srcFile);
515    
516                    mergeDirectory(mergeDir, srcFile);
517    
518                    processPluginPackageProperties(srcFile, displayName, pluginPackage);
519    
520                    copyJars(srcFile, pluginPackage);
521                    copyProperties(srcFile, pluginPackage);
522                    copyTlds(srcFile, pluginPackage);
523                    copyXmls(srcFile, displayName, pluginPackage);
524                    copyPortalDependencies(srcFile);
525    
526                    updateGeronimoWebXml(srcFile, displayName, pluginPackage);
527    
528                    File webXml = new File(srcFile + "/WEB-INF/web.xml");
529    
530                    updateWebXml(webXml, srcFile, displayName, pluginPackage);
531    
532                    File extLibGlobalDir = new File(
533                            srcFile.getAbsolutePath() + "/WEB-INF/ext-lib/global");
534    
535                    if (extLibGlobalDir.exists()) {
536                            File globalLibDir = new File(PortalUtil.getGlobalLibDir());
537    
538                            CopyTask.copyDirectory(
539                                    extLibGlobalDir, globalLibDir, "*.jar", StringPool.BLANK,
540                                    overwrite, true);
541                    }
542    
543                    File extLibPortalDir = new File(
544                            srcFile.getAbsolutePath() + "/WEB-INF/ext-lib/portal");
545    
546                    if (extLibPortalDir.exists()) {
547                            File portalLibDir = new File(PortalUtil.getPortalLibDir());
548    
549                            CopyTask.copyDirectory(
550                                    extLibPortalDir, portalLibDir, "*.jar", StringPool.BLANK,
551                                    overwrite, true);
552                    }
553    
554                    if ((deployDir == null) || baseDir.equals(destDir)) {
555                            return;
556                    }
557    
558                    updateDeployDirectory(srcFile);
559    
560                    String excludes = StringPool.BLANK;
561    
562                    if (appServerType.equals(ServerDetector.JBOSS_ID)) {
563                            excludes += "**/WEB-INF/lib/log4j.jar,";
564                    }
565                    else if (appServerType.equals(ServerDetector.TOMCAT_ID)) {
566                            String[] libs = FileUtil.listFiles(tomcatLibDir);
567    
568                            for (int i = 0; i < libs.length; i++) {
569                                    excludes += "**/WEB-INF/lib/" + libs[i] + ",";
570                            }
571    
572                            File contextXml = new File(srcFile + "/META-INF/context.xml");
573    
574                            if (contextXml.exists()) {
575                                    String content = FileUtil.read(contextXml);
576    
577                                    if (content.indexOf(_PORTAL_CLASS_LOADER) != -1) {
578                                            excludes += "**/WEB-INF/lib/util-bridges.jar,";
579                                            excludes += "**/WEB-INF/lib/util-java.jar,";
580                                            excludes += "**/WEB-INF/lib/util-taglib.jar,";
581                                    }
582                            }
583    
584                            try {
585    
586                                    // LEP-2990
587    
588                                    Class.forName("javax.el.ELContext");
589    
590                                    excludes += "**/WEB-INF/lib/el-api.jar,";
591                            }
592                            catch (ClassNotFoundException cnfe) {
593                            }
594                    }
595    
596                    // LPS-11268
597    
598                    Properties properties = getPluginPackageProperties(srcFile);
599    
600                    if (properties != null) {
601                            String deployExcludes = properties.getProperty("deploy-excludes");
602    
603                            if (deployExcludes != null) {
604                                    excludes += deployExcludes.trim();
605    
606                                    if (!excludes.endsWith(",")) {
607                                            excludes += ",";
608                                    }
609                            }
610    
611                            deployExcludes = properties.getProperty(
612                                    "deploy-excludes-" + appServerType);
613    
614                            if (deployExcludes != null) {
615                                    excludes += deployExcludes.trim();
616    
617                                    if (!excludes.endsWith(",")) {
618                                            excludes += ",";
619                                    }
620                            }
621                    }
622    
623                    if (_log.isDebugEnabled()) {
624                            _log.debug("Excludes " + excludes);
625                    }
626    
627                    if (!unpackWar || appServerType.equals(ServerDetector.WEBSPHERE_ID)) {
628                            File tempDir = new File(
629                                    SystemProperties.get(SystemProperties.TMP_DIR) +
630                                            File.separator + Time.getTimestamp());
631    
632                            excludes += "**/WEB-INF/web.xml";
633    
634                            WarTask.war(srcFile, tempDir, excludes, webXml);
635    
636                            if (isJEEDeploymentEnabled()) {
637                                    File tempWarDir = new File(
638                                            tempDir.getParent(), deployDir.getName());
639    
640                                    if (tempWarDir.exists()) {
641                                            tempWarDir.delete();
642                                    }
643    
644                                    if (!tempDir.renameTo(tempWarDir)) {
645                                            tempWarDir = tempDir;
646                                    }
647    
648                                    DeploymentHandler deploymentHandler = getDeploymentHandler();
649    
650                                    deploymentHandler.deploy(tempWarDir, displayName);
651    
652                                    deploymentHandler.releaseDeploymentManager();
653    
654                                    DeleteTask.deleteDirectory(tempWarDir);
655                            }
656                            else {
657                                    if (!tempDir.renameTo(deployDir)) {
658                                            WarTask.war(srcFile, deployDir, excludes, webXml);
659                                    }
660    
661                                    DeleteTask.deleteDirectory(tempDir);
662                            }
663                    }
664                    else {
665    
666                            // The deployer might only copy files that have been modified.
667                            // However, the deployer always copies and overwrites web.xml after
668                            // the other files have been copied because application servers
669                            // usually detect that a WAR has been modified based on the web.xml
670                            // timestamp.
671    
672                            excludes += "**/WEB-INF/web.xml";
673    
674                            CopyTask.copyDirectory(
675                                    srcFile, deployDir, StringPool.BLANK, excludes, overwrite,
676                                    true);
677    
678                            CopyTask.copyDirectory(
679                                    srcFile, deployDir, "**/WEB-INF/web.xml", StringPool.BLANK,
680                                    true, false);
681    
682                            if (appServerType.equals(ServerDetector.TOMCAT_ID)) {
683    
684                                    // See org.apache.catalina.startup.HostConfig to see how Tomcat
685                                    // checks to make sure that web.xml was modified 5 seconds after
686                                    // WEB-INF
687    
688                                    File deployWebXml = new File(deployDir + "/WEB-INF/web.xml");
689    
690                                    deployWebXml.setLastModified(
691                                            System.currentTimeMillis() + (Time.SECOND * 6));
692                            }
693                    }
694    
695                    if (appServerType.equals(ServerDetector.JETTY_ID)) {
696                            DeployUtil.redeployJetty(displayName);
697                    }
698            }
699    
700            public void deployDirectory(
701                            File srcFile, String displayName, boolean override,
702                            PluginPackage pluginPackage)
703                    throws Exception {
704    
705                    deployDirectory(
706                            srcFile, null, null, displayName, override, pluginPackage);
707            }
708    
709            public boolean deployFile(
710                            File srcFile, File mergeDir, File deployDir, String displayName,
711                            boolean overwrite, PluginPackage pluginPackage)
712                    throws Exception {
713    
714                    boolean undeployOnRedeploy = false;
715    
716                    try {
717                            undeployOnRedeploy = PrefsPropsUtil.getBoolean(
718                                    PropsKeys.HOT_UNDEPLOY_ON_REDEPLOY,
719                                    PropsValues.HOT_UNDEPLOY_ON_REDEPLOY);
720                    }
721                    catch (Exception e) {
722    
723                            // This will only happen when running the deploy tool in Ant in the
724                            // classical way where the WAR file is actually massaged and
725                            // packaged.
726    
727                    }
728    
729                    if (undeployOnRedeploy) {
730                            DeployUtil.undeploy(appServerType, deployDir);
731                    }
732    
733                    if (!overwrite && UpToDateTask.isUpToDate(srcFile, deployDir)) {
734                            if (_log.isInfoEnabled()) {
735                                    _log.info(deployDir + " is already up to date");
736                            }
737    
738                            return false;
739                    }
740    
741                    File tempDir = new File(
742                            SystemProperties.get(SystemProperties.TMP_DIR) + File.separator +
743                                    Time.getTimestamp());
744    
745                    ExpandTask.expand(srcFile, tempDir);
746    
747                    deployDirectory(
748                            tempDir, mergeDir, deployDir, displayName, overwrite,
749                            pluginPackage);
750    
751                    DeleteTask.deleteDirectory(tempDir);
752    
753                    return true;
754            }
755    
756            public void deployFile(File srcFile, String specifiedContext)
757                    throws Exception {
758    
759                    PluginPackage pluginPackage = readPluginPackage(srcFile);
760    
761                    if (_log.isInfoEnabled()) {
762                            _log.info("Deploying " + srcFile.getName());
763                    }
764    
765                    String deployDir = null;
766                    String displayName = specifiedContext;
767                    boolean overwrite = false;
768                    String preliminaryContext = specifiedContext;
769    
770                    // The order of priority of the context is: 1.) the specified context,
771                    // 2.) if the file name starts with DEPLOY_TO_PREFIX, use the file name
772                    // after the prefix, or 3.) the recommended deployment context as
773                    // specified in liferay-plugin-package.properties, or 4.) the file name.
774    
775                    if ((specifiedContext != null) &&
776                            srcFile.getName().startsWith(DEPLOY_TO_PREFIX)) {
777    
778                            displayName = srcFile.getName().substring(
779                                    DEPLOY_TO_PREFIX.length(), srcFile.getName().length() - 4);
780    
781                            overwrite = true;
782                            preliminaryContext = displayName;
783                    }
784    
785                    if (preliminaryContext == null) {
786                            preliminaryContext = getDisplayName(srcFile);
787                    }
788    
789                    if (pluginPackage != null) {
790                            if (!PluginPackageUtil.isCurrentVersionSupported(
791                                            pluginPackage.getLiferayVersions())) {
792    
793                                    throw new AutoDeployException(
794                                            srcFile.getName() +
795                                                    " does not support this version of Liferay");
796                            }
797    
798                            if (displayName == null) {
799                                    displayName = pluginPackage.getRecommendedDeploymentContext();
800                            }
801    
802                            if (Validator.isNull(displayName)) {
803                                    displayName = getDisplayName(srcFile);
804                            }
805    
806                            pluginPackage.setContext(displayName);
807    
808                            PluginPackageUtil.updateInstallingPluginPackage(
809                                    preliminaryContext, pluginPackage);
810                    }
811    
812                    if (Validator.isNotNull(displayName)) {
813                            deployDir = displayName + ".war";
814                    }
815                    else {
816                            deployDir = srcFile.getName();
817                            displayName = getDisplayName(srcFile);
818                    }
819    
820                    if (appServerType.equals(ServerDetector.JBOSS_ID)) {
821                            deployDir = jbossPrefix + deployDir;
822                    }
823                    else if (appServerType.equals(ServerDetector.JETTY_ID) ||
824                                     appServerType.equals(ServerDetector.OC4J_ID) ||
825                                     appServerType.equals(ServerDetector.RESIN_ID) ||
826                                     appServerType.equals(ServerDetector.TOMCAT_ID)) {
827    
828                            if (unpackWar) {
829                                    deployDir = deployDir.substring(0, deployDir.length() - 4);
830                            }
831                    }
832    
833                    deployDir = destDir + "/" + deployDir;
834    
835                    File deployDirFile = new File(deployDir);
836    
837                    try {
838                            PluginPackage previousPluginPackage = readPluginPackage(
839                                    deployDirFile);
840    
841                            if ((pluginPackage != null) && (previousPluginPackage != null)) {
842                                    if (_log.isInfoEnabled()) {
843                                            String name = pluginPackage.getName();
844                                            String previousVersion = previousPluginPackage.getVersion();
845                                            String version = pluginPackage.getVersion();
846    
847                                            _log.info(
848                                                    "Updating " + name + " from version " +
849                                                            previousVersion + " to version " + version);
850                                    }
851    
852                                    if (pluginPackage.isLaterVersionThan(previousPluginPackage)) {
853                                            overwrite = true;
854                                    }
855                            }
856    
857                            File mergeDirFile = new File(
858                                    srcFile.getParent() + "/merge/" + srcFile.getName());
859    
860                            if (srcFile.isDirectory()) {
861                                    deployDirectory(
862                                            srcFile, mergeDirFile, deployDirFile, displayName,
863                                            overwrite, pluginPackage);
864                            }
865                            else {
866                                    boolean deployed = deployFile(
867                                            srcFile, mergeDirFile, deployDirFile, displayName,
868                                            overwrite, pluginPackage);
869    
870                                    if (!deployed) {
871                                            String context = preliminaryContext;
872    
873                                            if (pluginPackage != null) {
874                                                    context = pluginPackage.getContext();
875                                            }
876    
877                                            PluginPackageUtil.endPluginPackageInstallation(context);
878                                    }
879                                    else {
880                                            if (appServerType.equals(ServerDetector.JBOSS_ID)) {
881                                                    File doDeployFile = new File(deployDir + ".dodeploy");
882    
883                                                    FileUtil.write(doDeployFile, StringPool.BLANK);
884                                            }
885                                    }
886                            }
887                    }
888                    catch (Exception e) {
889                            if (pluginPackage != null) {
890                                    PluginPackageUtil.endPluginPackageInstallation(
891                                            pluginPackage.getContext());
892                            }
893    
894                            throw e;
895                    }
896            }
897    
898            public String downloadJar(String jar) throws Exception {
899                    String tmpDir = SystemProperties.get(SystemProperties.TMP_DIR);
900    
901                    File file = new File(
902                            tmpDir + "/liferay/com/liferay/portal/deploy/dependencies/" + jar);
903    
904                    if (!file.exists()) {
905                            synchronized (this) {
906                                    String url = PropsUtil.get(
907                                            PropsKeys.LIBRARY_DOWNLOAD_URL + jar);
908    
909                                    if (_log.isInfoEnabled()) {
910                                            _log.info("Downloading library from " + url);
911                                    }
912    
913                                    byte[] bytes = HttpUtil.URLtoByteArray(url);
914    
915                                    FileUtil.write(file, bytes);
916                            }
917                    }
918    
919                    return FileUtil.getAbsolutePath(file);
920            }
921    
922            public String fixPortalDependencyJar(String portalJar) {
923                    if (portalJar.equals("antlr.jar")) {
924                            portalJar = "antlr2.jar";
925                    }
926    
927                    return portalJar;
928            }
929    
930            public DeploymentHandler getDeploymentHandler() {
931                    String prefix = "auto.deploy." + ServerDetector.getServerId() + ".jee.";
932    
933                    String dmId = PropsUtil.get(prefix + "dm.id");
934                    String dmUser = PropsUtil.get(prefix + "dm.user");
935                    String dmPassword = PropsUtil.get(prefix + "dm.passwd");
936                    String dfClassName = PropsUtil.get(prefix + "df.classname");
937    
938                    return new DeploymentHandler(dmId, dmUser, dmPassword, dfClassName);
939            }
940    
941            public String getDisplayName(File srcFile) {
942                    String displayName = srcFile.getName();
943    
944                    if (StringUtil.endsWith(displayName, ".war") ||
945                            StringUtil.endsWith(displayName, ".xml")) {
946    
947                            displayName = displayName.substring(0, displayName.length() - 4);
948                    }
949    
950                    if (appServerType.equals(ServerDetector.JBOSS_ID) &&
951                            Validator.isNotNull(jbossPrefix) &&
952                            displayName.startsWith(jbossPrefix)) {
953    
954                            displayName = displayName.substring(1, displayName.length());
955                    }
956    
957                    return displayName;
958            }
959    
960            public String getExtraContent(
961                            double webXmlVersion, File srcFile, String displayName)
962                    throws Exception {
963    
964                    StringBundler sb = new StringBundler();
965    
966                    sb.append("<display-name>");
967                    sb.append(displayName);
968                    sb.append("</display-name>");
969    
970                    if (webXmlVersion < 2.4) {
971                            sb.append("<context-param>");
972                            sb.append("<param-name>liferay-invoker-enabled</param-name>");
973                            sb.append("<param-value>false</param-value>");
974                            sb.append("</context-param>");
975                    }
976    
977                    sb.append("<listener>");
978                    sb.append("<listener-class>");
979                    sb.append("com.liferay.portal.kernel.servlet.");
980                    sb.append("SerializableSessionAttributeListener");
981                    sb.append("</listener-class>");
982                    sb.append("</listener>");
983    
984                    File serviceXml = new File(srcFile + "/WEB-INF/service.xml");
985    
986                    if (serviceXml.exists()) {
987                            sb.append("<listener>");
988                            sb.append("<listener-class>");
989                            sb.append("com.liferay.portal.kernel.spring.context.");
990                            sb.append("PortletContextLoaderListener");
991                            sb.append("</listener-class>");
992                            sb.append("</listener>");
993                    }
994    
995                    File serverConfigWsdd = new File(
996                            srcFile + "/WEB-INF/server-config.wsdd");
997    
998                    if (serverConfigWsdd.exists()) {
999                            File webXml = new File(srcFile + "/WEB-INF/web.xml");
1000    
1001                            String content = FileUtil.read(webXml);
1002    
1003                            if (!content.contains("axis.servicesPath")) {
1004                                    String remotingContent = FileUtil.read(
1005                                            DeployUtil.getResourcePath("remoting-web.xml"));
1006    
1007                                    sb.append(remotingContent);
1008                            }
1009                    }
1010    
1011                    sb.append("<servlet>");
1012                    sb.append("<servlet-name>");
1013                    sb.append("Set Portlet Class Loader Servlet");
1014                    sb.append("</servlet-name>");
1015                    sb.append("<servlet-class>");
1016                    sb.append("com.liferay.portal.kernel.servlet.");
1017                    sb.append("SetPortletClassLoaderServlet");
1018                    sb.append("</servlet-class>");
1019                    sb.append("<load-on-startup>0</load-on-startup>");
1020                    sb.append("</servlet>");
1021    
1022                    boolean hasTaglib = false;
1023    
1024                    if (Validator.isNotNull(auiTaglibDTD) ||
1025                            Validator.isNotNull(portletTaglibDTD) ||
1026                            Validator.isNotNull(portletExtTaglibDTD) ||
1027                            Validator.isNotNull(securityTaglibDTD) ||
1028                            Validator.isNotNull(themeTaglibDTD) ||
1029                            Validator.isNotNull(uiTaglibDTD) ||
1030                            Validator.isNotNull(utilTaglibDTD)) {
1031    
1032                            hasTaglib = true;
1033                    }
1034    
1035                    if (hasTaglib && (webXmlVersion > 2.3)) {
1036                            sb.append("<jsp-config>");
1037                    }
1038    
1039                    if (Validator.isNotNull(auiTaglibDTD)) {
1040                            sb.append("<taglib>");
1041                            sb.append("<taglib-uri>http://liferay.com/tld/aui</taglib-uri>");
1042                            sb.append("<taglib-location>");
1043                            sb.append("/WEB-INF/tld/aui.tld");
1044                            sb.append("</taglib-location>");
1045                            sb.append("</taglib>");
1046                    }
1047    
1048                    if (Validator.isNotNull(portletTaglibDTD)) {
1049                            sb.append("<taglib>");
1050                            sb.append(
1051                                    "<taglib-uri>http://java.sun.com/portlet_2_0</taglib-uri>");
1052                            sb.append("<taglib-location>");
1053                            sb.append("/WEB-INF/tld/liferay-portlet.tld");
1054                            sb.append("</taglib-location>");
1055                            sb.append("</taglib>");
1056                    }
1057    
1058                    if (Validator.isNotNull(portletExtTaglibDTD)) {
1059                            sb.append("<taglib>");
1060                            sb.append("<taglib-uri>");
1061                            sb.append("http://liferay.com/tld/portlet");
1062                            sb.append("</taglib-uri>");
1063                            sb.append("<taglib-location>");
1064                            sb.append("/WEB-INF/tld/liferay-portlet-ext.tld");
1065                            sb.append("</taglib-location>");
1066                            sb.append("</taglib>");
1067                    }
1068    
1069                    if (Validator.isNotNull(securityTaglibDTD)) {
1070                            sb.append("<taglib>");
1071                            sb.append("<taglib-uri>");
1072                            sb.append("http://liferay.com/tld/security");
1073                            sb.append("</taglib-uri>");
1074                            sb.append("<taglib-location>");
1075                            sb.append("/WEB-INF/tld/liferay-security.tld");
1076                            sb.append("</taglib-location>");
1077                            sb.append("</taglib>");
1078                    }
1079    
1080                    if (Validator.isNotNull(themeTaglibDTD)) {
1081                            sb.append("<taglib>");
1082                            sb.append("<taglib-uri>http://liferay.com/tld/theme</taglib-uri>");
1083                            sb.append("<taglib-location>");
1084                            sb.append("/WEB-INF/tld/liferay-theme.tld");
1085                            sb.append("</taglib-location>");
1086                            sb.append("</taglib>");
1087                    }
1088    
1089                    if (Validator.isNotNull(uiTaglibDTD)) {
1090                            sb.append("<taglib>");
1091                            sb.append("<taglib-uri>http://liferay.com/tld/ui</taglib-uri>");
1092                            sb.append("<taglib-location>");
1093                            sb.append("/WEB-INF/tld/liferay-ui.tld");
1094                            sb.append("</taglib-location>");
1095                            sb.append("</taglib>");
1096                    }
1097    
1098                    if (Validator.isNotNull(utilTaglibDTD)) {
1099                            sb.append("<taglib>");
1100                            sb.append("<taglib-uri>http://liferay.com/tld/util</taglib-uri>");
1101                            sb.append("<taglib-location>");
1102                            sb.append("/WEB-INF/tld/liferay-util.tld");
1103                            sb.append("</taglib-location>");
1104                            sb.append("</taglib>");
1105                    }
1106    
1107                    if (hasTaglib && (webXmlVersion > 2.3)) {
1108                            sb.append("</jsp-config>");
1109                    }
1110    
1111                    return sb.toString();
1112            }
1113    
1114            public String getExtraFiltersContent(double webXmlVersion, File srcFile)
1115                    throws Exception {
1116    
1117                    if (webXmlVersion > 2.3) {
1118                            return getSessionFiltersContent();
1119                    }
1120                    else {
1121                            return StringPool.BLANK;
1122                    }
1123            }
1124    
1125            public String getIgnoreFiltersContent(File srcFile) throws Exception {
1126                    boolean ignoreFiltersEnabled = true;
1127    
1128                    Properties properties = getPluginPackageProperties(srcFile);
1129    
1130                    if (properties != null) {
1131                            ignoreFiltersEnabled = GetterUtil.getBoolean(
1132                                    properties.getProperty("ignore-filters-enabled"), true);
1133                    }
1134    
1135                    if (ignoreFiltersEnabled) {
1136                            String ignoreFiltersContent = FileUtil.read(
1137                                    DeployUtil.getResourcePath("ignore-filters-web.xml"));
1138    
1139                            return ignoreFiltersContent;
1140                    }
1141                    else {
1142                            return StringPool.BLANK;
1143                    }
1144            }
1145    
1146            public String getInvokerFilterContent() {
1147                    StringBundler sb = new StringBundler(4);
1148    
1149                    sb.append(getInvokerFilterContent("ERROR"));
1150                    sb.append(getInvokerFilterContent("FORWARD"));
1151                    sb.append(getInvokerFilterContent("INCLUDE"));
1152                    sb.append(getInvokerFilterContent("REQUEST"));
1153    
1154                    return sb.toString();
1155            }
1156    
1157            public String getInvokerFilterContent(String dispatcher) {
1158                    StringBundler sb = new StringBundler(23);
1159    
1160                    sb.append("<filter>");
1161                    sb.append("<filter-name>Invoker Filter - ");
1162                    sb.append(dispatcher);
1163                    sb.append("</filter-name>");
1164                    sb.append("<filter-class>");
1165                    sb.append(InvokerFilter.class.getName());
1166                    sb.append("</filter-class>");
1167                    sb.append("<init-param>");
1168                    sb.append("<param-name>dispatcher</param-name>");
1169                    sb.append("<param-value>");
1170                    sb.append(dispatcher);
1171                    sb.append("</param-value>");
1172                    sb.append("</init-param>");
1173                    sb.append("</filter>");
1174    
1175                    sb.append("<filter-mapping>");
1176                    sb.append("<filter-name>Invoker Filter - ");
1177                    sb.append(dispatcher);
1178                    sb.append("</filter-name>");
1179                    sb.append("<url-pattern>/*</url-pattern>");
1180                    sb.append("<dispatcher>");
1181                    sb.append(dispatcher);
1182                    sb.append("</dispatcher>");
1183                    sb.append("</filter-mapping>");
1184    
1185                    return sb.toString();
1186            }
1187    
1188            public String getPluginPackageLicensesXml(List<License> licenses) {
1189                    if (licenses.isEmpty()) {
1190                            return StringPool.BLANK;
1191                    }
1192    
1193                    StringBundler sb = new StringBundler(5 * licenses.size() + 2);
1194    
1195                    for (int i = 0; i < licenses.size(); i++) {
1196                            License license = licenses.get(i);
1197    
1198                            if (i == 0) {
1199                                    sb.append("\r\n");
1200                            }
1201    
1202                            sb.append("\t\t<license osi-approved=\"");
1203                            sb.append(license.isOsiApproved());
1204                            sb.append("\">");
1205                            sb.append(license.getName());
1206                            sb.append("</license>\r\n");
1207    
1208                            if ((i + 1) == licenses.size()) {
1209                                    sb.append("\t");
1210                            }
1211                    }
1212    
1213                    return sb.toString();
1214            }
1215    
1216            public String getPluginPackageLiferayVersionsXml(
1217                    List<String> liferayVersions) {
1218    
1219                    if (liferayVersions.isEmpty()) {
1220                            return StringPool.BLANK;
1221                    }
1222    
1223                    StringBundler sb = new StringBundler(liferayVersions.size() * 3 + 2);
1224    
1225                    for (int i = 0; i < liferayVersions.size(); i++) {
1226                            String liferayVersion = liferayVersions.get(i);
1227    
1228                            if (i == 0) {
1229                                    sb.append("\r\n");
1230                            }
1231    
1232                            sb.append("\t\t<liferay-version>");
1233                            sb.append(liferayVersion);
1234                            sb.append("</liferay-version>\r\n");
1235    
1236                            if ((i + 1) == liferayVersions.size()) {
1237                                    sb.append("\t");
1238                            }
1239                    }
1240    
1241                    return sb.toString();
1242            }
1243    
1244            public Properties getPluginPackageProperties(File srcFile)
1245                    throws Exception {
1246    
1247                    File propertiesFile = new File(
1248                            srcFile + "/WEB-INF/liferay-plugin-package.properties");
1249    
1250                    if (!propertiesFile.exists()) {
1251                            return null;
1252                    }
1253    
1254                    String propertiesString = FileUtil.read(propertiesFile);
1255    
1256                    return PropertiesUtil.load(propertiesString);
1257            }
1258    
1259            public String getPluginPackageTagsXml(List<String> tags) {
1260                    if (tags.isEmpty()) {
1261                            return StringPool.BLANK;
1262                    }
1263    
1264                    StringBundler sb = new StringBundler(tags.size() * 3 + 2);
1265    
1266                    for (int i = 0; i < tags.size(); i++) {
1267                            String tag = tags.get(i);
1268    
1269                            if (i == 0) {
1270                                    sb.append("\r\n");
1271                            }
1272    
1273                            sb.append("\t\t<tag>");
1274                            sb.append(tag);
1275                            sb.append("</tag>\r\n");
1276    
1277                            if ((i + 1) == tags.size()) {
1278                                    sb.append("\t");
1279                            }
1280                    }
1281    
1282                    return sb.toString();
1283            }
1284    
1285            public Map<String, String> getPluginPackageXmlFilterMap(
1286                    PluginPackage pluginPackage) {
1287    
1288                    List<String> pluginTypes = pluginPackage.getTypes();
1289    
1290                    String pluginType = pluginTypes.get(0);
1291    
1292                    if (!pluginType.equals(getPluginType())) {
1293                            return null;
1294                    }
1295    
1296                    Map<String, String> filterMap = new HashMap<String, String>();
1297    
1298                    filterMap.put("module_group_id", pluginPackage.getGroupId());
1299                    filterMap.put("module_artifact_id", pluginPackage.getArtifactId());
1300                    filterMap.put("module_version", pluginPackage.getVersion());
1301    
1302                    filterMap.put("plugin_name", pluginPackage.getName());
1303                    filterMap.put("plugin_type", pluginType);
1304                    filterMap.put(
1305                            "plugin_type_name",
1306                            TextFormatter.format(pluginType, TextFormatter.J));
1307    
1308                    filterMap.put("tags", getPluginPackageTagsXml(pluginPackage.getTags()));
1309                    filterMap.put("short_description", pluginPackage.getShortDescription());
1310                    filterMap.put("long_description", pluginPackage.getLongDescription());
1311                    filterMap.put("change_log", pluginPackage.getChangeLog());
1312                    filterMap.put("page_url", pluginPackage.getPageURL());
1313                    filterMap.put("author", pluginPackage.getAuthor());
1314                    filterMap.put(
1315                            "licenses",
1316                            getPluginPackageLicensesXml(pluginPackage.getLicenses()));
1317                    filterMap.put(
1318                            "liferay_versions",
1319                            getPluginPackageLiferayVersionsXml(
1320                                    pluginPackage.getLiferayVersions()));
1321    
1322                    return filterMap;
1323            }
1324    
1325            public String getPluginType() {
1326                    return null;
1327            }
1328    
1329            public String getServletContextIncludeFiltersContent(
1330                            double webXmlVersion, File srcFile)
1331                    throws Exception {
1332    
1333                    boolean servletContextIncludeFiltersEnabled = true;
1334    
1335                    Properties properties = getPluginPackageProperties(srcFile);
1336    
1337                    if (properties != null) {
1338                            servletContextIncludeFiltersEnabled = GetterUtil.getBoolean(
1339                                    properties.getProperty(
1340                                            "servlet-context-include-filters-enabled"), true);
1341                    }
1342    
1343                    if (servletContextIncludeFiltersEnabled) {
1344                            String servletContextIncludeFiltersContent = FileUtil.read(
1345                                    DeployUtil.getResourcePath(
1346                                            "servlet-context-include-filters-web.xml"));
1347    
1348                            if (webXmlVersion < 2.4) {
1349                                    int x = servletContextIncludeFiltersContent.indexOf(
1350                                            "<dispatcher>");
1351                                    int y = servletContextIncludeFiltersContent.indexOf(
1352                                            "</filter-mapping>");
1353    
1354                                    if (x != -1) {
1355                                            if (_log.isWarnEnabled()) {
1356                                                    _log.warn("Please update web.xml to at least 2.4");
1357                                            }
1358    
1359                                            servletContextIncludeFiltersContent =
1360                                                    servletContextIncludeFiltersContent.substring(0, x) +
1361                                                            servletContextIncludeFiltersContent.substring(y);
1362                                    }
1363                            }
1364    
1365                            return servletContextIncludeFiltersContent;
1366                    }
1367                    else {
1368                            return StringPool.BLANK;
1369                    }
1370            }
1371    
1372            public String getSessionFiltersContent() throws Exception {
1373                    String sessionFiltersContent = FileUtil.read(
1374                            DeployUtil.getResourcePath("session-filters-web.xml"));
1375    
1376                    return sessionFiltersContent;
1377            }
1378    
1379            public String getSpeedFiltersContent(File srcFile) throws Exception {
1380                    boolean speedFiltersEnabled = true;
1381    
1382                    Properties properties = getPluginPackageProperties(srcFile);
1383    
1384                    if (properties != null) {
1385                            speedFiltersEnabled = GetterUtil.getBoolean(
1386                                    properties.getProperty("speed-filters-enabled"), true);
1387                    }
1388    
1389                    if (speedFiltersEnabled) {
1390                            String speedFiltersContent = FileUtil.read(
1391                                    DeployUtil.getResourcePath("speed-filters-web.xml"));
1392    
1393                            return speedFiltersContent;
1394                    }
1395                    else {
1396                            return StringPool.BLANK;
1397                    }
1398            }
1399    
1400            public boolean isJEEDeploymentEnabled() {
1401                    return GetterUtil.getBoolean(PropsUtil.get(
1402                            "auto.deploy." + ServerDetector.getServerId() +
1403                                    ".jee.deployment.enabled"));
1404            }
1405    
1406            public void mergeDirectory(File mergeDir, File targetDir) {
1407                    if ((mergeDir == null) || (!mergeDir.exists())) {
1408                            return;
1409                    }
1410    
1411                    CopyTask.copyDirectory(mergeDir, targetDir, null, null, true, false);
1412            }
1413    
1414            public Map<String, String> processPluginPackageProperties(
1415                            File srcFile, String displayName, PluginPackage pluginPackage)
1416                    throws Exception {
1417    
1418                    if (pluginPackage == null) {
1419                            return null;
1420                    }
1421    
1422                    Properties properties = getPluginPackageProperties(srcFile);
1423    
1424                    if ((properties == null) || (properties.size() == 0)) {
1425                            return null;
1426                    }
1427    
1428                    Map<String, String> filterMap = getPluginPackageXmlFilterMap(
1429                            pluginPackage);
1430    
1431                    if (filterMap == null) {
1432                            return null;
1433                    }
1434    
1435                    copyDependencyXml(
1436                            "liferay-plugin-package.xml", srcFile + "/WEB-INF", filterMap,
1437                            true);
1438    
1439                    return filterMap;
1440            }
1441    
1442            public PluginPackage readPluginPackage(File file) {
1443                    if (!file.exists()) {
1444                            return null;
1445                    }
1446    
1447                    InputStream is = null;
1448                    ZipFile zipFile = null;
1449    
1450                    try {
1451                            boolean parseProps = false;
1452    
1453                            if (file.isDirectory()) {
1454                                    String path = file.getPath();
1455    
1456                                    File pluginPackageXmlFile = new File(
1457                                            file.getParent() + "/merge/" + file.getName() +
1458                                                    "/WEB-INF/liferay-plugin-package.xml");
1459    
1460                                    if (pluginPackageXmlFile.exists()) {
1461                                            is = new FileInputStream(pluginPackageXmlFile);
1462                                    }
1463                                    else {
1464                                            pluginPackageXmlFile = new File(
1465                                                    path + "/WEB-INF/liferay-plugin-package.xml");
1466    
1467                                            if (pluginPackageXmlFile.exists()) {
1468                                                    is = new FileInputStream(pluginPackageXmlFile);
1469                                            }
1470                                    }
1471    
1472                                    File pluginPackagePropertiesFile = new File(
1473                                            file.getParent() + "/merge/" + file.getName() +
1474                                                    "/WEB-INF/liferay-plugin-package.properties");
1475    
1476                                    if ((is == null) && pluginPackagePropertiesFile.exists()) {
1477                                            is = new FileInputStream(pluginPackagePropertiesFile);
1478    
1479                                            parseProps = true;
1480                                    }
1481                                    else {
1482                                            pluginPackagePropertiesFile = new File(
1483                                                    path + "/WEB-INF/liferay-plugin-package.properties");
1484    
1485                                            if ((is == null) && pluginPackagePropertiesFile.exists()) {
1486                                                    is = new FileInputStream(pluginPackagePropertiesFile);
1487    
1488                                                    parseProps = true;
1489                                            }
1490                                    }
1491                            }
1492                            else {
1493                                    zipFile = new ZipFile(file);
1494    
1495                                    File pluginPackageXmlFile = new File(
1496                                            file.getParent() + "/merge/" + file.getName() +
1497                                                    "/WEB-INF/liferay-plugin-package.xml");
1498    
1499                                    if (pluginPackageXmlFile.exists()) {
1500                                            is = new FileInputStream(pluginPackageXmlFile);
1501                                    }
1502                                    else {
1503                                            ZipEntry zipEntry = zipFile.getEntry(
1504                                                    "WEB-INF/liferay-plugin-package.xml");
1505    
1506                                            if (zipEntry != null) {
1507                                                    is = zipFile.getInputStream(zipEntry);
1508                                            }
1509                                    }
1510    
1511                                    File pluginPackagePropertiesFile = new File(
1512                                            file.getParent() + "/merge/" + file.getName() +
1513                                                    "/WEB-INF/liferay-plugin-package.properties");
1514    
1515                                    if ((is == null) && pluginPackagePropertiesFile.exists()) {
1516                                            is = new FileInputStream(pluginPackagePropertiesFile);
1517    
1518                                            parseProps = true;
1519                                    }
1520                                    else {
1521                                            ZipEntry zipEntry = zipFile.getEntry(
1522                                                    "WEB-INF/liferay-plugin-package.properties");
1523    
1524                                            if ((is == null) && (zipEntry != null)) {
1525                                                    is = zipFile.getInputStream(zipEntry);
1526    
1527                                                    parseProps = true;
1528                                            }
1529                                    }
1530                            }
1531    
1532                            if (is == null) {
1533                                    if (_log.isInfoEnabled()) {
1534                                            _log.info(
1535                                                    file.getPath() + " does not have a " +
1536                                                            "WEB-INF/liferay-plugin-package.xml or " +
1537                                                                    "WEB-INF/liferay-plugin-package.properties");
1538                                    }
1539    
1540                                    return null;
1541                            }
1542    
1543                            if (parseProps) {
1544                                    String displayName = getDisplayName(file);
1545    
1546                                    String propertiesString = StringUtil.read(is);
1547    
1548                                    Properties properties = PropertiesUtil.load(propertiesString);
1549    
1550                                    return PluginPackageUtil.readPluginPackageProperties(
1551                                            displayName, properties);
1552                            }
1553                            else {
1554                                    String xml = StringUtil.read(is);
1555    
1556                                    xml = XMLFormatter.fixProlog(xml);
1557    
1558                                    return PluginPackageUtil.readPluginPackageXml(xml);
1559                            }
1560                    }
1561                    catch (Exception e) {
1562                            _log.error(file.getPath() + ": " + e.toString());
1563                    }
1564                    finally {
1565                            if (is != null) {
1566                                    try {
1567                                            is.close();
1568                                    }
1569                                    catch (IOException ioe) {
1570                                    }
1571                            }
1572    
1573                            if (zipFile != null) {
1574                                    try {
1575                                            zipFile.close();
1576                                    }
1577                                    catch (IOException ioe) {
1578                                    }
1579                            }
1580                    }
1581    
1582                    return null;
1583            }
1584    
1585            public void rewriteFiles(File srcDir) throws Exception {
1586                    String[] files = FileUtil.listFiles(srcDir + "/WEB-INF/");
1587    
1588                    for (int i = 0; i < files.length; i++) {
1589                            String fileName = GetterUtil.getString(
1590                                    FileUtil.getShortFileName(files[i]));
1591    
1592                            // LEP-6415
1593    
1594                            if (fileName.equalsIgnoreCase("mule-config.xml")) {
1595                                    continue;
1596                            }
1597    
1598                            String ext = GetterUtil.getString(FileUtil.getExtension(files[i]));
1599    
1600                            if (!ext.equalsIgnoreCase("xml")) {
1601                                    continue;
1602                            }
1603    
1604                            // Make sure to rewrite any XML files to include external entities
1605                            // into same file. See LEP-3142.
1606    
1607                            File file = new File(srcDir + "/WEB-INF/" + files[i]);
1608    
1609                            try {
1610                                    Document doc = SAXReaderUtil.read(file);
1611    
1612                                    String content = doc.formattedString(StringPool.TAB, true);
1613    
1614                                    FileUtil.write(file, content);
1615                            }
1616                            catch (Exception e) {
1617                                    if (_log.isWarnEnabled()) {
1618                                            _log.warn(
1619                                                    "Unable to format " + file + ": " + e.getMessage());
1620                                    }
1621                            }
1622                    }
1623            }
1624    
1625            public void setAppServerType(String appServerType) {
1626                    this.appServerType = appServerType;
1627            }
1628    
1629            public void setAuiTaglibDTD(String auiTaglibDTD) {
1630                    this.auiTaglibDTD = auiTaglibDTD;
1631            }
1632    
1633            public void setBaseDir(String baseDir) {
1634                    this.baseDir = baseDir;
1635            }
1636    
1637            public void setDestDir(String destDir) {
1638                    this.destDir = destDir;
1639            }
1640    
1641            public void setFilePattern(String filePattern) {
1642                    this.filePattern = filePattern;
1643            }
1644    
1645            public void setJars(List<String> jars) {
1646                    this.jars = jars;
1647            }
1648    
1649            public void setJbossPrefix(String jbossPrefix) {
1650                    this.jbossPrefix = jbossPrefix;
1651            }
1652    
1653            public void setPortletExtTaglibDTD(String portletExtTaglibDTD) {
1654                    this.portletExtTaglibDTD = portletExtTaglibDTD;
1655            }
1656    
1657            public void setPortletTaglibDTD(String portletTaglibDTD) {
1658                    this.portletTaglibDTD = portletTaglibDTD;
1659            }
1660    
1661            public void setSecurityTaglibDTD(String securityTaglibDTD) {
1662                    this.securityTaglibDTD = securityTaglibDTD;
1663            }
1664    
1665            public void setThemeTaglibDTD(String themeTaglibDTD) {
1666                    this.themeTaglibDTD = themeTaglibDTD;
1667            }
1668    
1669            public void setTomcatLibDir(String tomcatLibDir) {
1670                    this.tomcatLibDir = tomcatLibDir;
1671            }
1672    
1673            public void setUiTaglibDTD(String uiTaglibDTD) {
1674                    this.uiTaglibDTD = uiTaglibDTD;
1675            }
1676    
1677            public void setUnpackWar(boolean unpackWar) {
1678                    this.unpackWar = unpackWar;
1679            }
1680    
1681            public void setUtilTaglibDTD(String utilTaglibDTD) {
1682                    this.utilTaglibDTD = utilTaglibDTD;
1683            }
1684    
1685            public void setWars(List<String> wars) {
1686                    this.wars = wars;
1687            }
1688    
1689            public void updateDeployDirectory(File srcFile) throws Exception {
1690            }
1691    
1692            public void updateGeronimoWebXml(
1693                            File srcFile, String displayName, PluginPackage pluginPackage)
1694                    throws Exception {
1695    
1696                    if (!appServerType.equals(ServerDetector.GERONIMO_ID)) {
1697                            return;
1698                    }
1699    
1700                    File geronimoWebXml = new File(srcFile + "/WEB-INF/geronimo-web.xml");
1701    
1702                    Document doc = SAXReaderUtil.read(geronimoWebXml);
1703    
1704                    Element root = doc.getRootElement();
1705    
1706                    Element environmentEl = root.element("environment");
1707    
1708                    Element moduleIdEl = environmentEl.element("moduleId");
1709    
1710                    Element artifactIdEl = moduleIdEl.element("artifactId");
1711    
1712                    artifactIdEl.setText(displayName);
1713    
1714                    Element versionEl = moduleIdEl.element("version");
1715    
1716                    versionEl.setText(pluginPackage.getVersion());
1717    
1718                    String content = doc.formattedString();
1719    
1720                    FileUtil.write(geronimoWebXml, content);
1721    
1722                    if (_log.isInfoEnabled()) {
1723                            _log.info("Modifying Geronimo " + geronimoWebXml);
1724                    }
1725            }
1726    
1727            public String updateLiferayWebXml(
1728                            double webXmlVersion, File srcFile, String webXmlContent)
1729                    throws Exception {
1730    
1731                    boolean liferayWebXmlEnabled = true;
1732    
1733                    Properties properties = getPluginPackageProperties(srcFile);
1734    
1735                    if (properties != null) {
1736                            liferayWebXmlEnabled = GetterUtil.getBoolean(
1737                                    properties.getProperty("liferay-web-xml-enabled"), true);
1738                    }
1739    
1740                    webXmlContent = WebXMLBuilder.organizeWebXML(webXmlContent);
1741    
1742                    int x = webXmlContent.indexOf("<filter>");
1743                    int y = webXmlContent.lastIndexOf("</filter-mapping>");
1744    
1745                    String webXmlFiltersContent = StringPool.BLANK;
1746    
1747                    if ((x == -1) || (y == -1)) {
1748                            x = webXmlContent.lastIndexOf("</display-name>") + 15;
1749                            y = x;
1750                    }
1751                    else {
1752                            if (liferayWebXmlEnabled && webXmlVersion > 2.3) {
1753                                    webXmlFiltersContent = webXmlContent.substring(x, y + 17);
1754    
1755                                    y = y + 17;
1756                            }
1757                            else {
1758                                    x = y + 17;
1759                                    y = y + 17;
1760                            }
1761                    }
1762    
1763                    if (webXmlVersion < 2.4) {
1764                            webXmlContent =
1765                                    webXmlContent.substring(0, x) +
1766                                            getExtraFiltersContent(webXmlVersion, srcFile) +
1767                                                    webXmlContent.substring(y);
1768    
1769                            return webXmlContent;
1770                    }
1771    
1772                    String filtersContent =
1773                            webXmlFiltersContent +
1774                                    getExtraFiltersContent(webXmlVersion, srcFile);
1775    
1776                    String liferayWebXmlContent = FileUtil.read(
1777                            DeployUtil.getResourcePath("web.xml"));
1778    
1779                    int z = liferayWebXmlContent.indexOf("</web-app>");
1780    
1781                    liferayWebXmlContent =
1782                            liferayWebXmlContent.substring(0, z) + filtersContent +
1783                                    liferayWebXmlContent.substring(z);
1784    
1785                    liferayWebXmlContent = WebXMLBuilder.organizeWebXML(
1786                            liferayWebXmlContent);
1787    
1788                    FileUtil.write(
1789                            srcFile + "/WEB-INF/liferay-web.xml", liferayWebXmlContent);
1790    
1791                    webXmlContent =
1792                            webXmlContent.substring(0, x) + getInvokerFilterContent() +
1793                                    webXmlContent.substring(y);
1794    
1795                    return webXmlContent;
1796            }
1797    
1798            public void updateWebXml(
1799                            File webXml, File srcFile, String displayName,
1800                            PluginPackage pluginPackage)
1801                    throws Exception {
1802    
1803                    String content = FileUtil.read(webXml);
1804    
1805                    int x = content.indexOf("<display-name>");
1806    
1807                    if (x != -1) {
1808                            int y = content.indexOf("</display-name>", x);
1809    
1810                            y = content.indexOf(">", y) + 1;
1811    
1812                            content = content.substring(0, x) + content.substring(y);
1813                    }
1814    
1815                    double webXmlVersion = 2.3;
1816    
1817                    Document webXmlDoc = SAXReaderUtil.read(content);
1818    
1819                    Element webXmlRoot = webXmlDoc.getRootElement();
1820    
1821                    webXmlVersion = GetterUtil.getDouble(
1822                            webXmlRoot.attributeValue("version"), webXmlVersion);
1823    
1824                    // Merge extra content
1825    
1826                    String extraContent = getExtraContent(
1827                            webXmlVersion, srcFile, displayName);
1828    
1829                    int pos = content.indexOf("</web-app>");
1830    
1831                    String newContent =
1832                            content.substring(0, pos) + extraContent +
1833                                    content.substring(pos, content.length());
1834    
1835                    // Replace old package names
1836    
1837                    newContent = StringUtil.replace(
1838                            newContent, "com.liferay.portal.shared.",
1839                            "com.liferay.portal.kernel.");
1840    
1841                    // Update liferay-web.xml
1842    
1843                    newContent = updateLiferayWebXml(webXmlVersion, srcFile, newContent);
1844    
1845                    // Update web.xml
1846    
1847                    newContent = WebXMLBuilder.organizeWebXML(newContent);
1848    
1849                    FileUtil.write(webXml, newContent, true);
1850    
1851                    if (_log.isInfoEnabled()) {
1852                            _log.info("Modifying Servlet " + webXmlVersion + " " + webXml);
1853                    }
1854            }
1855    
1856            protected String appServerType;
1857            protected String auiTaglibDTD;
1858            protected String baseDir;
1859            protected String destDir;
1860            protected String filePattern;
1861            protected List<String> jars;
1862            protected String jbossPrefix;
1863            protected String portletExtTaglibDTD;
1864            protected String portletTaglibDTD;
1865            protected String securityTaglibDTD;
1866            protected String themeTaglibDTD;
1867            protected String tomcatLibDir;
1868            protected String uiTaglibDTD;
1869            protected boolean unpackWar;
1870            protected String utilTaglibDTD;
1871            protected List<String> wars;
1872    
1873            private static final String _PORTAL_CLASS_LOADER =
1874                    "com.liferay.support.tomcat.loader.PortalClassLoader";
1875    
1876            private static Log _log = LogFactoryUtil.getLog(BaseDeployer.class);
1877    
1878    }