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