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