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