001    /**
002     * Copyright (c) 2000-2011 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 void deployFile(File srcFile, String specifiedContext)
710                    throws Exception {
711    
712                    PluginPackage pluginPackage = readPluginPackage(srcFile);
713    
714                    if (_log.isInfoEnabled()) {
715                            _log.info("Deploying " + srcFile.getName());
716                    }
717    
718                    String deployDir = null;
719                    String displayName = specifiedContext;
720                    boolean overwrite = false;
721                    String preliminaryContext = specifiedContext;
722    
723                    // The order of priority of the context is: 1.) the specified context,
724                    // 2.) if the file name starts with DEPLOY_TO_PREFIX, use the file name
725                    // after the prefix, or 3.) the recommended deployment context as
726                    // specified in liferay-plugin-package.properties, or 4.) the file name.
727    
728                    if ((specifiedContext != null) &&
729                            srcFile.getName().startsWith(DEPLOY_TO_PREFIX)) {
730    
731                            displayName = srcFile.getName().substring(
732                                    DEPLOY_TO_PREFIX.length(), srcFile.getName().length() - 4);
733    
734                            overwrite = true;
735                            preliminaryContext = displayName;
736                    }
737    
738                    if (preliminaryContext == null) {
739                            preliminaryContext = getDisplayName(srcFile);
740                    }
741    
742                    if (pluginPackage != null) {
743                            if (!PluginPackageUtil.isCurrentVersionSupported(
744                                            pluginPackage.getLiferayVersions())) {
745    
746                                    throw new AutoDeployException(
747                                            srcFile.getName() +
748                                                    " does not support this version of Liferay");
749                            }
750    
751                            if (displayName == null) {
752                                    displayName = pluginPackage.getRecommendedDeploymentContext();
753                            }
754    
755                            if (Validator.isNull(displayName)) {
756                                    displayName = getDisplayName(srcFile);
757                            }
758    
759                            pluginPackage.setContext(displayName);
760    
761                            PluginPackageUtil.updateInstallingPluginPackage(
762                                    preliminaryContext, pluginPackage);
763                    }
764    
765                    if (Validator.isNotNull(displayName)) {
766                            deployDir = displayName + ".war";
767                    }
768                    else {
769                            deployDir = srcFile.getName();
770                            displayName = getDisplayName(srcFile);
771                    }
772    
773                    if (appServerType.equals(ServerDetector.JBOSS_ID)) {
774                            deployDir = jbossPrefix + deployDir;
775                    }
776                    else if (appServerType.equals(ServerDetector.JETTY_ID) ||
777                                     appServerType.equals(ServerDetector.OC4J_ID) ||
778                                     appServerType.equals(ServerDetector.RESIN_ID) ||
779                                     appServerType.equals(ServerDetector.TOMCAT_ID)) {
780    
781                            if (unpackWar) {
782                                    deployDir = deployDir.substring(0, deployDir.length() - 4);
783                            }
784                    }
785    
786                    deployDir = destDir + "/" + deployDir;
787    
788                    File deployDirFile = new File(deployDir);
789    
790                    try {
791                            PluginPackage previousPluginPackage = readPluginPackage(
792                                    deployDirFile);
793    
794                            if ((pluginPackage != null) && (previousPluginPackage != null)) {
795                                    if (_log.isInfoEnabled()) {
796                                            String name = pluginPackage.getName();
797                                            String previousVersion = previousPluginPackage.getVersion();
798                                            String version = pluginPackage.getVersion();
799    
800                                            _log.info(
801                                                    "Updating " + name + " from version " +
802                                                            previousVersion + " to version " + version);
803                                    }
804    
805                                    if (pluginPackage.isLaterVersionThan(previousPluginPackage)) {
806                                            overwrite = true;
807                                    }
808                            }
809    
810                            File mergeDirFile = new File(
811                                    srcFile.getParent() + "/merge/" + srcFile.getName());
812    
813                            if (srcFile.isDirectory()) {
814                                    deployDirectory(
815                                            srcFile, mergeDirFile, deployDirFile, displayName,
816                                            overwrite, pluginPackage);
817                            }
818                            else {
819                                    boolean deployed = deployFile(
820                                            srcFile, mergeDirFile, deployDirFile, displayName,
821                                            overwrite, pluginPackage);
822    
823                                    if (!deployed) {
824                                            String context = preliminaryContext;
825    
826                                            if (pluginPackage != null) {
827                                                    context = pluginPackage.getContext();
828                                            }
829    
830                                            PluginPackageUtil.endPluginPackageInstallation(context);
831                                    }
832                                    else {
833                                            if (appServerType.equals(ServerDetector.JBOSS_ID)) {
834                                                    File doDeployFile = new File(deployDir + ".dodeploy");
835    
836                                                    FileUtil.write(doDeployFile, StringPool.BLANK);
837                                            }
838                                    }
839                            }
840                    }
841                    catch (Exception e) {
842                            if (pluginPackage != null) {
843                                    PluginPackageUtil.endPluginPackageInstallation(
844                                            pluginPackage.getContext());
845                            }
846    
847                            throw e;
848                    }
849            }
850    
851            public boolean deployFile(
852                            File srcFile, File mergeDir, File deployDir, String displayName,
853                            boolean overwrite, PluginPackage pluginPackage)
854                    throws Exception {
855    
856                    boolean undeployOnRedeploy = false;
857    
858                    try {
859                            undeployOnRedeploy = PrefsPropsUtil.getBoolean(
860                                    PropsKeys.HOT_UNDEPLOY_ON_REDEPLOY,
861                                    PropsValues.HOT_UNDEPLOY_ON_REDEPLOY);
862                    }
863                    catch (Exception e) {
864    
865                            // This will only happen when running the deploy tool in Ant in the
866                            // classical way where the WAR file is actually massaged and
867                            // packaged.
868    
869                    }
870    
871                    if (undeployOnRedeploy) {
872                            DeployUtil.undeploy(appServerType, deployDir);
873                    }
874    
875                    if (!overwrite && UpToDateTask.isUpToDate(srcFile, deployDir)) {
876                            if (_log.isInfoEnabled()) {
877                                    _log.info(deployDir + " is already up to date");
878                            }
879    
880                            return false;
881                    }
882    
883                    File tempDir = new File(
884                            SystemProperties.get(SystemProperties.TMP_DIR) + File.separator +
885                                    Time.getTimestamp());
886    
887                    ExpandTask.expand(srcFile, tempDir);
888    
889                    deployDirectory(
890                            tempDir, mergeDir, deployDir, displayName, overwrite,
891                            pluginPackage);
892    
893                    DeleteTask.deleteDirectory(tempDir);
894    
895                    return true;
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/" +
903                                    jar);
904    
905                    if (!file.exists()) {
906                            synchronized (this) {
907                                    String url = PropsUtil.get(
908                                            PropsKeys.LIBRARY_DOWNLOAD_URL + jar);
909    
910                                    if (_log.isInfoEnabled()) {
911                                            _log.info("Downloading library from " + url);
912                                    }
913    
914                                    byte[] bytes = HttpUtil.URLtoByteArray(url);
915    
916                                    FileUtil.write(file, bytes);
917                            }
918                    }
919    
920                    return FileUtil.getAbsolutePath(file);
921            }
922    
923            public String fixPortalDependencyJar(String portalJar) {
924                    if (portalJar.equals("antlr.jar")) {
925                            portalJar = "antlr2.jar";
926                    }
927    
928                    return portalJar;
929            }
930    
931            public DeploymentHandler getDeploymentHandler() {
932                    String prefix = "auto.deploy." + ServerDetector.getServerId() + ".jee.";
933    
934                    String dmId = PropsUtil.get(prefix + "dm.id");
935                    String dmUser = PropsUtil.get(prefix + "dm.user");
936                    String dmPassword = PropsUtil.get(prefix + "dm.passwd");
937                    String dfClassName = PropsUtil.get(prefix + "df.classname");
938    
939                    return new DeploymentHandler(dmId, dmUser, dmPassword, dfClassName);
940            }
941    
942            public String getDisplayName(File srcFile) {
943                    String displayName = srcFile.getName();
944    
945                    if (StringUtil.endsWith(displayName, ".war") ||
946                            StringUtil.endsWith(displayName, ".xml")) {
947    
948                            displayName = displayName.substring(0, displayName.length() - 4);
949                    }
950    
951                    if (appServerType.equals(ServerDetector.JBOSS_ID) &&
952                            Validator.isNotNull(jbossPrefix) &&
953                            displayName.startsWith(jbossPrefix)) {
954    
955                            displayName = displayName.substring(1, displayName.length());
956                    }
957    
958                    return displayName;
959            }
960    
961            public String getExtraContent(
962                            double webXmlVersion, File srcFile, String displayName)
963                    throws Exception {
964    
965                    StringBundler sb = new StringBundler();
966    
967                    sb.append("<display-name>");
968                    sb.append(displayName);
969                    sb.append("</display-name>");
970    
971                    sb.append("<listener>");
972                    sb.append("<listener-class>");
973                    sb.append("com.liferay.portal.kernel.servlet.");
974                    sb.append("SerializableSessionAttributeListener");
975                    sb.append("</listener-class>");
976                    sb.append("</listener>");
977    
978                    File serviceXml = new File(srcFile + "/WEB-INF/service.xml");
979    
980                    if (serviceXml.exists()) {
981                            sb.append("<listener>");
982                            sb.append("<listener-class>");
983                            sb.append("com.liferay.portal.kernel.spring.context.");
984                            sb.append("PortletContextLoaderListener");
985                            sb.append("</listener-class>");
986                            sb.append("</listener>");
987                    }
988    
989                    File serverConfigWsdd = new File(
990                            srcFile + "/WEB-INF/server-config.wsdd");
991    
992                    if (serverConfigWsdd.exists()) {
993                            File webXml = new File(srcFile + "/WEB-INF/web.xml");
994    
995                            String content = FileUtil.read(webXml);
996    
997                            if (!content.contains("axis.servicesPath")) {
998                                    String remotingContent = FileUtil.read(
999                                            DeployUtil.getResourcePath("remoting-web.xml"));
1000    
1001                                    sb.append(remotingContent);
1002                            }
1003                    }
1004    
1005                    sb.append("<servlet>");
1006                    sb.append("<servlet-name>");
1007                    sb.append("Set Portlet Class Loader Servlet");
1008                    sb.append("</servlet-name>");
1009                    sb.append("<servlet-class>");
1010                    sb.append("com.liferay.portal.kernel.servlet.");
1011                    sb.append("SetPortletClassLoaderServlet");
1012                    sb.append("</servlet-class>");
1013                    sb.append("<load-on-startup>0</load-on-startup>");
1014                    sb.append("</servlet>");
1015    
1016                    boolean hasTaglib = false;
1017    
1018                    if (Validator.isNotNull(auiTaglibDTD) ||
1019                            Validator.isNotNull(portletTaglibDTD) ||
1020                            Validator.isNotNull(portletExtTaglibDTD) ||
1021                            Validator.isNotNull(securityTaglibDTD) ||
1022                            Validator.isNotNull(themeTaglibDTD) ||
1023                            Validator.isNotNull(uiTaglibDTD) ||
1024                            Validator.isNotNull(utilTaglibDTD)) {
1025    
1026                            hasTaglib = true;
1027                    }
1028    
1029                    if (hasTaglib && (webXmlVersion > 2.3)) {
1030                            sb.append("<jsp-config>");
1031                    }
1032    
1033                    if (Validator.isNotNull(auiTaglibDTD)) {
1034                            sb.append("<taglib>");
1035                            sb.append("<taglib-uri>http://liferay.com/tld/aui</taglib-uri>");
1036                            sb.append("<taglib-location>");
1037                            sb.append("/WEB-INF/tld/aui.tld");
1038                            sb.append("</taglib-location>");
1039                            sb.append("</taglib>");
1040                    }
1041    
1042                    if (Validator.isNotNull(portletTaglibDTD)) {
1043                            sb.append("<taglib>");
1044                            sb.append(
1045                                    "<taglib-uri>http://java.sun.com/portlet_2_0</taglib-uri>");
1046                            sb.append("<taglib-location>");
1047                            sb.append("/WEB-INF/tld/liferay-portlet.tld");
1048                            sb.append("</taglib-location>");
1049                            sb.append("</taglib>");
1050                    }
1051    
1052                    if (Validator.isNotNull(portletExtTaglibDTD)) {
1053                            sb.append("<taglib>");
1054                            sb.append("<taglib-uri>");
1055                            sb.append("http://liferay.com/tld/portlet");
1056                            sb.append("</taglib-uri>");
1057                            sb.append("<taglib-location>");
1058                            sb.append("/WEB-INF/tld/liferay-portlet-ext.tld");
1059                            sb.append("</taglib-location>");
1060                            sb.append("</taglib>");
1061                    }
1062    
1063                    if (Validator.isNotNull(securityTaglibDTD)) {
1064                            sb.append("<taglib>");
1065                            sb.append("<taglib-uri>");
1066                            sb.append("http://liferay.com/tld/security");
1067                            sb.append("</taglib-uri>");
1068                            sb.append("<taglib-location>");
1069                            sb.append("/WEB-INF/tld/liferay-security.tld");
1070                            sb.append("</taglib-location>");
1071                            sb.append("</taglib>");
1072                    }
1073    
1074                    if (Validator.isNotNull(themeTaglibDTD)) {
1075                            sb.append("<taglib>");
1076                            sb.append("<taglib-uri>http://liferay.com/tld/theme</taglib-uri>");
1077                            sb.append("<taglib-location>");
1078                            sb.append("/WEB-INF/tld/liferay-theme.tld");
1079                            sb.append("</taglib-location>");
1080                            sb.append("</taglib>");
1081                    }
1082    
1083                    if (Validator.isNotNull(uiTaglibDTD)) {
1084                            sb.append("<taglib>");
1085                            sb.append("<taglib-uri>http://liferay.com/tld/ui</taglib-uri>");
1086                            sb.append("<taglib-location>");
1087                            sb.append("/WEB-INF/tld/liferay-ui.tld");
1088                            sb.append("</taglib-location>");
1089                            sb.append("</taglib>");
1090                    }
1091    
1092                    if (Validator.isNotNull(utilTaglibDTD)) {
1093                            sb.append("<taglib>");
1094                            sb.append("<taglib-uri>http://liferay.com/tld/util</taglib-uri>");
1095                            sb.append("<taglib-location>");
1096                            sb.append("/WEB-INF/tld/liferay-util.tld");
1097                            sb.append("</taglib-location>");
1098                            sb.append("</taglib>");
1099                    }
1100    
1101                    if (hasTaglib && (webXmlVersion > 2.3)) {
1102                            sb.append("</jsp-config>");
1103                    }
1104    
1105                    sb.append(getSessionFiltersContent());
1106    
1107                    return sb.toString();
1108            }
1109    
1110            public String getIgnoreFiltersContent(File srcFile) throws Exception {
1111                    boolean ignoreFiltersEnabled = true;
1112    
1113                    Properties properties = getPluginPackageProperties(srcFile);
1114    
1115                    if (properties != null) {
1116                            ignoreFiltersEnabled = GetterUtil.getBoolean(
1117                                    properties.getProperty("ignore-filters-enabled"), true);
1118                    }
1119    
1120                    if (ignoreFiltersEnabled) {
1121                            String ignoreFiltersContent = FileUtil.read(
1122                                    DeployUtil.getResourcePath("ignore-filters-web.xml"));
1123    
1124                            return ignoreFiltersContent;
1125                    }
1126                    else {
1127                            return StringPool.BLANK;
1128                    }
1129            }
1130    
1131            public String getInvokerFilterContent() {
1132                    StringBundler sb = new StringBundler(4);
1133    
1134                    sb.append(getInvokerFilterContent("ERROR"));
1135                    sb.append(getInvokerFilterContent("FORWARD"));
1136                    sb.append(getInvokerFilterContent("INCLUDE"));
1137                    sb.append(getInvokerFilterContent("REQUEST"));
1138    
1139                    return sb.toString();
1140            }
1141    
1142            public String getInvokerFilterContent(String dispatcher) {
1143                    StringBundler sb = new StringBundler(23);
1144    
1145                    sb.append("<filter>");
1146                    sb.append("<filter-name>Invoker Filter - ");
1147                    sb.append(dispatcher);
1148                    sb.append("</filter-name>");
1149                    sb.append("<filter-class>");
1150                    sb.append(InvokerFilter.class.getName());
1151                    sb.append("</filter-class>");
1152                    sb.append("<init-param>");
1153                    sb.append("<param-name>dispatcher</param-name>");
1154                    sb.append("<param-value>");
1155                    sb.append(dispatcher);
1156                    sb.append("</param-value>");
1157                    sb.append("</init-param>");
1158                    sb.append("</filter>");
1159    
1160                    sb.append("<filter-mapping>");
1161                    sb.append("<filter-name>Invoker Filter - ");
1162                    sb.append(dispatcher);
1163                    sb.append("</filter-name>");
1164                    sb.append("<url-pattern>/*</url-pattern>");
1165                    sb.append("<dispatcher>");
1166                    sb.append(dispatcher);
1167                    sb.append("</dispatcher>");
1168                    sb.append("</filter-mapping>");
1169    
1170                    return sb.toString();
1171            }
1172    
1173            public String getPluginPackageLicensesXml(List<License> licenses) {
1174                    if (licenses.isEmpty()) {
1175                            return StringPool.BLANK;
1176                    }
1177    
1178                    StringBundler sb = new StringBundler(5 * licenses.size() + 2);
1179    
1180                    for (int i = 0; i < licenses.size(); i++) {
1181                            License license = licenses.get(i);
1182    
1183                            if (i == 0) {
1184                                    sb.append("\r\n");
1185                            }
1186    
1187                            sb.append("\t\t<license osi-approved=\"");
1188                            sb.append(license.isOsiApproved());
1189                            sb.append("\">");
1190                            sb.append(license.getName());
1191                            sb.append("</license>\r\n");
1192    
1193                            if ((i + 1) == licenses.size()) {
1194                                    sb.append("\t");
1195                            }
1196                    }
1197    
1198                    return sb.toString();
1199            }
1200    
1201            public String getPluginPackageLiferayVersionsXml(
1202                    List<String> liferayVersions) {
1203    
1204                    if (liferayVersions.isEmpty()) {
1205                            return StringPool.BLANK;
1206                    }
1207    
1208                    StringBundler sb = new StringBundler(liferayVersions.size() * 3 + 2);
1209    
1210                    for (int i = 0; i < liferayVersions.size(); i++) {
1211                            String liferayVersion = liferayVersions.get(i);
1212    
1213                            if (i == 0) {
1214                                    sb.append("\r\n");
1215                            }
1216    
1217                            sb.append("\t\t<liferay-version>");
1218                            sb.append(liferayVersion);
1219                            sb.append("</liferay-version>\r\n");
1220    
1221                            if ((i + 1) == liferayVersions.size()) {
1222                                    sb.append("\t");
1223                            }
1224                    }
1225    
1226                    return sb.toString();
1227            }
1228    
1229            public Properties getPluginPackageProperties(File srcFile)
1230                    throws Exception {
1231    
1232                    File propertiesFile = new File(
1233                            srcFile + "/WEB-INF/liferay-plugin-package.properties");
1234    
1235                    if (!propertiesFile.exists()) {
1236                            return null;
1237                    }
1238    
1239                    String propertiesString = FileUtil.read(propertiesFile);
1240    
1241                    return PropertiesUtil.load(propertiesString);
1242            }
1243    
1244            public String getPluginPackageTagsXml(List<String> tags) {
1245                    if (tags.isEmpty()) {
1246                            return StringPool.BLANK;
1247                    }
1248    
1249                    StringBundler sb = new StringBundler(tags.size() * 3 + 2);
1250    
1251                    for (int i = 0; i < tags.size(); i++) {
1252                            String tag = tags.get(i);
1253    
1254                            if (i == 0) {
1255                                    sb.append("\r\n");
1256                            }
1257    
1258                            sb.append("\t\t<tag>");
1259                            sb.append(tag);
1260                            sb.append("</tag>\r\n");
1261    
1262                            if ((i + 1) == tags.size()) {
1263                                    sb.append("\t");
1264                            }
1265                    }
1266    
1267                    return sb.toString();
1268            }
1269    
1270            public Map<String, String> getPluginPackageXmlFilterMap(
1271                    PluginPackage pluginPackage) {
1272    
1273                    List<String> pluginTypes = pluginPackage.getTypes();
1274    
1275                    String pluginType = pluginTypes.get(0);
1276    
1277                    if (!pluginType.equals(getPluginType())) {
1278                            return null;
1279                    }
1280    
1281                    Map<String, String> filterMap = new HashMap<String, String>();
1282    
1283                    filterMap.put("module_group_id", pluginPackage.getGroupId());
1284                    filterMap.put("module_artifact_id", pluginPackage.getArtifactId());
1285                    filterMap.put("module_version", pluginPackage.getVersion());
1286    
1287                    filterMap.put("plugin_name", pluginPackage.getName());
1288                    filterMap.put("plugin_type", pluginType);
1289                    filterMap.put(
1290                            "plugin_type_name",
1291                            TextFormatter.format(pluginType, TextFormatter.J));
1292    
1293                    filterMap.put("tags", getPluginPackageTagsXml(pluginPackage.getTags()));
1294                    filterMap.put("short_description", pluginPackage.getShortDescription());
1295                    filterMap.put("long_description", pluginPackage.getLongDescription());
1296                    filterMap.put("change_log", pluginPackage.getChangeLog());
1297                    filterMap.put("page_url", pluginPackage.getPageURL());
1298                    filterMap.put("author", pluginPackage.getAuthor());
1299                    filterMap.put(
1300                            "licenses",
1301                            getPluginPackageLicensesXml(pluginPackage.getLicenses()));
1302                    filterMap.put(
1303                            "liferay_versions",
1304                            getPluginPackageLiferayVersionsXml(
1305                                    pluginPackage.getLiferayVersions()));
1306    
1307                    return filterMap;
1308            }
1309    
1310            public String getPluginType() {
1311                    return null;
1312            }
1313    
1314            public String getServletContextIncludeFiltersContent(
1315                            double webXmlVersion, File srcFile)
1316                    throws Exception {
1317    
1318                    boolean servletContextIncludeFiltersEnabled = true;
1319    
1320                    Properties properties = getPluginPackageProperties(srcFile);
1321    
1322                    if (properties != null) {
1323                            servletContextIncludeFiltersEnabled = GetterUtil.getBoolean(
1324                                    properties.getProperty(
1325                                            "servlet-context-include-filters-enabled"), true);
1326                    }
1327    
1328                    if (servletContextIncludeFiltersEnabled) {
1329                            String servletContextIncludeFiltersContent = FileUtil.read(
1330                                    DeployUtil.getResourcePath(
1331                                            "servlet-context-include-filters-web.xml"));
1332    
1333                            if (webXmlVersion < 2.4) {
1334                                    int x = servletContextIncludeFiltersContent.indexOf(
1335                                            "<dispatcher>");
1336                                    int y = servletContextIncludeFiltersContent.indexOf(
1337                                            "</filter-mapping>");
1338    
1339                                    if (x != -1) {
1340                                            if (_log.isWarnEnabled()) {
1341                                                    _log.warn("Please update web.xml to at least 2.4");
1342                                            }
1343    
1344                                            servletContextIncludeFiltersContent =
1345                                                    servletContextIncludeFiltersContent.substring(0, x) +
1346                                                            servletContextIncludeFiltersContent.substring(y);
1347                                    }
1348                            }
1349    
1350                            return servletContextIncludeFiltersContent;
1351                    }
1352                    else {
1353                            return StringPool.BLANK;
1354                    }
1355            }
1356    
1357            public String getSessionFiltersContent() throws Exception {
1358                    String sessionFiltersContent = FileUtil.read(
1359                            DeployUtil.getResourcePath("session-filters-web.xml"));
1360    
1361                    return sessionFiltersContent;
1362            }
1363    
1364            public String getSpeedFiltersContent(File srcFile) throws Exception {
1365                    boolean speedFiltersEnabled = true;
1366    
1367                    Properties properties = getPluginPackageProperties(srcFile);
1368    
1369                    if (properties != null) {
1370                            speedFiltersEnabled = GetterUtil.getBoolean(
1371                                    properties.getProperty("speed-filters-enabled"), true);
1372                    }
1373    
1374                    if (speedFiltersEnabled) {
1375                            String speedFiltersContent = FileUtil.read(
1376                                    DeployUtil.getResourcePath("speed-filters-web.xml"));
1377    
1378                            return speedFiltersContent;
1379                    }
1380                    else {
1381                            return StringPool.BLANK;
1382                    }
1383            }
1384    
1385            public boolean isJEEDeploymentEnabled() {
1386                    return GetterUtil.getBoolean(PropsUtil.get(
1387                            "auto.deploy." + ServerDetector.getServerId() +
1388                                    ".jee.deployment.enabled"));
1389            }
1390    
1391            public void mergeDirectory(File mergeDir, File targetDir) {
1392                    if ((mergeDir == null) || (!mergeDir.exists())) {
1393                            return;
1394                    }
1395    
1396                    CopyTask.copyDirectory(mergeDir, targetDir, null, null, true, false);
1397            }
1398    
1399            public Map<String, String> processPluginPackageProperties(
1400                            File srcFile, String displayName, PluginPackage pluginPackage)
1401                    throws Exception {
1402    
1403                    if (pluginPackage == null) {
1404                            return null;
1405                    }
1406    
1407                    Properties properties = getPluginPackageProperties(srcFile);
1408    
1409                    if ((properties == null) || (properties.size() == 0)) {
1410                            return null;
1411                    }
1412    
1413                    Map<String, String> filterMap = getPluginPackageXmlFilterMap(
1414                            pluginPackage);
1415    
1416                    if (filterMap == null) {
1417                            return null;
1418                    }
1419    
1420                    copyDependencyXml(
1421                            "liferay-plugin-package.xml", srcFile + "/WEB-INF", filterMap,
1422                            true);
1423    
1424                    return filterMap;
1425            }
1426    
1427            public PluginPackage readPluginPackage(File file) {
1428                    if (!file.exists()) {
1429                            return null;
1430                    }
1431    
1432                    InputStream is = null;
1433                    ZipFile zipFile = null;
1434    
1435                    try {
1436                            boolean parseProps = false;
1437    
1438                            if (file.isDirectory()) {
1439                                    String path = file.getPath();
1440    
1441                                    File pluginPackageXmlFile = new File(
1442                                            file.getParent() + "/merge/" + file.getName() +
1443                                                    "/WEB-INF/liferay-plugin-package.xml");
1444    
1445                                    if (pluginPackageXmlFile.exists()) {
1446                                            is = new FileInputStream(pluginPackageXmlFile);
1447                                    }
1448                                    else {
1449                                            pluginPackageXmlFile = new File(
1450                                                    path + "/WEB-INF/liferay-plugin-package.xml");
1451    
1452                                            if (pluginPackageXmlFile.exists()) {
1453                                                    is = new FileInputStream(pluginPackageXmlFile);
1454                                            }
1455                                    }
1456    
1457                                    File pluginPackagePropertiesFile = new File(
1458                                            file.getParent() + "/merge/" + file.getName() +
1459                                                    "/WEB-INF/liferay-plugin-package.properties");
1460    
1461                                    if ((is == null) && pluginPackagePropertiesFile.exists()) {
1462                                            is = new FileInputStream(pluginPackagePropertiesFile);
1463    
1464                                            parseProps = true;
1465                                    }
1466                                    else {
1467                                            pluginPackagePropertiesFile = new File(
1468                                                    path + "/WEB-INF/liferay-plugin-package.properties");
1469    
1470                                            if ((is == null) && pluginPackagePropertiesFile.exists()) {
1471                                                    is = new FileInputStream(pluginPackagePropertiesFile);
1472    
1473                                                    parseProps = true;
1474                                            }
1475                                    }
1476                            }
1477                            else {
1478                                    zipFile = new ZipFile(file);
1479    
1480                                    File pluginPackageXmlFile = new File(
1481                                            file.getParent() + "/merge/" + file.getName() +
1482                                                    "/WEB-INF/liferay-plugin-package.xml");
1483    
1484                                    if (pluginPackageXmlFile.exists()) {
1485                                            is = new FileInputStream(pluginPackageXmlFile);
1486                                    }
1487                                    else {
1488                                            ZipEntry zipEntry = zipFile.getEntry(
1489                                                    "WEB-INF/liferay-plugin-package.xml");
1490    
1491                                            if (zipEntry != null) {
1492                                                    is = zipFile.getInputStream(zipEntry);
1493                                            }
1494                                    }
1495    
1496                                    File pluginPackagePropertiesFile = new File(
1497                                            file.getParent() + "/merge/" + file.getName() +
1498                                                    "/WEB-INF/liferay-plugin-package.properties");
1499    
1500                                    if ((is == null) && pluginPackagePropertiesFile.exists()) {
1501                                            is = new FileInputStream(pluginPackagePropertiesFile);
1502    
1503                                            parseProps = true;
1504                                    }
1505                                    else {
1506                                            ZipEntry zipEntry = zipFile.getEntry(
1507                                                    "WEB-INF/liferay-plugin-package.properties");
1508    
1509                                            if ((is == null) && (zipEntry != null)) {
1510                                                    is = zipFile.getInputStream(zipEntry);
1511    
1512                                                    parseProps = true;
1513                                            }
1514                                    }
1515                            }
1516    
1517                            if (is == null) {
1518                                    if (_log.isInfoEnabled()) {
1519                                            _log.info(
1520                                                    file.getPath() + " does not have a " +
1521                                                            "WEB-INF/liferay-plugin-package.xml or " +
1522                                                                    "WEB-INF/liferay-plugin-package.properties");
1523                                    }
1524    
1525                                    return null;
1526                            }
1527    
1528                            if (parseProps) {
1529                                    String displayName = getDisplayName(file);
1530    
1531                                    String propertiesString = StringUtil.read(is);
1532    
1533                                    Properties properties = PropertiesUtil.load(propertiesString);
1534    
1535                                    return PluginPackageUtil.readPluginPackageProperties(
1536                                            displayName, properties);
1537                            }
1538                            else {
1539                                    String xml = StringUtil.read(is);
1540    
1541                                    xml = XMLFormatter.fixProlog(xml);
1542    
1543                                    return PluginPackageUtil.readPluginPackageXml(xml);
1544                            }
1545                    }
1546                    catch (Exception e) {
1547                            _log.error(file.getPath() + ": " + e.toString());
1548                    }
1549                    finally {
1550                            if (is != null) {
1551                                    try {
1552                                            is.close();
1553                                    }
1554                                    catch (IOException ioe) {
1555                                    }
1556                            }
1557    
1558                            if (zipFile != null) {
1559                                    try {
1560                                            zipFile.close();
1561                                    }
1562                                    catch (IOException ioe) {
1563                                    }
1564                            }
1565                    }
1566    
1567                    return null;
1568            }
1569    
1570            public void rewriteFiles(File srcDir) throws Exception {
1571                    String[] files = FileUtil.listFiles(srcDir + "/WEB-INF/");
1572    
1573                    for (int i = 0; i < files.length; i++) {
1574                            String fileName = GetterUtil.getString(
1575                                    FileUtil.getShortFileName(files[i]));
1576    
1577                            // LEP-6415
1578    
1579                            if (fileName.equalsIgnoreCase("mule-config.xml")) {
1580                                    continue;
1581                            }
1582    
1583                            String ext = GetterUtil.getString(FileUtil.getExtension(files[i]));
1584    
1585                            if (!ext.equalsIgnoreCase("xml")) {
1586                                    continue;
1587                            }
1588    
1589                            // Make sure to rewrite any XML files to include external entities
1590                            // into same file. See LEP-3142.
1591    
1592                            File file = new File(srcDir + "/WEB-INF/" + files[i]);
1593    
1594                            try {
1595                                    Document doc = SAXReaderUtil.read(file);
1596    
1597                                    String content = doc.formattedString(StringPool.TAB, true);
1598    
1599                                    FileUtil.write(file, content);
1600                            }
1601                            catch (Exception e) {
1602                                    if (_log.isWarnEnabled()) {
1603                                            _log.warn(
1604                                                    "Unable to format " + file + ": " + e.getMessage());
1605                                    }
1606                            }
1607                    }
1608            }
1609    
1610            public void setAppServerType(String appServerType) {
1611                    this.appServerType = appServerType;
1612            }
1613    
1614            public void setAuiTaglibDTD(String auiTaglibDTD) {
1615                    this.auiTaglibDTD = auiTaglibDTD;
1616            }
1617    
1618            public void setBaseDir(String baseDir) {
1619                    this.baseDir = baseDir;
1620            }
1621    
1622            public void setDestDir(String destDir) {
1623                    this.destDir = destDir;
1624            }
1625    
1626            public void setFilePattern(String filePattern) {
1627                    this.filePattern = filePattern;
1628            }
1629    
1630            public void setJars(List<String> jars) {
1631                    this.jars = jars;
1632            }
1633    
1634            public void setJbossPrefix(String jbossPrefix) {
1635                    this.jbossPrefix = jbossPrefix;
1636            }
1637    
1638            public void setPortletExtTaglibDTD(String portletExtTaglibDTD) {
1639                    this.portletExtTaglibDTD = portletExtTaglibDTD;
1640            }
1641    
1642            public void setPortletTaglibDTD(String portletTaglibDTD) {
1643                    this.portletTaglibDTD = portletTaglibDTD;
1644            }
1645    
1646            public void setSecurityTaglibDTD(String securityTaglibDTD) {
1647                    this.securityTaglibDTD = securityTaglibDTD;
1648            }
1649    
1650            public void setThemeTaglibDTD(String themeTaglibDTD) {
1651                    this.themeTaglibDTD = themeTaglibDTD;
1652            }
1653    
1654            public void setTomcatLibDir(String tomcatLibDir) {
1655                    this.tomcatLibDir = tomcatLibDir;
1656            }
1657    
1658            public void setUiTaglibDTD(String uiTaglibDTD) {
1659                    this.uiTaglibDTD = uiTaglibDTD;
1660            }
1661    
1662            public void setUnpackWar(boolean unpackWar) {
1663                    this.unpackWar = unpackWar;
1664            }
1665    
1666            public void setUtilTaglibDTD(String utilTaglibDTD) {
1667                    this.utilTaglibDTD = utilTaglibDTD;
1668            }
1669    
1670            public void setWars(List<String> wars) {
1671                    this.wars = wars;
1672            }
1673    
1674            public void updateDeployDirectory(File srcFile) throws Exception {
1675            }
1676    
1677            public void updateGeronimoWebXml(
1678                            File srcFile, String displayName, PluginPackage pluginPackage)
1679                    throws Exception {
1680    
1681                    if (!appServerType.equals(ServerDetector.GERONIMO_ID)) {
1682                            return;
1683                    }
1684    
1685                    File geronimoWebXml = new File(srcFile + "/WEB-INF/geronimo-web.xml");
1686    
1687                    Document doc = SAXReaderUtil.read(geronimoWebXml);
1688    
1689                    Element root = doc.getRootElement();
1690    
1691                    Element environmentEl = root.element("environment");
1692    
1693                    Element moduleIdEl = environmentEl.element("moduleId");
1694    
1695                    Element artifactIdEl = moduleIdEl.element("artifactId");
1696    
1697                    artifactIdEl.setText(displayName);
1698    
1699                    Element versionEl = moduleIdEl.element("version");
1700    
1701                    versionEl.setText(pluginPackage.getVersion());
1702    
1703                    String content = doc.formattedString();
1704    
1705                    FileUtil.write(geronimoWebXml, content);
1706    
1707                    if (_log.isInfoEnabled()) {
1708                            _log.info("Modifying Geronimo " + geronimoWebXml);
1709                    }
1710            }
1711    
1712            public String updateLiferayWebXml(File srcFile, String webXmlContent)
1713                    throws Exception {
1714    
1715                    webXmlContent = WebXMLBuilder.organizeWebXML(webXmlContent);
1716    
1717                    int x = webXmlContent.indexOf("<filter>");
1718                    int y = webXmlContent.lastIndexOf("</filter-mapping>");
1719    
1720                    if ((x == -1) || (y == -1)) {
1721                            return webXmlContent;
1722                    }
1723    
1724                    boolean liferayWebXmlEnabled = true;
1725    
1726                    Properties properties = getPluginPackageProperties(srcFile);
1727    
1728                    if (properties != null) {
1729                            liferayWebXmlEnabled = GetterUtil.getBoolean(
1730                                    properties.getProperty("liferay-web-xml-enabled"), true);
1731                    }
1732    
1733                    if (!liferayWebXmlEnabled) {
1734                            return webXmlContent;
1735                    }
1736    
1737                    String filterContent = webXmlContent.substring(x, y + 17);
1738    
1739                    String liferayWebXmlContent = FileUtil.read(
1740                            DeployUtil.getResourcePath("web.xml"));
1741    
1742                    int z = liferayWebXmlContent.indexOf("</web-app>");
1743    
1744                    liferayWebXmlContent =
1745                            liferayWebXmlContent.substring(0, z) + filterContent +
1746                                    liferayWebXmlContent.substring(z);
1747    
1748                    liferayWebXmlContent = WebXMLBuilder.organizeWebXML(
1749                            liferayWebXmlContent);
1750    
1751                    FileUtil.write(
1752                            srcFile + "/WEB-INF/liferay-web.xml", liferayWebXmlContent);
1753    
1754                    webXmlContent =
1755                            webXmlContent.substring(0, x) + getInvokerFilterContent() +
1756                                    webXmlContent.substring(y + 17);
1757    
1758                    return webXmlContent;
1759            }
1760    
1761            public void updateWebXml(
1762                            File webXml, File srcFile, String displayName,
1763                            PluginPackage pluginPackage)
1764                    throws Exception {
1765    
1766                    String content = FileUtil.read(webXml);
1767    
1768                    int x = content.indexOf("<display-name>");
1769    
1770                    if (x != -1) {
1771                            int y = content.indexOf("</display-name>", x);
1772    
1773                            y = content.indexOf(">", y) + 1;
1774    
1775                            content = content.substring(0, x) + content.substring(y);
1776                    }
1777    
1778                    double webXmlVersion = 2.3;
1779    
1780                    Document webXmlDoc = SAXReaderUtil.read(content);
1781    
1782                    Element webXmlRoot = webXmlDoc.getRootElement();
1783    
1784                    webXmlVersion = GetterUtil.getDouble(
1785                            webXmlRoot.attributeValue("version"), webXmlVersion);
1786    
1787                    // Merge extra content
1788    
1789                    String extraContent = getExtraContent(
1790                            webXmlVersion, srcFile, displayName);
1791    
1792                    int pos = content.indexOf("</web-app>");
1793    
1794                    String newContent =
1795                            content.substring(0, pos) + extraContent +
1796                                    content.substring(pos, content.length());
1797    
1798                    // Replace old package names
1799    
1800                    newContent = StringUtil.replace(
1801                            newContent, "com.liferay.portal.shared.",
1802                            "com.liferay.portal.kernel.");
1803    
1804                    // Update liferay-web.xml
1805    
1806                    if (webXmlVersion > 2.3) {
1807                            newContent = updateLiferayWebXml(srcFile, newContent);
1808                    }
1809    
1810                    // Update web.xml
1811    
1812                    newContent = WebXMLBuilder.organizeWebXML(newContent);
1813    
1814                    FileUtil.write(webXml, newContent, true);
1815    
1816                    if (_log.isInfoEnabled()) {
1817                            _log.info("Modifying Servlet " + webXmlVersion + " " + webXml);
1818                    }
1819            }
1820    
1821            protected String appServerType;
1822            protected String auiTaglibDTD;
1823            protected String baseDir;
1824            protected String destDir;
1825            protected String filePattern;
1826            protected List<String> jars;
1827            protected String jbossPrefix;
1828            protected String portletExtTaglibDTD;
1829            protected String portletTaglibDTD;
1830            protected String securityTaglibDTD;
1831            protected String themeTaglibDTD;
1832            protected String tomcatLibDir;
1833            protected String uiTaglibDTD;
1834            protected boolean unpackWar;
1835            protected String utilTaglibDTD;
1836            protected List<String> wars;
1837    
1838            private static final String _PORTAL_CLASS_LOADER =
1839                    "com.liferay.support.tomcat.loader.PortalClassLoader";
1840    
1841            private static Log _log = LogFactoryUtil.getLog(BaseDeployer.class);
1842    
1843    }