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