001: /*
002: * Copyright 2007 The Kuali Foundation.
003: *
004: * Licensed under the Educational Community License, Version 1.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.opensource.org/licenses/ecl1.php
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.kuali.kfs.context;
017:
018: import java.io.IOException;
019: import java.io.InputStream;
020: import java.util.Arrays;
021: import java.util.List;
022: import java.util.Properties;
023:
024: import org.springframework.beans.factory.FactoryBean;
025: import org.springframework.core.io.DefaultResourceLoader;
026:
027: import edu.iu.uis.eden.util.ClassLoaderUtils;
028:
029: public class PropertyLoadingFactoryBean implements FactoryBean {
030: private static final String PROPERTY_FILE_NAMES_KEY = "property.files";
031: private static final String PROPERTY_TEST_FILE_NAMES_KEY = "property.test.files";
032: private static final String CONFIGURATION_FILE_NAME = "configuration";
033: private static final Properties BASE_PROPERTIES = new Properties();
034: private boolean testMode;
035:
036: public Object getObject() throws Exception {
037: loadBaseProperties();
038: loadPropertyList(PROPERTY_FILE_NAMES_KEY);
039: if (testMode) {
040: loadPropertyList(PROPERTY_TEST_FILE_NAMES_KEY);
041: }
042: return BASE_PROPERTIES;
043: }
044:
045: public Class getObjectType() {
046: return Properties.class;
047: }
048:
049: public boolean isSingleton() {
050: return true;
051: }
052:
053: private void loadPropertyList(String listPropertyName) {
054: for (String propertyFileName : getBaseListProperty(listPropertyName)) {
055: loadProperties(propertyFileName);
056: }
057: }
058:
059: private static void loadProperties(String propertyFileName) {
060: InputStream propertyFileInputStream = null;
061: try {
062: try {
063: propertyFileInputStream = new DefaultResourceLoader(
064: ClassLoaderUtils.getDefaultClassLoader())
065: .getResource(propertyFileName).getInputStream();
066: BASE_PROPERTIES.load(propertyFileInputStream);
067: } finally {
068: if (propertyFileInputStream != null) {
069: propertyFileInputStream.close();
070: }
071: }
072: } catch (IOException e) {
073: throw new RuntimeException(
074: "PropertyLoadingFactoryBean unable to load property file: "
075: + propertyFileName);
076: }
077: }
078:
079: public static String getBaseProperty(String propertyName) {
080: loadBaseProperties();
081: return BASE_PROPERTIES.getProperty(propertyName);
082: }
083:
084: protected static List<String> getBaseListProperty(
085: String propertyName) {
086: loadBaseProperties();
087: return Arrays.asList(BASE_PROPERTIES.getProperty(propertyName)
088: .split(","));
089: }
090:
091: protected static void loadBaseProperties() {
092: if (BASE_PROPERTIES.isEmpty()) {
093: loadProperties(new StringBuffer("classpath:").append(
094: CONFIGURATION_FILE_NAME).append(".properties")
095: .toString());
096: }
097: }
098:
099: public void setTestMode(boolean testMode) {
100: this.testMode = testMode;
101: }
102: }
|