1   /**
2    * Copyright (c) 2000-2009 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.util;
24  
25  import com.liferay.portal.configuration.ConfigurationImpl;
26  import com.liferay.portal.kernel.configuration.Configuration;
27  import com.liferay.portal.kernel.configuration.Filter;
28  import com.liferay.portal.kernel.util.ServerDetector;
29  import com.liferay.portal.kernel.util.StringPool;
30  import com.liferay.portal.kernel.util.StringUtil;
31  import com.liferay.util.SystemProperties;
32  
33  import java.util.Properties;
34  
35  /**
36   * <a href="PropsUtil.java.html"><b><i>View Source</i></b></a>
37   *
38   * @author Brian Wing Shun Chan
39   *
40   */
41  public class PropsUtil {
42  
43      public static void addProperties(Properties properties) {
44          _instance._addProperties(properties);
45      }
46  
47      public static boolean contains(String key) {
48          return _instance._contains(key);
49      }
50  
51      public static String get(String key) {
52          return _instance._get(key);
53      }
54  
55      public static String get(String key, Filter filter) {
56          return _instance._get(key, filter);
57      }
58  
59      public static String[] getArray(String key) {
60          return _instance._getArray(key);
61      }
62  
63      public static String[] getArray(String key, Filter filter) {
64          return _instance._getArray(key, filter);
65      }
66  
67      public static Properties getProperties() {
68          return _instance._getProperties();
69      }
70  
71      public static Properties getProperties(
72          String prefix, boolean removePrefix) {
73  
74          return _instance._getProperties(prefix, removePrefix);
75      }
76  
77      public static void removeProperties(Properties properties) {
78          _instance._removeProperties(properties);
79      }
80  
81      public static void set(String key, String value) {
82          _instance._set(key, value);
83      }
84  
85      private PropsUtil() {
86          SystemProperties.set("default.liferay.home", _getDefaultLiferayHome());
87  
88          _configuration = new ConfigurationImpl(
89              PropsUtil.class.getClassLoader(), PropsFiles.PORTAL);
90  
91          String liferayHome = _get(PropsKeys.LIFERAY_HOME);
92  
93          SystemProperties.set(
94              "ehcache.disk.store.dir", liferayHome + "/data/ehcache");
95      }
96  
97      private void _addProperties(Properties properties) {
98          _configuration.addProperties(properties);
99      }
100 
101     private boolean _contains(String key) {
102         return _configuration.contains(key);
103     }
104 
105     private String _get(String key) {
106         return _configuration.get(key);
107     }
108 
109     private String _get(String key, Filter filter) {
110         return _configuration.get(key, filter);
111     }
112 
113     private String[] _getArray(String key) {
114         return _configuration.getArray(key);
115     }
116 
117     private String[] _getArray(String key, Filter filter) {
118         return _configuration.getArray(key, filter);
119     }
120 
121     private String _getDefaultLiferayHome() {
122         String defaultLiferayHome = null;
123 
124         if (ServerDetector.isGeronimo()) {
125             defaultLiferayHome =
126                 SystemProperties.get("org.apache.geronimo.base.dir") + "/..";
127         }
128         else if (ServerDetector.isGlassfish()) {
129             defaultLiferayHome =
130                 SystemProperties.get("com.sun.aas.installRoot") + "/..";
131         }
132         else if (ServerDetector.isJBoss()) {
133             defaultLiferayHome = SystemProperties.get("jboss.home.dir") + "/..";
134         }
135         else if (ServerDetector.isJOnAS()) {
136             defaultLiferayHome = SystemProperties.get("jonas.base") + "/..";
137         }
138         else if (ServerDetector.isWebLogic()) {
139             defaultLiferayHome =
140                 SystemProperties.get("env.DOMAIN_HOME") + "/..";
141         }
142         else if (ServerDetector.isJetty()) {
143             defaultLiferayHome = SystemProperties.get("jetty.home") + "/..";
144         }
145         else if (ServerDetector.isResin()) {
146             defaultLiferayHome = SystemProperties.get("resin.home") + "/..";
147         }
148         else if (ServerDetector.isTomcat()) {
149             defaultLiferayHome = SystemProperties.get("catalina.base") + "/..";
150         }
151         else {
152             defaultLiferayHome = SystemProperties.get("user.home") + "/liferay";
153         }
154 
155         defaultLiferayHome = StringUtil.replace(
156             defaultLiferayHome, StringPool.BACK_SLASH, StringPool.SLASH);
157 
158         defaultLiferayHome = StringUtil.replace(
159             defaultLiferayHome, StringPool.DOUBLE_SLASH, StringPool.SLASH);
160 
161         if (defaultLiferayHome.endsWith("/..")) {
162             int pos = defaultLiferayHome.lastIndexOf(
163                 StringPool.SLASH, defaultLiferayHome.length() - 4);
164 
165             if (pos != -1) {
166                 defaultLiferayHome = defaultLiferayHome.substring(0, pos);
167             }
168         }
169 
170         return defaultLiferayHome;
171     }
172 
173     private Properties _getProperties() {
174         return _configuration.getProperties();
175     }
176 
177     private Properties _getProperties(String prefix, boolean removePrefix) {
178         return _configuration.getProperties(prefix, removePrefix);
179     }
180 
181     private void _removeProperties(Properties properties) {
182         _configuration.removeProperties(properties);
183     }
184 
185     private void _set(String key, String value) {
186         _configuration.set(key, value);
187     }
188 
189     private static PropsUtil _instance = new PropsUtil();
190 
191     private Configuration _configuration;
192 
193 }