1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.deploy;
16  
17  import com.liferay.portal.kernel.log.Log;
18  import com.liferay.portal.kernel.log.LogFactoryUtil;
19  import com.liferay.portal.kernel.util.FileUtil;
20  import com.liferay.portal.kernel.util.GetterUtil;
21  import com.liferay.portal.kernel.util.PropsKeys;
22  import com.liferay.portal.kernel.util.ServerDetector;
23  import com.liferay.portal.kernel.util.StreamUtil;
24  import com.liferay.portal.kernel.util.StringPool;
25  import com.liferay.portal.kernel.util.StringUtil;
26  import com.liferay.portal.kernel.util.Validator;
27  import com.liferay.portal.util.PrefsPropsUtil;
28  import com.liferay.portal.util.PropsValues;
29  import com.liferay.util.SystemProperties;
30  import com.liferay.util.ant.DeleteTask;
31  
32  import java.io.File;
33  import java.io.FileOutputStream;
34  import java.io.IOException;
35  import java.io.InputStream;
36  
37  /**
38   * <a href="DeployUtil.java.html"><b><i>View Source</i></b></a>
39   *
40   * @author Brian Wing Shun Chan
41   */
42  public class DeployUtil {
43  
44      public static String getAutoDeployDestDir() throws Exception {
45          String destDir = PrefsPropsUtil.getString(
46              PropsKeys.AUTO_DEPLOY_DEST_DIR, PropsValues.AUTO_DEPLOY_DEST_DIR);
47  
48          if (Validator.isNull(destDir)) {
49              destDir = getAutoDeployServerDestDir();
50          }
51  
52          return destDir;
53      }
54  
55      public static String getAutoDeployServerDestDir() throws Exception {
56          String destDir = null;
57  
58          String serverId = GetterUtil.getString(ServerDetector.getServerId());
59  
60          if (serverId.equals(ServerDetector.TOMCAT_ID)) {
61              destDir = PrefsPropsUtil.getString(
62                  PropsKeys.AUTO_DEPLOY_TOMCAT_DEST_DIR,
63                  PropsValues.AUTO_DEPLOY_TOMCAT_DEST_DIR);
64          }
65          else {
66              destDir = PrefsPropsUtil.getString(
67                  "auto.deploy." + serverId + ".dest.dir");
68          }
69  
70          if (Validator.isNull(destDir)) {
71              destDir = PrefsPropsUtil.getString(
72                  PropsKeys.AUTO_DEPLOY_DEFAULT_DEST_DIR,
73                  PropsValues.AUTO_DEPLOY_DEFAULT_DEST_DIR);
74          }
75  
76          destDir = StringUtil.replace(
77              destDir, StringPool.BACK_SLASH, StringPool.SLASH);
78  
79          return destDir;
80      }
81  
82      public static String getResourcePath(String resource)
83          throws Exception {
84  
85          return _instance._getResourcePath(resource);
86      }
87  
88      public static void undeploy(String appServerType, File deployDir)
89          throws Exception {
90  
91          boolean undeployEnabled = PrefsPropsUtil.getBoolean(
92              PropsKeys.HOT_UNDEPLOY_ENABLED, PropsValues.HOT_UNDEPLOY_ENABLED);
93  
94          if (!undeployEnabled) {
95              return;
96          }
97  
98          if (!appServerType.startsWith(ServerDetector.JBOSS_ID) &&
99              !appServerType.equals(ServerDetector.TOMCAT_ID)) {
100 
101             return;
102         }
103 
104         File webXml = new File(deployDir + "/WEB-INF/web.xml");
105 
106         if (!webXml.exists()) {
107             return;
108         }
109 
110         if (_log.isInfoEnabled()) {
111             _log.info("Undeploy " + deployDir);
112         }
113 
114         FileUtil.delete(deployDir + "/WEB-INF/web.xml");
115 
116         DeleteTask.deleteDirectory(deployDir);
117 
118         int undeployInterval = PrefsPropsUtil.getInteger(
119             PropsKeys.HOT_UNDEPLOY_INTERVAL,
120             PropsValues.HOT_UNDEPLOY_INTERVAL);
121 
122         if (_log.isInfoEnabled()) {
123             _log.info(
124                 "Wait " + undeployInterval +
125                     " ms to allow the plugin time to fully undeploy");
126         }
127 
128         if (undeployInterval > 0) {
129             Thread.sleep(undeployInterval);
130         }
131     }
132 
133     private DeployUtil() {
134     }
135 
136     private String _getResourcePath(String resource) throws IOException {
137         InputStream is = getClass().getResourceAsStream(
138             "dependencies/" + resource);
139 
140         if (is == null) {
141             return null;
142         }
143 
144         String tmpDir = SystemProperties.get(SystemProperties.TMP_DIR);
145 
146         File file = new File(
147             tmpDir + "/liferay/com/liferay/portal/deploy/dependencies/" +
148                 resource);
149 
150         //if (!file.exists() || resource.startsWith("ext-")) {
151             File parentFile = file.getParentFile();
152 
153             if (parentFile != null) {
154                 parentFile.mkdirs();
155             }
156 
157             StreamUtil.transfer(is, new FileOutputStream(file));
158         //}
159 
160         return FileUtil.getAbsolutePath(file);
161     }
162 
163     private static Log _log = LogFactoryUtil.getLog(DeployUtil.class);
164 
165     private static DeployUtil _instance = new DeployUtil();
166 
167 }