1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.deploy;
21  
22  import com.liferay.portal.kernel.log.Log;
23  import com.liferay.portal.kernel.log.LogFactoryUtil;
24  import com.liferay.portal.kernel.util.FileUtil;
25  import com.liferay.portal.kernel.util.GetterUtil;
26  import com.liferay.portal.kernel.util.ServerDetector;
27  import com.liferay.portal.kernel.util.StringPool;
28  import com.liferay.portal.kernel.util.StringUtil;
29  import com.liferay.portal.kernel.util.Validator;
30  import com.liferay.portal.util.PrefsPropsUtil;
31  import com.liferay.portal.util.PropsKeys;
32  import com.liferay.portal.util.PropsValues;
33  import com.liferay.util.SystemProperties;
34  import com.liferay.util.ant.DeleteTask;
35  
36  import java.io.File;
37  import java.io.FileOutputStream;
38  import java.io.IOException;
39  import java.io.OutputStream;
40  
41  /**
42   * <a href="DeployUtil.java.html"><b><i>View Source</i></b></a>
43   *
44   * @author Brian Wing Shun Chan
45   *
46   */
47  public class DeployUtil {
48  
49      public static String getAutoDeployDestDir() throws Exception {
50          String destDir = PrefsPropsUtil.getString(
51              PropsKeys.AUTO_DEPLOY_DEST_DIR, PropsValues.AUTO_DEPLOY_DEST_DIR);
52  
53          if (Validator.isNull(destDir)) {
54              destDir = getAutoDeployServerDestDir();
55          }
56  
57          return destDir;
58      }
59  
60      public static String getAutoDeployServerDestDir() throws Exception {
61          String destDir = null;
62  
63          String serverId = GetterUtil.getString(ServerDetector.getServerId());
64  
65          if (serverId.equals(ServerDetector.TOMCAT_ID)) {
66              destDir = PrefsPropsUtil.getString(
67                  PropsKeys.AUTO_DEPLOY_TOMCAT_DEST_DIR,
68                  PropsValues.AUTO_DEPLOY_TOMCAT_DEST_DIR);
69          }
70          else {
71              destDir = PrefsPropsUtil.getString(
72                  "auto.deploy." + serverId + ".dest.dir");
73          }
74  
75          if (Validator.isNull(destDir)) {
76              destDir = PrefsPropsUtil.getString(
77                  PropsKeys.AUTO_DEPLOY_DEFAULT_DEST_DIR,
78                  PropsValues.AUTO_DEPLOY_DEFAULT_DEST_DIR);
79          }
80  
81          destDir = StringUtil.replace(
82              destDir, StringPool.BACK_SLASH, StringPool.SLASH);
83  
84          return destDir;
85      }
86  
87      public static String getResourcePath(String resource)
88          throws Exception {
89  
90          return _instance._getResourcePath(resource);
91      }
92  
93      public static void undeploy(String appServerType, File deployDir)
94          throws Exception {
95  
96          boolean undeployEnabled = PrefsPropsUtil.getBoolean(
97              PropsKeys.HOT_UNDEPLOY_ENABLED, PropsValues.HOT_UNDEPLOY_ENABLED);
98  
99          if (!undeployEnabled) {
100             return;
101         }
102 
103         if (!appServerType.startsWith(ServerDetector.JBOSS_ID) &&
104             !appServerType.equals(ServerDetector.TOMCAT_ID)) {
105 
106             return;
107         }
108 
109         File webXml = new File(deployDir + "/WEB-INF/web.xml");
110 
111         if (!webXml.exists()) {
112             return;
113         }
114 
115         if (_log.isInfoEnabled()) {
116             _log.info("Undeploy " + deployDir);
117         }
118 
119         FileUtil.delete(deployDir + "/WEB-INF/web.xml");
120 
121         DeleteTask.deleteDirectory(deployDir);
122 
123         int undeployInterval = PrefsPropsUtil.getInteger(
124             PropsKeys.HOT_UNDEPLOY_INTERVAL,
125             PropsValues.HOT_UNDEPLOY_INTERVAL);
126 
127         if (_log.isInfoEnabled()) {
128             _log.info(
129                 "Wait " + undeployInterval +
130                     " ms to allow the plugin time to fully undeploy");
131         }
132 
133         if (undeployInterval > 0) {
134             Thread.sleep(undeployInterval);
135         }
136     }
137 
138     private DeployUtil() {
139     }
140 
141     private String _getResourcePath(String resource) throws IOException {
142         String tmpDir = SystemProperties.get(SystemProperties.TMP_DIR);
143 
144         File file = new File(
145             tmpDir + "/liferay/com/liferay/portal/deploy/dependencies/" +
146                 resource);
147 
148         File parentFile = file.getParentFile();
149 
150         if (parentFile != null) {
151             parentFile.mkdirs();
152         }
153 
154         byte[] bytes = FileUtil.getBytes(
155             getClass().getResourceAsStream("dependencies/" + resource));
156 
157         OutputStream os = new FileOutputStream(file);
158 
159         os.write(bytes);
160 
161         return FileUtil.getAbsolutePath(file);
162     }
163 
164     private static Log _log = LogFactoryUtil.getLog(DeployUtil.class);
165 
166     private static DeployUtil _instance = new DeployUtil();
167 
168 }