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.PropsKeys;
30  import com.liferay.portal.kernel.util.ServerDetector;
31  import com.liferay.portal.kernel.util.StreamUtil;
32  import com.liferay.portal.kernel.util.StringPool;
33  import com.liferay.portal.kernel.util.StringUtil;
34  import com.liferay.portal.kernel.util.Validator;
35  import com.liferay.portal.util.PrefsPropsUtil;
36  import com.liferay.portal.util.PropsValues;
37  import com.liferay.util.SystemProperties;
38  import com.liferay.util.ant.DeleteTask;
39  
40  import java.io.File;
41  import java.io.FileOutputStream;
42  import java.io.IOException;
43  import java.io.InputStream;
44  
45  /**
46   * <a href="DeployUtil.java.html"><b><i>View Source</i></b></a>
47   *
48   * @author Brian Wing Shun Chan
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         InputStream is = getClass().getResourceAsStream(
146             "dependencies/" + resource);
147 
148         if (is == null) {
149             return null;
150         }
151 
152         String tmpDir = SystemProperties.get(SystemProperties.TMP_DIR);
153 
154         File file = new File(
155             tmpDir + "/liferay/com/liferay/portal/deploy/dependencies/" +
156                 resource);
157 
158         if (!file.exists() || resource.startsWith("ext-")) {
159             File parentFile = file.getParentFile();
160 
161             if (parentFile != null) {
162                 parentFile.mkdirs();
163             }
164 
165             StreamUtil.transfer(is, new FileOutputStream(file));
166         }
167 
168         return FileUtil.getAbsolutePath(file);
169     }
170 
171     private static Log _log = LogFactoryUtil.getLog(DeployUtil.class);
172 
173     private static DeployUtil _instance = new DeployUtil();
174 
175 }