1
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
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
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
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 }