001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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                                    FileUtil.write(
384                                            portletPropertiesFile,
385                                            "plugin.package.name=" + pluginPackage.getName());
386                            }
387                    }
388            }
389    
390            public void copyTlds(File srcFile, PluginPackage pluginPackage)
391                    throws Exception {
392    
393                    if (Validator.isNotNull(auiTaglibDTD)) {
394                            FileUtil.copyFile(
395                                    auiTaglibDTD, srcFile + "/WEB-INF/tld/liferay-aui.tld", true);
396                    }
397    
398                    if (Validator.isNotNull(portletTaglibDTD)) {
399                            FileUtil.copyFile(
400                                    portletTaglibDTD, srcFile + "/WEB-INF/tld/liferay-portlet.tld",
401                                    true);
402                    }
403    
404                    if (Validator.isNotNull(portletExtTaglibDTD)) {
405                            FileUtil.copyFile(
406                                    portletExtTaglibDTD,
407                                    srcFile + "/WEB-INF/tld/liferay-portlet-ext.tld", true);
408                    }
409    
410                    if (Validator.isNotNull(securityTaglibDTD)) {
411                            FileUtil.copyFile(
412                                    securityTaglibDTD,
413                                    srcFile + "/WEB-INF/tld/liferay-security.tld", true);
414                    }
415    
416                    if (Validator.isNotNull(themeTaglibDTD)) {
417                            FileUtil.copyFile(
418                                    themeTaglibDTD, srcFile + "/WEB-INF/tld/liferay-theme.tld",
419                                    true);
420                    }
421    
422                    if (Validator.isNotNull(uiTaglibDTD)) {
423                            FileUtil.copyFile(
424                                    uiTaglibDTD, srcFile + "/WEB-INF/tld/liferay-ui.tld", true);
425                    }
426    
427                    if (Validator.isNotNull(utilTaglibDTD)) {
428                            FileUtil.copyFile(
429                                    utilTaglibDTD, srcFile + "/WEB-INF/tld/liferay-util.tld", true);
430                    }
431            }
432    
433            public void copyXmls(
434                            File srcFile, String displayName, PluginPackage pluginPackage)
435                    throws Exception {
436    
437                    if (appServerType.equals(ServerDetector.GERONIMO_ID)) {
438                            copyDependencyXml("geronimo-web.xml", srcFile + "/WEB-INF");
439                    }
440                    else if (appServerType.equals(ServerDetector.JBOSS_ID)) {
441                            copyDependencyXml(
442                                    "jboss-deployment-structure.xml", srcFile + "/WEB-INF");
443                    }
444                    else if (appServerType.equals(ServerDetector.WEBLOGIC_ID)) {
445                            copyDependencyXml("weblogic.xml", srcFile + "/WEB-INF");
446                    }
447                    else if (appServerType.equals(ServerDetector.WEBSPHERE_ID)) {
448                            copyDependencyXml("ibm-web-ext.xmi", srcFile + "/WEB-INF");
449                    }
450    
451                    copyDependencyXml("web.xml", srcFile + "/WEB-INF");
452            }
453    
454            public void deploy(String context) throws Exception {
455                    try {
456                            File baseDirFile = new File(baseDir);
457    
458                            File[] files = baseDirFile.listFiles();
459    
460                            if (files == null) {
461                                    return;
462                            }
463    
464                            files = FileUtil.sortFiles(files);
465    
466                            for (int i = 0; i < files.length; i++) {
467                                    File srcFile = files[i];
468    
469                                    String fileName = srcFile.getName().toLowerCase();
470    
471                                    boolean deploy = false;
472    
473                                    if (fileName.endsWith(".war") || fileName.endsWith(".zip")) {
474                                            deploy = true;
475    
476                                            if (wars.size() > 0) {
477                                                    if (!wars.contains(srcFile.getName())) {
478                                                            deploy = false;
479                                                    }
480                                            }
481                                            else if (Validator.isNotNull(filePattern)) {
482                                                    if (!StringUtil.matchesIgnoreCase(
483                                                                    fileName, filePattern)) {
484    
485                                                            deploy = false;
486                                                    }
487                                            }
488                                    }
489    
490                                    if (deploy) {
491                                            deployFile(srcFile, context);
492                                    }
493                            }
494                    }
495                    catch (Exception e) {
496                            e.printStackTrace();
497                    }
498            }
499    
500            public void deployDirectory(
501                            File srcFile, File mergeDir, File deployDir, String displayName,
502                            boolean overwrite, PluginPackage pluginPackage)
503                    throws Exception {
504    
505                    rewriteFiles(srcFile);
506    
507                    mergeDirectory(mergeDir, srcFile);
508    
509                    processPluginPackageProperties(srcFile, displayName, pluginPackage);
510    
511                    copyJars(srcFile, pluginPackage);
512                    copyProperties(srcFile, pluginPackage);
513                    copyTlds(srcFile, pluginPackage);
514                    copyXmls(srcFile, displayName, pluginPackage);
515                    copyPortalDependencies(srcFile);
516    
517                    updateGeronimoWebXml(srcFile, displayName, pluginPackage);
518    
519                    File webXml = new File(srcFile + "/WEB-INF/web.xml");
520    
521                    updateWebXml(webXml, srcFile, displayName, pluginPackage);
522    
523                    File extLibGlobalDir = new File(
524                            srcFile.getAbsolutePath() + "/WEB-INF/ext-lib/global");
525    
526                    if (extLibGlobalDir.exists()) {
527                            File globalLibDir = new File(PortalUtil.getGlobalLibDir());
528    
529                            CopyTask.copyDirectory(
530                                    extLibGlobalDir, globalLibDir, "*.jar", StringPool.BLANK,
531                                    overwrite, true);
532                    }
533    
534                    File extLibPortalDir = new File(
535                            srcFile.getAbsolutePath() + "/WEB-INF/ext-lib/portal");
536    
537                    if (extLibPortalDir.exists()) {
538                            File portalLibDir = new File(PortalUtil.getPortalLibDir());
539    
540                            CopyTask.copyDirectory(
541                                    extLibPortalDir, portalLibDir, "*.jar", StringPool.BLANK,
542                                    overwrite, true);
543                    }
544    
545                    if ((deployDir == null) || baseDir.equals(destDir)) {
546                            return;
547                    }
548    
549                    updateDeployDirectory(srcFile);
550    
551                    String excludes = StringPool.BLANK;
552    
553                    if (appServerType.equals(ServerDetector.JBOSS_ID)) {
554                            excludes += "**/WEB-INF/lib/log4j.jar,";
555                    }
556                    else if (appServerType.equals(ServerDetector.TOMCAT_ID)) {
557                            String[] libs = FileUtil.listFiles(tomcatLibDir);
558    
559                            for (int i = 0; i < libs.length; i++) {
560                                    excludes += "**/WEB-INF/lib/" + libs[i] + ",";
561                            }
562    
563                            File contextXml = new File(srcFile + "/META-INF/context.xml");
564    
565                            if (contextXml.exists()) {
566                                    String content = FileUtil.read(contextXml);
567    
568                                    if (content.indexOf(_PORTAL_CLASS_LOADER) != -1) {
569                                            excludes += "**/WEB-INF/lib/util-bridges.jar,";
570                                            excludes += "**/WEB-INF/lib/util-java.jar,";
571                                            excludes += "**/WEB-INF/lib/util-taglib.jar,";
572                                    }
573                            }
574    
575                            try {
576    
577                                    // LEP-2990
578    
579                                    Class.forName("javax.el.ELContext");
580    
581                                    excludes += "**/WEB-INF/lib/el-api.jar,";
582                            }
583                            catch (ClassNotFoundException cnfe) {
584                            }
585                    }
586    
587                    // LPS-11268
588    
589                    Properties properties = getPluginPackageProperties(srcFile);
590    
591                    if (properties != null) {
592                            String deployExcludes = properties.getProperty("deploy-excludes");
593    
594                            if (deployExcludes != null) {
595                                    excludes += deployExcludes.trim();
596    
597                                    if (!excludes.endsWith(",")) {
598                                            excludes += ",";
599                                    }
600                            }
601    
602                            deployExcludes = properties.getProperty(
603                                    "deploy-excludes-" + appServerType);
604    
605                            if (deployExcludes != null) {
606                                    excludes += deployExcludes.trim();
607    
608                                    if (!excludes.endsWith(",")) {
609                                            excludes += ",";
610                                    }
611                            }
612                    }
613    
614                    if (_log.isDebugEnabled()) {
615                            _log.debug("Excludes " + excludes);
616                    }
617    
618                    if (!unpackWar || appServerType.equals(ServerDetector.WEBSPHERE_ID)) {
619                            File tempDir = new File(
620                                    SystemProperties.get(SystemProperties.TMP_DIR) +
621                                            File.separator + Time.getTimestamp());
622    
623                            excludes += "**/WEB-INF/web.xml";
624    
625                            WarTask.war(srcFile, tempDir, excludes, webXml);
626    
627                            if (isJEEDeploymentEnabled()) {
628                                    File tempWarDir = new File(
629                                            tempDir.getParent(), deployDir.getName());
630    
631                                    if (tempWarDir.exists()) {
632                                            tempWarDir.delete();
633                                    }
634    
635                                    if (!tempDir.renameTo(tempWarDir)) {
636                                            tempWarDir = tempDir;
637                                    }
638    
639                                    DeploymentHandler deploymentHandler = getDeploymentHandler();
640    
641                                    deploymentHandler.deploy(tempWarDir, displayName);
642    
643                                    deploymentHandler.releaseDeploymentManager();
644    
645                                    DeleteTask.deleteDirectory(tempWarDir);
646                            }
647                            else {
648                                    if (!tempDir.renameTo(deployDir)) {
649                                            WarTask.war(srcFile, deployDir, excludes, webXml);
650                                    }
651    
652                                    DeleteTask.deleteDirectory(tempDir);
653                            }
654                    }
655                    else {
656    
657                            // The deployer might only copy files that have been modified.
658                            // However, the deployer always copies and overwrites web.xml after
659                            // the other files have been copied because application servers
660                            // usually detect that a WAR has been modified based on the web.xml
661                            // timestamp.
662    
663                            excludes += "**/WEB-INF/web.xml";
664    
665                            CopyTask.copyDirectory(
666                                    srcFile, deployDir, StringPool.BLANK, excludes, overwrite,
667                                    true);
668    
669                            CopyTask.copyDirectory(
670                                    srcFile, deployDir, "**/WEB-INF/web.xml", StringPool.BLANK,
671                                    true, false);
672    
673                            if (appServerType.equals(ServerDetector.TOMCAT_ID)) {
674    
675                                    // See org.apache.catalina.startup.HostConfig to see how Tomcat
676                                    // checks to make sure that web.xml was modified 5 seconds after
677                                    // WEB-INF
678    
679                                    File deployWebXml = new File(deployDir + "/WEB-INF/web.xml");
680    
681                                    deployWebXml.setLastModified(
682                                            System.currentTimeMillis() + (Time.SECOND * 6));
683                            }
684                    }
685    
686                    if (appServerType.equals(ServerDetector.JETTY_ID)) {
687                            DeployUtil.redeployJetty(displayName);
688                    }
689            }
690    
691            public void deployDirectory(
692                            File srcFile, String displayName, boolean override,
693                            PluginPackage pluginPackage)
694                    throws Exception {
695    
696                    deployDirectory(
697                            srcFile, null, null, displayName, override, pluginPackage);
698            }
699    
700            public void deployFile(File srcFile, String specifiedContext)
701                    throws Exception {
702    
703                    PluginPackage pluginPackage = readPluginPackage(srcFile);
704    
705                    if (_log.isInfoEnabled()) {
706                            _log.info("Deploying " + srcFile.getName());
707                    }
708    
709                    String deployDir = null;
710                    String displayName = specifiedContext;
711                    boolean overwrite = false;
712                    String preliminaryContext = specifiedContext;
713    
714                    // The order of priority of the context is: 1.) the specified context,
715                    // 2.) if the file name starts with DEPLOY_TO_PREFIX, use the file name
716                    // after the prefix, or 3.) the recommended deployment context as
717                    // specified in liferay-plugin-package.properties, or 4.) the file name.
718    
719                    if ((specifiedContext != null) &&
720                            srcFile.getName().startsWith(DEPLOY_TO_PREFIX)) {
721    
722                            displayName = srcFile.getName().substring(
723                                    DEPLOY_TO_PREFIX.length(), srcFile.getName().length() - 4);
724    
725                            overwrite = true;
726                            preliminaryContext = displayName;
727                    }
728    
729                    if (preliminaryContext == null) {
730                            preliminaryContext = getDisplayName(srcFile);
731                    }
732    
733                    if (pluginPackage != null) {
734                            if (!PluginPackageUtil.isCurrentVersionSupported(
735                                            pluginPackage.getLiferayVersions())) {
736    
737                                    throw new AutoDeployException(
738                                            srcFile.getName() +
739                                                    " does not support this version of Liferay");
740                            }
741    
742                            if (displayName == null) {
743                                    displayName = pluginPackage.getRecommendedDeploymentContext();
744                            }
745    
746                            if (Validator.isNull(displayName)) {
747                                    displayName = getDisplayName(srcFile);
748                            }
749    
750                            pluginPackage.setContext(displayName);
751    
752                            PluginPackageUtil.updateInstallingPluginPackage(
753                                    preliminaryContext, pluginPackage);
754                    }
755    
756                    if (Validator.isNotNull(displayName)) {
757                            deployDir = displayName + ".war";
758                    }
759                    else {
760                            deployDir = srcFile.getName();
761                            displayName = getDisplayName(srcFile);
762                    }
763    
764                    if (appServerType.equals(ServerDetector.JBOSS_ID)) {
765                            deployDir = jbossPrefix + deployDir;
766                    }
767                    else if (appServerType.equals(ServerDetector.JETTY_ID) ||
768                                     appServerType.equals(ServerDetector.OC4J_ID) ||
769                                     appServerType.equals(ServerDetector.RESIN_ID) ||
770                                     appServerType.equals(ServerDetector.TOMCAT_ID)) {
771    
772                            if (unpackWar) {
773                                    deployDir = deployDir.substring(0, deployDir.length() - 4);
774                            }
775                    }
776    
777                    deployDir = destDir + "/" + deployDir;
778    
779                    File deployDirFile = new File(deployDir);
780    
781                    try {
782                            PluginPackage previousPluginPackage = readPluginPackage(
783                                    deployDirFile);
784    
785                            if ((pluginPackage != null) && (previousPluginPackage != null)) {
786                                    if (_log.isInfoEnabled()) {
787                                            String name = pluginPackage.getName();
788                                            String previousVersion = previousPluginPackage.getVersion();
789                                            String version = pluginPackage.getVersion();
790    
791                                            _log.info(
792                                                    "Updating " + name + " from version " +
793                                                            previousVersion + " to version " + version);
794                                    }
795    
796                                    if (pluginPackage.isLaterVersionThan(previousPluginPackage)) {
797                                            overwrite = true;
798                                    }
799                            }
800    
801                            File mergeDirFile = new File(
802                                    srcFile.getParent() + "/merge/" + srcFile.getName());
803    
804                            if (srcFile.isDirectory()) {
805                                    deployDirectory(
806                                            srcFile, mergeDirFile, deployDirFile, displayName,
807                                            overwrite, pluginPackage);
808                            }
809                            else {
810                                    boolean deployed = deployFile(
811                                            srcFile, mergeDirFile, deployDirFile, displayName,
812                                            overwrite, pluginPackage);
813    
814                                    if (!deployed) {
815                                            String context = preliminaryContext;
816    
817                                            if (pluginPackage != null) {
818                                                    context = pluginPackage.getContext();
819                                            }
820    
821                                            PluginPackageUtil.endPluginPackageInstallation(context);
822                                    }
823                                    else {
824                                            if (appServerType.equals(ServerDetector.JBOSS_ID)) {
825                                                    File doDeployFile = new File(deployDir + ".dodeploy");
826    
827                                                    FileUtil.write(doDeployFile, StringPool.BLANK);
828                                            }
829                                    }
830                            }
831                    }
832                    catch (Exception e) {
833                            if (pluginPackage != null) {
834                                    PluginPackageUtil.endPluginPackageInstallation(
835                                            pluginPackage.getContext());
836                            }
837    
838                            throw e;
839                    }
840            }
841    
842            public boolean deployFile(
843                            File srcFile, File mergeDir, File deployDir, String displayName,
844                            boolean overwrite, PluginPackage pluginPackage)
845                    throws Exception {
846    
847                    boolean undeployOnRedeploy = false;
848    
849                    try {
850                            undeployOnRedeploy = PrefsPropsUtil.getBoolean(
851                                    PropsKeys.HOT_UNDEPLOY_ON_REDEPLOY,
852                                    PropsValues.HOT_UNDEPLOY_ON_REDEPLOY);
853                    }
854                    catch (Exception e) {
855    
856                            // This will only happen when running the deploy tool in Ant in the
857                            // classical way where the WAR file is actually massaged and
858                            // packaged.
859    
860                    }
861    
862                    if (undeployOnRedeploy) {
863                            DeployUtil.undeploy(appServerType, deployDir);
864                    }
865    
866                    if (!overwrite && UpToDateTask.isUpToDate(srcFile, deployDir)) {
867                            if (_log.isInfoEnabled()) {
868                                    _log.info(deployDir + " is already up to date");
869                            }
870    
871                            return false;
872                    }
873    
874                    File tempDir = new File(
875                            SystemProperties.get(SystemProperties.TMP_DIR) + File.separator +
876                                    Time.getTimestamp());
877    
878                    ExpandTask.expand(srcFile, tempDir);
879    
880                    deployDirectory(
881                            tempDir, mergeDir, deployDir, displayName, overwrite,
882                            pluginPackage);
883    
884                    DeleteTask.deleteDirectory(tempDir);
885    
886                    return true;
887            }
888    
889            public String downloadJar(String jar) throws Exception {
890                    String tmpDir = SystemProperties.get(SystemProperties.TMP_DIR);
891    
892                    File file = new File(
893                            tmpDir + "/liferay/com/liferay/portal/deploy/dependencies/" +
894                                    jar);
895    
896                    if (!file.exists()) {
897                            synchronized (this) {
898                                    String url = PropsUtil.get(
899                                            PropsKeys.LIBRARY_DOWNLOAD_URL + jar);
900    
901                                    if (_log.isInfoEnabled()) {
902                                            _log.info("Downloading library from " + url);
903                                    }
904    
905                                    byte[] bytes = HttpUtil.URLtoByteArray(url);
906    
907                                    FileUtil.write(file, bytes);
908                            }
909                    }
910    
911                    return FileUtil.getAbsolutePath(file);
912            }
913    
914            public String fixPortalDependencyJar(String portalJar) {
915                    if (portalJar.equals("antlr.jar")) {
916                            portalJar = "antlr2.jar";
917                    }
918    
919                    return portalJar;
920            }
921    
922            public DeploymentHandler getDeploymentHandler() {
923                    String prefix = "auto.deploy." + ServerDetector.getServerId() + ".jee.";
924    
925                    String dmId = PropsUtil.get(prefix + "dm.id");
926                    String dmUser =  PropsUtil.get(prefix + "dm.user");
927                    String dmPassword =  PropsUtil.get(prefix + "dm.passwd");
928                    String dfClassName = PropsUtil.get(prefix + "df.classname");
929    
930                    return new DeploymentHandler(dmId, dmUser, dmPassword, dfClassName);
931            }
932    
933            public String getDisplayName(File srcFile) {
934                    String displayName = srcFile.getName();
935    
936                    if (StringUtil.endsWith(displayName, ".war") ||
937                            StringUtil.endsWith(displayName, ".xml")) {
938    
939                            displayName = displayName.substring(0, displayName.length() - 4);
940                    }
941    
942                    if (appServerType.equals(ServerDetector.JBOSS_ID) &&
943                            Validator.isNotNull(jbossPrefix) &&
944                            displayName.startsWith(jbossPrefix)) {
945    
946                            displayName = displayName.substring(1, displayName.length());
947                    }
948    
949                    return displayName;
950            }
951    
952            public String getExtraContent(
953                            double webXmlVersion, File srcFile, String displayName)
954                    throws Exception {
955    
956                    StringBundler sb = new StringBundler();
957    
958                    sb.append("<display-name>");
959                    sb.append(displayName);
960                    sb.append("</display-name>");
961    
962                    sb.append("<listener>");
963                    sb.append("<listener-class>");
964                    sb.append("com.liferay.portal.kernel.servlet.");
965                    sb.append("SerializableSessionAttributeListener");
966                    sb.append("</listener-class>");
967                    sb.append("</listener>");
968    
969                    File serviceXml = new File(srcFile + "/WEB-INF/service.xml");
970    
971                    if (serviceXml.exists()) {
972                            sb.append("<listener>");
973                            sb.append("<listener-class>");
974                            sb.append("com.liferay.portal.kernel.spring.context.");
975                            sb.append("PortletContextLoaderListener");
976                            sb.append("</listener-class>");
977                            sb.append("</listener>");
978                    }
979    
980                    File serverConfigWsdd = new File(
981                            srcFile + "/WEB-INF/server-config.wsdd");
982    
983                    if (serverConfigWsdd.exists()) {
984                            File webXml = new File(srcFile + "/WEB-INF/web.xml");
985    
986                            String content = FileUtil.read(webXml);
987    
988                            if (!content.contains("axis.servicesPath")) {
989                                    String remotingContent = FileUtil.read(
990                                            DeployUtil.getResourcePath("remoting-web.xml"));
991    
992                                    sb.append(remotingContent);
993                            }
994                    }
995    
996                    boolean hasTaglib = false;
997    
998                    if (Validator.isNotNull(auiTaglibDTD) ||
999                            Validator.isNotNull(portletTaglibDTD) ||
1000                            Validator.isNotNull(portletExtTaglibDTD) ||
1001                            Validator.isNotNull(securityTaglibDTD) ||
1002                            Validator.isNotNull(themeTaglibDTD) ||
1003                            Validator.isNotNull(uiTaglibDTD) ||
1004                            Validator.isNotNull(utilTaglibDTD)) {
1005    
1006                            hasTaglib = true;
1007                    }
1008    
1009                    if (hasTaglib && (webXmlVersion > 2.3)) {
1010                            sb.append("<jsp-config>");
1011                    }
1012    
1013                    if (Validator.isNotNull(auiTaglibDTD)) {
1014                            sb.append("<taglib>");
1015                            sb.append("<taglib-uri>http://liferay.com/tld/aui</taglib-uri>");
1016                            sb.append("<taglib-location>");
1017                            sb.append("/WEB-INF/tld/liferay-aui.tld");
1018                            sb.append("</taglib-location>");
1019                            sb.append("</taglib>");
1020                    }
1021    
1022                    if (Validator.isNotNull(portletTaglibDTD)) {
1023                            sb.append("<taglib>");
1024                            sb.append(
1025                                    "<taglib-uri>http://java.sun.com/portlet_2_0</taglib-uri>");
1026                            sb.append("<taglib-location>");
1027                            sb.append("/WEB-INF/tld/liferay-portlet.tld");
1028                            sb.append("</taglib-location>");
1029                            sb.append("</taglib>");
1030                    }
1031    
1032                    if (Validator.isNotNull(portletExtTaglibDTD)) {
1033                            sb.append("<taglib>");
1034                            sb.append("<taglib-uri>");
1035                            sb.append("http://liferay.com/tld/portlet");
1036                            sb.append("</taglib-uri>");
1037                            sb.append("<taglib-location>");
1038                            sb.append("/WEB-INF/tld/liferay-portlet-ext.tld");
1039                            sb.append("</taglib-location>");
1040                            sb.append("</taglib>");
1041                    }
1042    
1043                    if (Validator.isNotNull(securityTaglibDTD)) {
1044                            sb.append("<taglib>");
1045                            sb.append("<taglib-uri>");
1046                            sb.append("http://liferay.com/tld/security");
1047                            sb.append("</taglib-uri>");
1048                            sb.append("<taglib-location>");
1049                            sb.append("/WEB-INF/tld/liferay-security.tld");
1050                            sb.append("</taglib-location>");
1051                            sb.append("</taglib>");
1052                    }
1053    
1054                    if (Validator.isNotNull(themeTaglibDTD)) {
1055                            sb.append("<taglib>");
1056                            sb.append("<taglib-uri>http://liferay.com/tld/theme</taglib-uri>");
1057                            sb.append("<taglib-location>");
1058                            sb.append("/WEB-INF/tld/liferay-theme.tld");
1059                            sb.append("</taglib-location>");
1060                            sb.append("</taglib>");
1061                    }
1062    
1063                    if (Validator.isNotNull(uiTaglibDTD)) {
1064                            sb.append("<taglib>");
1065                            sb.append("<taglib-uri>http://liferay.com/tld/ui</taglib-uri>");
1066                            sb.append("<taglib-location>");
1067                            sb.append("/WEB-INF/tld/liferay-ui.tld");
1068                            sb.append("</taglib-location>");
1069                            sb.append("</taglib>");
1070                    }
1071    
1072                    if (Validator.isNotNull(utilTaglibDTD)) {
1073                            sb.append("<taglib>");
1074                            sb.append("<taglib-uri>http://liferay.com/tld/util</taglib-uri>");
1075                            sb.append("<taglib-location>");
1076                            sb.append("/WEB-INF/tld/liferay-util.tld");
1077                            sb.append("</taglib-location>");
1078                            sb.append("</taglib>");
1079                    }
1080    
1081                    if (hasTaglib && (webXmlVersion > 2.3)) {
1082                            sb.append("</jsp-config>");
1083                    }
1084    
1085                    sb.append(getSessionFiltersContent());
1086    
1087                    return sb.toString();
1088            }
1089    
1090            public String getIgnoreFiltersContent(File srcFile) throws Exception {
1091                    boolean ignoreFiltersEnabled = true;
1092    
1093                    Properties properties = getPluginPackageProperties(srcFile);
1094    
1095                    if (properties != null) {
1096                            ignoreFiltersEnabled = GetterUtil.getBoolean(
1097                                    properties.getProperty("ignore-filters-enabled"), true);
1098                    }
1099    
1100                    if (ignoreFiltersEnabled) {
1101                            String ignoreFiltersContent = FileUtil.read(
1102                                    DeployUtil.getResourcePath("ignore-filters-web.xml"));
1103    
1104                            return ignoreFiltersContent;
1105                    }
1106                    else {
1107                            return StringPool.BLANK;
1108                    }
1109            }
1110    
1111            public String getInvokerFilterContent() {
1112                    StringBundler sb = new StringBundler(4);
1113    
1114                    sb.append(getInvokerFilterContent("ERROR"));
1115                    sb.append(getInvokerFilterContent("FORWARD"));
1116                    sb.append(getInvokerFilterContent("INCLUDE"));
1117                    sb.append(getInvokerFilterContent("REQUEST"));
1118    
1119                    return sb.toString();
1120            }
1121    
1122            public String getInvokerFilterContent(String dispatcher) {
1123                    StringBundler sb = new StringBundler(23);
1124    
1125                    sb.append("<filter>");
1126                    sb.append("<filter-name>Invoker Filter - ");
1127                    sb.append(dispatcher);
1128                    sb.append("</filter-name>");
1129                    sb.append("<filter-class>");
1130                    sb.append(InvokerFilter.class.getName());
1131                    sb.append("</filter-class>");
1132                    sb.append("<init-param>");
1133                    sb.append("<param-name>dispatcher</param-name>");
1134                    sb.append("<param-value>");
1135                    sb.append(dispatcher);
1136                    sb.append("</param-value>");
1137                    sb.append("</init-param>");
1138                    sb.append("</filter>");
1139    
1140                    sb.append("<filter-mapping>");
1141                    sb.append("<filter-name>Invoker Filter - ");
1142                    sb.append(dispatcher);
1143                    sb.append("</filter-name>");
1144                    sb.append("<url-pattern>/*</url-pattern>");
1145                    sb.append("<dispatcher>");
1146                    sb.append(dispatcher);
1147                    sb.append("</dispatcher>");
1148                    sb.append("</filter-mapping>");
1149    
1150                    return sb.toString();
1151            }
1152    
1153            public String getPluginPackageLicensesXml(List<License> licenses) {
1154                    if (licenses.isEmpty()) {
1155                            return StringPool.BLANK;
1156                    }
1157    
1158                    StringBundler sb = new StringBundler(5 * licenses.size() + 2);
1159    
1160                    for (int i = 0; i < licenses.size(); i++) {
1161                            License license = licenses.get(i);
1162    
1163                            if (i == 0) {
1164                                    sb.append("\r\n");
1165                            }
1166    
1167                            sb.append("\t\t<license osi-approved=\"");
1168                            sb.append(license.isOsiApproved());
1169                            sb.append("\">");
1170                            sb.append(license.getName());
1171                            sb.append("</license>\r\n");
1172    
1173                            if ((i + 1) == licenses.size()) {
1174                                    sb.append("\t");
1175                            }
1176                    }
1177    
1178                    return sb.toString();
1179            }
1180    
1181            public String getPluginPackageLiferayVersionsXml(
1182                    List<String> liferayVersions) {
1183    
1184                    if (liferayVersions.isEmpty()) {
1185                            return StringPool.BLANK;
1186                    }
1187    
1188                    StringBundler sb = new StringBundler(liferayVersions.size() * 3 + 2);
1189    
1190                    for (int i = 0; i < liferayVersions.size(); i++) {
1191                            String liferayVersion = liferayVersions.get(i);
1192    
1193                            if (i == 0) {
1194                                    sb.append("\r\n");
1195                            }
1196    
1197                            sb.append("\t\t<liferay-version>");
1198                            sb.append(liferayVersion);
1199                            sb.append("</liferay-version>\r\n");
1200    
1201                            if ((i + 1) == liferayVersions.size()) {
1202                                    sb.append("\t");
1203                            }
1204                    }
1205    
1206                    return sb.toString();
1207            }
1208    
1209            public Properties getPluginPackageProperties(File srcFile)
1210                    throws Exception {
1211    
1212                    File propertiesFile = new File(
1213                            srcFile + "/WEB-INF/liferay-plugin-package.properties");
1214    
1215                    if (!propertiesFile.exists()) {
1216                            return null;
1217                    }
1218    
1219                    String propertiesString = FileUtil.read(propertiesFile);
1220    
1221                    return PropertiesUtil.load(propertiesString);
1222            }
1223    
1224            public String getPluginPackageTagsXml(List<String> tags) {
1225                    if (tags.isEmpty()) {
1226                            return StringPool.BLANK;
1227                    }
1228    
1229                    StringBundler sb = new StringBundler(tags.size() * 3 + 2);
1230    
1231                    for (int i = 0; i < tags.size(); i++) {
1232                            String tag = tags.get(i);
1233    
1234                            if (i == 0) {
1235                                    sb.append("\r\n");
1236                            }
1237    
1238                            sb.append("\t\t<tag>");
1239                            sb.append(tag);
1240                            sb.append("</tag>\r\n");
1241    
1242                            if ((i + 1) == tags.size()) {
1243                                    sb.append("\t");
1244                            }
1245                    }
1246    
1247                    return sb.toString();
1248            }
1249    
1250            public Map<String, String> getPluginPackageXmlFilterMap(
1251                    PluginPackage pluginPackage) {
1252    
1253                    List<String> pluginTypes = pluginPackage.getTypes();
1254    
1255                    String pluginType = pluginTypes.get(0);
1256    
1257                    if (!pluginType.equals(getPluginType())) {
1258                            return null;
1259                    }
1260    
1261                    Map<String, String> filterMap = new HashMap<String, String>();
1262    
1263                    filterMap.put("module_group_id", pluginPackage.getGroupId());
1264                    filterMap.put("module_artifact_id", pluginPackage.getArtifactId());
1265                    filterMap.put("module_version", pluginPackage.getVersion());
1266    
1267                    filterMap.put("plugin_name", pluginPackage.getName());
1268                    filterMap.put("plugin_type", pluginType);
1269                    filterMap.put(
1270                            "plugin_type_name",
1271                            TextFormatter.format(pluginType, TextFormatter.J));
1272    
1273                    filterMap.put("tags", getPluginPackageTagsXml(pluginPackage.getTags()));
1274                    filterMap.put("short_description", pluginPackage.getShortDescription());
1275                    filterMap.put("long_description", pluginPackage.getLongDescription());
1276                    filterMap.put("change_log", pluginPackage.getChangeLog());
1277                    filterMap.put("page_url", pluginPackage.getPageURL());
1278                    filterMap.put("author", pluginPackage.getAuthor());
1279                    filterMap.put(
1280                            "licenses",
1281                            getPluginPackageLicensesXml(pluginPackage.getLicenses()));
1282                    filterMap.put(
1283                            "liferay_versions",
1284                            getPluginPackageLiferayVersionsXml(
1285                                    pluginPackage.getLiferayVersions()));
1286    
1287                    return filterMap;
1288            }
1289    
1290            public String getPluginType() {
1291                    return null;
1292            }
1293    
1294            public String getServletContextIncludeFiltersContent(
1295                            double webXmlVersion, File srcFile)
1296                    throws Exception {
1297    
1298                    boolean servletContextIncludeFiltersEnabled = true;
1299    
1300                    Properties properties = getPluginPackageProperties(srcFile);
1301    
1302                    if (properties != null) {
1303                            servletContextIncludeFiltersEnabled = GetterUtil.getBoolean(
1304                                    properties.getProperty(
1305                                            "servlet-context-include-filters-enabled"), true);
1306                    }
1307    
1308                    if (servletContextIncludeFiltersEnabled) {
1309                            String servletContextIncludeFiltersContent = FileUtil.read(
1310                                    DeployUtil.getResourcePath(
1311                                            "servlet-context-include-filters-web.xml"));
1312    
1313                            if (webXmlVersion < 2.4) {
1314                                    int x = servletContextIncludeFiltersContent.indexOf(
1315                                            "<dispatcher>");
1316                                    int y = servletContextIncludeFiltersContent.indexOf(
1317                                            "</filter-mapping>");
1318    
1319                                    if (x != -1) {
1320                                            if (_log.isWarnEnabled()) {
1321                                                    _log.warn("Please update web.xml to at least 2.4");
1322                                            }
1323    
1324                                            servletContextIncludeFiltersContent =
1325                                                    servletContextIncludeFiltersContent.substring(0, x) +
1326                                                            servletContextIncludeFiltersContent.substring(y);
1327                                    }
1328                            }
1329    
1330                            return servletContextIncludeFiltersContent;
1331                    }
1332                    else {
1333                            return StringPool.BLANK;
1334                    }
1335            }
1336    
1337            public String getSessionFiltersContent() throws Exception {
1338                    String sessionFiltersContent = FileUtil.read(
1339                            DeployUtil.getResourcePath("session-filters-web.xml"));
1340    
1341                    return sessionFiltersContent;
1342            }
1343    
1344            public String getSpeedFiltersContent(File srcFile) throws Exception {
1345                    boolean speedFiltersEnabled = true;
1346    
1347                    Properties properties = getPluginPackageProperties(srcFile);
1348    
1349                    if (properties != null) {
1350                            speedFiltersEnabled = GetterUtil.getBoolean(
1351                                    properties.getProperty("speed-filters-enabled"), true);
1352                    }
1353    
1354                    if (speedFiltersEnabled) {
1355                            String speedFiltersContent = FileUtil.read(
1356                                    DeployUtil.getResourcePath("speed-filters-web.xml"));
1357    
1358                            return speedFiltersContent;
1359                    }
1360                    else {
1361                            return StringPool.BLANK;
1362                    }
1363            }
1364    
1365            public boolean isJEEDeploymentEnabled() {
1366                    return GetterUtil.getBoolean(PropsUtil.get(
1367                            "auto.deploy." + ServerDetector.getServerId() +
1368                                    ".jee.deployment.enabled"));
1369            }
1370    
1371            public void mergeDirectory(File mergeDir, File targetDir) {
1372                    if ((mergeDir == null) || (!mergeDir.exists())) {
1373                            return;
1374                    }
1375    
1376                    CopyTask.copyDirectory(mergeDir, targetDir, null, null, true, false);
1377            }
1378    
1379            public Map<String, String> processPluginPackageProperties(
1380                            File srcFile, String displayName, PluginPackage pluginPackage)
1381                    throws Exception {
1382    
1383                    if (pluginPackage == null) {
1384                            return null;
1385                    }
1386    
1387                    Properties properties = getPluginPackageProperties(srcFile);
1388    
1389                    if ((properties == null) || (properties.size() == 0)) {
1390                            return null;
1391                    }
1392    
1393                    Map<String, String> filterMap = getPluginPackageXmlFilterMap(
1394                            pluginPackage);
1395    
1396                    if (filterMap == null) {
1397                            return null;
1398                    }
1399    
1400                    copyDependencyXml(
1401                            "liferay-plugin-package.xml", srcFile + "/WEB-INF", filterMap,
1402                            true);
1403    
1404                    return filterMap;
1405            }
1406    
1407            public PluginPackage readPluginPackage(File file) {
1408                    if (!file.exists()) {
1409                            return null;
1410                    }
1411    
1412                    InputStream is = null;
1413                    ZipFile zipFile = null;
1414    
1415                    try {
1416                            boolean parseProps = false;
1417    
1418                            if (file.isDirectory()) {
1419                                    String path = file.getPath();
1420    
1421                                    File pluginPackageXmlFile = new File(
1422                                            file.getParent() + "/merge/" + file.getName() +
1423                                                    "/WEB-INF/liferay-plugin-package.xml");
1424    
1425                                    if (pluginPackageXmlFile.exists()) {
1426                                            is = new FileInputStream(pluginPackageXmlFile);
1427                                    }
1428                                    else {
1429                                            pluginPackageXmlFile = new File(
1430                                                    path + "/WEB-INF/liferay-plugin-package.xml");
1431    
1432                                            if (pluginPackageXmlFile.exists()) {
1433                                                    is = new FileInputStream(pluginPackageXmlFile);
1434                                            }
1435                                    }
1436    
1437                                    File pluginPackagePropertiesFile = new File(
1438                                            file.getParent() + "/merge/" + file.getName() +
1439                                                    "/WEB-INF/liferay-plugin-package.properties");
1440    
1441                                    if ((is == null) && pluginPackagePropertiesFile.exists()) {
1442                                            is = new FileInputStream(pluginPackagePropertiesFile);
1443    
1444                                            parseProps = true;
1445                                    }
1446                                    else {
1447                                            pluginPackagePropertiesFile = new File(
1448                                                    path + "/WEB-INF/liferay-plugin-package.properties");
1449    
1450                                            if ((is == null) && pluginPackagePropertiesFile.exists()) {
1451                                                    is = new FileInputStream(pluginPackagePropertiesFile);
1452    
1453                                                    parseProps = true;
1454                                            }
1455                                    }
1456                            }
1457                            else {
1458                                    zipFile = new ZipFile(file);
1459    
1460                                    File pluginPackageXmlFile = new File(
1461                                            file.getParent() + "/merge/" + file.getName() +
1462                                                    "/WEB-INF/liferay-plugin-package.xml");
1463    
1464                                    if (pluginPackageXmlFile.exists()) {
1465                                            is = new FileInputStream(pluginPackageXmlFile);
1466                                    }
1467                                    else {
1468                                            ZipEntry zipEntry = zipFile.getEntry(
1469                                                    "WEB-INF/liferay-plugin-package.xml");
1470    
1471                                            if (zipEntry != null) {
1472                                                    is = zipFile.getInputStream(zipEntry);
1473                                            }
1474                                    }
1475    
1476                                    File pluginPackagePropertiesFile = new File(
1477                                            file.getParent() + "/merge/" + file.getName() +
1478                                                    "/WEB-INF/liferay-plugin-package.properties");
1479    
1480                                    if ((is == null) && pluginPackagePropertiesFile.exists()) {
1481                                            is = new FileInputStream(pluginPackagePropertiesFile);
1482    
1483                                            parseProps = true;
1484                                    }
1485                                    else {
1486                                            ZipEntry zipEntry = zipFile.getEntry(
1487                                                    "WEB-INF/liferay-plugin-package.properties");
1488    
1489                                            if ((is == null) && (zipEntry != null)) {
1490                                                    is = zipFile.getInputStream(zipEntry);
1491    
1492                                                    parseProps = true;
1493                                            }
1494                                    }
1495                            }
1496    
1497                            if (is == null) {
1498                                    if (_log.isInfoEnabled()) {
1499                                            _log.info(
1500                                                    file.getPath() + " does not have a " +
1501                                                            "WEB-INF/liferay-plugin-package.xml or " +
1502                                                                    "WEB-INF/liferay-plugin-package.properties");
1503                                    }
1504    
1505                                    return null;
1506                            }
1507    
1508                            if (parseProps) {
1509                                    String displayName = getDisplayName(file);
1510    
1511                                    String propertiesString = StringUtil.read(is);
1512    
1513                                    Properties properties = PropertiesUtil.load(propertiesString);
1514    
1515                                    return PluginPackageUtil.readPluginPackageProperties(
1516                                            displayName, properties);
1517                            }
1518                            else {
1519                                    String xml = StringUtil.read(is);
1520    
1521                                    xml = XMLFormatter.fixProlog(xml);
1522    
1523                                    return PluginPackageUtil.readPluginPackageXml(xml);
1524                            }
1525                    }
1526                    catch (Exception e) {
1527                            _log.error(file.getPath() + ": " + e.toString());
1528                    }
1529                    finally {
1530                            if (is != null) {
1531                                    try {
1532                                            is.close();
1533                                    }
1534                                    catch (IOException ioe) {
1535                                    }
1536                            }
1537    
1538                            if (zipFile != null) {
1539                                    try {
1540                                            zipFile.close();
1541                                    }
1542                                    catch (IOException ioe) {
1543                                    }
1544                            }
1545                    }
1546    
1547                    return null;
1548            }
1549    
1550            public void rewriteFiles(File srcDir) throws Exception {
1551                    String[] files = FileUtil.listFiles(srcDir + "/WEB-INF/");
1552    
1553                    for (int i = 0; i < files.length; i++) {
1554                            String fileName = GetterUtil.getString(
1555                                    FileUtil.getShortFileName(files[i]));
1556    
1557                            // LEP-6415
1558    
1559                            if (fileName.equalsIgnoreCase("mule-config.xml")) {
1560                                    continue;
1561                            }
1562    
1563                            String ext = GetterUtil.getString(FileUtil.getExtension(files[i]));
1564    
1565                            if (!ext.equalsIgnoreCase("xml")) {
1566                                    continue;
1567                            }
1568    
1569                            // Make sure to rewrite any XML files to include external entities
1570                            // into same file. See LEP-3142.
1571    
1572                            File file = new File(srcDir + "/WEB-INF/" + files[i]);
1573    
1574                            try {
1575                                    Document doc = SAXReaderUtil.read(file);
1576    
1577                                    String content = doc.formattedString(StringPool.TAB, true);
1578    
1579                                    FileUtil.write(file, content);
1580                            }
1581                            catch (Exception e) {
1582                                    if (_log.isWarnEnabled()) {
1583                                            _log.warn(
1584                                                    "Unable to format " + file + ": " + e.getMessage());
1585                                    }
1586                            }
1587                    }
1588            }
1589    
1590            public void setAppServerType(String appServerType) {
1591                    this.appServerType = appServerType;
1592            }
1593    
1594            public void setAuiTaglibDTD(String auiTaglibDTD) {
1595                    this.auiTaglibDTD = auiTaglibDTD;
1596            }
1597    
1598            public void setBaseDir(String baseDir) {
1599                    this.baseDir = baseDir;
1600            }
1601    
1602            public void setDestDir(String destDir) {
1603                    this.destDir = destDir;
1604            }
1605    
1606            public void setFilePattern(String filePattern) {
1607                    this.filePattern = filePattern;
1608            }
1609    
1610            public void setJars(List<String> jars) {
1611                    this.jars = jars;
1612            }
1613    
1614            public void setJbossPrefix(String jbossPrefix) {
1615                    this.jbossPrefix = jbossPrefix;
1616            }
1617    
1618            public void setPortletExtTaglibDTD(String portletExtTaglibDTD) {
1619                    this.portletExtTaglibDTD = portletExtTaglibDTD;
1620            }
1621    
1622            public void setPortletTaglibDTD(String portletTaglibDTD) {
1623                    this.portletTaglibDTD = portletTaglibDTD;
1624            }
1625    
1626            public void setSecurityTaglibDTD(String securityTaglibDTD) {
1627                    this.securityTaglibDTD = securityTaglibDTD;
1628            }
1629    
1630            public void setThemeTaglibDTD(String themeTaglibDTD) {
1631                    this.themeTaglibDTD = themeTaglibDTD;
1632            }
1633    
1634            public void setTomcatLibDir(String tomcatLibDir) {
1635                    this.tomcatLibDir = tomcatLibDir;
1636            }
1637    
1638            public void setUiTaglibDTD(String uiTaglibDTD) {
1639                    this.uiTaglibDTD = uiTaglibDTD;
1640            }
1641    
1642            public void setUnpackWar(boolean unpackWar) {
1643                    this.unpackWar = unpackWar;
1644            }
1645    
1646            public void setUtilTaglibDTD(String utilTaglibDTD) {
1647                    this.utilTaglibDTD = utilTaglibDTD;
1648            }
1649    
1650            public void setWars(List<String> wars) {
1651                    this.wars = wars;
1652            }
1653    
1654            public void updateDeployDirectory(File srcFile) throws Exception {
1655            }
1656    
1657            public void updateGeronimoWebXml(
1658                            File srcFile, String displayName, PluginPackage pluginPackage)
1659                    throws Exception {
1660    
1661                    if (!appServerType.equals(ServerDetector.GERONIMO_ID)) {
1662                            return;
1663                    }
1664    
1665                    File geronimoWebXml = new File(srcFile + "/WEB-INF/geronimo-web.xml");
1666    
1667                    Document doc = SAXReaderUtil.read(geronimoWebXml);
1668    
1669                    Element root = doc.getRootElement();
1670    
1671                    Element environmentEl = root.element("environment");
1672    
1673                    Element moduleIdEl = environmentEl.element("moduleId");
1674    
1675                    Element artifactIdEl = moduleIdEl.element("artifactId");
1676    
1677                    artifactIdEl.setText(displayName);
1678    
1679                    Element versionEl = moduleIdEl.element("version");
1680    
1681                    versionEl.setText(pluginPackage.getVersion());
1682    
1683                    String content = doc.formattedString();
1684    
1685                    FileUtil.write(geronimoWebXml, content);
1686    
1687                    if (_log.isInfoEnabled()) {
1688                            _log.info("Modifying Geronimo " + geronimoWebXml);
1689                    }
1690            }
1691    
1692            public String updateLiferayWebXml(File srcFile, String webXmlContent)
1693                    throws Exception {
1694    
1695                    webXmlContent = WebXMLBuilder.organizeWebXML(webXmlContent);
1696    
1697                    int x = webXmlContent.indexOf("<filter>");
1698                    int y = webXmlContent.lastIndexOf("</filter-mapping>");
1699    
1700                    if ((x == -1) || (y == -1)) {
1701                            return webXmlContent;
1702                    }
1703    
1704                    String filterContent = webXmlContent.substring(x, y + 17);
1705    
1706                    String liferayWebXmlContent = FileUtil.read(
1707                            DeployUtil.getResourcePath("web.xml"));
1708    
1709                    int z = liferayWebXmlContent.indexOf("</web-app>");
1710    
1711                    liferayWebXmlContent =
1712                            liferayWebXmlContent.substring(0, z) + filterContent +
1713                                    liferayWebXmlContent.substring(z);
1714    
1715                    liferayWebXmlContent = WebXMLBuilder.organizeWebXML(
1716                            liferayWebXmlContent);
1717    
1718                    FileUtil.write(
1719                            srcFile + "/WEB-INF/liferay-web.xml", liferayWebXmlContent);
1720    
1721                    webXmlContent =
1722                            webXmlContent.substring(0, x) + getInvokerFilterContent() +
1723                                    webXmlContent.substring(y + 17);
1724    
1725                    return webXmlContent;
1726            }
1727    
1728            public void updateWebXml(
1729                            File webXml, File srcFile, String displayName,
1730                            PluginPackage pluginPackage)
1731                    throws Exception {
1732    
1733                    String content = FileUtil.read(webXml);
1734    
1735                    int x = content.indexOf("<display-name>");
1736    
1737                    if (x != -1) {
1738                            int y = content.indexOf("</display-name>", x);
1739    
1740                            y = content.indexOf(">", y) + 1;
1741    
1742                            content = content.substring(0, x) + content.substring(y);
1743                    }
1744    
1745                    double webXmlVersion = 2.3;
1746    
1747                    Document webXmlDoc = SAXReaderUtil.read(content);
1748    
1749                    Element webXmlRoot = webXmlDoc.getRootElement();
1750    
1751                    webXmlVersion = GetterUtil.getDouble(
1752                            webXmlRoot.attributeValue("version"), webXmlVersion);
1753    
1754                    // Merge extra content
1755    
1756                    String extraContent = getExtraContent(
1757                            webXmlVersion, srcFile, displayName);
1758    
1759                    int pos = content.indexOf("</web-app>");
1760    
1761                    String newContent =
1762                            content.substring(0, pos) + extraContent +
1763                                    content.substring(pos, content.length());
1764    
1765                    // Replace old package names
1766    
1767                    newContent = StringUtil.replace(
1768                            newContent, "com.liferay.portal.shared.",
1769                            "com.liferay.portal.kernel.");
1770    
1771                    // Update liferay-web.xml
1772    
1773                    if (webXmlVersion > 2.3) {
1774                            newContent = updateLiferayWebXml(srcFile, newContent);
1775                    }
1776    
1777                    // Update web.xml
1778    
1779                    newContent = WebXMLBuilder.organizeWebXML(newContent);
1780    
1781                    FileUtil.write(webXml, newContent, true);
1782    
1783                    if (_log.isInfoEnabled()) {
1784                            _log.info("Modifying Servlet " + webXmlVersion + " " + webXml);
1785                    }
1786            }
1787    
1788            protected String appServerType;
1789            protected String auiTaglibDTD;
1790            protected String baseDir;
1791            protected String destDir;
1792            protected String filePattern;
1793            protected List<String> jars;
1794            protected String jbossPrefix;
1795            protected String portletExtTaglibDTD;
1796            protected String portletTaglibDTD;
1797            protected String securityTaglibDTD;
1798            protected String themeTaglibDTD;
1799            protected String tomcatLibDir;
1800            protected String uiTaglibDTD;
1801            protected boolean unpackWar;
1802            protected String utilTaglibDTD;
1803            protected List<String> wars;
1804    
1805            private static final String _PORTAL_CLASS_LOADER =
1806                    "com.liferay.support.tomcat.loader.PortalClassLoader";
1807    
1808            private static Log _log = LogFactoryUtil.getLog(BaseDeployer.class);
1809    
1810    }