1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.util;
16  
17  import com.liferay.portal.kernel.util.GetterUtil;
18  import com.liferay.portal.kernel.util.PropertiesUtil;
19  import com.liferay.portal.kernel.util.StringUtil;
20  import com.liferay.portal.kernel.util.SystemEnv;
21  import com.liferay.portal.kernel.util.Validator;
22  
23  import java.io.InputStream;
24  
25  import java.net.URL;
26  
27  import java.util.Enumeration;
28  import java.util.Map;
29  import java.util.Properties;
30  import java.util.concurrent.ConcurrentHashMap;
31  
32  /**
33   * <a href="SystemProperties.java.html"><b><i>View Source</i></b></a>
34   *
35   * @author Brian Wing Shun Chan
36   * @author Mirco Tamburini
37   * @author Brett Randall
38   */
39  public class SystemProperties {
40  
41      public static final String SYSTEM_PROPERTIES_LOAD =
42          "system.properties.load";
43  
44      public static final String SYSTEM_PROPERTIES_FINAL =
45          "system.properties.final";
46  
47      public static final String TMP_DIR = "java.io.tmpdir";
48  
49      public static String get(String key) {
50          String value = _instance._props.get(key);
51  
52          if (value == null) {
53              value = System.getProperty(key);
54          }
55  
56          return value;
57      }
58  
59      public static void set(String key, String value) {
60          System.setProperty(key, value);
61  
62          _instance._props.put(key, value);
63      }
64  
65      public static String[] getArray(String key) {
66          String value = get(key);
67  
68          if (value == null) {
69              return new String[0];
70          }
71          else {
72              return StringUtil.split(value);
73          }
74      }
75  
76      public static Properties getProperties() {
77          return PropertiesUtil.fromMap(_instance._props);
78      }
79  
80      private SystemProperties() {
81          Properties p = new Properties();
82  
83          ClassLoader classLoader = getClass().getClassLoader();
84  
85          // system.properties
86  
87          try {
88              URL url = classLoader.getResource("system.properties");
89  
90              if (url != null) {
91                  InputStream is = url.openStream();
92  
93                  p.load(is);
94  
95                  is.close();
96  
97                  System.out.println("Loading " + url);
98              }
99          }
100         catch (Exception e) {
101             e.printStackTrace();
102         }
103 
104         // system-ext.properties
105 
106         try {
107             URL url = classLoader.getResource("system-ext.properties");
108 
109             if (url != null) {
110                 InputStream is = url.openStream();
111 
112                 p.load(is);
113 
114                 is.close();
115 
116                 System.out.println("Loading " + url);
117             }
118         }
119         catch (Exception e) {
120             e.printStackTrace();
121         }
122 
123         // Set environment properties
124 
125         SystemEnv.setProperties(p);
126 
127         // Set system properties
128 
129         boolean systemPropertiesLoad = GetterUtil.getBoolean(
130             System.getProperty(SYSTEM_PROPERTIES_LOAD), true);
131 
132         boolean systemPropertiesFinal = GetterUtil.getBoolean(
133             System.getProperty(SYSTEM_PROPERTIES_FINAL), true);
134 
135         if (systemPropertiesLoad) {
136             Enumeration<String> enu = (Enumeration<String>)p.propertyNames();
137 
138             while (enu.hasMoreElements()) {
139                 String key = enu.nextElement();
140 
141                 if (systemPropertiesFinal ||
142                     Validator.isNull(System.getProperty(key))) {
143 
144                     System.setProperty(key, p.getProperty(key));
145                 }
146             }
147         }
148 
149         _props = new ConcurrentHashMap<String, String>();
150 
151         // Use a fast concurrent hash map implementation instead of the slower
152         // java.util.Properties
153 
154         PropertiesUtil.fromProperties(p, _props);
155     }
156 
157     private static SystemProperties _instance = new SystemProperties();
158 
159     private Map<String, String> _props;
160 
161 }