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 = SystemProperties.get("jboss.home.dir") + "/..";
142         }
143         else if (ServerDetector.isJOnAS()) {
144             defaultLiferayHome = SystemProperties.get("jonas.base") + "/..";
145         }
146         else if (ServerDetector.isWebLogic()) {
147             defaultLiferayHome =
148                 SystemProperties.get("env.DOMAIN_HOME") + "/..";
149         }
150         else if (ServerDetector.isJetty()) {
151             defaultLiferayHome = SystemProperties.get("jetty.home") + "/..";
152         }
153         else if (ServerDetector.isResin()) {
154             defaultLiferayHome = SystemProperties.get("resin.home") + "/..";
155         }
156         else if (ServerDetector.isTomcat()) {
157             defaultLiferayHome = SystemProperties.get("catalina.base") + "/..";
158         }
159         else {
160             defaultLiferayHome = SystemProperties.get("user.home") + "/liferay";
161         }
162 
163         defaultLiferayHome = StringUtil.replace(
164             defaultLiferayHome, StringPool.BACK_SLASH, StringPool.SLASH);
165 
166         defaultLiferayHome = StringUtil.replace(
167             defaultLiferayHome, StringPool.DOUBLE_SLASH, StringPool.SLASH);
168 
169         if (defaultLiferayHome.endsWith("/..")) {
170             int pos = defaultLiferayHome.lastIndexOf(
171                 StringPool.SLASH, defaultLiferayHome.length() - 4);
172 
173             if (pos != -1) {
174                 defaultLiferayHome = defaultLiferayHome.substring(0, pos);
175             }
176         }
177 
178         return defaultLiferayHome;
179     }
180 
181     private Properties _getProperties() {
182         return _configuration.getProperties();
183     }
184 
185     private Properties _getProperties(String prefix, boolean removePrefix) {
186         return _configuration.getProperties(prefix, removePrefix);
187     }
188 
189     private void _removeProperties(Properties properties) {
190         _configuration.removeProperties(properties);
191     }
192 
193     private void _set(String key, String value) {
194         _configuration.set(key, value);
195     }
196 
197     private static PropsUtil _instance = new PropsUtil();
198 
199     private Configuration _configuration;
200 
201 }