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.UnsecureSAXReaderUtil;
054 import com.liferay.portal.plugin.PluginPackageUtil;
055 import com.liferay.portal.security.lang.SecurityManagerUtil;
056 import com.liferay.portal.tools.ToolDependencies;
057 import com.liferay.portal.tools.WebXMLBuilder;
058 import com.liferay.portal.util.ExtRegistry;
059 import com.liferay.portal.util.PortalUtil;
060 import com.liferay.portal.util.PrefsPropsUtil;
061 import com.liferay.portal.util.PropsUtil;
062 import com.liferay.portal.util.PropsValues;
063 import com.liferay.portal.webserver.DynamicResourceServlet;
064 import com.liferay.util.ant.CopyTask;
065 import com.liferay.util.ant.DeleteTask;
066 import com.liferay.util.ant.ExpandTask;
067 import com.liferay.util.ant.UpToDateTask;
068 import com.liferay.util.ant.WarTask;
069 import com.liferay.util.xml.DocUtil;
070 import com.liferay.util.xml.XMLFormatter;
071
072 import java.io.File;
073 import java.io.FileInputStream;
074 import java.io.IOException;
075 import java.io.InputStream;
076
077 import java.util.ArrayList;
078 import java.util.HashMap;
079 import java.util.List;
080 import java.util.Map;
081 import java.util.Properties;
082 import java.util.Set;
083 import java.util.zip.ZipEntry;
084 import java.util.zip.ZipFile;
085
086 import org.apache.oro.io.GlobFilenameFilter;
087
088
092 public class BaseDeployer implements AutoDeployer, Deployer {
093
094 public static final String DEPLOY_TO_PREFIX = "DEPLOY_TO__";
095
096 public static void main(String[] args) {
097 ToolDependencies.wireDeployers();
098
099 List<String> wars = new ArrayList<>();
100 List<String> jars = new ArrayList<>();
101
102 for (String arg : args) {
103 String fileName = StringUtil.toLowerCase(arg);
104
105 if (fileName.endsWith(".war")) {
106 wars.add(arg);
107 }
108 else if (fileName.endsWith(".jar")) {
109 jars.add(arg);
110 }
111 }
112
113 new BaseDeployer(wars, jars);
114 }
115
116 public BaseDeployer() {
117 }
118
119 public BaseDeployer(List<String> wars, List<String> jars) {
120 baseDir = System.getProperty("deployer.base.dir");
121 destDir = System.getProperty("deployer.dest.dir");
122 appServerType = System.getProperty("deployer.app.server.type");
123 auiTaglibDTD = System.getProperty("deployer.aui.taglib.dtd");
124 portletTaglibDTD = System.getProperty("deployer.portlet.taglib.dtd");
125 portletExtTaglibDTD = System.getProperty(
126 "deployer.portlet.ext.taglib.dtd");
127 securityTaglibDTD = System.getProperty("deployer.security.taglib.dtd");
128 themeTaglibDTD = System.getProperty("deployer.theme.taglib.dtd");
129 uiTaglibDTD = System.getProperty("deployer.ui.taglib.dtd");
130 utilTaglibDTD = System.getProperty("deployer.util.taglib.dtd");
131 unpackWar = GetterUtil.getBoolean(
132 System.getProperty("deployer.unpack.war"), true);
133 filePattern = System.getProperty("deployer.file.pattern");
134 jbossPrefix = GetterUtil.getString(
135 System.getProperty("deployer.jboss.prefix"));
136 tomcatLibDir = System.getProperty("deployer.tomcat.lib.dir");
137 this.wars = wars;
138 this.jars = jars;
139
140 checkArguments();
141
142 String context = System.getProperty("deployer.context");
143
144 try {
145 deploy(context);
146 }
147 catch (Exception e) {
148 _log.error(e, e);
149 }
150 }
151
152 @Override
153 public void addExtJar(List<String> jars, String resource) throws Exception {
154 Set<String> servletContextNames = ExtRegistry.getServletContextNames();
155
156 for (String servletContextName : servletContextNames) {
157 String extResource =
158 "ext-" + servletContextName + resource.substring(3);
159
160 String path = DeployUtil.getResourcePath(extResource);
161
162 if (_log.isDebugEnabled()) {
163 if (path == null) {
164 _log.debug("Resource " + extResource + " is not available");
165 }
166 else {
167 _log.debug(
168 "Resource " + extResource + " is available at " + path);
169 }
170 }
171
172 if (path != null) {
173 jars.add(path);
174 }
175 }
176 }
177
178 @Override
179 public void addRequiredJar(List<String> jars, String resource)
180 throws Exception {
181
182 String path = DeployUtil.getResourcePath(resource);
183
184 if (path == null) {
185 throw new RuntimeException(
186 "Resource " + resource + " does not exist");
187 }
188
189 if (_log.isDebugEnabled()) {
190 _log.debug("Resource " + resource + " is available at " + path);
191 }
192
193 jars.add(path);
194 }
195
196 @Override
197 public int autoDeploy(AutoDeploymentContext autoDeploymentContext)
198 throws AutoDeployException {
199
200 List<String> wars = new ArrayList<>();
201
202 File file = autoDeploymentContext.getFile();
203
204 wars.add(file.getName());
205
206 this.wars = wars;
207
208 try {
209 return deployFile(autoDeploymentContext);
210 }
211 catch (Exception e) {
212 throw new AutoDeployException(e);
213 }
214 }
215
216 @Override
217 public void checkArguments() {
218 if (Validator.isNull(baseDir)) {
219 throw new IllegalArgumentException(
220 "The system property deployer.base.dir is not set");
221 }
222
223 if (Validator.isNull(destDir)) {
224 throw new IllegalArgumentException(
225 "The system property deployer.dest.dir is not set");
226 }
227
228 if (Validator.isNull(appServerType)) {
229 throw new IllegalArgumentException(
230 "The system property deployer.app.server.type is not set");
231 }
232
233 if (!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 if (!FileUtil.exists(jarFullName)) {
358 DeployUtil.getResourcePath(jarName);
359 }
360
361 FileUtil.copyFile(
362 jarFullName, srcFile + "/WEB-INF/lib/" + jarName, false);
363 }
364 }
365
366 public void copyPortalDependencies(File srcFile) throws Exception {
367 Properties properties = getPluginPackageProperties(srcFile);
368
369 if (properties == null) {
370 return;
371 }
372
373
374
375 String[] portalJars = StringUtil.split(
376 properties.getProperty(
377 "portal-dependency-jars",
378 properties.getProperty("portal.dependency.jars")));
379
380 for (String portalJar : portalJars) {
381 portalJar = portalJar.trim();
382
383 portalJar = fixPortalDependencyJar(portalJar);
384
385 if (_log.isDebugEnabled()) {
386 _log.debug("Copy portal JAR " + portalJar);
387 }
388
389 try {
390 String portalJarPath = PortalUtil.getPortalLibDir() + portalJar;
391
392 FileUtil.copyFile(
393 portalJarPath, srcFile + "/WEB-INF/lib/" + portalJar, true);
394 }
395 catch (Exception e) {
396 _log.error("Unable to copy portal JAR " + portalJar, e);
397 }
398 }
399
400
401
402 String[] portalTlds = StringUtil.split(
403 properties.getProperty(
404 "portal-dependency-tlds",
405 properties.getProperty("portal.dependency.tlds")));
406
407 for (String portalTld : portalTlds) {
408 portalTld = portalTld.trim();
409
410 if (_log.isDebugEnabled()) {
411 _log.debug("Copy portal TLD " + portalTld);
412 }
413
414 try {
415 String portalTldPath = DeployUtil.getResourcePath(portalTld);
416
417 FileUtil.copyFile(
418 portalTldPath, srcFile + "/WEB-INF/tld/" + portalTld, true);
419 }
420 catch (Exception e) {
421 _log.error("Unable to copy portal TLD " + portalTld, e);
422 }
423 }
424
425
426
427 File pluginLibDir = new File(srcFile + "/WEB-INF/lib/");
428
429 if (PropsValues.AUTO_DEPLOY_COPY_COMMONS_LOGGING) {
430 String[] commonsLoggingJars = pluginLibDir.list(
431 new GlobFilenameFilter("commons-logging*.jar"));
432
433 if (ArrayUtil.isEmpty(commonsLoggingJars)) {
434 String portalJarPath =
435 PortalUtil.getPortalLibDir() + "commons-logging.jar";
436
437 FileUtil.copyFile(
438 portalJarPath, srcFile + "/WEB-INF/lib/commons-logging.jar",
439 true);
440 }
441 }
442
443
444
445 if (PropsValues.AUTO_DEPLOY_COPY_LOG4J) {
446 String[] log4jJars = pluginLibDir.list(
447 new GlobFilenameFilter("log4j*.jar"));
448
449 if (ArrayUtil.isEmpty(log4jJars)) {
450 String portalJarPath =
451 PortalUtil.getPortalLibDir() + "log4j.jar";
452
453 FileUtil.copyFile(
454 portalJarPath, srcFile + "/WEB-INF/lib/log4j.jar", true);
455
456 portalJarPath =
457 PortalUtil.getPortalLibDir() + "log4j-extras.jar";
458
459 FileUtil.copyFile(
460 portalJarPath, srcFile + "/WEB-INF/lib/log4j-extras.jar",
461 true);
462 }
463 }
464 }
465
466 @Override
467 public void copyProperties(File srcFile, PluginPackage pluginPackage)
468 throws Exception {
469
470 if (PropsValues.AUTO_DEPLOY_COPY_COMMONS_LOGGING) {
471 copyDependencyXml(
472 "logging.properties", srcFile + "/WEB-INF/classes");
473 }
474
475 if (PropsValues.AUTO_DEPLOY_COPY_LOG4J) {
476 copyDependencyXml("log4j.properties", srcFile + "/WEB-INF/classes");
477 }
478
479 File servicePropertiesFile = new File(
480 srcFile.getAbsolutePath() + "/WEB-INF/classes/service.properties");
481
482 if (!servicePropertiesFile.exists()) {
483 return;
484 }
485
486 File portletPropertiesFile = new File(
487 srcFile.getAbsolutePath() + "/WEB-INF/classes/portlet.properties");
488
489 if (portletPropertiesFile.exists()) {
490 return;
491 }
492
493 String pluginPackageName = null;
494
495 if (pluginPackage != null) {
496 pluginPackageName = pluginPackage.getName();
497 }
498 else {
499 pluginPackageName = srcFile.getName();
500 }
501
502 FileUtil.write(
503 portletPropertiesFile, "plugin.package.name=" + pluginPackageName);
504 }
505
506 @Override
507 public void copyTlds(File srcFile, PluginPackage pluginPackage)
508 throws Exception {
509
510 if (Validator.isNotNull(auiTaglibDTD)) {
511 FileUtil.copyFile(
512 auiTaglibDTD, srcFile + "/WEB-INF/tld/aui.tld", true);
513 }
514
515 if (Validator.isNotNull(portletTaglibDTD)) {
516 FileUtil.copyFile(
517 portletTaglibDTD, srcFile + "/WEB-INF/tld/liferay-portlet.tld",
518 true);
519 }
520
521 if (Validator.isNotNull(portletExtTaglibDTD)) {
522 FileUtil.copyFile(
523 portletExtTaglibDTD,
524 srcFile + "/WEB-INF/tld/liferay-portlet-ext.tld", true);
525 }
526
527 if (Validator.isNotNull(securityTaglibDTD)) {
528 FileUtil.copyFile(
529 securityTaglibDTD,
530 srcFile + "/WEB-INF/tld/liferay-security.tld", true);
531 }
532
533 if (Validator.isNotNull(themeTaglibDTD)) {
534 FileUtil.copyFile(
535 themeTaglibDTD, srcFile + "/WEB-INF/tld/liferay-theme.tld",
536 true);
537 }
538
539 if (Validator.isNotNull(uiTaglibDTD)) {
540 FileUtil.copyFile(
541 uiTaglibDTD, srcFile + "/WEB-INF/tld/liferay-ui.tld", true);
542 }
543
544 if (Validator.isNotNull(utilTaglibDTD)) {
545 FileUtil.copyFile(
546 utilTaglibDTD, srcFile + "/WEB-INF/tld/liferay-util.tld", true);
547 }
548 }
549
550 public void copyTomcatContextXml(File targetDir) throws Exception {
551 if (!appServerType.equals(ServerDetector.TOMCAT_ID) ||
552 SecurityManagerUtil.ENABLED) {
553
554 return;
555 }
556
557 File targetFile = new File(targetDir, "META-INF/context.xml");
558
559 if (targetFile.exists()) {
560 return;
561 }
562
563 String contextPath = DeployUtil.getResourcePath("context.xml");
564
565 String content = FileUtil.read(contextPath);
566
567 if (!PropsValues.AUTO_DEPLOY_UNPACK_WAR) {
568 content = StringUtil.replace(
569 content, "antiResourceLocking=\"true\"", StringPool.BLANK);
570 }
571
572 FileUtil.write(targetFile, content);
573 }
574
575 @Override
576 public void copyXmls(
577 File srcFile, String displayName, PluginPackage pluginPackage)
578 throws Exception {
579
580 if (appServerType.equals(ServerDetector.JBOSS_ID)) {
581 if (ServerDetector.isJBoss5()) {
582 copyDependencyXml("jboss-web.xml", srcFile + "/WEB-INF");
583 }
584 else {
585 copyDependencyXml(
586 "jboss-deployment-structure.xml", srcFile + "/WEB-INF");
587 }
588 }
589 else if (appServerType.equals(ServerDetector.WEBLOGIC_ID)) {
590 copyDependencyXml("weblogic.xml", srcFile + "/WEB-INF");
591 }
592 else if (appServerType.equals(ServerDetector.WEBSPHERE_ID)) {
593 copyDependencyXml("ibm-web-ext.xmi", srcFile + "/WEB-INF");
594 }
595
596 copyDependencyXml("web.xml", srcFile + "/WEB-INF");
597 }
598
599 public void deploy(String context) throws Exception {
600 try {
601 File baseDirFile = new File(baseDir);
602
603 File[] files = baseDirFile.listFiles();
604
605 if (files == null) {
606 return;
607 }
608
609 files = FileUtil.sortFiles(files);
610
611 for (File srcFile : files) {
612 String fileName = StringUtil.toLowerCase(srcFile.getName());
613
614 boolean deploy = false;
615
616 if (fileName.endsWith(".war") || fileName.endsWith(".zip")) {
617 deploy = true;
618
619 if (!wars.isEmpty()) {
620 if (!wars.contains(srcFile.getName())) {
621 deploy = false;
622 }
623 }
624 else if (Validator.isNotNull(filePattern)) {
625 if (!StringUtil.matchesIgnoreCase(
626 fileName, filePattern)) {
627
628 deploy = false;
629 }
630 }
631 }
632
633 if (deploy) {
634 AutoDeploymentContext autoDeploymentContext =
635 new AutoDeploymentContext();
636
637 autoDeploymentContext.setContext(context);
638 autoDeploymentContext.setFile(srcFile);
639
640 deployFile(autoDeploymentContext);
641 }
642 }
643 }
644 catch (Exception e) {
645 _log.error(e, e);
646 }
647 }
648
649 public void deployDirectory(
650 File srcFile, File mergeDir, File deployDir, String displayName,
651 boolean overwrite, PluginPackage pluginPackage)
652 throws Exception {
653
654 rewriteFiles(srcFile);
655
656 mergeDirectory(mergeDir, srcFile);
657
658 processPluginPackageProperties(srcFile, displayName, pluginPackage);
659
660 copyDtds(srcFile, pluginPackage);
661 copyJars(srcFile, pluginPackage);
662 copyProperties(srcFile, pluginPackage);
663 copyTlds(srcFile, pluginPackage);
664 copyXmls(srcFile, displayName, pluginPackage);
665 copyPortalDependencies(srcFile);
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 if (tempDir.isDirectory()) {
801 DeleteTask.deleteDirectory(tempDir);
802 }
803 else {
804 tempDir.delete();
805 }
806 }
807 }
808 else {
809
810
811
812
813
814
815
816 excludes += "**/WEB-INF/web.xml";
817
818 CopyTask.copyDirectory(
819 srcFile, deployDir, StringPool.BLANK, excludes, overwrite,
820 true);
821
822 CopyTask.copyDirectory(
823 srcFile, deployDir, "**/WEB-INF/web.xml", StringPool.BLANK,
824 true, false);
825
826 if (appServerType.equals(ServerDetector.TOMCAT_ID)) {
827
828
829
830
831
832 File deployWebXml = new File(deployDir + "/WEB-INF/web.xml");
833
834 deployWebXml.setLastModified(
835 System.currentTimeMillis() + (Time.SECOND * 6));
836 }
837 }
838
839 if (appServerType.equals(ServerDetector.JETTY_ID)) {
840 DeployUtil.redeployJetty(displayName);
841 }
842 }
843
844 public void deployDirectory(
845 File srcFile, String displayName, boolean override,
846 PluginPackage pluginPackage)
847 throws Exception {
848
849 deployDirectory(
850 srcFile, null, null, displayName, override, pluginPackage);
851 }
852
853 public int deployFile(AutoDeploymentContext autoDeploymentContext)
854 throws Exception {
855
856 File srcFile = autoDeploymentContext.getFile();
857
858 PluginPackage pluginPackage = autoDeploymentContext.getPluginPackage();
859
860 if (pluginPackage == null) {
861 pluginPackage = readPluginPackage(srcFile);
862
863 autoDeploymentContext.setPluginPackage(pluginPackage);
864 }
865
866 if (_log.isInfoEnabled()) {
867 _log.info("Deploying " + srcFile.getName());
868 }
869
870 String autoDeploymentContextAppServerType =
871 autoDeploymentContext.getAppServerType();
872
873 if (Validator.isNotNull(autoDeploymentContextAppServerType)) {
874 appServerType = autoDeploymentContextAppServerType;
875 }
876
877 String specifiedContext = autoDeploymentContext.getContext();
878
879 String displayName = specifiedContext;
880 boolean overwrite = false;
881 String preliminaryContext = specifiedContext;
882
883
884
885
886
887
888 if (Validator.isNull(specifiedContext) &&
889 srcFile.getName().startsWith(DEPLOY_TO_PREFIX)) {
890
891 displayName = srcFile.getName().substring(
892 DEPLOY_TO_PREFIX.length(), srcFile.getName().length() - 4);
893
894 overwrite = true;
895 preliminaryContext = displayName;
896 }
897
898 if (preliminaryContext == null) {
899 preliminaryContext = getDisplayName(srcFile);
900 }
901
902 if (pluginPackage != null) {
903 if (!PluginPackageUtil.isCurrentVersionSupported(
904 pluginPackage.getLiferayVersions())) {
905
906 throw new AutoDeployException(
907 srcFile.getName() +
908 " does not support this version of Liferay");
909 }
910
911 if (displayName == null) {
912 displayName = pluginPackage.getRecommendedDeploymentContext();
913 }
914
915 if (Validator.isNull(displayName)) {
916 displayName = getDisplayName(srcFile);
917 }
918
919 pluginPackage.setContext(displayName);
920
921 PluginPackageUtil.updateInstallingPluginPackage(
922 preliminaryContext, pluginPackage);
923 }
924
925 String deployDir = null;
926
927 if (Validator.isNotNull(displayName)) {
928 deployDir = displayName + ".war";
929 }
930 else {
931 deployDir = srcFile.getName();
932 displayName = getDisplayName(srcFile);
933 }
934
935 if (appServerType.equals(ServerDetector.JBOSS_ID)) {
936 deployDir = jbossPrefix + deployDir;
937 }
938 else if (appServerType.equals(ServerDetector.GLASSFISH_ID) ||
939 appServerType.equals(ServerDetector.JETTY_ID) ||
940 appServerType.equals(ServerDetector.JONAS_ID) ||
941 appServerType.equals(ServerDetector.OC4J_ID) ||
942 appServerType.equals(ServerDetector.RESIN_ID) ||
943 appServerType.equals(ServerDetector.TOMCAT_ID) ||
944 appServerType.equals(ServerDetector.WEBLOGIC_ID)) {
945
946 if (unpackWar) {
947 deployDir = deployDir.substring(0, deployDir.length() - 4);
948 }
949 }
950
951 String destDir = this.destDir;
952
953 if (autoDeploymentContext.getDestDir() != null) {
954 destDir = autoDeploymentContext.getDestDir();
955 }
956
957 File deployDirFile = new File(destDir + "/" + deployDir);
958
959 try {
960 PluginPackage previousPluginPackage = readPluginPackage(
961 deployDirFile);
962
963 if ((pluginPackage != null) && (previousPluginPackage != null)) {
964 String name = pluginPackage.getName();
965 String previousVersion = previousPluginPackage.getVersion();
966 String version = pluginPackage.getVersion();
967
968 if (_log.isInfoEnabled()) {
969 _log.info(
970 "Updating " + name + " from version " +
971 previousVersion + " to version " + version);
972 }
973
974 if (pluginPackage.isPreviousVersionThan(
975 previousPluginPackage)) {
976
977 if (_log.isInfoEnabled()) {
978 _log.info(
979 "Not updating " + name + " because version " +
980 previousVersion + " is newer than version " +
981 version);
982 }
983
984 return AutoDeployer.CODE_SKIP_NEWER_VERSION;
985 }
986
987 overwrite = true;
988 }
989
990 File mergeDirFile = new File(
991 srcFile.getParent() + "/merge/" + srcFile.getName());
992
993 if (srcFile.isDirectory()) {
994 deployDirectory(
995 srcFile, mergeDirFile, deployDirFile, displayName,
996 overwrite, pluginPackage);
997 }
998 else {
999 boolean deployed = deployFile(
1000 srcFile, mergeDirFile, deployDirFile, displayName,
1001 overwrite, pluginPackage);
1002
1003 if (!deployed) {
1004 String context = preliminaryContext;
1005
1006 if (pluginPackage != null) {
1007 context = pluginPackage.getContext();
1008 }
1009
1010 PluginPackageUtil.endPluginPackageInstallation(context);
1011 }
1012 else {
1013 postDeploy(destDir, deployDir);
1014 }
1015 }
1016
1017 return AutoDeployer.CODE_DEFAULT;
1018 }
1019 catch (Exception e) {
1020 if (pluginPackage != null) {
1021 PluginPackageUtil.endPluginPackageInstallation(
1022 pluginPackage.getContext());
1023 }
1024
1025 throw e;
1026 }
1027 }
1028
1029 public boolean deployFile(
1030 File srcFile, File mergeDir, File deployDir, String displayName,
1031 boolean overwrite, PluginPackage pluginPackage)
1032 throws Exception {
1033
1034 boolean undeployOnRedeploy = false;
1035
1036 try {
1037 undeployOnRedeploy = PrefsPropsUtil.getBoolean(
1038 PropsKeys.HOT_UNDEPLOY_ON_REDEPLOY,
1039 PropsValues.HOT_UNDEPLOY_ON_REDEPLOY);
1040 }
1041 catch (Exception e) {
1042
1043
1044
1045
1046
1047 }
1048
1049 if (undeployOnRedeploy) {
1050 DeployUtil.undeploy(appServerType, deployDir);
1051 }
1052
1053 if (!overwrite && UpToDateTask.isUpToDate(srcFile, deployDir)) {
1054 if (_log.isInfoEnabled()) {
1055 _log.info(deployDir + " is already up to date");
1056 }
1057
1058 return false;
1059 }
1060
1061 File tempDir = new File(
1062 SystemProperties.get(SystemProperties.TMP_DIR) + File.separator +
1063 Time.getTimestamp());
1064
1065 ExpandTask.expand(srcFile, tempDir);
1066
1067 deployDirectory(
1068 tempDir, mergeDir, deployDir, displayName, overwrite,
1069 pluginPackage);
1070
1071 DeleteTask.deleteDirectory(tempDir);
1072
1073 return true;
1074 }
1075
1076 public String downloadJar(String jar) throws Exception {
1077 String tmpDir = SystemProperties.get(SystemProperties.TMP_DIR);
1078
1079 File file = new File(
1080 tmpDir + "/liferay/com/liferay/portal/deploy/dependencies/" + jar);
1081
1082 if (!file.exists()) {
1083 synchronized (this) {
1084 String url = PropsUtil.get(
1085 PropsKeys.LIBRARY_DOWNLOAD_URL + jar);
1086
1087 if (_log.isInfoEnabled()) {
1088 _log.info("Downloading library from " + url);
1089 }
1090
1091 byte[] bytes = HttpUtil.URLtoByteArray(url);
1092
1093 FileUtil.write(file, bytes);
1094 }
1095 }
1096
1097 return FileUtil.getAbsolutePath(file);
1098 }
1099
1100 public String fixPortalDependencyJar(String portalJar) {
1101 if (portalJar.equals("antlr.jar")) {
1102 portalJar = "antlr2.jar";
1103 }
1104
1105 return portalJar;
1106 }
1107
1108 public DeploymentHandler getDeploymentHandler() {
1109 String prefix = "auto.deploy." + ServerDetector.getServerId() + ".jee.";
1110
1111 String dmId = PropsUtil.get(prefix + "dm.id");
1112 String dmUser = PropsUtil.get(prefix + "dm.user");
1113 String dmPassword = PropsUtil.get(prefix + "dm.passwd");
1114 String dfClassName = PropsUtil.get(prefix + "df.classname");
1115
1116 return new DeploymentHandler(dmId, dmUser, dmPassword, dfClassName);
1117 }
1118
1119 public String getDisplayName(File srcFile) {
1120 String displayName = srcFile.getName();
1121
1122 if (StringUtil.endsWith(displayName, ".war") ||
1123 StringUtil.endsWith(displayName, ".xml")) {
1124
1125 displayName = displayName.substring(0, displayName.length() - 4);
1126 }
1127
1128 if (appServerType.equals(ServerDetector.JBOSS_ID) &&
1129 Validator.isNotNull(jbossPrefix) &&
1130 displayName.startsWith(jbossPrefix)) {
1131
1132 displayName = displayName.substring(1);
1133 }
1134
1135 return displayName;
1136 }
1137
1138 public String getDynamicResourceServletContent() {
1139 StringBundler sb = new StringBundler();
1140
1141 sb.append("<servlet>");
1142 sb.append("<servlet-name>");
1143 sb.append("Dynamic Resource Servlet");
1144 sb.append("</servlet-name>");
1145 sb.append("<servlet-class>");
1146 sb.append(PortalClassLoaderServlet.class.getName());
1147 sb.append("</servlet-class>");
1148 sb.append("<init-param>");
1149 sb.append("<param-name>");
1150 sb.append("servlet-class");
1151 sb.append("</param-name>");
1152 sb.append("<param-value>");
1153 sb.append(DynamicResourceServlet.class.getName());
1154 sb.append("</param-value>");
1155 sb.append("</init-param>");
1156 sb.append("<load-on-startup>1</load-on-startup>");
1157 sb.append("</servlet>");
1158
1159 for (String allowedPath :
1160 PropsValues.DYNAMIC_RESOURCE_SERVLET_ALLOWED_PATHS) {
1161
1162 sb.append("<servlet-mapping>");
1163 sb.append("<servlet-name>");
1164 sb.append("Dynamic Resource Servlet");
1165 sb.append("</servlet-name>");
1166 sb.append("<url-pattern>");
1167 sb.append(allowedPath);
1168
1169 if (!allowedPath.endsWith(StringPool.SLASH)) {
1170 sb.append(StringPool.SLASH);
1171 }
1172
1173 sb.append(StringPool.STAR);
1174 sb.append("</url-pattern>");
1175 sb.append("</servlet-mapping>");
1176 }
1177
1178 return sb.toString();
1179 }
1180
1181 public String getExtraContent(
1182 double webXmlVersion, File srcFile, String displayName)
1183 throws Exception {
1184
1185 StringBundler sb = new StringBundler();
1186
1187 sb.append("<display-name>");
1188 sb.append(displayName);
1189 sb.append("</display-name>");
1190
1191 if (webXmlVersion < 2.4) {
1192 sb.append("<context-param>");
1193 sb.append("<param-name>liferay-invoker-enabled</param-name>");
1194 sb.append("<param-value>false</param-value>");
1195 sb.append("</context-param>");
1196 }
1197
1198 if (PropsValues.SESSION_VERIFY_SERIALIZABLE_ATTRIBUTE) {
1199 sb.append("<listener>");
1200 sb.append("<listener-class>");
1201 sb.append(SerializableSessionAttributeListener.class.getName());
1202 sb.append("</listener-class>");
1203 sb.append("</listener>");
1204 }
1205
1206 sb.append(getDynamicResourceServletContent());
1207
1208 File serverConfigWsdd = new File(
1209 srcFile + "/WEB-INF/server-config.wsdd");
1210
1211 if (serverConfigWsdd.exists()) {
1212 File webXml = new File(srcFile + "/WEB-INF/web.xml");
1213
1214 String content = FileUtil.read(webXml);
1215
1216 if (!content.contains("axis.servicesPath")) {
1217 String remotingContent = FileUtil.read(
1218 DeployUtil.getResourcePath("remoting-web.xml"));
1219
1220 sb.append(remotingContent);
1221 }
1222 }
1223
1224 boolean hasTaglib = false;
1225
1226 if (Validator.isNotNull(auiTaglibDTD) ||
1227 Validator.isNotNull(portletTaglibDTD) ||
1228 Validator.isNotNull(portletExtTaglibDTD) ||
1229 Validator.isNotNull(securityTaglibDTD) ||
1230 Validator.isNotNull(themeTaglibDTD) ||
1231 Validator.isNotNull(uiTaglibDTD) ||
1232 Validator.isNotNull(utilTaglibDTD)) {
1233
1234 hasTaglib = true;
1235 }
1236
1237 if (hasTaglib && (webXmlVersion > 2.3)) {
1238 sb.append("<jsp-config>");
1239 }
1240
1241 if (Validator.isNotNull(auiTaglibDTD)) {
1242 sb.append("<taglib>");
1243 sb.append("<taglib-uri>http:
1244 sb.append("<taglib-location>");
1245 sb.append("/WEB-INF/tld/aui.tld");
1246 sb.append("</taglib-location>");
1247 sb.append("</taglib>");
1248 }
1249
1250 if (Validator.isNotNull(portletTaglibDTD)) {
1251 sb.append("<taglib>");
1252 sb.append(
1253 "<taglib-uri>http:
1254 sb.append("<taglib-location>");
1255 sb.append("/WEB-INF/tld/liferay-portlet.tld");
1256 sb.append("</taglib-location>");
1257 sb.append("</taglib>");
1258 }
1259
1260 if (Validator.isNotNull(portletExtTaglibDTD)) {
1261 sb.append("<taglib>");
1262 sb.append("<taglib-uri>");
1263 sb.append("http:
1264 sb.append("</taglib-uri>");
1265 sb.append("<taglib-location>");
1266 sb.append("/WEB-INF/tld/liferay-portlet-ext.tld");
1267 sb.append("</taglib-location>");
1268 sb.append("</taglib>");
1269 }
1270
1271 if (Validator.isNotNull(securityTaglibDTD)) {
1272 sb.append("<taglib>");
1273 sb.append("<taglib-uri>");
1274 sb.append("http:
1275 sb.append("</taglib-uri>");
1276 sb.append("<taglib-location>");
1277 sb.append("/WEB-INF/tld/liferay-security.tld");
1278 sb.append("</taglib-location>");
1279 sb.append("</taglib>");
1280 }
1281
1282 if (Validator.isNotNull(themeTaglibDTD)) {
1283 sb.append("<taglib>");
1284 sb.append("<taglib-uri>http:
1285 sb.append("<taglib-location>");
1286 sb.append("/WEB-INF/tld/liferay-theme.tld");
1287 sb.append("</taglib-location>");
1288 sb.append("</taglib>");
1289 }
1290
1291 if (Validator.isNotNull(uiTaglibDTD)) {
1292 sb.append("<taglib>");
1293 sb.append("<taglib-uri>http:
1294 sb.append("<taglib-location>");
1295 sb.append("/WEB-INF/tld/liferay-ui.tld");
1296 sb.append("</taglib-location>");
1297 sb.append("</taglib>");
1298 }
1299
1300 if (Validator.isNotNull(utilTaglibDTD)) {
1301 sb.append("<taglib>");
1302 sb.append("<taglib-uri>http:
1303 sb.append("<taglib-location>");
1304 sb.append("/WEB-INF/tld/liferay-util.tld");
1305 sb.append("</taglib-location>");
1306 sb.append("</taglib>");
1307 }
1308
1309 if (hasTaglib && (webXmlVersion > 2.3)) {
1310 sb.append("</jsp-config>");
1311 }
1312
1313 return sb.toString();
1314 }
1315
1316 public String getExtraFiltersContent(double webXmlVersion, File srcFile)
1317 throws Exception {
1318
1319 return getSessionFiltersContent();
1320 }
1321
1322 public String getIgnoreFiltersContent(File srcFile) throws Exception {
1323 boolean ignoreFiltersEnabled = true;
1324
1325 Properties properties = getPluginPackageProperties(srcFile);
1326
1327 if (properties != null) {
1328 ignoreFiltersEnabled = GetterUtil.getBoolean(
1329 properties.getProperty("ignore-filters-enabled"), true);
1330 }
1331
1332 if (ignoreFiltersEnabled) {
1333 String ignoreFiltersContent = FileUtil.read(
1334 DeployUtil.getResourcePath("ignore-filters-web.xml"));
1335
1336 return ignoreFiltersContent;
1337 }
1338 else {
1339 return StringPool.BLANK;
1340 }
1341 }
1342
1343 public String getInvokerFilterContent() {
1344 StringBundler sb = new StringBundler(4);
1345
1346 sb.append(getInvokerFilterContent("ERROR"));
1347 sb.append(getInvokerFilterContent("FORWARD"));
1348 sb.append(getInvokerFilterContent("INCLUDE"));
1349 sb.append(getInvokerFilterContent("REQUEST"));
1350
1351 return sb.toString();
1352 }
1353
1354 public String getInvokerFilterContent(String dispatcher) {
1355 StringBundler sb = new StringBundler(23);
1356
1357 sb.append("<filter>");
1358 sb.append("<filter-name>Invoker Filter - ");
1359 sb.append(dispatcher);
1360 sb.append("</filter-name>");
1361 sb.append("<filter-class>");
1362 sb.append(InvokerFilter.class.getName());
1363 sb.append("</filter-class>");
1364 sb.append("<init-param>");
1365 sb.append("<param-name>dispatcher</param-name>");
1366 sb.append("<param-value>");
1367 sb.append(dispatcher);
1368 sb.append("</param-value>");
1369 sb.append("</init-param>");
1370 sb.append("</filter>");
1371
1372 sb.append("<filter-mapping>");
1373 sb.append("<filter-name>Invoker Filter - ");
1374 sb.append(dispatcher);
1375 sb.append("</filter-name>");
1376 sb.append("<url-pattern>