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