1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.util;
21  
22  import com.liferay.portal.kernel.util.GetterUtil;
23  import com.liferay.portal.kernel.util.PropertiesUtil;
24  import com.liferay.portal.kernel.util.StringUtil;
25  import com.liferay.portal.kernel.util.SystemEnv;
26  import com.liferay.portal.kernel.util.Validator;
27  
28  import java.io.InputStream;
29  
30  import java.net.URL;
31  
32  import java.util.Enumeration;
33  import java.util.Map;
34  import java.util.Properties;
35  import java.util.concurrent.ConcurrentHashMap;
36  
37  /**
38   * <a href="SystemProperties.java.html"><b><i>View Source</i></b></a>
39   *
40   * @author Brian Wing Shun Chan
41   * @author Mirco Tamburini
42   * @author Brett Randall
43   *
44   */
45  public class SystemProperties {
46  
47      public static final String SYSTEM_PROPERTIES_LOAD =
48          "system.properties.load";
49  
50      public static final String SYSTEM_PROPERTIES_FINAL =
51          "system.properties.final";
52  
53      public static final String TMP_DIR = "java.io.tmpdir";
54  
55      public static String get(String key) {
56          String value = _instance._props.get(key);
57  
58          if (value == null) {
59              value = System.getProperty(key);
60          }
61  
62          return value;
63      }
64  
65      public static void set(String key, String value) {
66          System.setProperty(key, value);
67  
68          _instance._props.put(key, value);
69      }
70  
71      public static String[] getArray(String key) {
72          String value = get(key);
73  
74          if (value == null) {
75              return new String[0];
76          }
77          else {
78              return StringUtil.split(value);
79          }
80      }
81  
82      public static Properties getProperties() {
83          return PropertiesUtil.fromMap(_instance._props);
84      }
85  
86      private SystemProperties() {
87          Properties p = new Properties();
88  
89          ClassLoader classLoader = getClass().getClassLoader();
90  
91          // system.properties
92  
93          try {
94              URL url = classLoader.getResource("system.properties");
95  
96              if (url != null) {
97                  InputStream is = url.openStream();
98  
99                  p.load(is);
100 
101                 is.close();
102 
103                 System.out.println("Loading " + url);
104             }
105         }
106         catch (Exception e) {
107             e.printStackTrace();
108         }
109 
110         // system-ext.properties
111 
112         try {
113             URL url = classLoader.getResource("system-ext.properties");
114 
115             if (url != null) {
116                 InputStream is = url.openStream();
117 
118                 p.load(is);
119 
120                 is.close();
121 
122                 System.out.println("Loading " + url);
123             }
124         }
125         catch (Exception e) {
126             e.printStackTrace();
127         }
128 
129         // Set environment properties
130 
131         SystemEnv.setProperties(p);
132 
133         // Set system properties
134 
135         boolean systemPropertiesLoad = GetterUtil.getBoolean(
136             System.getProperty(SYSTEM_PROPERTIES_LOAD), true);
137 
138         boolean systemPropertiesFinal = GetterUtil.getBoolean(
139             System.getProperty(SYSTEM_PROPERTIES_FINAL), true);
140 
141         if (systemPropertiesLoad) {
142             Enumeration<String> enu = (Enumeration<String>)p.propertyNames();
143 
144             while (enu.hasMoreElements()) {
145                 String key = enu.nextElement();
146 
147                 if (systemPropertiesFinal ||
148                     Validator.isNull(System.getProperty(key))) {
149 
150                     System.setProperty(key, p.getProperty(key));
151                 }
152             }
153         }
154 
155         _props = new ConcurrentHashMap<String, String>();
156 
157         // Use a fast concurrent hash map implementation instead of the slower
158         // java.util.Properties
159 
160         PropertiesUtil.fromProperties(p, _props);
161     }
162 
163     private static SystemProperties _instance = new SystemProperties();
164 
165     private Map<String, String> _props;
166 
167 }