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.Enumeration;
34 import java.util.List;
35 import java.util.Map;
36 import java.util.Properties;
37
38
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
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
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 }