1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class SystemProperties {
48  
49      public static final String SYSTEM_PROPERTIES_LOAD =
50          "system.properties.load";
51  
52      public static final String SYSTEM_PROPERTIES_FINAL =
53          "system.properties.final";
54  
55      public static final String TMP_DIR = "java.io.tmpdir";
56  
57      public static String get(String key) {
58          String value = _instance._props.get(key);
59  
60          if (value == null) {
61              value = System.getProperty(key);
62          }
63  
64          return value;
65      }
66  
67      public static void set(String key, String value) {
68          System.setProperty(key, value);
69  
70          _instance._props.put(key, value);
71      }
72  
73      public static String[] getArray(String key) {
74          String value = get(key);
75  
76          if (value == null) {
77              return new String[0];
78          }
79          else {
80              return StringUtil.split(value);
81          }
82      }
83  
84      public static Properties getProperties() {
85          return PropertiesUtil.fromMap(_instance._props);
86      }
87  
88      private SystemProperties() {
89          Properties p = new Properties();
90  
91          ClassLoader classLoader = getClass().getClassLoader();
92  
93          // system.properties
94  
95          try {
96              URL url = classLoader.getResource("system.properties");
97  
98              if (url != null) {
99                  InputStream is = url.openStream();
100 
101                 p.load(is);
102 
103                 is.close();
104 
105                 System.out.println("Loading " + url);
106             }
107         }
108         catch (Exception e) {
109             e.printStackTrace();
110         }
111 
112         // system-ext.properties
113 
114         try {
115             URL url = classLoader.getResource("system-ext.properties");
116 
117             if (url != null) {
118                 InputStream is = url.openStream();
119 
120                 p.load(is);
121 
122                 is.close();
123 
124                 System.out.println("Loading " + url);
125             }
126         }
127         catch (Exception e) {
128             e.printStackTrace();
129         }
130 
131         // Set environment properties
132 
133         SystemEnv.setProperties(p);
134 
135         // Set system properties
136 
137         boolean systemPropertiesLoad = GetterUtil.getBoolean(
138             System.getProperty(SYSTEM_PROPERTIES_LOAD), true);
139 
140         boolean systemPropertiesFinal = GetterUtil.getBoolean(
141             System.getProperty(SYSTEM_PROPERTIES_FINAL), true);
142 
143         if (systemPropertiesLoad) {
144             Enumeration<String> enu = (Enumeration<String>)p.propertyNames();
145 
146             while (enu.hasMoreElements()) {
147                 String key = enu.nextElement();
148 
149                 if (systemPropertiesFinal ||
150                     Validator.isNull(System.getProperty(key))) {
151 
152                     System.setProperty(key, p.getProperty(key));
153                 }
154             }
155         }
156 
157         _props = new ConcurrentHashMap<String, String>();
158 
159         // Use a fast concurrent hash map implementation instead of the slower
160         // java.util.Properties
161 
162         PropertiesUtil.fromProperties(p, _props);
163     }
164 
165     private static SystemProperties _instance = new SystemProperties();
166 
167     private Map<String, String> _props;
168 
169 }