001: package com.sun.syndication.io.impl;
002:
003: import java.io.IOException;
004: import java.io.InputStream;
005: import java.net.URL;
006: import java.util.*;
007:
008: /**
009: * Properties loader that aggregates a master properties file and several extra properties files,
010: * all from the current classpath.
011: * <P>
012: * The master properties file has to be in a distinct location than the extra properties files.
013: * First the master properties file is loaded, then all the extra properties files in their order
014: * of appearance in the classpath.
015: * <P>
016: * Current use cases (plugin manager for parsers/converters/generators for feeds and modules
017: * and date formats) assume properties are list of tokens, that why the only method to get
018: * property values is the getTokenizedProperty().
019: * <p>
020: *
021: * @author Alejandro Abdelnur
022: *
023: */
024: public class PropertiesLoader {
025:
026: private static final String MASTER_PLUGIN_FILE = "com/sun/syndication/rome.properties";
027: private static final String EXTRA_PLUGIN_FILE = "rome.properties";
028:
029: private static PropertiesLoader PROPERTIES_LOADER;
030:
031: static {
032: try {
033: PROPERTIES_LOADER = new PropertiesLoader(
034: MASTER_PLUGIN_FILE, EXTRA_PLUGIN_FILE);
035: } catch (IOException ioex) {
036: throw new RuntimeException(ioex.getMessage(), ioex);
037: }
038: }
039:
040: /**
041: * Returns the PropertiesLoader singleton used by ROME to load plugin components.
042: *
043: * @return PropertiesLoader singleton.
044: *
045: */
046: public static PropertiesLoader getPropertiesLoader() {
047: return PROPERTIES_LOADER;
048: }
049:
050: private Properties[] _properties;
051:
052: /**
053: * Creates a PropertiesLoader.
054: * <p>
055: * @param masterFileLocation master file location, there must be only one.
056: * @param extraFileLocation extra file location, there may be many.
057: * @throws IOException thrown if one of the properties file could not be read.
058: *
059: */
060: private PropertiesLoader(String masterFileLocation,
061: String extraFileLocation) throws IOException {
062: List propertiesList = new ArrayList();
063: ClassLoader classLoader = PluginManager.class.getClassLoader();
064:
065: try {
066: InputStream is = classLoader
067: .getResourceAsStream(masterFileLocation);
068: Properties p = new Properties();
069: p.load(is);
070: is.close();
071: propertiesList.add(p);
072: } catch (IOException ioex) {
073: IOException ex = new IOException(
074: "could not load ROME master plugins file ["
075: + masterFileLocation + "], "
076: + ioex.getMessage());
077: ex.setStackTrace(ioex.getStackTrace());
078: throw ex;
079: }
080:
081: Enumeration urls = classLoader.getResources(extraFileLocation);
082: while (urls.hasMoreElements()) {
083: URL url = (URL) urls.nextElement();
084: Properties p = new Properties();
085: try {
086: InputStream is = url.openStream();
087: p.load(is);
088: is.close();
089: } catch (IOException ioex) {
090: IOException ex = new IOException(
091: "could not load ROME extensions plugins file ["
092: + url.toString() + "], "
093: + ioex.getMessage());
094: ex.setStackTrace(ioex.getStackTrace());
095: throw ex;
096: }
097: propertiesList.add(p);
098: }
099:
100: _properties = new Properties[propertiesList.size()];
101: propertiesList.toArray(_properties);
102: }
103:
104: /**
105: * Returns an array of tokenized values stored under a property key in all properties files.
106: * If the master file has this property its tokens will be the first ones in the array.
107: * <p>
108: * @param key property key to retrieve values
109: * @param separator String with all separator characters to tokenize from the values in all
110: * properties files.
111: * @return all the tokens for the given property key from all the properties files.
112: *
113: */
114: public String[] getTokenizedProperty(String key, String separator) {
115: List entriesList = new ArrayList();
116: for (int i = 0; i < _properties.length; i++) {
117: String values = _properties[i].getProperty(key);
118: if (values != null) {
119: StringTokenizer st = new StringTokenizer(values,
120: separator);
121: while (st.hasMoreTokens()) {
122: String token = st.nextToken();
123: entriesList.add(token);
124: }
125: }
126: }
127: String[] entries = new String[entriesList.size()];
128: entriesList.toArray(entries);
129: return entries;
130: }
131:
132: /**
133: * Returns an array of values stored under a property key in all properties files.
134: * If the master file has this property it will be the first ones in the array.
135: * <p>
136: * @param key property key to retrieve values
137: * @return all the values for the given property key from all the properties files.
138: *
139: */
140: public String[] getProperty(String key) {
141: List entriesList = new ArrayList();
142: for (int i = 0; i < _properties.length; i++) {
143: String values = _properties[i].getProperty(key);
144: if (values != null) {
145: entriesList.add(values);
146: }
147: }
148: String[] entries = new String[entriesList.size()];
149: entriesList.toArray(entries);
150: return entries;
151: }
152:
153: }
|