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.util;
24  
25  import com.liferay.portal.kernel.util.GetterUtil;
26  import com.liferay.portal.kernel.util.PropertiesUtil;
27  import com.liferay.portal.kernel.util.StringUtil;
28  import com.liferay.portal.kernel.util.SystemEnv;
29  import com.liferay.portal.kernel.util.Validator;
30  
31  import java.io.InputStream;
32  
33  import java.net.URL;
34  
35  import java.util.Enumeration;
36  import java.util.Map;
37  import java.util.Properties;
38  import java.util.concurrent.ConcurrentHashMap;
39  
40  /**
41   * <a href="SystemProperties.java.html"><b><i>View Source</i></b></a>
42   *
43   * @author Brian Wing Shun Chan
44   * @author Mirco Tamburini
45   * @author Brett Randall
46   *
47   */
48  public class SystemProperties {
49  
50      public static final String SYSTEM_PROPERTIES_LOAD =
51          "system.properties.load";
52  
53      public static final String SYSTEM_PROPERTIES_FINAL =
54          "system.properties.final";
55  
56      public static final String TMP_DIR = "java.io.tmpdir";
57  
58      public static String get(String key) {
59          String value = _instance._props.get(key);
60  
61          if (value == null) {
62              value = System.getProperty(key);
63          }
64  
65          return value;
66      }
67  
68      public static void set(String key, String value) {
69          System.setProperty(key, value);
70  
71          _instance._props.put(key, value);
72      }
73  
74      public static String[] getArray(String key) {
75          String value = get(key);
76  
77          if (value == null) {
78              return new String[0];
79          }
80          else {
81              return StringUtil.split(value);
82          }
83      }
84  
85      public static Properties getProperties() {
86          return PropertiesUtil.fromMap(_instance._props);
87      }
88  
89      private SystemProperties() {
90          Properties p = new Properties();
91  
92          ClassLoader classLoader = getClass().getClassLoader();
93  
94          // system.properties
95  
96          try {
97              URL url = classLoader.getResource("system.properties");
98  
99              if (url != null) {
100                 InputStream is = url.openStream();
101 
102                 p.load(is);
103 
104                 is.close();
105 
106                 System.out.println("Loading " + url);
107             }
108         }
109         catch (Exception e) {
110             e.printStackTrace();
111         }
112 
113         // system-ext.properties
114 
115         try {
116             URL url = classLoader.getResource("system-ext.properties");
117 
118             if (url != null) {
119                 InputStream is = url.openStream();
120 
121                 p.load(is);
122 
123                 is.close();
124 
125                 System.out.println("Loading " + url);
126             }
127         }
128         catch (Exception e) {
129             e.printStackTrace();
130         }
131 
132         // Set environment properties
133 
134         SystemEnv.setProperties(p);
135 
136         // Set system properties
137 
138         boolean systemPropertiesLoad = GetterUtil.getBoolean(
139             System.getProperty(SYSTEM_PROPERTIES_LOAD), true);
140 
141         boolean systemPropertiesFinal = GetterUtil.getBoolean(
142             System.getProperty(SYSTEM_PROPERTIES_FINAL), true);
143 
144         if (systemPropertiesLoad) {
145             Enumeration<String> enu = (Enumeration<String>)p.propertyNames();
146 
147             while (enu.hasMoreElements()) {
148                 String key = enu.nextElement();
149 
150                 if (systemPropertiesFinal ||
151                     Validator.isNull(System.getProperty(key))) {
152 
153                     System.setProperty(key, p.getProperty(key));
154                 }
155             }
156         }
157 
158         _props = new ConcurrentHashMap<String, String>();
159 
160         // Use a fast concurrent hash map implementation instead of the slower
161         // java.util.Properties
162 
163         PropertiesUtil.fromProperties(p, _props);
164     }
165 
166     private static SystemProperties _instance = new SystemProperties();
167 
168     private Map<String, String> _props;
169 
170 }