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