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.germinus.easyconf.ComponentConfiguration;
26  import com.germinus.easyconf.ComponentProperties;
27  import com.germinus.easyconf.EasyConf;
28  
29  import com.liferay.portal.kernel.util.Validator;
30  import com.liferay.portal.model.Company;
31  import com.liferay.portal.service.CompanyLocalServiceUtil;
32  
33  import java.util.Enumeration;
34  import java.util.List;
35  import java.util.Map;
36  import java.util.Properties;
37  
38  /**
39   * <a href="ExtPropertiesLoader.java.html"><b><i>View Source</i></b></a>
40   *
41   * @author Brian Wing Shun Chan
42   *
43   */
44  public class ExtPropertiesLoader {
45  
46      public static ExtPropertiesLoader getInstance(String name) {
47          ExtPropertiesLoader props =
48              (ExtPropertiesLoader)_propsPool.get(name);
49  
50          if (props == null) {
51              props = new ExtPropertiesLoader(name);
52  
53              _propsPool.put(name, props);
54          }
55  
56          return props;
57      }
58  
59      public static ExtPropertiesLoader getInstance(String name, long companyId) {
60          String key = name + _COMPANY_ID_SEPARATOR + companyId;
61  
62          ExtPropertiesLoader props =
63              (ExtPropertiesLoader)_propsPool.get(key);
64  
65          if (props == null) {
66              props = new ExtPropertiesLoader(name, companyId);
67  
68              _propsPool.put(key, props);
69          }
70  
71          return props;
72      }
73  
74      public boolean containsKey(String key) {
75          return getComponentProperties().containsKey(key);
76      }
77  
78      public String get(String key) {
79          return getComponentProperties().getString(key);
80      }
81  
82      public void set(String key, String value) {
83          getComponentProperties().setProperty(key, value);
84      }
85  
86      public String[] getArray(String key) {
87          String[] array = getComponentProperties().getStringArray(key);
88  
89          if (array == null) {
90              return new String[0];
91          }
92          else if (array.length > 0) {
93  
94              // Commons Configuration parses an empty property into a String
95              // array with one String containing one space. It also leaves a
96              // trailing array member if you set a property in more than one
97              // line.
98  
99              if (Validator.isNull(array[array.length - 1])) {
100                 String[] subArray = new String[array.length - 1];
101 
102                 System.arraycopy(array, 0, subArray, 0, subArray.length);
103 
104                 array = subArray;
105             }
106         }
107 
108         return array;
109     }
110 
111     public Properties getProperties() {
112 
113         // For some strange reason, componentProperties.getProperties() returns
114         // values with spaces after commas. So a property setting of "xyz=1,2,3"
115         // actually returns "xyz=1, 2, 3". This can break applications that
116         // don't expect that extra space. However, getting the property value
117         // directly through componentProperties returns the correct value. This
118         // method fixes the weird behavior by returing properties with the
119         // correct values.
120 
121         Properties props = new Properties();
122 
123         ComponentProperties componentProps = getComponentProperties();
124 
125         Enumeration enu = componentProps.getProperties().propertyNames();
126 
127         while (enu.hasMoreElements()) {
128             String key = (String)enu.nextElement();
129 
130             props.setProperty(key, componentProps.getString(key));
131         }
132 
133         return props;
134     }
135 
136     public ComponentProperties getComponentProperties() {
137         return _conf.getProperties();
138     }
139 
140     private ExtPropertiesLoader(String name) {
141         _conf = EasyConf.getConfiguration(name);
142 
143         _printSources(name);
144     }
145 
146     private ExtPropertiesLoader(String name, long companyId) {
147         String webId = null;
148 
149         if (companyId > 0) {
150             try {
151                 Company company = CompanyLocalServiceUtil.getCompanyById(
152                     companyId);
153 
154                 webId = company.getWebId();
155             }
156             catch (Exception e) {
157             }
158         }
159 
160         _conf = EasyConf.getConfiguration(webId, name);
161 
162         _printSources(name, companyId, webId);
163     }
164 
165     private void _printSources(String name) {
166         _printSources(name, 0, null);
167     }
168 
169     private void _printSources(String name, long companyId, String webId) {
170         List sources = getComponentProperties().getLoadedSources();
171 
172         for (int i = sources.size() - 1; i >= 0; i--) {
173             String source = (String)sources.get(i);
174 
175             String info = "Loading " + source;
176 
177             if (companyId > 0) {
178                 info +=
179                     " for {companyId=" + companyId + ", webId=" + webId + "}";
180             }
181 
182             System.out.println(info);
183         }
184     }
185 
186     private static Map _propsPool = CollectionFactory.getSyncHashMap();
187 
188     private static final String _COMPANY_ID_SEPARATOR = "_COMPANY_ID_";
189 
190     private ComponentConfiguration _conf;
191 
192 }