1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class DeployUtil {
50  
51      public static String getAutoDeployDestDir() throws Exception {
52          String destDir = PrefsPropsUtil.getString(
53              PropsKeys.AUTO_DEPLOY_DEST_DIR, PropsValues.AUTO_DEPLOY_DEST_DIR);
54  
55          if (Validator.isNull(destDir)) {
56              destDir = getAutoDeployServerDestDir();
57          }
58  
59          return destDir;
60      }
61  
62      public static String getAutoDeployServerDestDir() throws Exception {
63          String destDir = null;
64  
65          String serverId = GetterUtil.getString(ServerDetector.getServerId());
66  
67          if (serverId.equals(ServerDetector.TOMCAT_ID)) {
68              destDir = PrefsPropsUtil.getString(
69                  PropsKeys.AUTO_DEPLOY_TOMCAT_DEST_DIR,
70                  PropsValues.AUTO_DEPLOY_TOMCAT_DEST_DIR);
71          }
72          else {
73              destDir = PrefsPropsUtil.getString(
74                  "auto.deploy." + serverId + ".dest.dir");
75          }
76  
77          if (Validator.isNull(destDir)) {
78              destDir = PrefsPropsUtil.getString(
79                  PropsKeys.AUTO_DEPLOY_DEFAULT_DEST_DIR,
80                  PropsValues.AUTO_DEPLOY_DEFAULT_DEST_DIR);
81          }
82  
83          destDir = StringUtil.replace(
84              destDir, StringPool.BACK_SLASH, StringPool.SLASH);
85  
86          return destDir;
87      }
88  
89      public static String getResourcePath(String resource)
90          throws Exception {
91  
92          return _instance._getResourcePath(resource);
93      }
94  
95      public static void undeploy(String appServerType, File deployDir)
96          throws Exception {
97  
98          boolean undeployEnabled = PrefsPropsUtil.getBoolean(
99              PropsKeys.HOT_UNDEPLOY_ENABLED, PropsValues.HOT_UNDEPLOY_ENABLED);
100 
101         if (!undeployEnabled) {
102             return;
103         }
104 
105         if (!appServerType.startsWith(ServerDetector.JBOSS_ID) &&
106             !appServerType.equals(ServerDetector.TOMCAT_ID)) {
107 
108             return;
109         }
110 
111         File webXml = new File(deployDir + "/WEB-INF/web.xml");
112 
113         if (!webXml.exists()) {
114             return;
115         }
116 
117         if (_log.isInfoEnabled()) {
118             _log.info("Undeploy " + deployDir);
119         }
120 
121         FileUtil.delete(deployDir + "/WEB-INF/web.xml");
122 
123         DeleteTask.deleteDirectory(deployDir);
124 
125         int undeployInterval = PrefsPropsUtil.getInteger(
126             PropsKeys.HOT_UNDEPLOY_INTERVAL,
127             PropsValues.HOT_UNDEPLOY_INTERVAL);
128 
129         if (_log.isInfoEnabled()) {
130             _log.info(
131                 "Wait " + undeployInterval +
132                     " ms to allow the plugin time to fully undeploy");
133         }
134 
135         if (undeployInterval > 0) {
136             Thread.sleep(undeployInterval);
137         }
138     }
139 
140     private DeployUtil() {
141     }
142 
143     private String _getResourcePath(String resource) throws IOException {
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         File parentFile = file.getParentFile();
151 
152         if (parentFile != null) {
153             parentFile.mkdirs();
154         }
155 
156         byte[] bytes = FileUtil.getBytes(
157             getClass().getResourceAsStream("dependencies/" + resource));
158 
159         OutputStream os = new FileOutputStream(file);
160 
161         os.write(bytes);
162 
163         return FileUtil.getAbsolutePath(file);
164     }
165 
166     private static Log _log = LogFactoryUtil.getLog(DeployUtil.class);
167 
168     private static DeployUtil _instance = new DeployUtil();
169 
170 }