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