001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.util;
022:
023: import com.germinus.easyconf.ComponentConfiguration;
024: import com.germinus.easyconf.ComponentProperties;
025: import com.germinus.easyconf.EasyConf;
026:
027: import com.liferay.portal.kernel.util.Validator;
028: import com.liferay.portal.model.Company;
029: import com.liferay.portal.service.CompanyLocalServiceUtil;
030:
031: import java.util.Enumeration;
032: import java.util.List;
033: import java.util.Map;
034: import java.util.Properties;
035:
036: /**
037: * <a href="ExtPropertiesLoader.java.html"><b><i>View Source</i></b></a>
038: *
039: * @author Brian Wing Shun Chan
040: *
041: */
042: public class ExtPropertiesLoader {
043:
044: public static ExtPropertiesLoader getInstance(String name) {
045: ExtPropertiesLoader props = (ExtPropertiesLoader) _propsPool
046: .get(name);
047:
048: if (props == null) {
049: props = new ExtPropertiesLoader(name);
050:
051: _propsPool.put(name, props);
052: }
053:
054: return props;
055: }
056:
057: public static ExtPropertiesLoader getInstance(String name,
058: long companyId) {
059: String key = name + _COMPANY_ID_SEPARATOR + companyId;
060:
061: ExtPropertiesLoader props = (ExtPropertiesLoader) _propsPool
062: .get(key);
063:
064: if (props == null) {
065: props = new ExtPropertiesLoader(name, companyId);
066:
067: _propsPool.put(key, props);
068: }
069:
070: return props;
071: }
072:
073: public boolean containsKey(String key) {
074: return getComponentProperties().containsKey(key);
075: }
076:
077: public String get(String key) {
078: return getComponentProperties().getString(key);
079: }
080:
081: public void set(String key, String value) {
082: getComponentProperties().setProperty(key, value);
083: }
084:
085: public String[] getArray(String key) {
086: String[] array = getComponentProperties().getStringArray(key);
087:
088: if (array == null) {
089: return new String[0];
090: } else if (array.length > 0) {
091:
092: // Commons Configuration parses an empty property into a String
093: // array with one String containing one space. It also leaves a
094: // trailing array member if you set a property in more than one
095: // line.
096:
097: if (Validator.isNull(array[array.length - 1])) {
098: String[] subArray = new String[array.length - 1];
099:
100: System
101: .arraycopy(array, 0, subArray, 0,
102: 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()
126: .propertyNames();
127:
128: while (enu.hasMoreElements()) {
129: String key = (String) enu.nextElement();
130:
131: props.setProperty(key, componentProps.getString(key));
132: }
133:
134: return props;
135: }
136:
137: public ComponentProperties getComponentProperties() {
138: return _conf.getProperties();
139: }
140:
141: private ExtPropertiesLoader(String name) {
142: _conf = EasyConf.getConfiguration(name);
143:
144: _printSources(name);
145: }
146:
147: private ExtPropertiesLoader(String name, long companyId) {
148: String webId = null;
149:
150: if (companyId > 0) {
151: try {
152: Company company = CompanyLocalServiceUtil
153: .getCompanyById(companyId);
154:
155: webId = company.getWebId();
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 += " for {companyId=" + companyId + ", webId="
179: + 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: }
|