001
014
015 package com.liferay.portal.tools.deploy;
016
017 import com.liferay.portal.deploy.DeployUtil;
018 import com.liferay.portal.kernel.deploy.Deployer;
019 import com.liferay.portal.kernel.deploy.auto.AutoDeployException;
020 import com.liferay.portal.kernel.deploy.auto.AutoDeployer;
021 import com.liferay.portal.kernel.deploy.auto.context.AutoDeploymentContext;
022 import com.liferay.portal.kernel.log.Log;
023 import com.liferay.portal.kernel.log.LogFactoryUtil;
024 import com.liferay.portal.kernel.plugin.License;
025 import com.liferay.portal.kernel.plugin.PluginPackage;
026 import com.liferay.portal.kernel.servlet.PluginContextListener;
027 import com.liferay.portal.kernel.servlet.PortalClassLoaderServlet;
028 import com.liferay.portal.kernel.servlet.PortalDelegateServlet;
029 import com.liferay.portal.kernel.servlet.PortletServlet;
030 import com.liferay.portal.kernel.servlet.SecurePluginContextListener;
031 import com.liferay.portal.kernel.servlet.SecureServlet;
032 import com.liferay.portal.kernel.servlet.SerializableSessionAttributeListener;
033 import com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilter;
034 import com.liferay.portal.kernel.util.ArrayUtil;
035 import com.liferay.portal.kernel.util.CharPool;
036 import com.liferay.portal.kernel.util.FileUtil;
037 import com.liferay.portal.kernel.util.GetterUtil;
038 import com.liferay.portal.kernel.util.HttpUtil;
039 import com.liferay.portal.kernel.util.OSDetector;
040 import com.liferay.portal.kernel.util.PropertiesUtil;
041 import com.liferay.portal.kernel.util.PropsKeys;
042 import com.liferay.portal.kernel.util.ServerDetector;
043 import com.liferay.portal.kernel.util.StreamUtil;
044 import com.liferay.portal.kernel.util.StringBundler;
045 import com.liferay.portal.kernel.util.StringPool;
046 import com.liferay.portal.kernel.util.StringUtil;
047 import com.liferay.portal.kernel.util.SystemProperties;
048 import com.liferay.portal.kernel.util.TextFormatter;
049 import com.liferay.portal.kernel.util.Time;
050 import com.liferay.portal.kernel.util.Validator;
051 import com.liferay.portal.kernel.xml.Document;
052 import com.liferay.portal.kernel.xml.Element;
053 import com.liferay.portal.kernel.xml.UnsecureSAXReaderUtil;
054 import com.liferay.portal.plugin.PluginPackageUtil;
055 import com.liferay.portal.security.lang.SecurityManagerUtil;
056 import com.liferay.portal.tools.WebXMLBuilder;
057 import com.liferay.portal.util.ExtRegistry;
058 import com.liferay.portal.util.InitUtil;
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 boolean SESSION_VERIFY_SERIALIZABLE_ATTRIBUTE =
095 GetterUtil.getBoolean(
096 PropsUtil.get(PropsKeys.SESSION_VERIFY_SERIALIZABLE_ATTRIBUTE));
097
098 public static final String DEPLOY_TO_PREFIX = "DEPLOY_TO__";
099
100 public static void main(String[] args) {
101 InitUtil.initWithSpring();
102
103 List<String> wars = new ArrayList<String>();
104 List<String> jars = new ArrayList<String>();
105
106 for (String arg : args) {
107 String fileName = StringUtil.toLowerCase(arg);
108
109 if (fileName.endsWith(".war")) {
110 wars.add(arg);
111 }
112 else if (fileName.endsWith(".jar")) {
113 jars.add(arg);
114 }
115 }
116
117 new BaseDeployer(wars, jars);
118 }
119
120 public BaseDeployer() {
121 }
122
123 public BaseDeployer(List<String> wars, List<String> jars) {
124 baseDir = System.getProperty("deployer.base.dir");
125 destDir = System.getProperty("deployer.dest.dir");
126 appServerType = System.getProperty("deployer.app.server.type");
127 auiTaglibDTD = System.getProperty("deployer.aui.taglib.dtd");
128 portletTaglibDTD = System.getProperty("deployer.portlet.taglib.dtd");
129 portletExtTaglibDTD = System.getProperty(
130 "deployer.portlet.ext.taglib.dtd");
131 securityTaglibDTD = System.getProperty("deployer.security.taglib.dtd");
132 themeTaglibDTD = System.getProperty("deployer.theme.taglib.dtd");
133 uiTaglibDTD = System.getProperty("deployer.ui.taglib.dtd");
134 utilTaglibDTD = System.getProperty("deployer.util.taglib.dtd");
135 unpackWar = GetterUtil.getBoolean(
136 System.getProperty("deployer.unpack.war"), true);
137 filePattern = System.getProperty("deployer.file.pattern");
138 jbossPrefix = GetterUtil.getString(
139 System.getProperty("deployer.jboss.prefix"));
140 tomcatLibDir = System.getProperty("deployer.tomcat.lib.dir");
141 this.wars = wars;
142 this.jars = jars;
143
144 checkArguments();
145
146 String context = System.getProperty("deployer.context");
147
148 try {
149 deploy(context);
150 }
151 catch (Exception e) {
152 _log.error(e, e);
153 }
154 }
155
156 @Override
157 public void addExtJar(List<String> jars, String resource) throws Exception {
158 Set<String> servletContextNames = ExtRegistry.getServletContextNames();
159
160 for (String servletContextName : servletContextNames) {
161 String extResource =
162 "ext-" + servletContextName + resource.substring(3);
163
164 String path = DeployUtil.getResourcePath(extResource);
165
166 if (_log.isDebugEnabled()) {
167 if (path == null) {
168 _log.debug("Resource " + extResource + " is not available");
169 }
170 else {
171 _log.debug(
172 "Resource " + extResource + " is available at " + path);
173 }
174 }
175
176 if (path != null) {
177 jars.add(path);
178 }
179 }
180 }
181
182 @Override
183 public void addRequiredJar(List<String> jars, String resource)
184 throws Exception {
185
186 String path = DeployUtil.getResourcePath(resource);
187
188 if (path == null) {
189 throw new RuntimeException(
190 "Resource " + resource + " does not exist");
191 }
192
193 if (_log.isDebugEnabled()) {
194 _log.debug("Resource " + resource + " is available at " + path);
195 }
196
197 jars.add(path);
198 }
199
200 @Override
201 public int autoDeploy(AutoDeploymentContext autoDeploymentContext)
202 throws AutoDeployException {
203
204 List<String> wars = new ArrayList<String>();
205
206 File file = autoDeploymentContext.getFile();
207
208 wars.add(file.getName());
209
210 this.wars = wars;
211
212 try {
213 return deployFile(autoDeploymentContext);
214 }
215 catch (Exception e) {
216 throw new AutoDeployException(e);
217 }
218 }
219
220 @Override
221 public void checkArguments() {
222 if (Validator.isNull(baseDir)) {
223 throw new IllegalArgumentException(
224 "The system property deployer.base.dir is not set");
225 }
226
227 if (Validator.isNull(destDir)) {
228 throw new IllegalArgumentException(
229 "The system property deployer.dest.dir is not set");
230 }
231
232 if (Validator.isNull(appServerType)) {
233 throw new IllegalArgumentException(
234 "The system property deployer.app.server.type is not set");
235 }
236
237 if (!appServerType.equals(ServerDetector.GERONIMO_ID) &&
238 !appServerType.equals(ServerDetector.GLASSFISH_ID) &&
239 !appServerType.equals(ServerDetector.JBOSS_ID) &&
240 !appServerType.equals(ServerDetector.JONAS_ID) &&
241 !appServerType.equals(ServerDetector.JETTY_ID) &&
242 !appServerType.equals(ServerDetector.OC4J_ID) &&
243 !appServerType.equals(ServerDetector.RESIN_ID) &&
244 !appServerType.equals(ServerDetector.TOMCAT_ID) &&
245 !appServerType.equals(ServerDetector.WEBLOGIC_ID) &&
246 !appServerType.equals(ServerDetector.WEBSPHERE_ID)) {
247
248 throw new IllegalArgumentException(
249 appServerType + " is not a valid application server type");
250 }
251
252 if (appServerType.equals(ServerDetector.GLASSFISH_ID) ||
253 appServerType.equals(ServerDetector.WEBSPHERE_ID)) {
254
255 unpackWar = false;
256 }
257
258 if (Validator.isNotNull(jbossPrefix) &&
259 !Validator.isNumber(jbossPrefix)) {
260
261 jbossPrefix = "1";
262 }
263 }
264
265 @Override
266 public AutoDeployer cloneAutoDeployer() throws AutoDeployException {
267 try {
268 Class<?> clazz = getClass();
269
270 Deployer deployer = (Deployer)clazz.newInstance();
271
272 deployer.setAppServerType(appServerType);
273 deployer.setAuiTaglibDTD(auiTaglibDTD);
274 deployer.setBaseDir(baseDir);
275 deployer.setDestDir(destDir);
276 deployer.setFilePattern(filePattern);
277 deployer.setJars(jars);
278 deployer.setJbossPrefix(jbossPrefix);
279 deployer.setPortletExtTaglibDTD(portletExtTaglibDTD);
280 deployer.setPortletTaglibDTD(portletTaglibDTD);
281 deployer.setSecurityTaglibDTD(securityTaglibDTD);
282 deployer.setThemeTaglibDTD(themeTaglibDTD);
283 deployer.setTomcatLibDir(tomcatLibDir);
284 deployer.setUiTaglibDTD(uiTaglibDTD);
285 deployer.setUnpackWar(unpackWar);
286 deployer.setUtilTaglibDTD(utilTaglibDTD);
287 deployer.setWars(wars);
288
289 return (AutoDeployer)deployer;
290 }
291 catch (Exception e) {
292 throw new AutoDeployException(e);
293 }
294 }
295
296 @Override
297 public void copyDependencyXml(String fileName, String targetDir)
298 throws Exception {
299
300 copyDependencyXml(fileName, targetDir, null);
301 }
302
303 @Override
304 public void copyDependencyXml(
305 String fileName, String targetDir, Map<String, String> filterMap)
306 throws Exception {
307
308 copyDependencyXml(fileName, targetDir, filterMap, false);
309 }
310
311 @Override
312 public void copyDependencyXml(
313 String fileName, String targetDir, Map<String, String> filterMap,
314 boolean overwrite)
315 throws Exception {
316
317 DeployUtil.copyDependencyXml(
318 fileName, targetDir, fileName, filterMap, overwrite);
319 }
320
321 public void copyDtds(File srcFile, PluginPackage pluginPackage)
322 throws Exception {
323
324 File portalLog4jXml = new File(
325 srcFile.getAbsolutePath() +
326 "/WEB-INF/classes/META-INF/portal-log4j.xml");
327
328 if (!portalLog4jXml.exists()) {
329 return;
330 }
331
332 InputStream is = null;
333
334 try {
335 Class<?> clazz = getClass();
336
337 ClassLoader classLoader = clazz.getClassLoader();
338
339 is = classLoader.getResourceAsStream("META-INF/log4j.dtd");
340
341 File file = new File(
342 srcFile.getAbsolutePath() +
343 "/WEB-INF/classes/META-INF/log4j.dtd");
344
345 FileUtil.write(file, is);
346 }
347 finally {
348 StreamUtil.cleanUp(is);
349 }
350 }
351
352 @Override
353 public void copyJars(File srcFile, PluginPackage pluginPackage)
354 throws Exception {
355
356 for (int i = 0; i < jars.size(); i++) {
357 String jarFullName = jars.get(i);
358
359 String jarName = jarFullName.substring(
360 jarFullName.lastIndexOf("/") + 1);
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.size() > 0) {
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 DeleteTask.deleteDirectory(tempDir);
808 }
809 }
810 else {
811
812
813
814
815
816
817
818 excludes += "**/WEB-INF/web.xml";
819
820 CopyTask.copyDirectory(
821 srcFile, deployDir, StringPool.BLANK, excludes, overwrite,
822 true);
823
824 CopyTask.copyDirectory(
825 srcFile, deployDir, "**/WEB-INF/web.xml", StringPool.BLANK,
826 true, false);
827
828 if (appServerType.equals(ServerDetector.TOMCAT_ID)) {
829
830
831
832
833
834 File deployWebXml = new File(deployDir + "/WEB-INF/web.xml");
835
836 deployWebXml.setLastModified(
837 System.currentTimeMillis() + (Time.SECOND * 6));
838 }
839 }
840
841 if (appServerType.equals(ServerDetector.JETTY_ID)) {
842 DeployUtil.redeployJetty(displayName);
843 }
844 }
845
846 public void deployDirectory(
847 File srcFile, String displayName, boolean override,
848 PluginPackage pluginPackage)
849 throws Exception {
850
851 deployDirectory(
852 srcFile, null, null, displayName, override, pluginPackage);
853 }
854
855 public int deployFile(AutoDeploymentContext autoDeploymentContext)
856 throws Exception {
857
858 File srcFile = autoDeploymentContext.getFile();
859
860 PluginPackage pluginPackage = readPluginPackage(srcFile);
861
862 if (_log.isInfoEnabled()) {
863 _log.info("Deploying " + srcFile.getName());
864 }
865
866 String autoDeploymentContextAppServerType =
867 autoDeploymentContext.getAppServerType();
868
869 if (Validator.isNotNull(autoDeploymentContextAppServerType)) {
870 appServerType = autoDeploymentContextAppServerType;
871 }
872
873 String specifiedContext = autoDeploymentContext.getContext();
874
875 String displayName = specifiedContext;
876 boolean overwrite = false;
877 String preliminaryContext = specifiedContext;
878
879
880
881
882
883
884 if (Validator.isNull(specifiedContext) &&
885 srcFile.getName().startsWith(DEPLOY_TO_PREFIX)) {
886
887 displayName = srcFile.getName().substring(
888 DEPLOY_TO_PREFIX.length(), srcFile.getName().length() - 4);
889
890 overwrite = true;
891 preliminaryContext = displayName;
892 }
893
894 if (preliminaryContext == null) {
895 preliminaryContext = getDisplayName(srcFile);
896 }
897
898 if (pluginPackage != null) {
899 if (!PluginPackageUtil.isCurrentVersionSupported(
900 pluginPackage.getLiferayVersions())) {
901
902 throw new AutoDeployException(
903 srcFile.getName() +
904 " does not support this version of Liferay");
905 }
906
907 if (displayName == null) {
908 displayName = pluginPackage.getRecommendedDeploymentContext();
909 }
910
911 if (Validator.isNull(displayName)) {
912 displayName = getDisplayName(srcFile);
913 }
914
915 pluginPackage.setContext(displayName);
916
917 PluginPackageUtil.updateInstallingPluginPackage(
918 preliminaryContext, pluginPackage);
919 }
920
921 String deployDir = null;
922
923 if (Validator.isNotNull(displayName)) {
924 deployDir = displayName + ".war";
925 }
926 else {
927 deployDir = srcFile.getName();
928 displayName = getDisplayName(srcFile);
929 }
930
931 if (appServerType.equals(ServerDetector.JBOSS_ID)) {
932 deployDir = jbossPrefix + deployDir;
933 }
934 else if (appServerType.equals(ServerDetector.GERONIMO_ID) ||
935 appServerType.equals(ServerDetector.GLASSFISH_ID) ||
936 appServerType.equals(ServerDetector.JETTY_ID) ||
937 appServerType.equals(ServerDetector.JONAS_ID) ||
938 appServerType.equals(ServerDetector.OC4J_ID) ||
939 appServerType.equals(ServerDetector.RESIN_ID) ||
940 appServerType.equals(ServerDetector.TOMCAT_ID) ||
941 appServerType.equals(ServerDetector.WEBLOGIC_ID)) {
942
943 if (unpackWar) {
944 deployDir = deployDir.substring(0, deployDir.length() - 4);
945 }
946 }
947
948 String destDir = this.destDir;
949
950 if (autoDeploymentContext.getDestDir() != null) {
951 destDir = autoDeploymentContext.getDestDir();
952 }
953
954 File deployDirFile = new File(destDir + "/" + deployDir);
955
956 try {
957 PluginPackage previousPluginPackage = readPluginPackage(
958 deployDirFile);
959
960 if ((pluginPackage != null) && (previousPluginPackage != null)) {
961 String name = pluginPackage.getName();
962 String previousVersion = previousPluginPackage.getVersion();
963 String version = pluginPackage.getVersion();
964
965 if (_log.isInfoEnabled()) {
966 _log.info(
967 "Updating " + name + " from version " +
968 previousVersion + " to version " + version);
969 }
970
971 if (pluginPackage.isPreviousVersionThan(
972 previousPluginPackage)) {
973
974 if (_log.isInfoEnabled()) {
975 _log.info(
976 "Not updating " + name + " because version " +
977 previousVersion + " is newer than version " +
978 version);
979 }
980
981 return AutoDeployer.CODE_SKIP_NEWER_VERSION;
982 }
983
984 overwrite = true;
985 }
986
987 File mergeDirFile = new File(
988 srcFile.getParent() + "/merge/" + srcFile.getName());
989
990 if (srcFile.isDirectory()) {
991 deployDirectory(
992 srcFile, mergeDirFile, deployDirFile, displayName,
993 overwrite, pluginPackage);
994 }
995 else {
996 boolean deployed = deployFile(
997 srcFile, mergeDirFile, deployDirFile, displayName,
998 overwrite, pluginPackage);
999
1000 if (!deployed) {
1001 String context = preliminaryContext;
1002
1003 if (pluginPackage != null) {
1004 context = pluginPackage.getContext();
1005 }
1006
1007 PluginPackageUtil.endPluginPackageInstallation(context);
1008 }
1009 else {
1010 postDeploy(destDir, deployDir);
1011 }
1012 }
1013
1014 return AutoDeployer.CODE_DEFAULT;
1015 }
1016 catch (Exception e) {
1017 if (pluginPackage != null) {
1018 PluginPackageUtil.endPluginPackageInstallation(
1019 pluginPackage.getContext());
1020 }
1021
1022 throw e;
1023 }
1024 }
1025
1026 public boolean deployFile(
1027 File srcFile, File mergeDir, File deployDir, String displayName,
1028 boolean overwrite, PluginPackage pluginPackage)
1029 throws Exception {
1030
1031 boolean undeployOnRedeploy = false;
1032
1033 try {
1034 undeployOnRedeploy = PrefsPropsUtil.getBoolean(
1035 PropsKeys.HOT_UNDEPLOY_ON_REDEPLOY,
1036 PropsValues.HOT_UNDEPLOY_ON_REDEPLOY);
1037 }
1038 catch (Exception e) {
1039
1040
1041
1042
1043
1044 }
1045
1046 if (undeployOnRedeploy) {
1047 DeployUtil.undeploy(appServerType, deployDir);
1048 }
1049
1050 if (!overwrite && UpToDateTask.isUpToDate(srcFile, deployDir)) {
1051 if (_log.isInfoEnabled()) {
1052 _log.info(deployDir + " is already up to date");
1053 }
1054
1055 return false;
1056 }
1057
1058 File tempDir = new File(
1059 SystemProperties.get(SystemProperties.TMP_DIR) + File.separator +
1060 Time.getTimestamp());
1061
1062 ExpandTask.expand(srcFile, tempDir);
1063
1064 deployDirectory(
1065 tempDir, mergeDir, deployDir, displayName, overwrite,
1066 pluginPackage);
1067
1068 DeleteTask.deleteDirectory(tempDir);
1069
1070 return true;
1071 }
1072
1073 public String downloadJar(String jar) throws Exception {
1074 String tmpDir = SystemProperties.get(SystemProperties.TMP_DIR);
1075
1076 File file = new File(
1077 tmpDir + "/liferay/com/liferay/portal/deploy/dependencies/" + jar);
1078
1079 if (!file.exists()) {
1080 synchronized (this) {
1081 String url = PropsUtil.get(
1082 PropsKeys.LIBRARY_DOWNLOAD_URL + jar);
1083
1084 if (_log.isInfoEnabled()) {
1085 _log.info("Downloading library from " + url);
1086 }
1087
1088 byte[] bytes = HttpUtil.URLtoByteArray(url);
1089
1090 FileUtil.write(file, bytes);
1091 }
1092 }
1093
1094 return FileUtil.getAbsolutePath(file);
1095 }
1096
1097 public String fixPortalDependencyJar(String portalJar) {
1098 if (portalJar.equals("antlr.jar")) {
1099 portalJar = "antlr2.jar";
1100 }
1101
1102 return portalJar;
1103 }
1104
1105 public DeploymentHandler getDeploymentHandler() {
1106 String prefix = "auto.deploy." + ServerDetector.getServerId() + ".jee.";
1107
1108 String dmId = PropsUtil.get(prefix + "dm.id");
1109 String dmUser = PropsUtil.get(prefix + "dm.user");
1110 String dmPassword = PropsUtil.get(prefix + "dm.passwd");
1111 String dfClassName = PropsUtil.get(prefix + "df.classname");
1112
1113 return new DeploymentHandler(dmId, dmUser, dmPassword, dfClassName);
1114 }
1115
1116 public String getDisplayName(File srcFile) {
1117 String displayName = srcFile.getName();
1118
1119 if (StringUtil.endsWith(displayName, ".war") ||
1120 StringUtil.endsWith(displayName, ".xml")) {
1121
1122 displayName = displayName.substring(0, displayName.length() - 4);
1123 }
1124
1125 if (appServerType.equals(ServerDetector.JBOSS_ID) &&
1126 Validator.isNotNull(jbossPrefix) &&
1127 displayName.startsWith(jbossPrefix)) {
1128
1129 displayName = displayName.substring(1);
1130 }
1131
1132 return displayName;
1133 }
1134
1135 public String getDynamicResourceServletContent() {
1136 StringBundler sb = new StringBundler();
1137
1138 sb.append("<servlet>");
1139 sb.append("<servlet-name>");
1140 sb.append("Dynamic Resource Servlet");
1141 sb.append("</servlet-name>");
1142 sb.append("<servlet-class>");
1143 sb.append(PortalClassLoaderServlet.class.getName());
1144 sb.append("</servlet-class>");
1145 sb.append("<init-param>");
1146 sb.append("<param-name>");
1147 sb.append("servlet-class");
1148 sb.append("</param-name>");
1149 sb.append("<param-value>");
1150 sb.append(DynamicResourceServlet.class.getName());
1151 sb.append("</param-value>");
1152 sb.append("</init-param>");
1153 sb.append("<load-on-startup>1</load-on-startup>");
1154 sb.append("</servlet>");
1155
1156 for (String allowedPath :
1157 PropsValues.DYNAMIC_RESOURCE_SERVLET_ALLOWED_PATHS) {
1158
1159 sb.append("<servlet-mapping>");
1160 sb.append("<servlet-name>");
1161 sb.append("Dynamic Resource Servlet");
1162 sb.append("</servlet-name>");
1163 sb.append("<url-pattern>");
1164 sb.append(allowedPath);
1165
1166 if (!allowedPath.endsWith(StringPool.SLASH)) {
1167 sb.append(StringPool.SLASH);
1168 }
1169
1170 sb.append(StringPool.STAR);
1171 sb.append("</url-pattern>");
1172 sb.append("</servlet-mapping>");
1173 }
1174
1175 return sb.toString();
1176 }
1177
1178 public String getExtraContent(
1179 double webXmlVersion, File srcFile, String displayName)
1180 throws Exception {
1181
1182 StringBundler sb = new StringBundler();
1183
1184 sb.append("<display-name>");
1185 sb.append(displayName);
1186 sb.append("</display-name>");
1187
1188 if (webXmlVersion < 2.4) {
1189 sb.append("<context-param>");
1190 sb.append("<param-name>liferay-invoker-enabled</param-name>");
1191 sb.append("<param-value>false</param-value>");
1192 sb.append("</context-param>");
1193 }
1194
1195 if (SESSION_VERIFY_SERIALIZABLE_ATTRIBUTE) {
1196 sb.append("<listener>");
1197 sb.append("<listener-class>");
1198 sb.append(SerializableSessionAttributeListener.class.getName());
1199 sb.append("</listener-class>");
1200 sb.append("</listener>");
1201 }
1202
1203 sb.append(getDynamicResourceServletContent());
1204
1205 File serverConfigWsdd = new File(
1206 srcFile + "/WEB-INF/server-config.wsdd");
1207
1208 if (serverConfigWsdd.exists()) {
1209 File webXml = new File(srcFile + "/WEB-INF/web.xml");
1210
1211 String content = FileUtil.read(webXml);
1212
1213 if (!content.contains("axis.servicesPath")) {
1214 String remotingContent = FileUtil.read(
1215 DeployUtil.getResourcePath("remoting-web.xml"));
1216
1217 sb.append(remotingContent);
1218 }
1219 }
1220
1221 boolean hasTaglib = false;
1222
1223 if (Validator.isNotNull(auiTaglibDTD) ||
1224 Validator.isNotNull(portletTaglibDTD) ||
1225 Validator.isNotNull(portletExtTaglibDTD) ||
1226 Validator.isNotNull(securityTaglibDTD) ||
1227 Validator.isNotNull(themeTaglibDTD) ||
1228 Validator.isNotNull(uiTaglibDTD) ||
1229 Validator.isNotNull(utilTaglibDTD)) {
1230
1231 hasTaglib = true;
1232 }
1233
1234 if (hasTaglib && (webXmlVersion > 2.3)) {
1235 sb.append("<jsp-config>");
1236 }
1237
1238 if (Validator.isNotNull(auiTaglibDTD)) {
1239 sb.append("<taglib>");
1240 sb.append("<taglib-uri>http:
1241 sb.append("<taglib-location>");
1242 sb.append("/WEB-INF/tld/aui.tld");
1243 sb.append("</taglib-location>");
1244 sb.append("</taglib>");
1245 }
1246
1247 if (Validator.isNotNull(portletTaglibDTD)) {
1248 sb.append("<taglib>");
1249 sb.append(
1250 "<taglib-uri>http:
1251 sb.append("<taglib-location>");
1252 sb.append("/WEB-INF/tld/liferay-portlet.tld");
1253 sb.append("</taglib-location>");
1254 sb.append("</taglib>");
1255 }
1256
1257 if (Validator.isNotNull(portletExtTaglibDTD)) {
1258 sb.append("<taglib>");
1259 sb.append("<taglib-uri>");
1260 sb.append("http:
1261 sb.append("</taglib-uri>");
1262 sb.append("<taglib-location>");
1263 sb.append("/WEB-INF/tld/liferay-portlet-ext.tld");
1264 sb.append("</taglib-location>");
1265 sb.append("</taglib>");
1266 }
1267
1268 if (Validator.isNotNull(securityTaglibDTD)) {
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-security.tld");
1275 sb.append("</taglib-location>");
1276 sb.append("</taglib>");
1277 }
1278
1279 if (Validator.isNotNull(themeTaglibDTD)) {
1280 sb.append("<taglib>");
1281 sb.append("<taglib-uri>http:
1282 sb.append("<taglib-location>");
1283 sb.append("/WEB-INF/tld/liferay-theme.tld");
1284 sb.append("</taglib-location>");
1285 sb.append("</taglib>");
1286 }
1287
1288 if (Validator.isNotNull(uiTaglibDTD)) {
1289 sb.append("<taglib>");
1290 sb.append("<taglib-uri>http:
1291 sb.append("<taglib-location>");
1292 sb.append("/WEB-INF/tld/liferay-ui.tld");
1293 sb.append("</taglib-location>");
1294 sb.append("</taglib>");
1295 }
1296
1297 if (Validator.isNotNull(utilTaglibDTD)) {
1298 sb.append("<taglib>");
1299 sb.append("<taglib-uri>http:
1300 sb.append("<taglib-location>");
1301 sb.append("/WEB-INF/tld/liferay-util.tld");
1302 sb.append("</taglib-location>");
1303 sb.append("</taglib>");
1304 }
1305
1306 if (hasTaglib && (webXmlVersion > 2.3)) {
1307 sb.append("</jsp-config>");
1308 }
1309
1310 return sb.toString();
1311 }
1312
1313 public String getExtraFiltersContent(double webXmlVersion, File srcFile)
1314 throws Exception {
1315
1316 return getSessionFiltersContent();
1317 }
1318
1319 public String getIgnoreFiltersContent(File srcFile) throws Exception {
1320 boolean ignoreFiltersEnabled = true;
1321
1322 Properties properties = getPluginPackageProperties(srcFile);
1323
1324 if (properties != null) {
1325 ignoreFiltersEnabled = GetterUtil.getBoolean(
1326 properties.getProperty("ignore-filters-enabled"), true);
1327 }
1328
1329 if (ignoreFiltersEnabled) {
1330 String ignoreFiltersContent = FileUtil.read(
1331 DeployUtil.getResourcePath("ignore-filters-web.xml"));
1332
1333 return ignoreFiltersContent;
1334 }
1335 else {
1336 return StringPool.BLANK;
1337 }
1338 }
1339
1340 public String getInvokerFilterContent() {
1341 StringBundler sb = new StringBundler(4);
1342
1343 sb.append(getInvokerFilterContent("ERROR"));
1344 sb.append(getInvokerFilterContent("FORWARD"));
1345 sb.append(getInvokerFilterContent("INCLUDE"));
1346 sb.append(getInvokerFilterContent("REQUEST"));
1347
1348 return sb.toString();
1349 }
1350
1351 public String getInvokerFilterContent(String dispatcher) {
1352 StringBundler sb = new StringBundler(23);
1353
1354 sb.append("<filter>");
1355 sb.append("<filter-name>Invoker Filter - ");
1356 sb.append(dispatcher);
1357 sb.append("</filter-name>");
1358 sb.append("<filter-class>");
1359 sb.append(InvokerFilter.class.getName());
1360 sb.append("</filter-class>");
1361 sb.append("<init-param>");
1362 sb.append("<param-name>dispatcher</param-name>");
1363 sb.append("<param-value>");
1364 sb.append(dispatcher);
1365 sb.append("</param-value>");
1366 sb.append("</init-param>");
1367 sb.append("</filter>");
1368
1369 sb.append("<filter-mapping>");
1370 sb.append("<filter-name>Invoker Filter - ");
1371 sb.append(dispatcher);
1372 sb.append("</filter-name>");
1373 sb.append("<url-pattern>