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