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          // Set the portal property "liferay.home" as a system property as well
92          // so it can be referenced by Ehcache.
93  
94          SystemProperties.set(
95              PropsKeys.LIFERAY_HOME, _get(PropsKeys.LIFERAY_HOME));
96  
97          // Set the portal property "resource.repositories.root" for backwards
98          // compatibility.
99  
100         SystemProperties.set(
101             PropsKeys.RESOURCE_REPOSITORIES_ROOT,
102             _get(PropsKeys.RESOURCE_REPOSITORIES_ROOT));
103     }
104 
105     private void _addProperties(Properties properties) {
106         _configuration.addProperties(properties);
107     }
108 
109     private boolean _contains(String key) {
110         return _configuration.contains(key);
111     }
112 
113     private String _get(String key) {
114         return _configuration.get(key);
115     }
116 
117     private String _get(String key, Filter filter) {
118         return _configuration.get(key, filter);
119     }
120 
121     private String[] _getArray(String key) {
122         return _configuration.getArray(key);
123     }
124 
125     private String[] _getArray(String key, Filter filter) {
126         return _configuration.getArray(key, filter);
127     }
128 
129     private String _getDefaultLiferayHome() {
130         String defaultLiferayHome = null;
131 
132         if (ServerDetector.isGeronimo()) {
133             defaultLiferayHome =
134                 SystemProperties.get("org.apache.geronimo.base.dir") + "/..";
135         }
136         else if (ServerDetector.isGlassfish()) {
137             defaultLiferayHome =
138                 SystemProperties.get("com.sun.aas.installRoot") + "/..";
139         }
140         else if (ServerDetector.isJBoss()) {
141             defaultLiferayHome =
142                 SystemProperties.get("jboss.server.home.dir") + "/..";
143         }
144         else if (ServerDetector.isJOnAS()) {
145             defaultLiferayHome = SystemProperties.get("jonas.base") + "/..";
146         }
147         else if (ServerDetector.isWebLogic()) {
148             defaultLiferayHome =
149                 SystemProperties.get("env.DOMAIN_HOME") + "/..";
150         }
151         else if (ServerDetector.isJetty()) {
152             defaultLiferayHome = SystemProperties.get("jetty.home") + "/..";
153         }
154         else if (ServerDetector.isResin()) {
155             defaultLiferayHome = SystemProperties.get("resin.home") + "/..";
156         }
157         else if (ServerDetector.isTomcat()) {
158             defaultLiferayHome = SystemProperties.get("catalina.base") + "/..";
159         }
160         else {
161             defaultLiferayHome = SystemProperties.get("user.home") + "/liferay";
162         }
163 
164         defaultLiferayHome = StringUtil.replace(
165             defaultLiferayHome, StringPool.BACK_SLASH, StringPool.SLASH);
166 
167         defaultLiferayHome = StringUtil.replace(
168             defaultLiferayHome, StringPool.DOUBLE_SLASH, StringPool.SLASH);
169 
170         if (defaultLiferayHome.endsWith("/..")) {
171             int pos = defaultLiferayHome.lastIndexOf(
172                 StringPool.SLASH, defaultLiferayHome.length() - 4);
173 
174             if (pos != -1) {
175                 defaultLiferayHome = defaultLiferayHome.substring(0, pos);
176             }
177         }
178 
179         return defaultLiferayHome;
180     }
181 
182     private Properties _getProperties() {
183         return _configuration.getProperties();
184     }
185 
186     private Properties _getProperties(String prefix, boolean removePrefix) {
187         return _configuration.getProperties(prefix, removePrefix);
188     }
189 
190     private void _removeProperties(Properties properties) {
191         _configuration.removeProperties(properties);
192     }
193 
194     private void _set(String key, String value) {
195         _configuration.set(key, value);
196     }
197 
198     private static PropsUtil _instance = new PropsUtil();
199 
200     private Configuration _configuration;
201 
202 }