1   /**
2    * Copyright (c) 2000-2007 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 edu.emory.mathcs.backport.java.util.concurrent.ConcurrentHashMap;
32  
33  import java.io.InputStream;
34  
35  import java.net.URL;
36  
37  import java.util.Enumeration;
38  import java.util.Map;
39  import java.util.Properties;
40  
41  /**
42   * <a href="SystemProperties.java.html"><b><i>View Source</i></b></a>
43   *
44   * @author Brian Wing Shun Chan
45   * @author Mirco Tamburini
46   * @author Brett Randall
47   *
48   */
49  public class SystemProperties {
50  
51      public static final String SYSTEM_PROPERTIES_LOAD =
52          "system.properties.load";
53  
54      public static final String SYSTEM_PROPERTIES_FINAL =
55          "system.properties.final";
56  
57      public static final String TMP_DIR = "java.io.tmpdir";
58  
59      public static String get(String key) {
60          String value = (String)_instance._props.get(key);
61  
62          if (value == null) {
63              value = System.getProperty(key);
64          }
65  
66          return value;
67      }
68  
69      public static void set(String key, String value) {
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 enu = p.propertyNames();
145 
146             while (enu.hasMoreElements()) {
147                 String key = (String)enu.nextElement();
148 
149                 if (systemPropertiesFinal ||
150                     Validator.isNull(System.getProperty(key))) {
151 
152                     System.setProperty(key, (String)p.get(key));
153                 }
154             }
155         }
156 
157         // You cannot call CollectionFactory directly because CollectionFactory
158         // also references SystemProperties
159 
160         //_props = CollectionFactory.getSyncHashMap();
161         _props = new ConcurrentHashMap();
162 
163         // Use a fast concurrent hash map implementation instead of the slower
164         // java.util.Properties
165 
166         PropertiesUtil.fromProperties(p, _props);
167     }
168 
169     private static SystemProperties _instance = new SystemProperties();
170 
171     private Map _props;
172 
173 }