1
22
23 package com.liferay.portal.tools.deploy;
24
25 import com.liferay.portal.deploy.DeployUtil;
26 import com.liferay.portal.kernel.deploy.auto.AutoDeployException;
27 import com.liferay.portal.kernel.log.Log;
28 import com.liferay.portal.kernel.log.LogFactoryUtil;
29 import com.liferay.portal.kernel.plugin.PluginPackage;
30 import com.liferay.portal.kernel.util.FileUtil;
31 import com.liferay.portal.kernel.util.GetterUtil;
32 import com.liferay.portal.kernel.util.HttpUtil;
33 import com.liferay.portal.kernel.util.PropertiesUtil;
34 import com.liferay.portal.kernel.util.ServerDetector;
35 import com.liferay.portal.kernel.util.StringPool;
36 import com.liferay.portal.kernel.util.StringUtil;
37 import com.liferay.portal.kernel.util.Time;
38 import com.liferay.portal.kernel.util.Validator;
39 import com.liferay.portal.kernel.xml.Document;
40 import com.liferay.portal.kernel.xml.Element;
41 import com.liferay.portal.kernel.xml.SAXReaderUtil;
42 import com.liferay.portal.plugin.PluginPackageUtil;
43 import com.liferay.portal.tools.WebXMLBuilder;
44 import com.liferay.portal.util.InitUtil;
45 import com.liferay.portal.util.PortalUtil;
46 import com.liferay.portal.util.PrefsPropsUtil;
47 import com.liferay.portal.util.PropsKeys;
48 import com.liferay.portal.util.PropsUtil;
49 import com.liferay.portal.util.PropsValues;
50 import com.liferay.util.License;
51 import com.liferay.util.SystemProperties;
52 import com.liferay.util.ant.CopyTask;
53 import com.liferay.util.ant.DeleteTask;
54 import com.liferay.util.ant.ExpandTask;
55 import com.liferay.util.ant.UpToDateTask;
56 import com.liferay.util.ant.WarTask;
57 import com.liferay.util.xml.XMLFormatter;
58
59 import com.sun.portal.portletcontainer.warupdater.PortletWarUpdater;
60
61 import java.io.File;
62 import java.io.FileInputStream;
63 import java.io.IOException;
64 import java.io.InputStream;
65
66 import java.util.ArrayList;
67 import java.util.List;
68 import java.util.Map;
69 import java.util.Properties;
70 import java.util.zip.ZipEntry;
71 import java.util.zip.ZipFile;
72
73 import org.apache.oro.io.GlobFilenameFilter;
74
75
82 public class BaseDeployer {
83
84 public static final String DEPLOY_TO_PREFIX = "DEPLOY_TO__";
85
86 public static void main(String[] args) {
87 InitUtil.initWithSpring();
88
89 List<String> wars = new ArrayList<String>();
90 List<String> jars = new ArrayList<String>();
91
92 for (String arg : args) {
93 String fileName = arg.toLowerCase();
94
95 if (fileName.endsWith(".war")) {
96 wars.add(arg);
97 }
98 else if (fileName.endsWith(".jar")) {
99 jars.add(arg);
100 }
101 }
102
103 new BaseDeployer(wars, jars);
104 }
105
106 protected BaseDeployer() {
107 }
108
109 protected BaseDeployer(List<String> wars, List<String> jars) {
110 baseDir = System.getProperty("deployer.base.dir");
111 destDir = System.getProperty("deployer.dest.dir");
112 appServerType = System.getProperty("deployer.app.server.type");
113 portletTaglibDTD = System.getProperty("deployer.portlet.taglib.dtd");
114 portletExtTaglibDTD = System.getProperty(
115 "deployer.portlet.ext.taglib.dtd");
116 securityTaglibDTD = System.getProperty("deployer.security.taglib.dtd");
117 themeTaglibDTD = System.getProperty("deployer.theme.taglib.dtd");
118 uiTaglibDTD = System.getProperty("deployer.ui.taglib.dtd");
119 utilTaglibDTD = System.getProperty("deployer.util.taglib.dtd");
120 unpackWar = GetterUtil.getBoolean(
121 System.getProperty("deployer.unpack.war"), true);
122 filePattern = System.getProperty("deployer.file.pattern");
123 jbossPrefix = GetterUtil.getString(
124 System.getProperty("deployer.jboss.prefix"));
125 tomcatLibDir = System.getProperty("deployer.tomcat.lib.dir");
126 this.wars = wars;
127 this.jars = jars;
128
129 checkArguments();
130
131 try {
132 deploy();
133 }
134 catch (Exception e) {
135 e.printStackTrace();
136 }
137 }
138
139 protected void checkArguments() {
140 if (Validator.isNull(baseDir)) {
141 throw new IllegalArgumentException(
142 "The system property deployer.base.dir is not set");
143 }
144
145 if (Validator.isNull(destDir)) {
146 throw new IllegalArgumentException(
147 "The system property deployer.dest.dir is not set");
148 }
149
150 if (Validator.isNull(appServerType)) {
151 throw new IllegalArgumentException(
152 "The system property deployer.app.server.type is not set");
153 }
154
155 if (!appServerType.startsWith(ServerDetector.GERONIMO_ID) &&
156 !appServerType.startsWith(ServerDetector.GLASSFISH_ID) &&
157 !appServerType.startsWith(ServerDetector.JBOSS_ID) &&
158 !appServerType.startsWith(ServerDetector.JONAS_ID) &&
159 !appServerType.equals(ServerDetector.JETTY_ID) &&
160 !appServerType.equals(ServerDetector.OC4J_ID) &&
161 !appServerType.equals(ServerDetector.RESIN_ID) &&
162 !appServerType.equals(ServerDetector.TOMCAT_ID) &&
163 !appServerType.equals(ServerDetector.WEBLOGIC_ID) &&
164 !appServerType.equals(ServerDetector.WEBSPHERE_ID)) {
165
166 throw new IllegalArgumentException(
167 appServerType + " is not a valid application server type");
168 }
169
170 if (appServerType.startsWith(ServerDetector.GLASSFISH_ID) ||
171 appServerType.equals(ServerDetector.WEBLOGIC_ID)) {
172
173 unpackWar = false;
174 }
175
176 if (Validator.isNotNull(jbossPrefix) &&
177 !Validator.isNumber(jbossPrefix)) {
178
179 jbossPrefix = "1";
180 }
181 }
182
183 protected void copyDependencyXml(String fileName, String targetDir)
184 throws Exception {
185
186 copyDependencyXml(fileName, targetDir, null);
187 }
188
189 protected void copyDependencyXml(
190 String fileName, String targetDir, Map<String, String> filterMap)
191 throws Exception {
192
193 copyDependencyXml(fileName, targetDir, filterMap, false);
194 }
195
196 protected void copyDependencyXml(
197 String fileName, String targetDir, Map<String, String> filterMap,
198 boolean overwrite)
199 throws Exception {
200
201 File file = new File(DeployUtil.getResourcePath(fileName));
202 File targetFile = new File(targetDir + "/" + fileName);
203
204 if (!targetFile.exists()) {
205 CopyTask.copyFile(
206 file, new File(targetDir), filterMap, overwrite, true);
207 }
208 }
209
210 protected void copyJars(File srcFile, PluginPackage pluginPackage)
211 throws Exception {
212
213 for (int i = 0; i < jars.size(); i++) {
214 String jarFullName = jars.get(i);
215 String jarName = jarFullName.substring(
216 jarFullName.lastIndexOf("/") + 1, jarFullName.length());
217
218 if ((!appServerType.equals(ServerDetector.TOMCAT_ID)) ||
219 (appServerType.equals(ServerDetector.TOMCAT_ID) &&
220 !jarFullName.equals("util-java.jar"))) {
221
222 FileUtil.copyFile(
223 jarFullName, srcFile + "/WEB-INF/lib/" + jarName, true);
224 }
225 }
226
227 FileUtil.delete(srcFile + "/WEB-INF/lib/util-jsf.jar");
228 }
229
230 protected void copyPortalDependencies(File srcFile) throws Exception {
231 Properties properties = getPluginPackageProperties(srcFile);
232
233 if (properties == null) {
234 return;
235 }
236
237
239 String[] portalJars = StringUtil.split(
240 properties.getProperty(
241 "portal-dependency-jars",
242 properties.getProperty("portal.dependency.jars")));
243
244 for (int i = 0; i < portalJars.length; i++) {
245 String portalJar = portalJars[i].trim();
246
247 if (_log.isDebugEnabled()) {
248 _log.debug("Copy portal JAR " + portalJar);
249 }
250
251 try {
252 String portalJarPath = PortalUtil.getPortalLibDir() + portalJar;
253
254 FileUtil.copyFile(
255 portalJarPath, srcFile + "/WEB-INF/lib/" + portalJar, true);
256 }
257 catch (Exception e) {
258 _log.error("Unable to copy portal JAR " + portalJar, e);
259 }
260 }
261
262
264 String[] portalTlds = StringUtil.split(
265 properties.getProperty(
266 "portal-dependency-tlds",
267 properties.getProperty("portal.dependency.tlds")));
268
269 for (int i = 0; i < portalTlds.length; i++) {
270 String portalTld = portalTlds[i].trim();
271
272 if (_log.isDebugEnabled()) {
273 _log.debug("Copy portal TLD " + portalTld);
274 }
275
276 try {
277 String portalTldPath = DeployUtil.getResourcePath(portalTld);
278
279 FileUtil.copyFile(
280 portalTldPath, srcFile + "/WEB-INF/tld/" + portalTld, true);
281 }
282 catch (Exception e) {
283 _log.error("Unable to copy portal TLD " + portalTld, e);
284 }
285 }
286
287
289 File pluginLibDir = new File(srcFile + "/WEB-INF/lib/");
290
291 String[] commonsLoggingJars = pluginLibDir.list(
292 new GlobFilenameFilter("commons-logging*.jar"));
293
294 if ((commonsLoggingJars == null) || (commonsLoggingJars.length == 0)) {
295 String portalJarPath =
296 PortalUtil.getPortalLibDir() + "commons-logging.jar";
297
298 FileUtil.copyFile(
299 portalJarPath, srcFile + "/WEB-INF/lib/commons-logging.jar",
300 true);
301 }
302
303
305 String[] log4jJars = pluginLibDir.list(
306 new GlobFilenameFilter("log4j*.jar"));
307
308 if ((log4jJars == null) || (log4jJars.length == 0)) {
309 String portalJarPath = PortalUtil.getPortalLibDir() + "log4j.jar";
310
311 FileUtil.copyFile(
312 portalJarPath, srcFile + "/WEB-INF/lib/log4j.jar", true);
313 }
314 }
315
316 protected void copyProperties(File srcFile, PluginPackage pluginPackage)
317 throws Exception {
318
319 copyDependencyXml("log4j.properties", srcFile + "/WEB-INF/classes");
320 copyDependencyXml("logging.properties", srcFile + "/WEB-INF/classes");
321 }
322
323 protected void copyTlds(File srcFile, PluginPackage pluginPackage)
324 throws Exception {
325
326 if (Validator.isNotNull(portletTaglibDTD)) {
327 FileUtil.copyFile(
328 portletTaglibDTD, srcFile + "/WEB-INF/tld/liferay-portlet.tld",
329 true);
330 }
331
332 if (Validator.isNotNull(portletExtTaglibDTD)) {
333 FileUtil.copyFile(
334 portletExtTaglibDTD,
335 srcFile + "/WEB-INF/tld/liferay-portlet-ext.tld", true);
336 }
337
338 if (Validator.isNotNull(securityTaglibDTD)) {
339 FileUtil.copyFile(
340 securityTaglibDTD,
341 srcFile + "/WEB-INF/tld/liferay-security.tld", true);
342 }
343
344 if (Validator.isNotNull(themeTaglibDTD)) {
345 FileUtil.copyFile(
346 themeTaglibDTD, srcFile + "/WEB-INF/tld/liferay-theme.tld",
347 true);
348 }
349
350 if (Validator.isNotNull(uiTaglibDTD)) {
351 FileUtil.copyFile(
352 uiTaglibDTD, srcFile + "/WEB-INF/tld/liferay-ui.tld", true);
353 }
354
355 if (Validator.isNotNull(utilTaglibDTD)) {
356 FileUtil.copyFile(
357 utilTaglibDTD, srcFile + "/WEB-INF/tld/liferay-util.tld", true);
358 }
359 }
360
361 protected void copyXmls(
362 File srcFile, String displayName, PluginPackage pluginPackage)
363 throws Exception {
364
365 if (appServerType.startsWith(ServerDetector.GERONIMO_ID)) {
366 copyDependencyXml("geronimo-web.xml", srcFile + "/WEB-INF");
367 }
368
369 copyDependencyXml("web.xml", srcFile + "/WEB-INF");
370 }
371
372 protected void deploy() throws Exception {
373 try {
374 File baseDirFile = new File(baseDir);
375
376 File[] files = baseDirFile.listFiles();
377
378 if (files == null) {
379 return;
380 }
381
382 files = FileUtil.sortFiles(files);
383
384 for (int i = 0; i < files.length; i++) {
385 File srcFile = files[i];
386
387 String fileName = srcFile.getName().toLowerCase();
388
389 boolean deploy = false;
390
391 if (fileName.endsWith(".war") || fileName.endsWith(".zip")) {
392 deploy = true;
393
394 if (wars.size() > 0) {
395 if (!wars.contains(srcFile.getName())) {
396 deploy = false;
397 }
398 }
399 else if (Validator.isNotNull(filePattern)) {
400 if (!StringUtil.matches(fileName, filePattern)) {
401 deploy = false;
402 }
403 }
404 }
405
406 if (deploy) {
407 deployFile(srcFile);
408 }
409 }
410 }
411 catch (Exception e) {
412 e.printStackTrace();
413 }
414 }
415
416 protected void deployDirectory(
417 File srcFile, String displayName, boolean override,
418 PluginPackage pluginPackage)
419 throws Exception {
420
421 deployDirectory(
422 srcFile, null, null, displayName, override, pluginPackage);
423 }
424
425 protected void deployDirectory(
426 File srcFile, File mergeDir, File deployDir, String displayName,
427 boolean overwrite, PluginPackage pluginPackage)
428 throws Exception {
429
430 if ((PropsValues.PORTLET_CONTAINER_IMPL_SUN) &&
431 (this instanceof PortletDeployer)) {
432
433 Properties properties = new Properties();
434
435 properties.setProperty(PortletWarUpdater.ADD_WEB_XML, "true");
436
437 PortletWarUpdater portletWarUpdater = new PortletWarUpdater(
438 properties);
439
440 portletWarUpdater.preparePortlet(displayName, srcFile);
441 }
442
443 rewriteFiles(srcFile);
444
445 mergeDirectory(mergeDir, srcFile);
446
447 processPluginPackageProperties(srcFile, displayName, pluginPackage);
448
449 copyJars(srcFile, pluginPackage);
450 copyProperties(srcFile, pluginPackage);
451 copyTlds(srcFile, pluginPackage);
452 copyXmls(srcFile, displayName, pluginPackage);
453 copyPortalDependencies(srcFile);
454
455 updateGeronimoWebXml(srcFile, displayName, pluginPackage);
456
457 File webXml = new File(srcFile + "/WEB-INF/web.xml");
458
459 updateWebXml(webXml, srcFile, displayName, pluginPackage);
460
461 if ((deployDir == null) || baseDir.equals(destDir)) {
462 return;
463 }
464
465 updateDeployDirectory(srcFile);
466
467 String excludes = StringPool.BLANK;
468
469 if (appServerType.startsWith("jboss")) {
470 excludes += "**/WEB-INF/lib/log4j.jar,";
471 }
472 else if (appServerType.equals(ServerDetector.TOMCAT_ID)) {
473 String[] libs = FileUtil.listFiles(tomcatLibDir);
474
475 for (int i = 0; i < libs.length; i++) {
476 excludes += "**/WEB-INF/lib/" + libs[i] + ",";
477 }
478
479 File contextXml = new File(srcFile + "/META-INF/context.xml");
480
481 if (contextXml.exists()) {
482 String content = FileUtil.read(contextXml);
483
484 if (content.indexOf(_PORTAL_CLASS_LOADER) != -1) {
485 excludes += "**/WEB-INF/lib/util-bridges.jar,";
486 excludes += "**/WEB-INF/lib/util-java.jar,";
487 excludes += "**/WEB-INF/lib/util-taglib.jar,";
488 }
489 }
490
491 try {
492
493
495 Class.forName("javax.el.ELContext");
496
497 excludes += "**/WEB-INF/lib/el-api.jar,";
498 }
499 catch (ClassNotFoundException cnfe) {
500 }
501 }
502
503 if (!unpackWar || appServerType.equals("websphere")) {
504 File tempDir = new File(
505 SystemProperties.get(SystemProperties.TMP_DIR) +
506 File.separator + Time.getTimestamp());
507
508 WarTask.war(srcFile, tempDir, "WEB-INF/web.xml", webXml);
509
510 if (isJEEDeploymentEnabled()) {
511 File tempWarDir = new File(
512 tempDir.getParent(), deployDir.getName());
513
514 if (tempWarDir.exists()) {
515 tempWarDir.delete();
516 }
517
518 if (!tempDir.renameTo(tempWarDir)) {
519 tempWarDir = tempDir;
520 }
521
522 DeploymentHandler deploymentHandler = getDeploymentHandler();
523
524 deploymentHandler.deploy(tempWarDir, displayName);
525
526 deploymentHandler.releaseDeploymentManager();
527
528 DeleteTask.deleteDirectory(tempWarDir);
529 }
530 else {
531 if (!tempDir.renameTo(deployDir)) {
532 WarTask.war(srcFile, deployDir, "WEB-INF/web.xml", webXml);
533 }
534
535 DeleteTask.deleteDirectory(tempDir);
536 }
537 }
538 else {
539
540
546 excludes += "**/WEB-INF/web.xml";
547
548 CopyTask.copyDirectory(
549 srcFile, deployDir, StringPool.BLANK, excludes, overwrite,
550 true);
551
552 CopyTask.copyDirectory(
553 srcFile, deployDir, "**/WEB-INF/web.xml", StringPool.BLANK,
554 true, false);
555
556 if (appServerType.equals(ServerDetector.TOMCAT_ID)) {
557
558
562 File deployWebXml = new File(deployDir + "/WEB-INF/web.xml");
563
564 deployWebXml.setLastModified(
565 System.currentTimeMillis() + (Time.SECOND * 6));
566 }
567 }
568 }
569
570 protected void deployFile(File srcFile) throws Exception {
571 PluginPackage pluginPackage = readPluginPackage(srcFile);
572
573 if (_log.isInfoEnabled()) {
574 _log.info("Deploying " + srcFile.getName());
575 }
576
577 String deployDir = null;
578 String displayName = null;
579 boolean overwrite = false;
580 String preliminaryContext = null;
581
582
585 if (srcFile.getName().startsWith(DEPLOY_TO_PREFIX)) {
586 displayName = srcFile.getName().substring(
587 DEPLOY_TO_PREFIX.length(), srcFile.getName().length() - 4);
588
589 overwrite = true;
590 preliminaryContext = displayName;
591 }
592
593 if (preliminaryContext == null) {
594 preliminaryContext = getDisplayName(srcFile);
595 }
596
597 if (pluginPackage != null) {
598 if (!PluginPackageUtil.isCurrentVersionSupported(
599 pluginPackage.getLiferayVersions())) {
600
601 throw new AutoDeployException(
602 srcFile.getName() +
603 " does not support this version of Liferay");
604 }
605
606 if (displayName == null) {
607 displayName = pluginPackage.getRecommendedDeploymentContext();
608 }
609
610 if (Validator.isNull(displayName)) {
611 displayName = getDisplayName(srcFile);
612 }
613
614 pluginPackage.setContext(displayName);
615
616 PluginPackageUtil.updateInstallingPluginPackage(
617 preliminaryContext, pluginPackage);
618 }
619
620 if (Validator.isNotNull(displayName)) {
621 deployDir = displayName + ".war";
622 }
623 else {
624 deployDir = srcFile.getName();
625 displayName = getDisplayName(srcFile);
626 }
627
628 if (appServerType.startsWith(ServerDetector.JBOSS_ID)) {
629 deployDir = jbossPrefix + deployDir;
630 }
631 else if (appServerType.equals(ServerDetector.JETTY_ID) ||
632 appServerType.equals(ServerDetector.OC4J_ID) ||
633 appServerType.equals(ServerDetector.RESIN_ID) ||
634 appServerType.equals(ServerDetector.TOMCAT_ID)) {
635
636 if (unpackWar) {
637 deployDir = deployDir.substring(0, deployDir.length() - 4);
638 }
639 }
640
641 deployDir = destDir + "/" + deployDir;
642
643 File deployDirFile = new File(deployDir);
644
645 try {
646 PluginPackage previousPluginPackage =
647 readPluginPackage(deployDirFile);
648
649 if ((pluginPackage != null) && (previousPluginPackage != null)) {
650 if (_log.isInfoEnabled()) {
651 String name = pluginPackage.getName();
652 String previousVersion = previousPluginPackage.getVersion();
653 String version = pluginPackage.getVersion();
654
655 _log.info(
656 "Updating " + name + " from version " +
657 previousVersion + " to version " + version);
658 }
659
660 if (pluginPackage.isLaterVersionThan(
661 previousPluginPackage)) {
662
663 overwrite = true;
664 }
665 }
666
667 File mergeDirFile = new File(
668 srcFile.getParent() + "/merge/" + srcFile.getName());
669
670 if (srcFile.isDirectory()) {
671 deployDirectory(
672 srcFile, mergeDirFile, deployDirFile, displayName,
673 overwrite, pluginPackage);
674 }
675 else {
676 boolean deployed = deployFile(
677 srcFile, mergeDirFile, deployDirFile, displayName,
678 overwrite, pluginPackage);
679
680 if (!deployed) {
681 String context = preliminaryContext;
682
683 if (pluginPackage != null) {
684 context = pluginPackage.getContext();
685 }
686
687 PluginPackageUtil.endPluginPackageInstallation(context);
688 }
689 }
690 }
691 catch (Exception e) {
692 if (pluginPackage != null) {
693 PluginPackageUtil.endPluginPackageInstallation(
694 pluginPackage.getContext());
695 }
696
697 throw e;
698 }
699 }
700
701 protected boolean deployFile(
702 File srcFile, File mergeDir, File deployDir, String displayName,
703 boolean overwrite, PluginPackage pluginPackage)
704 throws Exception {
705
706 boolean undeployOnRedeploy = false;
707
708 try {
709 undeployOnRedeploy = PrefsPropsUtil.getBoolean(
710 PropsKeys.HOT_UNDEPLOY_ON_REDEPLOY,
711 PropsValues.HOT_UNDEPLOY_ON_REDEPLOY);
712 }
713 catch (Exception e) {
714
715
719 }
720
721 if (undeployOnRedeploy) {
722 DeployUtil.undeploy(appServerType, deployDir);
723 }
724
725 if (!overwrite && UpToDateTask.isUpToDate(srcFile, deployDir)) {
726 if (_log.isInfoEnabled()) {
727 _log.info(deployDir + " is already up to date");
728 }
729
730 return false;
731 }
732
733 File tempDir = new File(
734 SystemProperties.get(SystemProperties.TMP_DIR) + File.separator +
735 Time.getTimestamp());
736
737 ExpandTask.expand(srcFile, tempDir);
738
739 deployDirectory(
740 tempDir, mergeDir, deployDir, displayName, overwrite,
741 pluginPackage);
742
743 DeleteTask.deleteDirectory(tempDir);
744
745 return true;
746 }
747
748 protected String downloadJar(String jar) throws Exception {
749 String tmpDir = SystemProperties.get(SystemProperties.TMP_DIR);
750
751 File file = new File(
752 tmpDir + "/liferay/com/liferay/portal/deploy/dependencies/" +
753 jar);
754
755 if (!file.exists()) {
756 synchronized (this) {
757 String url = PropsUtil.get(
758 PropsKeys.LIBRARY_DOWNLOAD_URL + jar);
759
760 if (_log.isInfoEnabled()) {
761 _log.info("Downloading library from " + url);
762 }
763
764 byte[] bytes = HttpUtil.URLtoByteArray(url);
765
766 FileUtil.write(file, bytes);
767 }
768 }
769
770 return FileUtil.getAbsolutePath(file);
771 }
772
773 protected String getDisplayName(File srcFile) {
774 String displayName = srcFile.getName();
775
776 if (StringUtil.endsWith(displayName, ".war") ||
777 StringUtil.endsWith(displayName, ".xml")) {
778
779 displayName = displayName.substring(0, displayName.length() - 4);
780 }
781
782 if (appServerType.startsWith("jboss") &&
783 Validator.isNotNull(jbossPrefix) &&
784 displayName.startsWith(jbossPrefix)) {
785
786 displayName = displayName.substring(1, displayName.length());
787 }
788
789 return displayName;
790 }
791
792 protected DeploymentHandler getDeploymentHandler() {
793 String prefix = "auto.deploy." + ServerDetector.getServerId() + ".jee.";
794
795 String dmId = PropsUtil.get(prefix + "dm.id");
796 String dmUser = PropsUtil.get(prefix + "dm.user");
797 String dmPassword = PropsUtil.get(prefix + "dm.passwd");
798 String dfClassName = PropsUtil.get(prefix + "df.classname");
799
800 return new DeploymentHandler(dmId, dmUser, dmPassword, dfClassName);
801 }
802
803 protected String getExtraContent(
804 double webXmlVersion, File srcFile, String displayName)
805 throws Exception {
806
807 StringBuilder sb = new StringBuilder();
808
809 sb.append("<display-name>");
810 sb.append(displayName);
811 sb.append("</display-name>");
812
813 boolean hasTaglib = false;
814
815 if (Validator.isNotNull(portletTaglibDTD) ||
816 Validator.isNotNull(portletExtTaglibDTD) ||
817 Validator.isNotNull(securityTaglibDTD) ||
818 Validator.isNotNull(themeTaglibDTD) ||
819 Validator.isNotNull(uiTaglibDTD) ||
820 Validator.isNotNull(utilTaglibDTD)) {
821
822 hasTaglib = true;
823 }
824
825 if (hasTaglib && (webXmlVersion > 2.3)) {
826 sb.append("<jsp-config>");
827 }
828
829 if (Validator.isNotNull(portletTaglibDTD)) {
830 sb.append("<taglib>");
831 sb.append(
832 "<taglib-uri>http://java.sun.com/portlet_2_0</taglib-uri>");
833 sb.append("<taglib-location>");
834 sb.append("/WEB-INF/tld/liferay-portlet.tld");
835 sb.append("</taglib-location>");
836 sb.append("</taglib>");
837 }
838
839 if (Validator.isNotNull(portletExtTaglibDTD)) {
840 sb.append("<taglib>");
841 sb.append("<taglib-uri>");
842 sb.append("http://liferay.com/tld/portlet");
843 sb.append("</taglib-uri>");
844 sb.append("<taglib-location>");
845 sb.append("/WEB-INF/tld/liferay-portlet-ext.tld");
846 sb.append("</taglib-location>");
847 sb.append("</taglib>");
848 }
849
850 if (Validator.isNotNull(securityTaglibDTD)) {
851 sb.append("<taglib>");
852 sb.append("<taglib-uri>");
853 sb.append("http://liferay.com/tld/security");
854 sb.append("</taglib-uri>");
855 sb.append("<taglib-location>");
856 sb.append("/WEB-INF/tld/liferay-security.tld");
857 sb.append("</taglib-location>");
858 sb.append("</taglib>");
859 }
860
861 if (Validator.isNotNull(themeTaglibDTD)) {
862 sb.append("<taglib>");
863 sb.append("<taglib-uri>http://liferay.com/tld/theme</taglib-uri>");
864 sb.append("<taglib-location>");
865 sb.append("/WEB-INF/tld/liferay-theme.tld");
866 sb.append("</taglib-location>");
867 sb.append("</taglib>");
868 }
869
870 if (Validator.isNotNull(uiTaglibDTD)) {
871 sb.append("<taglib>");
872 sb.append("<taglib-uri>http://liferay.com/tld/ui</taglib-uri>");
873 sb.append("<taglib-location>");
874 sb.append("/WEB-INF/tld/liferay-ui.tld");
875 sb.append("</taglib-location>");
876 sb.append("</taglib>");
877 }
878
879 if (Validator.isNotNull(utilTaglibDTD)) {
880 sb.append("<taglib>");
881 sb.append("<taglib-uri>http://liferay.com/tld/util</taglib-uri>");
882 sb.append("<taglib-location>");
883 sb.append("/WEB-INF/tld/liferay-util.tld");
884 sb.append("</taglib-location>");
885 sb.append("</taglib>");
886 }
887
888 if (hasTaglib && (webXmlVersion > 2.3)) {
889 sb.append("</jsp-config>");
890 }
891
892 return sb.toString();
893 }
894
895 protected String getPluginPackageLicensesXml(List<License> licenses) {
896 StringBuilder sb = new StringBuilder();
897
898 for (int i = 0; i < licenses.size(); i++) {
899 License license = licenses.get(i);
900
901 if (i == 0) {
902 sb.append("\r\n");
903 }
904
905 sb.append("\t\t<license osi-approved=\"");
906 sb.append(license.isOsiApproved());
907 sb.append("\">");
908 sb.append(license.getName());
909 sb.append("</license>\r\n");
910
911 if ((i + 1) == licenses.size()) {
912 sb.append("\t");
913 }
914 }
915
916 return sb.toString();
917 }
918
919 protected String getPluginPackageLiferayVersionsXml(
920 List<String> liferayVersions) {
921
922 StringBuilder sb = new StringBuilder();
923
924 for (int i = 0; i < liferayVersions.size(); i++) {
925 String liferayVersion = liferayVersions.get(i);
926
927 if (i == 0) {
928 sb.append("\r\n");
929 }
930
931 sb.append("\t\t<liferay-version>");
932 sb.append(liferayVersion);
933 sb.append("</liferay-version>\r\n");
934
935 if ((i + 1) == liferayVersions.size()) {
936 sb.append("\t");
937 }
938 }
939
940 return sb.toString();
941 }
942
943 protected Properties getPluginPackageProperties(File srcFile)
944 throws Exception {
945
946 File propertiesFile = new File(
947 srcFile + "/WEB-INF/liferay-plugin-package.properties");
948
949 if (!propertiesFile.exists()) {
950 return null;
951 }
952
953 String propertiesString = FileUtil.read(propertiesFile);
954
955 return PropertiesUtil.load(propertiesString);
956 }
957
958 protected String getPluginPackageTagsXml(List<String> tags) {
959 StringBuilder sb = new StringBuilder();
960
961 for (int i = 0; i < tags.size(); i++) {
962 String tag = tags.get(i);
963
964 if (i == 0) {
965 sb.append("\r\n");
966 }
967
968 sb.append("\t\t<tag>");
969 sb.append(tag);
970 sb.append("</tag>\r\n");
971
972 if ((i + 1) == tags.size()) {
973 sb.append("\t");
974 }
975 }
976
977 return sb.toString();
978 }
979
980 protected boolean isJEEDeploymentEnabled() {
981 return GetterUtil.getBoolean(PropsUtil.get(
982 "auto.deploy." + ServerDetector.getServerId() +
983 ".jee.deployment.enabled"));
984 }
985
986 protected void mergeDirectory(File mergeDir, File targetDir) {
987 if ((mergeDir == null) || (!mergeDir.exists())) {
988 return;
989 }
990
991 CopyTask.copyDirectory(mergeDir, targetDir, null, null, true, false);
992 }
993
994 protected void processPluginPackageProperties(
995 File srcFile, String displayName, PluginPackage pluginPackage)
996 throws Exception {
997 }
998
999 protected PluginPackage readPluginPackage(File file) {
1000 if (!file.exists()) {
1001 return null;
1002 }
1003
1004 InputStream is = null;
1005 ZipFile zipFile = null;
1006
1007 try {
1008 boolean parseProps = false;
1009
1010 if (file.isDirectory()) {
1011 String path = file.getPath();
1012
1013 File pluginPackageXmlFile = new File(
1014 file.getParent() + "/merge/" + file.getName() +
1015 "/WEB-INF/liferay-plugin-package.xml");
1016
1017 if (pluginPackageXmlFile.exists()) {
1018 is = new FileInputStream(pluginPackageXmlFile);
1019 }
1020 else {
1021 pluginPackageXmlFile = new File(
1022 path + "/WEB-INF/liferay-plugin-package.xml");
1023
1024 if (pluginPackageXmlFile.exists()) {
1025 is = new FileInputStream(pluginPackageXmlFile);
1026 }
1027 }
1028
1029 File pluginPackagePropsFile = new File(
1030 file.getParent() + "/merge/" + file.getName() +
1031 "/WEB-INF/liferay-plugin-package.properties");
1032
1033 if ((is == null) && pluginPackagePropsFile.exists()) {
1034 is = new FileInputStream(pluginPackagePropsFile);
1035
1036 parseProps = true;
1037 }
1038 else {
1039 pluginPackagePropsFile = new File(
1040 path + "/WEB-INF/liferay-plugin-package.properties");
1041
1042 if ((is == null) && pluginPackagePropsFile.exists()) {
1043 is = new FileInputStream(pluginPackagePropsFile);
1044
1045 parseProps = true;
1046 }
1047 }
1048 }
1049 else {
1050 zipFile = new ZipFile(file);
1051
1052 File pluginPackageXmlFile = new File(
1053 file.getParent() + "/merge/" + file.getName() +
1054 "/WEB-INF/liferay-plugin-package.xml");
1055
1056 if (pluginPackageXmlFile.exists()) {
1057 is = new FileInputStream(pluginPackageXmlFile);
1058 }
1059 else {
1060 ZipEntry zipEntry = zipFile.getEntry(
1061 "WEB-INF/liferay-plugin-package.xml");
1062
1063 if (zipEntry != null) {
1064 is = zipFile.getInputStream(zipEntry);
1065 }
1066 }
1067
1068 File pluginPackagePropsFile = new File(
1069 file.getParent() + "/merge/" + file.getName() +
1070 "/WEB-INF/liferay-plugin-package.properties");
1071
1072 if ((is == null) && pluginPackagePropsFile.exists()) {
1073 is = new FileInputStream(pluginPackagePropsFile);
1074
1075 parseProps = true;
1076 }
1077 else {
1078 ZipEntry zipEntry = zipFile.getEntry(
1079 "WEB-INF/liferay-plugin-package.properties");
1080
1081 if ((is == null) && (zipEntry != null)) {
1082 is = zipFile.getInputStream(zipEntry);
1083
1084 parseProps = true;
1085 }
1086 }
1087 }
1088
1089 if (is == null) {
1090 if (_log.isInfoEnabled()) {
1091 _log.info(
1092 file.getPath() + " does not have a " +
1093 "WEB-INF/liferay-plugin-package.xml or " +
1094 "WEB-INF/liferay-plugin-package.properties");
1095 }
1096
1097 return null;
1098 }
1099
1100 if (parseProps) {
1101 String displayName = getDisplayName(file);
1102
1103 String propertiesString = StringUtil.read(is);
1104
1105 Properties properties = PropertiesUtil.load(propertiesString);
1106
1107 return PluginPackageUtil.readPluginPackageProperties(
1108 displayName, properties);
1109 }
1110 else {
1111 String xml = StringUtil.read(is);
1112
1113 xml = XMLFormatter.fixProlog(xml);
1114
1115 return PluginPackageUtil.readPluginPackageXml(xml);
1116 }
1117 }
1118 catch (Exception e) {
1119 _log.error(file.getPath() + ": " + e.toString());
1120 }
1121 finally {
1122 if (is != null) {
1123 try {
1124 is.close();
1125 }
1126 catch (IOException ioe) {
1127 }
1128 }
1129
1130 if (zipFile != null) {
1131 try {
1132 zipFile.close();
1133 }
1134 catch (IOException ioe) {
1135 }
1136 }
1137 }
1138
1139 return null;
1140 }
1141
1142 protected void rewriteFiles(File srcDir) throws Exception {
1143 String[] files = FileUtil.listFiles(srcDir + "/WEB-INF/");
1144
1145 for (int i = 0; i < files.length; i++) {
1146 String fileName = GetterUtil.getString(
1147 FileUtil.getShortFileName(files[i]));
1148
1149
1151 if (fileName.equalsIgnoreCase("mule-config.xml")) {
1152 continue;
1153 }
1154
1155 String ext = GetterUtil.getString(FileUtil.getExtension(files[i]));
1156
1157 if (!ext.equalsIgnoreCase("xml")) {
1158 continue;
1159 }
1160
1161
1164 File file = new File(srcDir + "/WEB-INF/" + files[i]);
1165
1166 try {
1167 Document doc = SAXReaderUtil.read(file);
1168
1169 String content = doc.formattedString(StringPool.TAB, true);
1170
1171 FileUtil.write(file, content);
1172 }
1173 catch (Exception e) {
1174 if (_log.isWarnEnabled()) {
1175 _log.warn(
1176 "Unable to format " + file + ": " + e.getMessage());
1177 }
1178 }
1179 }
1180 }
1181
1182 protected void updateDeployDirectory(File srcFile) throws Exception {
1183 }
1184
1185 protected void updateGeronimoWebXml(
1186 File srcFile, String displayName, PluginPackage pluginPackage)
1187 throws Exception {
1188
1189 if (!appServerType.startsWith(ServerDetector.GERONIMO_ID)) {
1190 return;
1191 }
1192
1193 File geronimoWebXml = new File(srcFile + "/WEB-INF/geronimo-web.xml");
1194
1195 Document doc = SAXReaderUtil.read(geronimoWebXml);
1196
1197 Element root = doc.getRootElement();
1198
1199 Element environmentEl = root.element("environment");
1200
1201 Element moduleIdEl = environmentEl.element("moduleId");
1202
1203 Element artifactIdEl = moduleIdEl.element("artifactId");
1204
1205 String artifactIdText = GetterUtil.getString(artifactIdEl.getText());
1206
1207 if (!artifactIdText.equals(displayName)) {
1208 artifactIdEl.setText(displayName);
1209
1210 String content = doc.formattedString();
1211
1212 FileUtil.write(geronimoWebXml, content);
1213
1214 if (_log.isInfoEnabled()) {
1215 _log.info("Modifying Geronimo " + geronimoWebXml);
1216 }
1217 }
1218 }
1219
1220 protected void updateWebXml(
1221 File webXml, File srcFile, String displayName,
1222 PluginPackage pluginPackage)
1223 throws Exception {
1224
1225 String content = FileUtil.read(webXml);
1226
1227 int x = content.indexOf("<display-name>");
1228
1229 if (x != -1) {
1230 int y = content.indexOf("</display-name>", x);
1231
1232 y = content.indexOf(">", y) + 1;
1233
1234 content = content.substring(0, x) + content.substring(y);
1235 }
1236
1237 double webXmlVersion = 2.3;
1238
1239 Document webXmlDoc = SAXReaderUtil.read(content);
1240
1241 Element webXmlRoot = webXmlDoc.getRootElement();
1242
1243 webXmlVersion = GetterUtil.getDouble(
1244 webXmlRoot.attributeValue("version"), webXmlVersion);
1245
1246
1248 String extraContent = getExtraContent(
1249 webXmlVersion, srcFile, displayName);
1250
1251 int pos = content.indexOf("</web-app>");
1252
1253 String newContent =
1254 content.substring(0, pos) + extraContent +
1255 content.substring(pos, content.length());
1256
1257
1259 newContent = StringUtil.replace(
1260 newContent, "com.liferay.portal.shared.",
1261 "com.liferay.portal.kernel.");
1262
1263 newContent = WebXMLBuilder.organizeWebXML(newContent);
1264
1265 FileUtil.write(webXml, newContent, true);
1266
1267 if (_log.isInfoEnabled()) {
1268 _log.info("Modifying Servlet " + webXmlVersion + " " + webXml);
1269 }
1270 }
1271
1272 protected String baseDir;
1273 protected String destDir;
1274 protected String appServerType;
1275 protected String portletTaglibDTD;
1276 protected String portletExtTaglibDTD;
1277 protected String securityTaglibDTD;
1278 protected String themeTaglibDTD;
1279 protected String uiTaglibDTD;
1280 protected String utilTaglibDTD;
1281 protected boolean unpackWar;
1282 protected String filePattern;
1283 protected String jbossPrefix;
1284 protected String tomcatLibDir;
1285 protected List<String> wars;
1286 protected List<String> jars;
1287
1288 private static final String _PORTAL_CLASS_LOADER =
1289 "com.liferay.support.tomcat.loader.PortalClassLoader";
1290
1291 private static Log _log = LogFactoryUtil.getLog(BaseDeployer.class);
1292
1293}