1   /**
2    * Copyright (c) 2000-2008 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          System.setProperty(key, value);
71  
72          _instance._props.put(key, value);
73      }
74  
75      public static String[] getArray(String key) {
76          String value = get(key);
77  
78          if (value == null) {
79              return new String[0];
80          }
81          else {
82              return StringUtil.split(value);
83          }
84      }
85  
86      public static Properties getProperties() {
87          return PropertiesUtil.fromMap(_instance._props);
88      }
89  
90      private SystemProperties() {
91          Properties p = new Properties();
92  
93          ClassLoader classLoader = getClass().getClassLoader();
94  
95          // system.properties
96  
97          try {
98              URL url = classLoader.getResource("system.properties");
99  
100             if (url != null) {
101                 InputStream is = url.openStream();
102 
103                 p.load(is);
104 
105                 is.close();
106 
107                 System.out.println("Loading " + url);
108             }
109         }
110         catch (Exception e) {
111             e.printStackTrace();
112         }
113 
114         // system-ext.properties
115 
116         try {
117             URL url = classLoader.getResource("system-ext.properties");
118 
119             if (url != null) {
120                 InputStream is = url.openStream();
121 
122                 p.load(is);
123 
124                 is.close();
125 
126                 System.out.println("Loading " + url);
127             }
128         }
129         catch (Exception e) {
130             e.printStackTrace();
131         }
132 
133         // Set environment properties
134 
135         SystemEnv.setProperties(p);
136 
137         // Set system properties
138 
139         boolean systemPropertiesLoad = GetterUtil.getBoolean(
140             System.getProperty(SYSTEM_PROPERTIES_LOAD), true);
141 
142         boolean systemPropertiesFinal = GetterUtil.getBoolean(
143             System.getProperty(SYSTEM_PROPERTIES_FINAL), true);
144 
145         if (systemPropertiesLoad) {
146             Enumeration enu = p.propertyNames();
147 
148             while (enu.hasMoreElements()) {
149                 String key = (String)enu.nextElement();
150 
151                 if (systemPropertiesFinal ||
152                     Validator.isNull(System.getProperty(key))) {
153 
154                     System.setProperty(key, (String)p.get(key));
155                 }
156             }
157         }
158 
159         // You cannot call CollectionFactory directly because CollectionFactory
160         // also references SystemProperties
161 
162         //_props = CollectionFactory.getSyncHashMap();
163         _props = new ConcurrentHashMap();
164 
165         // Use a fast concurrent hash map implementation instead of the slower
166         // java.util.Properties
167 
168         PropertiesUtil.fromProperties(p, _props);
169     }
170 
171     private static SystemProperties _instance = new SystemProperties();
172 
173     private Map _props;
174 
175 }