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.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.PropsUtil;
32  import com.liferay.portal.util.PropsValues;
33  import com.liferay.util.FileUtil;
34  import com.liferay.util.SystemProperties;
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              PropsUtil.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                  PropsUtil.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                  PropsUtil.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      private DeployUtil() {
94      }
95  
96      private String _getResourcePath(String resource) throws IOException {
97          String tmpDir = SystemProperties.get(SystemProperties.TMP_DIR);
98  
99          File file = new File(
100             tmpDir + "/liferay/com/liferay/portal/deploy/dependencies/" +
101                 resource);
102 
103         File parentFile = file.getParentFile();
104 
105         if (parentFile != null) {
106             parentFile.mkdirs();
107         }
108 
109         byte[] byteArray = FileUtil.getBytes(
110             getClass().getResourceAsStream("dependencies/" + resource));
111 
112         OutputStream os = new FileOutputStream(file);
113 
114         os.write(byteArray);
115 
116         return FileUtil.getAbsolutePath(file);
117     }
118 
119     private static DeployUtil _instance = new DeployUtil();
120 
121 }