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