001
014
015 package com.liferay.portal.tools.deploy;
016
017 import com.liferay.portal.deploy.DeployUtil;
018 import com.liferay.portal.deploy.auto.AutoDeployer;
019 import com.liferay.portal.kernel.deploy.Deployer;
020 import com.liferay.portal.kernel.deploy.auto.AutoDeployException;
021 import com.liferay.portal.kernel.deploy.auto.context.AutoDeploymentContext;
022 import com.liferay.portal.kernel.log.Log;
023 import com.liferay.portal.kernel.log.LogFactoryUtil;
024 import com.liferay.portal.kernel.plugin.License;
025 import com.liferay.portal.kernel.plugin.PluginPackage;
026 import com.liferay.portal.kernel.servlet.PluginContextListener;
027 import com.liferay.portal.kernel.servlet.PortalClassLoaderServlet;
028 import com.liferay.portal.kernel.servlet.PortletServlet;
029 import com.liferay.portal.kernel.servlet.SecurePluginContextListener;
030 import com.liferay.portal.kernel.servlet.SecureServlet;
031 import com.liferay.portal.kernel.servlet.SerializableSessionAttributeListener;
032 import com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilter;
033 import com.liferay.portal.kernel.util.CharPool;
034 import com.liferay.portal.kernel.util.FileUtil;
035 import com.liferay.portal.kernel.util.GetterUtil;
036 import com.liferay.portal.kernel.util.HttpUtil;
037 import com.liferay.portal.kernel.util.PropertiesUtil;
038 import com.liferay.portal.kernel.util.PropsKeys;
039 import com.liferay.portal.kernel.util.ServerDetector;
040 import com.liferay.portal.kernel.util.StreamUtil;
041 import com.liferay.portal.kernel.util.StringBundler;
042 import com.liferay.portal.kernel.util.StringPool;
043 import com.liferay.portal.kernel.util.StringUtil;
044 import com.liferay.portal.kernel.util.SystemProperties;
045 import com.liferay.portal.kernel.util.TextFormatter;
046 import com.liferay.portal.kernel.util.Time;
047 import com.liferay.portal.kernel.util.Validator;
048 import com.liferay.portal.kernel.xml.Document;
049 import com.liferay.portal.kernel.xml.Element;
050 import com.liferay.portal.kernel.xml.SAXReaderUtil;
051 import com.liferay.portal.plugin.PluginPackageUtil;
052 import com.liferay.portal.tools.WebXMLBuilder;
053 import com.liferay.portal.util.ExtRegistry;
054 import com.liferay.portal.util.InitUtil;
055 import com.liferay.portal.util.PortalUtil;
056 import com.liferay.portal.util.PrefsPropsUtil;
057 import com.liferay.portal.util.PropsUtil;
058 import com.liferay.portal.util.PropsValues;
059 import com.liferay.portal.webserver.DynamicResourceServlet;
060 import com.liferay.util.ant.CopyTask;
061 import com.liferay.util.ant.DeleteTask;
062 import com.liferay.util.ant.ExpandTask;
063 import com.liferay.util.ant.UpToDateTask;
064 import com.liferay.util.ant.WarTask;
065 import com.liferay.util.xml.DocUtil;
066 import com.liferay.util.xml.XMLFormatter;
067
068 import java.io.File;
069 import java.io.FileInputStream;
070 import java.io.IOException;
071 import java.io.InputStream;
072
073 import java.util.ArrayList;
074 import java.util.HashMap;
075 import java.util.List;
076 import java.util.Map;
077 import java.util.Properties;
078 import java.util.Set;
079 import java.util.zip.ZipEntry;
080 import java.util.zip.ZipFile;
081
082 import org.apache.oro.io.GlobFilenameFilter;
083
084
088 public class BaseDeployer implements AutoDeployer, Deployer {
089
090 public static final String DEPLOY_TO_PREFIX = "DEPLOY_TO__";
091
092 public static void main(String[] args) {
093 InitUtil.initWithSpring();
094
095 List<String> wars = new ArrayList<String>();
096 List<String> jars = new ArrayList<String>();
097
098 for (String arg : args) {
099 String fileName = arg.toLowerCase();
100
101 if (fileName.endsWith(".war")) {
102 wars.add(arg);
103 }
104 else if (fileName.endsWith(".jar")) {
105 jars.add(arg);
106 }
107 }
108
109 new BaseDeployer(wars, jars);
110 }
111
112 public BaseDeployer() {
113 }
114
115 public BaseDeployer(List<String> wars, List<String> jars) {
116 baseDir = System.getProperty("deployer.base.dir");
117 destDir = System.getProperty("deployer.dest.dir");
118 appServerType = System.getProperty("deployer.app.server.type");
119 auiTaglibDTD = System.getProperty("deployer.aui.taglib.dtd");
120 portletTaglibDTD = System.getProperty("deployer.portlet.taglib.dtd");
121 portletExtTaglibDTD = System.getProperty(
122 "deployer.portlet.ext.taglib.dtd");
123 securityTaglibDTD = System.getProperty("deployer.security.taglib.dtd");
124 themeTaglibDTD = System.getProperty("deployer.theme.taglib.dtd");
125 uiTaglibDTD = System.getProperty("deployer.ui.taglib.dtd");
126 utilTaglibDTD = System.getProperty("deployer.util.taglib.dtd");
127 unpackWar = GetterUtil.getBoolean(
128 System.getProperty("deployer.unpack.war"), true);
129 filePattern = System.getProperty("deployer.file.pattern");
130 jbossPrefix = GetterUtil.getString(
131 System.getProperty("deployer.jboss.prefix"));
132 tomcatLibDir = System.getProperty("deployer.tomcat.lib.dir");
133 this.wars = wars;
134 this.jars = jars;
135
136 checkArguments();
137
138 String context = System.getProperty("deployer.context");
139
140 try {
141 deploy(context);
142 }
143 catch (Exception e) {
144 e.printStackTrace();
145 }
146 }
147
148 public void addExtJar(List<String> jars, String resource) throws Exception {
149 Set<String> servletContextNames = ExtRegistry.getServletContextNames();
150
151 for (String servletContextName : servletContextNames) {
152 String extResource =
153 "ext-" + servletContextName + resource.substring(3);
154
155 String path = DeployUtil.getResourcePath(extResource);
156
157 if (_log.isDebugEnabled()) {
158 if (path == null) {
159 _log.debug("Resource " + extResource + " is not available");
160 }
161 else {
162 _log.debug(
163 "Resource " + extResource + " is available at " + path);
164 }
165 }
166
167 if (path != null) {
168 jars.add(path);
169 }
170 }
171 }
172
173 public void addRequiredJar(List<String> jars, String resource)
174 throws Exception {
175
176 String path = DeployUtil.getResourcePath(resource);
177
178 if (path == null) {
179 throw new RuntimeException(
180 "Resource " + resource + " does not exist");
181 }
182
183 if (_log.isDebugEnabled()) {
184 _log.debug("Resource " + resource + " is available at " + path);
185 }
186
187 jars.add(path);
188 }
189
190 public int autoDeploy(AutoDeploymentContext autoDeploymentContext)
191 throws AutoDeployException {
192
193 List<String> wars = new ArrayList<String>();
194
195 File file = autoDeploymentContext.getFile();
196
197 wars.add(file.getName());
198
199 this.wars = wars;
200
201 try {
202 return deployFile(autoDeploymentContext);
203 }
204 catch (Exception e) {
205 throw new AutoDeployException(e);
206 }
207 }
208
209 public void checkArguments() {
210 if (Validator.isNull(baseDir)) {
211 throw new IllegalArgumentException(
212 "The system property deployer.base.dir is not set");
213 }
214
215 if (Validator.isNull(destDir)) {
216 throw new IllegalArgumentException(
217 "The system property deployer.dest.dir is not set");
218 }
219
220 if (Validator.isNull(appServerType)) {
221 throw new IllegalArgumentException(
222 "The system property deployer.app.server.type is not set");
223 }
224
225 if (!appServerType.equals(ServerDetector.GERONIMO_ID) &&
226 !appServerType.equals(ServerDetector.GLASSFISH_ID) &&
227 !appServerType.equals(ServerDetector.JBOSS_ID) &&
228 !appServerType.equals(ServerDetector.JONAS_ID) &&
229 !appServerType.equals(ServerDetector.JETTY_ID) &&
230 !appServerType.equals(ServerDetector.OC4J_ID) &&
231 !appServerType.equals(ServerDetector.RESIN_ID) &&
232 !appServerType.equals(ServerDetector.TOMCAT_ID) &&
233 !appServerType.equals(ServerDetector.WEBLOGIC_ID) &&
234 !appServerType.equals(ServerDetector.WEBSPHERE_ID)) {
235
236 throw new IllegalArgumentException(
237 appServerType + " is not a valid application server type");
238 }
239
240 if (appServerType.equals(ServerDetector.GLASSFISH_ID) ||
241 appServerType.equals(ServerDetector.WEBSPHERE_ID)) {
242
243 unpackWar = false;
244 }
245
246 if (Validator.isNotNull(jbossPrefix) &&
247 !Validator.isNumber(jbossPrefix)) {
248
249 jbossPrefix = "1";
250 }
251 }
252
253 public void copyDependencyXml(String fileName, String targetDir)
254 throws Exception {
255
256 copyDependencyXml(fileName, targetDir, null);
257 }
258
259 public void copyDependencyXml(
260 String fileName, String targetDir, Map<String, String> filterMap)
261 throws Exception {
262
263 copyDependencyXml(fileName, targetDir, filterMap, false);
264 }
265
266 public void copyDependencyXml(
267 String fileName, String targetDir, Map<String, String> filterMap,
268 boolean overwrite)
269 throws Exception {
270
271 DeployUtil.copyDependencyXml(
272 fileName, targetDir, fileName, filterMap, overwrite);
273 }
274
275 public void copyDtds(File srcFile, PluginPackage pluginPackage)
276 throws Exception {
277
278 File portalLog4jXml = new File(
279 srcFile.getAbsolutePath() +
280 "/WEB-INF/classes/META-INF/portal-log4j.xml");
281
282 if (portalLog4jXml.exists()) {
283 InputStream is = null;
284
285 try {
286 Class<?> clazz = getClass();
287
288 ClassLoader classLoader = clazz.getClassLoader();
289
290 is = classLoader.getResourceAsStream("META-INF/log4j.dtd");
291
292 File file = new File(
293 srcFile.getAbsolutePath() +
294 "/WEB-INF/classes/META-INF/log4j.dtd");
295
296 FileUtil.write(file, is);
297 }
298 finally {
299 StreamUtil.cleanUp(is);
300 }
301 }
302 }
303
304 public void copyJars(File srcFile, PluginPackage pluginPackage)
305 throws Exception {
306
307 for (int i = 0; i < jars.size(); i++) {
308 String jarFullName = jars.get(i);
309
310 String jarName = jarFullName.substring(
311 jarFullName.lastIndexOf("/") + 1);
312
313 if (!appServerType.equals(ServerDetector.TOMCAT_ID) ||
314 (appServerType.equals(ServerDetector.TOMCAT_ID) &&
315 !jarFullName.equals("util-java.jar"))) {
316
317 FileUtil.copyFile(
318 jarFullName, srcFile + "/WEB-INF/lib/" + jarName, true);
319 }
320 }
321
322 FileUtil.delete(srcFile + "/WEB-INF/lib/util-jsf.jar");
323 }
324
325 public void copyPortalDependencies(File srcFile) throws Exception {
326 Properties properties = getPluginPackageProperties(srcFile);
327
328 if (properties == null) {
329 return;
330 }
331
332
333
334 String[] portalJars = StringUtil.split(
335 properties.getProperty(
336 "portal-dependency-jars",
337 properties.getProperty("portal.dependency.jars")));
338
339 for (String portalJar : portalJars) {
340 portalJar = portalJar.trim();
341
342 portalJar = fixPortalDependencyJar(portalJar);
343
344 if (_log.isDebugEnabled()) {
345 _log.debug("Copy portal JAR " + portalJar);
346 }
347
348 try {
349 String portalJarPath = PortalUtil.getPortalLibDir() + portalJar;
350
351 FileUtil.copyFile(
352 portalJarPath, srcFile + "/WEB-INF/lib/" + portalJar, true);
353 }
354 catch (Exception e) {
355 _log.error("Unable to copy portal JAR " + portalJar, e);
356 }
357 }
358
359
360
361 String[] portalTlds = StringUtil.split(
362 properties.getProperty(
363 "portal-dependency-tlds",
364 properties.getProperty("portal.dependency.tlds")));
365
366 for (String portalTld : portalTlds) {
367 portalTld = portalTld.trim();
368
369 if (_log.isDebugEnabled()) {
370 _log.debug("Copy portal TLD " + portalTld);
371 }
372
373 try {
374 String portalTldPath = DeployUtil.getResourcePath(portalTld);
375
376 FileUtil.copyFile(
377 portalTldPath, srcFile + "/WEB-INF/tld/" + portalTld, true);
378 }
379 catch (Exception e) {
380 _log.error("Unable to copy portal TLD " + portalTld, e);
381 }
382 }
383
384
385
386 File pluginLibDir = new File(srcFile + "/WEB-INF/lib/");
387
388 if (PropsValues.AUTO_DEPLOY_COPY_COMMONS_LOGGING) {
389 String[] commonsLoggingJars = pluginLibDir.list(
390 new GlobFilenameFilter("commons-logging*.jar"));
391
392 if ((commonsLoggingJars == null) ||
393 (commonsLoggingJars.length == 0)) {
394
395 String portalJarPath =
396 PortalUtil.getPortalLibDir() + "commons-logging.jar";
397
398 FileUtil.copyFile(
399 portalJarPath, srcFile + "/WEB-INF/lib/commons-logging.jar",
400 true);
401 }
402 }
403
404
405
406 if (PropsValues.AUTO_DEPLOY_COPY_LOG4J) {
407 String[] log4jJars = pluginLibDir.list(
408 new GlobFilenameFilter("log4j*.jar"));
409
410 if ((log4jJars == null) || (log4jJars.length == 0)) {
411 String portalJarPath =
412 PortalUtil.getPortalLibDir() + "log4j.jar";
413
414 FileUtil.copyFile(
415 portalJarPath, srcFile + "/WEB-INF/lib/log4j.jar", true);
416 }
417 }
418 }
419
420 public void copyProperties(File srcFile, PluginPackage pluginPackage)
421 throws Exception {
422
423 if (PropsValues.AUTO_DEPLOY_COPY_COMMONS_LOGGING) {
424 copyDependencyXml(
425 "logging.properties", srcFile + "/WEB-INF/classes");
426 }
427
428 if (PropsValues.AUTO_DEPLOY_COPY_LOG4J) {
429 copyDependencyXml("log4j.properties", srcFile + "/WEB-INF/classes");
430 }
431
432 File servicePropertiesFile = new File(
433 srcFile.getAbsolutePath() + "/WEB-INF/classes/service.properties");
434
435 if (servicePropertiesFile.exists()) {
436 File portletPropertiesFile = new File(
437 srcFile.getAbsolutePath() +
438 "/WEB-INF/classes/portlet.properties");
439
440 if (!portletPropertiesFile.exists()) {
441 String pluginPackageName = null;
442
443 if (pluginPackage != null) {
444 pluginPackageName = pluginPackage.getName();
445 }
446 else {
447 pluginPackageName = srcFile.getName();
448 }
449
450 FileUtil.write(
451 portletPropertiesFile,
452 "plugin.package.name=" + pluginPackageName);
453 }
454 }
455 }
456
457 public void copyTlds(File srcFile, PluginPackage pluginPackage)
458 throws Exception {
459
460 if (Validator.isNotNull(auiTaglibDTD)) {
461 FileUtil.copyFile(
462 auiTaglibDTD, srcFile + "/WEB-INF/tld/aui.tld", true);
463 }
464
465 if (Validator.isNotNull(portletTaglibDTD)) {
466 FileUtil.copyFile(
467 portletTaglibDTD, srcFile + "/WEB-INF/tld/liferay-portlet.tld",
468 true);
469 }
470
471 if (Validator.isNotNull(portletExtTaglibDTD)) {
472 FileUtil.copyFile(
473 portletExtTaglibDTD,
474 srcFile + "/WEB-INF/tld/liferay-portlet-ext.tld", true);
475 }
476
477 if (Validator.isNotNull(securityTaglibDTD)) {
478 FileUtil.copyFile(
479 securityTaglibDTD,
480 srcFile + "/WEB-INF/tld/liferay-security.tld", true);
481 }
482
483 if (Validator.isNotNull(themeTaglibDTD)) {
484 FileUtil.copyFile(
485 themeTaglibDTD, srcFile + "/WEB-INF/tld/liferay-theme.tld",
486 true);
487 }
488
489 if (Validator.isNotNull(uiTaglibDTD)) {
490 FileUtil.copyFile(
491 uiTaglibDTD, srcFile + "/WEB-INF/tld/liferay-ui.tld", true);
492 }
493
494 if (Validator.isNotNull(utilTaglibDTD)) {
495 FileUtil.copyFile(
496 utilTaglibDTD, srcFile + "/WEB-INF/tld/liferay-util.tld", true);
497 }
498 }
499
500 public void copyTomcatContextXml(File targetDir) throws Exception {
501 if (!appServerType.equals(ServerDetector.TOMCAT_ID)) {
502 return;
503 }
504
505 File targetFile = new File(targetDir, "META-INF/context.xml");
506
507 if (targetFile.exists()) {
508 return;
509 }
510
511 String contextPath = DeployUtil.getResourcePath("context.xml");
512
513 String content = FileUtil.read(contextPath);
514
515 if (!PropsValues.AUTO_DEPLOY_UNPACK_WAR) {
516 content = StringUtil.replace(
517 content, "antiResourceLocking=\"true\"", StringPool.BLANK);
518 }
519
520 FileUtil.write(targetFile, content);
521 }
522
523 public void copyXmls(
524 File srcFile, String displayName, PluginPackage pluginPackage)
525 throws Exception {
526
527 if (appServerType.equals(ServerDetector.GERONIMO_ID)) {
528 copyDependencyXml("geronimo-web.xml", srcFile + "/WEB-INF");
529 }
530 else if (appServerType.equals(ServerDetector.JBOSS_ID)) {
531 if (ServerDetector.isJBoss5()) {
532 copyDependencyXml("jboss-web.xml", srcFile + "/WEB-INF");
533 }
534 else {
535 copyDependencyXml(
536 "jboss-deployment-structure.xml", srcFile + "/WEB-INF");
537 }
538 }
539 else if (appServerType.equals(ServerDetector.WEBLOGIC_ID)) {
540 copyDependencyXml("weblogic.xml", srcFile + "/WEB-INF");
541 }
542 else if (appServerType.equals(ServerDetector.WEBSPHERE_ID)) {
543 copyDependencyXml("ibm-web-ext.xmi", srcFile + "/WEB-INF");
544 }
545
546 copyDependencyXml("web.xml", srcFile + "/WEB-INF");
547 }
548
549 public void deploy(String context) throws Exception {
550 try {
551 File baseDirFile = new File(baseDir);
552
553 File[] files = baseDirFile.listFiles();
554
555 if (files == null) {
556 return;
557 }
558
559 files = FileUtil.sortFiles(files);
560
561 for (File srcFile : files) {
562 String fileName = srcFile.getName().toLowerCase();
563
564 boolean deploy = false;
565
566 if (fileName.endsWith(".war") || fileName.endsWith(".zip")) {
567 deploy = true;
568
569 if (wars.size() > 0) {
570 if (!wars.contains(srcFile.getName())) {
571 deploy = false;
572 }
573 }
574 else if (Validator.isNotNull(filePattern)) {
575 if (!StringUtil.matchesIgnoreCase(
576 fileName, filePattern)) {
577
578 deploy = false;
579 }
580 }
581 }
582
583 if (deploy) {
584 AutoDeploymentContext autoDeploymentContext =
585 new AutoDeploymentContext();
586
587 autoDeploymentContext.setContext(context);
588 autoDeploymentContext.setFile(srcFile);
589
590 deployFile(autoDeploymentContext);
591 }
592 }
593 }
594 catch (Exception e) {
595 e.printStackTrace();
596 }
597 }
598
599 public void deployDirectory(
600 File srcFile, File mergeDir, File deployDir, String displayName,
601 boolean overwrite, PluginPackage pluginPackage)
602 throws Exception {
603
604 rewriteFiles(srcFile);
605
606 mergeDirectory(mergeDir, srcFile);
607
608 processPluginPackageProperties(srcFile, displayName, pluginPackage);
609
610 copyDtds(srcFile, pluginPackage);
611 copyJars(srcFile, pluginPackage);
612 copyProperties(srcFile, pluginPackage);
613 copyTlds(srcFile, pluginPackage);
614 copyXmls(srcFile, displayName, pluginPackage);
615 copyPortalDependencies(srcFile);
616
617 updateGeronimoWebXml(srcFile, displayName, pluginPackage);
618
619 File webXml = new File(srcFile + "/WEB-INF/web.xml");
620
621 updateWebXml(webXml, srcFile, displayName, pluginPackage);
622
623 File extLibGlobalDir = new File(
624 srcFile.getAbsolutePath() + "/WEB-INF/ext-lib/global");
625
626 if (extLibGlobalDir.exists()) {
627 File globalLibDir = new File(PortalUtil.getGlobalLibDir());
628
629 CopyTask.copyDirectory(
630 extLibGlobalDir, globalLibDir, "*.jar", StringPool.BLANK,
631 overwrite, true);
632 }
633
634 File extLibPortalDir = new File(
635 srcFile.getAbsolutePath() + "/WEB-INF/ext-lib/portal");
636
637 if (extLibPortalDir.exists()) {
638 File portalLibDir = new File(PortalUtil.getPortalLibDir());
639
640 CopyTask.copyDirectory(
641 extLibPortalDir, portalLibDir, "*.jar", StringPool.BLANK,
642 overwrite, true);
643 }
644
645 if ((deployDir == null) || baseDir.equals(destDir)) {
646 return;
647 }
648
649 updateDeployDirectory(srcFile);
650
651 String excludes = StringPool.BLANK;
652
653 if (appServerType.equals(ServerDetector.JBOSS_ID)) {
654 excludes += "**/WEB-INF/lib/log4j.jar,";
655 }
656 else if (appServerType.equals(ServerDetector.TOMCAT_ID)) {
657 String[] libs = FileUtil.listFiles(tomcatLibDir);
658
659 for (String lib : libs) {
660 excludes += "**/WEB-INF/lib/" + lib + ",";
661 }
662
663 File contextXml = new File(srcFile + "/META-INF/context.xml");
664
665 if (contextXml.exists()) {
666 String content = FileUtil.read(contextXml);
667
668 if (content.contains(_PORTAL_CLASS_LOADER)) {
669 excludes += "**/WEB-INF/lib/util-bridges.jar,";
670 excludes += "**/WEB-INF/lib/util-java.jar,";
671 excludes += "**/WEB-INF/lib/util-taglib.jar,";
672 }
673 }
674
675 try {
676
677
678
679 Class.forName("javax.el.ELContext");
680
681 excludes += "**/WEB-INF/lib/el-api.jar,";
682 }
683 catch (ClassNotFoundException cnfe) {
684 }
685 }
686
687
688
689 Properties properties = getPluginPackageProperties(srcFile);
690
691 if (properties != null) {
692 String deployExcludes = properties.getProperty("deploy-excludes");
693
694 if (deployExcludes != null) {
695 excludes += deployExcludes.trim();
696
697 if (!excludes.endsWith(",")) {
698 excludes += ",";
699 }
700 }
701
702 deployExcludes = properties.getProperty(
703 "deploy-excludes-" + appServerType);
704
705 if (deployExcludes != null) {
706 excludes += deployExcludes.trim();
707
708 if (!excludes.endsWith(",")) {
709 excludes += ",";
710 }
711 }
712 }
713
714 if (_log.isDebugEnabled()) {
715 _log.debug("Excludes " + excludes);
716 }
717
718 if (!unpackWar) {
719 File tempDir = new File(
720 SystemProperties.get(SystemProperties.TMP_DIR) +
721 File.separator + Time.getTimestamp());
722
723 excludes += "**/WEB-INF/web.xml";
724
725 WarTask.war(srcFile, tempDir, excludes, webXml);
726
727 if (isJEEDeploymentEnabled()) {
728 File tempWarDir = new File(
729 tempDir.getParent(), deployDir.getName());
730
731 if (tempWarDir.exists()) {
732 tempWarDir.delete();
733 }
734
735 if (!tempDir.renameTo(tempWarDir)) {
736 tempWarDir = tempDir;
737 }
738
739 DeploymentHandler deploymentHandler = getDeploymentHandler();
740
741 deploymentHandler.deploy(tempWarDir, displayName);
742
743 deploymentHandler.releaseDeploymentManager();
744
745 DeleteTask.deleteDirectory(tempWarDir);
746 }
747 else {
748 if (!tempDir.renameTo(deployDir)) {
749 WarTask.war(srcFile, deployDir, excludes, webXml);
750 }
751
752 DeleteTask.deleteDirectory(tempDir);
753 }
754 }
755 else {
756
757
758
759
760
761
762
763 excludes += "**/WEB-INF/web.xml";
764
765 CopyTask.copyDirectory(
766 srcFile, deployDir, StringPool.BLANK, excludes, overwrite,
767 true);
768
769 CopyTask.copyDirectory(
770 srcFile, deployDir, "**/WEB-INF/web.xml", StringPool.BLANK,
771 true, false);
772
773 if (appServerType.equals(ServerDetector.TOMCAT_ID)) {
774
775
776
777
778
779 File deployWebXml = new File(deployDir + "/WEB-INF/web.xml");
780
781 deployWebXml.setLastModified(
782 System.currentTimeMillis() + (Time.SECOND * 6));
783 }
784 }
785
786 if (appServerType.equals(ServerDetector.JETTY_ID)) {
787 DeployUtil.redeployJetty(displayName);
788 }
789 }
790
791 public void deployDirectory(
792 File srcFile, String displayName, boolean override,
793 PluginPackage pluginPackage)
794 throws Exception {
795
796 deployDirectory(
797 srcFile, null, null, displayName, override, pluginPackage);
798 }
799
800 public int deployFile(AutoDeploymentContext autoDeploymentContext)
801 throws Exception {
802
803 File srcFile = autoDeploymentContext.getFile();
804
805 PluginPackage pluginPackage = readPluginPackage(srcFile);
806
807 if (_log.isInfoEnabled()) {
808 _log.info("Deploying " + srcFile.getName());
809 }
810
811 String specifiedContext = autoDeploymentContext.getContext();
812
813 String displayName = specifiedContext;
814 boolean overwrite = false;
815 String preliminaryContext = specifiedContext;
816
817
818
819
820
821
822 if (Validator.isNull(specifiedContext) &&
823 srcFile.getName().startsWith(DEPLOY_TO_PREFIX)) {
824
825 displayName = srcFile.getName().substring(
826 DEPLOY_TO_PREFIX.length(), srcFile.getName().length() - 4);
827
828 overwrite = true;
829 preliminaryContext = displayName;
830 }
831
832 if (preliminaryContext == null) {
833 preliminaryContext = getDisplayName(srcFile);
834 }
835
836 if (pluginPackage != null) {
837 if (!PluginPackageUtil.isCurrentVersionSupported(
838 pluginPackage.getLiferayVersions())) {
839
840 throw new AutoDeployException(
841 srcFile.getName() +
842 " does not support this version of Liferay");
843 }
844
845 if (displayName == null) {
846 displayName = pluginPackage.getRecommendedDeploymentContext();
847 }
848
849 if (Validator.isNull(displayName)) {
850 displayName = getDisplayName(srcFile);
851 }
852
853 pluginPackage.setContext(displayName);
854
855 PluginPackageUtil.updateInstallingPluginPackage(
856 preliminaryContext, pluginPackage);
857 }
858
859 String deployDir = null;
860
861 if (Validator.isNotNull(displayName)) {
862 deployDir = displayName + ".war";
863 }
864 else {
865 deployDir = srcFile.getName();
866 displayName = getDisplayName(srcFile);
867 }
868
869 if (appServerType.equals(ServerDetector.JBOSS_ID)) {
870 deployDir = jbossPrefix + deployDir;
871 }
872 else if (appServerType.equals(ServerDetector.GERONIMO_ID) ||
873 appServerType.equals(ServerDetector.GLASSFISH_ID) ||
874 appServerType.equals(ServerDetector.JETTY_ID) ||
875 appServerType.equals(ServerDetector.JONAS_ID) ||
876 appServerType.equals(ServerDetector.OC4J_ID) ||
877 appServerType.equals(ServerDetector.RESIN_ID) ||
878 appServerType.equals(ServerDetector.TOMCAT_ID) ||
879 appServerType.equals(ServerDetector.WEBLOGIC_ID)) {
880
881 if (unpackWar) {
882 deployDir = deployDir.substring(0, deployDir.length() - 4);
883 }
884 }
885
886 String destDir = this.destDir;
887
888 if (autoDeploymentContext.getDestDir() != null) {
889 destDir = autoDeploymentContext.getDestDir();
890 }
891
892 File deployDirFile = new File(destDir + "/" + deployDir);
893
894 try {
895 PluginPackage previousPluginPackage = readPluginPackage(
896 deployDirFile);
897
898 if ((pluginPackage != null) && (previousPluginPackage != null)) {
899 String name = pluginPackage.getName();
900 String previousVersion = previousPluginPackage.getVersion();
901 String version = pluginPackage.getVersion();
902
903 if (_log.isInfoEnabled()) {
904 _log.info(
905 "Updating " + name + " from version " +
906 previousVersion + " to version " + version);
907 }
908
909 if (pluginPackage.isPreviousVersionThan(
910 previousPluginPackage)) {
911
912 if (_log.isInfoEnabled()) {
913 _log.info(
914 "Not updating " + name + " because version " +
915 previousVersion + " is newer than version " +
916 version);
917 }
918
919 return AutoDeployer.CODE_SKIP_NEWER_VERSION;
920 }
921
922 overwrite = true;
923 }
924
925 File mergeDirFile = new File(
926 srcFile.getParent() + "/merge/" + srcFile.getName());
927
928 if (srcFile.isDirectory()) {
929 deployDirectory(
930 srcFile, mergeDirFile, deployDirFile, displayName,
931 overwrite, pluginPackage);
932 }
933 else {
934 boolean deployed = deployFile(
935 srcFile, mergeDirFile, deployDirFile, displayName,
936 overwrite, pluginPackage);
937
938 if (!deployed) {
939 String context = preliminaryContext;
940
941 if (pluginPackage != null) {
942 context = pluginPackage.getContext();
943 }
944
945 PluginPackageUtil.endPluginPackageInstallation(context);
946 }
947 else {
948 postDeploy(destDir, deployDir);
949 }
950 }
951
952 return AutoDeployer.CODE_DEFAULT;
953 }
954 catch (Exception e) {
955 if (pluginPackage != null) {
956 PluginPackageUtil.endPluginPackageInstallation(
957 pluginPackage.getContext());
958 }
959
960 throw e;
961 }
962 }
963
964 public boolean deployFile(
965 File srcFile, File mergeDir, File deployDir, String displayName,
966 boolean overwrite, PluginPackage pluginPackage)
967 throws Exception {
968
969 boolean undeployOnRedeploy = false;
970
971 try {
972 undeployOnRedeploy = PrefsPropsUtil.getBoolean(
973 PropsKeys.HOT_UNDEPLOY_ON_REDEPLOY,
974 PropsValues.HOT_UNDEPLOY_ON_REDEPLOY);
975 }
976 catch (Exception e) {
977
978
979
980
981
982 }
983
984 if (undeployOnRedeploy) {
985 DeployUtil.undeploy(appServerType, deployDir);
986 }
987
988 if (!overwrite && UpToDateTask.isUpToDate(srcFile, deployDir)) {
989 if (_log.isInfoEnabled()) {
990 _log.info(deployDir + " is already up to date");
991 }
992
993 return false;
994 }
995
996 File tempDir = new File(
997 SystemProperties.get(SystemProperties.TMP_DIR) + File.separator +
998 Time.getTimestamp());
999
1000 ExpandTask.expand(srcFile, tempDir);
1001
1002 deployDirectory(
1003 tempDir, mergeDir, deployDir, displayName, overwrite,
1004 pluginPackage);
1005
1006 DeleteTask.deleteDirectory(tempDir);
1007
1008 return true;
1009 }
1010
1011 public String downloadJar(String jar) throws Exception {
1012 String tmpDir = SystemProperties.get(SystemProperties.TMP_DIR);
1013
1014 File file = new File(
1015 tmpDir + "/liferay/com/liferay/portal/deploy/dependencies/" + jar);
1016
1017 if (!file.exists()) {
1018 synchronized (this) {
1019 String url = PropsUtil.get(
1020 PropsKeys.LIBRARY_DOWNLOAD_URL + jar);
1021
1022 if (_log.isInfoEnabled()) {
1023 _log.info("Downloading library from " + url);
1024 }
1025
1026 byte[] bytes = HttpUtil.URLtoByteArray(url);
1027
1028 FileUtil.write(file, bytes);
1029 }
1030 }
1031
1032 return FileUtil.getAbsolutePath(file);
1033 }
1034
1035 public String fixPortalDependencyJar(String portalJar) {
1036 if (portalJar.equals("antlr.jar")) {
1037 portalJar = "antlr2.jar";
1038 }
1039
1040 return portalJar;
1041 }
1042
1043 public DeploymentHandler getDeploymentHandler() {
1044 String prefix = "auto.deploy." + ServerDetector.getServerId() + ".jee.";
1045
1046 String dmId = PropsUtil.get(prefix + "dm.id");
1047 String dmUser = PropsUtil.get(prefix + "dm.user");
1048 String dmPassword = PropsUtil.get(prefix + "dm.passwd");
1049 String dfClassName = PropsUtil.get(prefix + "df.classname");
1050
1051 return new DeploymentHandler(dmId, dmUser, dmPassword, dfClassName);
1052 }
1053
1054 public String getDisplayName(File srcFile) {
1055 String displayName = srcFile.getName();
1056
1057 if (StringUtil.endsWith(displayName, ".war") ||
1058 StringUtil.endsWith(displayName, ".xml")) {
1059
1060 displayName = displayName.substring(0, displayName.length() - 4);
1061 }
1062
1063 if (appServerType.equals(ServerDetector.JBOSS_ID) &&
1064 Validator.isNotNull(jbossPrefix) &&
1065 displayName.startsWith(jbossPrefix)) {
1066
1067 displayName = displayName.substring(1);
1068 }
1069
1070 return displayName;
1071 }
1072
1073 public String getDynamicResourceServletContent() {
1074 StringBundler sb = new StringBundler();
1075
1076 sb.append("<servlet>");
1077 sb.append("<servlet-name>");
1078 sb.append("Dynamic Resource Servlet");
1079 sb.append("</servlet-name>");
1080 sb.append("<servlet-class>");
1081 sb.append(PortalClassLoaderServlet.class.getName());
1082 sb.append("</servlet-class>");
1083 sb.append("<init-param>");
1084 sb.append("<param-name>");
1085 sb.append("servlet-class");
1086 sb.append("</param-name>");
1087 sb.append("<param-value>");
1088 sb.append(DynamicResourceServlet.class.getName());
1089 sb.append("</param-value>");
1090 sb.append("</init-param>");
1091 sb.append("<load-on-startup>1</load-on-startup>");
1092 sb.append("</servlet>");
1093
1094 for (String allowedPath :
1095 PropsValues.DYNAMIC_RESOURCE_SERVLET_ALLOWED_PATHS) {
1096
1097 sb.append("<servlet-mapping>");
1098 sb.append("<servlet-name>");
1099 sb.append("Dynamic Resource Servlet");
1100 sb.append("</servlet-name>");
1101 sb.append("<url-pattern>");
1102 sb.append(allowedPath);
1103
1104 if (!allowedPath.endsWith(StringPool.SLASH)) {
1105 sb.append(StringPool.SLASH);
1106 }
1107
1108 sb.append(StringPool.STAR);
1109 sb.append("</url-pattern>");
1110 sb.append("</servlet-mapping>");
1111 }
1112
1113 return sb.toString();
1114 }
1115
1116 public String getExtraContent(
1117 double webXmlVersion, File srcFile, String displayName)
1118 throws Exception {
1119
1120 StringBundler sb = new StringBundler();
1121
1122 sb.append("<display-name>");
1123 sb.append(displayName);
1124 sb.append("</display-name>");
1125
1126 if (webXmlVersion < 2.4) {
1127 sb.append("<context-param>");
1128 sb.append("<param-name>liferay-invoker-enabled</param-name>");
1129 sb.append("<param-value>false</param-value>");
1130 sb.append("</context-param>");
1131 }
1132
1133 sb.append("<listener>");
1134 sb.append("<listener-class>");
1135 sb.append(SerializableSessionAttributeListener.class.getName());
1136 sb.append("</listener-class>");
1137 sb.append("</listener>");
1138
1139 sb.append(getDynamicResourceServletContent());
1140
1141 File serverConfigWsdd = new File(
1142 srcFile + "/WEB-INF/server-config.wsdd");
1143
1144 if (serverConfigWsdd.exists()) {
1145 File webXml = new File(srcFile + "/WEB-INF/web.xml");
1146
1147 String content = FileUtil.read(webXml);
1148
1149 if (!content.contains("axis.servicesPath")) {
1150 String remotingContent = FileUtil.read(
1151 DeployUtil.getResourcePath("remoting-web.xml"));
1152
1153 sb.append(remotingContent);
1154 }
1155 }
1156
1157 boolean hasTaglib = false;
1158
1159 if (Validator.isNotNull(auiTaglibDTD) ||
1160 Validator.isNotNull(portletTaglibDTD) ||
1161 Validator.isNotNull(portletExtTaglibDTD) ||
1162 Validator.isNotNull(securityTaglibDTD) ||
1163 Validator.isNotNull(themeTaglibDTD) ||
1164 Validator.isNotNull(uiTaglibDTD) ||
1165 Validator.isNotNull(utilTaglibDTD)) {
1166
1167 hasTaglib = true;
1168 }
1169
1170 if (hasTaglib && (webXmlVersion > 2.3)) {
1171 sb.append("<jsp-config>");
1172 }
1173
1174 if (Validator.isNotNull(auiTaglibDTD)) {
1175 sb.append("<taglib>");
1176 sb.append("<taglib-uri>http:
1177 sb.append("<taglib-location>");
1178 sb.append("/WEB-INF/tld/aui.tld");
1179 sb.append("</taglib-location>");
1180 sb.append("</taglib>");
1181 }
1182
1183 if (Validator.isNotNull(portletTaglibDTD)) {
1184 sb.append("<taglib>");
1185 sb.append(
1186 "<taglib-uri>http:
1187 sb.append("<taglib-location>");
1188 sb.append("/WEB-INF/tld/liferay-portlet.tld");
1189 sb.append("</taglib-location>");
1190 sb.append("</taglib>");
1191 }
1192
1193 if (Validator.isNotNull(portletExtTaglibDTD)) {
1194 sb.append("<taglib>");
1195 sb.append("<taglib-uri>");
1196 sb.append("http:
1197 sb.append("</taglib-uri>");
1198 sb.append("<taglib-location>");
1199 sb.append("/WEB-INF/tld/liferay-portlet-ext.tld");
1200 sb.append("</taglib-location>");
1201 sb.append("</taglib>");
1202 }
1203
1204 if (Validator.isNotNull(securityTaglibDTD)) {
1205 sb.append("<taglib>");
1206 sb.append("<taglib-uri>");
1207 sb.append("http:
1208 sb.append("</taglib-uri>");
1209 sb.append("<taglib-location>");
1210 sb.append("/WEB-INF/tld/liferay-security.tld");
1211 sb.append("</taglib-location>");
1212 sb.append("</taglib>");
1213 }
1214
1215 if (Validator.isNotNull(themeTaglibDTD)) {
1216 sb.append("<taglib>");
1217 sb.append("<taglib-uri>http:
1218 sb.append("<taglib-location>");
1219 sb.append("/WEB-INF/tld/liferay-theme.tld");
1220 sb.append("</taglib-location>");
1221 sb.append("</taglib>");
1222 }
1223
1224 if (Validator.isNotNull(uiTaglibDTD)) {
1225 sb.append("<taglib>");
1226 sb.append("<taglib-uri>http:
1227 sb.append("<taglib-location>");
1228 sb.append("/WEB-INF/tld/liferay-ui.tld");
1229 sb.append("</taglib-location>");
1230 sb.append("</taglib>");
1231 }
1232
1233 if (Validator.isNotNull(utilTaglibDTD)) {
1234 sb.append("<taglib>");
1235 sb.append("<taglib-uri>http:
1236 sb.append("<taglib-location>");
1237 sb.append("/WEB-INF/tld/liferay-util.tld");
1238 sb.append("</taglib-location>");
1239 sb.append("</taglib>");
1240 }
1241
1242 if (hasTaglib && (webXmlVersion > 2.3)) {
1243 sb.append("</jsp-config>");
1244 }
1245
1246 return sb.toString();
1247 }
1248
1249 public String getExtraFiltersContent(double webXmlVersion, File srcFile)
1250 throws Exception {
1251
1252 return getSessionFiltersContent();
1253 }
1254
1255 public String getIgnoreFiltersContent(File srcFile) throws Exception {
1256 boolean ignoreFiltersEnabled = true;
1257
1258 Properties properties = getPluginPackageProperties(srcFile);
1259
1260 if (properties != null) {
1261 ignoreFiltersEnabled = GetterUtil.getBoolean(
1262 properties.getProperty("ignore-filters-enabled"), true);
1263 }
1264
1265 if (ignoreFiltersEnabled) {
1266 String ignoreFiltersContent = FileUtil.read(
1267 DeployUtil.getResourcePath("ignore-filters-web.xml"));
1268
1269 return ignoreFiltersContent;
1270 }
1271 else {
1272 return StringPool.BLANK;
1273 }
1274 }
1275
1276 public String getInvokerFilterContent() {
1277 StringBundler sb = new StringBundler(4);
1278
1279 sb.append(getInvokerFilterContent("ERROR"));
1280 sb.append(getInvokerFilterContent("FORWARD"));
1281 sb.append(getInvokerFilterContent("INCLUDE"));
1282 sb.append(getInvokerFilterContent("REQUEST"));
1283
1284 return sb.toString();
1285 }
1286
1287 public String getInvokerFilterContent(String dispatcher) {
1288 StringBundler sb = new StringBundler(23);
1289
1290 sb.append("<filter>");
1291 sb.append("<filter-name>Invoker Filter - ");
1292 sb.append(dispatcher);
1293 sb.append("</filter-name>");
1294 sb.append("<filter-class>");
1295 sb.append(InvokerFilter.class.getName());
1296 sb.append("</filter-class>");
1297 sb.append("<init-param>");
1298 sb.append("<param-name>dispatcher</param-name>");
1299 sb.append("<param-value>");
1300 sb.append(dispatcher);
1301 sb.append("</param-value>");
1302 sb.append("</init-param>");
1303 sb.append("</filter>");
1304
1305 sb.append("<filter-mapping>");
1306 sb.append("<filter-name>Invoker Filter - ");
1307 sb.append(dispatcher);
1308 sb.append("</filter-name>");
1309 sb.append("<url-pattern>