1   /**
2    * Copyright (c) 2000-2008 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.util.FileUtil;
26  import com.liferay.portal.kernel.util.GetterUtil;
27  import com.liferay.portal.kernel.util.ServerDetector;
28  import com.liferay.portal.kernel.util.StringPool;
29  import com.liferay.portal.kernel.util.StringUtil;
30  import com.liferay.portal.kernel.util.Validator;
31  import com.liferay.portal.util.PrefsPropsUtil;
32  import com.liferay.portal.util.PropsKeys;
33  import com.liferay.portal.util.PropsValues;
34  import com.liferay.util.SystemProperties;
35  import com.liferay.util.ant.DeleteTask;
36  
37  import java.io.File;
38  import java.io.FileOutputStream;
39  import java.io.IOException;
40  import java.io.OutputStream;
41  
42  import org.apache.commons.logging.Log;
43  import org.apache.commons.logging.LogFactory;
44  
45  /**
46   * <a href="DeployUtil.java.html"><b><i>View Source</i></b></a>
47   *
48   * @author Brian Wing Shun Chan
49   *
50   */
51  public class DeployUtil {
52  
53      public static String getAutoDeployDestDir() throws Exception {
54          String destDir = PrefsPropsUtil.getString(
55              PropsKeys.AUTO_DEPLOY_DEST_DIR, PropsValues.AUTO_DEPLOY_DEST_DIR);
56  
57          if (Validator.isNull(destDir)) {
58              destDir = getAutoDeployServerDestDir();
59          }
60  
61          return destDir;
62      }
63  
64      public static String getAutoDeployServerDestDir() throws Exception {
65          String destDir = null;
66  
67          String serverId = GetterUtil.getString(ServerDetector.getServerId());
68  
69          if (serverId.equals(ServerDetector.TOMCAT_ID)) {
70              destDir = PrefsPropsUtil.getString(
71                  PropsKeys.AUTO_DEPLOY_TOMCAT_DEST_DIR,
72                  PropsValues.AUTO_DEPLOY_TOMCAT_DEST_DIR);
73          }
74          else {
75              destDir = PrefsPropsUtil.getString(
76                  "auto.deploy." + serverId + ".dest.dir");
77          }
78  
79          if (Validator.isNull(destDir)) {
80              destDir = PrefsPropsUtil.getString(
81                  PropsKeys.AUTO_DEPLOY_DEFAULT_DEST_DIR,
82                  PropsValues.AUTO_DEPLOY_DEFAULT_DEST_DIR);
83          }
84  
85          destDir = StringUtil.replace(
86              destDir, StringPool.BACK_SLASH, StringPool.SLASH);
87  
88          return destDir;
89      }
90  
91      public static String getResourcePath(String resource)
92          throws Exception {
93  
94          return _instance._getResourcePath(resource);
95      }
96  
97      public static void undeploy(String appServerType, File deployDir)
98          throws Exception {
99  
100         boolean undeployEnabled = PrefsPropsUtil.getBoolean(
101             PropsKeys.HOT_UNDEPLOY_ENABLED, PropsValues.HOT_UNDEPLOY_ENABLED);
102 
103         if (!undeployEnabled) {
104             return;
105         }
106 
107         if (!appServerType.startsWith(ServerDetector.JBOSS_ID) &&
108             !appServerType.equals(ServerDetector.TOMCAT_ID)) {
109 
110             return;
111         }
112 
113         File webXml = new File(deployDir + "/WEB-INF/web.xml");
114 
115         if (!webXml.exists()) {
116             return;
117         }
118 
119         if (_log.isInfoEnabled()) {
120             _log.info("Undeploy " + deployDir);
121         }
122 
123         FileUtil.delete(deployDir + "/WEB-INF/web.xml");
124 
125         DeleteTask.deleteDirectory(deployDir);
126 
127         int undeployInterval = PrefsPropsUtil.getInteger(
128             PropsKeys.HOT_UNDEPLOY_INTERVAL,
129             PropsValues.HOT_UNDEPLOY_INTERVAL);
130 
131         if (_log.isInfoEnabled()) {
132             _log.info(
133                 "Wait " + undeployInterval +
134                     " ms to allow the plugin time to fully undeploy");
135         }
136 
137         if (undeployInterval > 0) {
138             Thread.sleep(undeployInterval);
139         }
140     }
141 
142     private DeployUtil() {
143     }
144 
145     private String _getResourcePath(String resource) throws IOException {
146         String tmpDir = SystemProperties.get(SystemProperties.TMP_DIR);
147 
148         File file = new File(
149             tmpDir + "/liferay/com/liferay/portal/deploy/dependencies/" +
150                 resource);
151 
152         File parentFile = file.getParentFile();
153 
154         if (parentFile != null) {
155             parentFile.mkdirs();
156         }
157 
158         byte[] bytes = FileUtil.getBytes(
159             getClass().getResourceAsStream("dependencies/" + resource));
160 
161         OutputStream os = new FileOutputStream(file);
162 
163         os.write(bytes);
164 
165         return FileUtil.getAbsolutePath(file);
166     }
167 
168     private static Log _log = LogFactory.getLog(DeployUtil.class);
169 
170     private static DeployUtil _instance = new DeployUtil();
171 
172 }