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