1   /**
2    * Copyright (c) 2000-2007 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.ServerDetector;
26  import com.liferay.portal.kernel.util.StringPool;
27  import com.liferay.portal.kernel.util.StringUtil;
28  import com.liferay.portal.kernel.util.Validator;
29  import com.liferay.portal.util.PrefsPropsUtil;
30  import com.liferay.portal.util.PropsUtil;
31  import com.liferay.util.FileUtil;
32  import com.liferay.util.SystemProperties;
33  
34  import java.io.File;
35  import java.io.FileOutputStream;
36  import java.io.IOException;
37  import java.io.OutputStream;
38  
39  /**
40   * <a href="DeployUtil.java.html"><b><i>View Source</i></b></a>
41   *
42   * @author Brian Wing Shun Chan
43   *
44   */
45  public class DeployUtil {
46  
47      public static String getAutoDeployDestDir() throws Exception {
48          String destDir = PrefsPropsUtil.getString(
49              PropsUtil.AUTO_DEPLOY_DEST_DIR);
50  
51          if (Validator.isNull(destDir)) {
52              destDir = getAutoDeployServerDestDir();
53          }
54  
55          return destDir;
56      }
57  
58      public static String getAutoDeployServerDestDir() throws Exception {
59          String destDir = PrefsPropsUtil.getString(
60              "auto.deploy." + ServerDetector.getServerId() + ".dest.dir");
61  
62          if (Validator.isNull(destDir)) {
63              destDir = PrefsPropsUtil.getString(
64                  PropsUtil.AUTO_DEPLOY_DEFAULT_DEST_DIR);
65          }
66  
67          destDir = StringUtil.replace(
68              destDir, StringPool.BACK_SLASH, StringPool.SLASH);
69  
70          return destDir;
71      }
72  
73      public static String getResourcePath(String resource)
74          throws Exception {
75  
76          return _instance._getResourcePath(resource);
77      }
78  
79      private DeployUtil() {
80      }
81  
82      private String _getResourcePath(String resource) throws IOException {
83          String tmpDir = SystemProperties.get(SystemProperties.TMP_DIR);
84  
85          File file = new File(
86              tmpDir + "/liferay/com/liferay/portal/deploy/dependencies/" +
87                  resource);
88  
89          File parentFile = file.getParentFile();
90  
91          if (parentFile != null) {
92              parentFile.mkdirs();
93          }
94  
95          byte[] byteArray = FileUtil.getBytes(
96              getClass().getResourceAsStream("dependencies/" + resource));
97  
98          OutputStream os = new FileOutputStream(file);
99  
100         os.write(byteArray);
101 
102         return FileUtil.getAbsolutePath(file);
103     }
104 
105     private static DeployUtil _instance = new DeployUtil();
106 
107 }