1 package com.sun.syndication.io.impl; 2 3 import java.io.IOException; 4 import java.io.InputStream; 5 import java.net.URL; 6 import java.util.*; 7 8 /*** 9 * Properties loader that aggregates a master properties file and several extra properties files, 10 * all from the current classpath. 11 * <P> 12 * The master properties file has to be in a distinct location than the extra properties files. 13 * First the master properties file is loaded, then all the extra properties files in their order 14 * of appearance in the classpath. 15 * <P> 16 * Current use cases (plugin manager for parsers/converters/generators for feeds and modules 17 * and date formats) assume properties are list of tokens, that why the only method to get 18 * property values is the getTokenizedProperty(). 19 * <p> 20 * 21 * @author Alejandro Abdelnur 22 * 23 */ 24 public class PropertiesLoader { 25 26 private static final String MASTER_PLUGIN_FILE = "com/sun/syndication/rome.properties"; 27 private static final String EXTRA_PLUGIN_FILE = "rome.properties"; 28 29 private static PropertiesLoader PROPERTIES_LOADER; 30 31 static { 32 try { 33 PROPERTIES_LOADER = new PropertiesLoader(MASTER_PLUGIN_FILE,EXTRA_PLUGIN_FILE); 34 } 35 catch (IOException ioex) { 36 throw new RuntimeException(ioex.getMessage(),ioex); 37 } 38 } 39 40 /*** 41 * Returns the PropertiesLoader singleton used by ROME to load plugin components. 42 * 43 * @return PropertiesLoader singleton. 44 * 45 */ 46 public static PropertiesLoader getPropertiesLoader() { 47 return PROPERTIES_LOADER; 48 } 49 50 private Properties[] _properties; 51 52 /*** 53 * Creates a PropertiesLoader. 54 * <p> 55 * @param masterFileLocation master file location, there must be only one. 56 * @param extraFileLocation extra file location, there may be many. 57 * @throws IOException thrown if one of the properties file could not be read. 58 * 59 */ 60 private PropertiesLoader(String masterFileLocation,String extraFileLocation) throws IOException { 61 List propertiesList = new ArrayList(); 62 ClassLoader classLoader = PluginManager.class.getClassLoader(); 63 64 try { 65 InputStream is = classLoader.getResourceAsStream(masterFileLocation); 66 Properties p = new Properties(); 67 p.load(is); 68 is.close(); 69 propertiesList.add(p); 70 } 71 catch (IOException ioex) { 72 IOException ex = new IOException("could not load ROME master plugins file ["+masterFileLocation+"], "+ 73 ioex.getMessage()); 74 ex.setStackTrace(ioex.getStackTrace()); 75 throw ex; 76 } 77 78 Enumeration urls = classLoader.getResources(extraFileLocation); 79 while (urls.hasMoreElements()) { 80 URL url = (URL) urls.nextElement(); 81 Properties p = new Properties(); 82 try { 83 InputStream is = url.openStream(); 84 p.load(is); 85 is.close(); 86 } 87 catch (IOException ioex) { 88 IOException ex = new IOException("could not load ROME extensions plugins file ["+url.toString()+"], "+ 89 ioex.getMessage()); 90 ex.setStackTrace(ioex.getStackTrace()); 91 throw ex; 92 } 93 propertiesList.add(p); 94 } 95 96 _properties = new Properties[propertiesList.size()]; 97 propertiesList.toArray(_properties); 98 } 99 100 /*** 101 * Returns an array of tokenized values stored under a property key in all properties files. 102 * If the master file has this property its tokens will be the first ones in the array. 103 * <p> 104 * @param key property key to retrieve values 105 * @param separator String with all separator characters to tokenize from the values in all 106 * properties files. 107 * @return all the tokens for the given property key from all the properties files. 108 * 109 */ 110 public String[] getTokenizedProperty(String key,String separator) { 111 List entriesList = new ArrayList(); 112 for (int i=0;i<_properties.length;i++) { 113 String values = _properties[i].getProperty(key); 114 if (values!=null) { 115 StringTokenizer st = new StringTokenizer(values,separator); 116 while (st.hasMoreTokens()) { 117 String token = st.nextToken(); 118 entriesList.add(token); 119 } 120 } 121 } 122 String[] entries = new String[entriesList.size()]; 123 entriesList.toArray(entries); 124 return entries; 125 } 126 127 /*** 128 * Returns an array of values stored under a property key in all properties files. 129 * If the master file has this property it will be the first ones in the array. 130 * <p> 131 * @param key property key to retrieve values 132 * @return all the values for the given property key from all the properties files. 133 * 134 */ 135 public String[] getProperty(String key) { 136 List entriesList = new ArrayList(); 137 for (int i=0;i<_properties.length;i++) { 138 String values = _properties[i].getProperty(key); 139 if (values!=null) { 140 entriesList.add(values); 141 } 142 } 143 String[] entries = new String[entriesList.size()]; 144 entriesList.toArray(entries); 145 return entries; 146 } 147 148 }