1 10 package org.mmbase.util.xml; 11 12 import java.util.*; 13 import java.net.URL ; 14 import java.io.IOException ; 15 import org.mmbase.util.*; 16 import org.mmbase.util.logging.*; 17 import org.w3c.dom.Element ; 18 39 public class UtilReader { 40 41 private static final Logger log = Logging.getLoggerInstance(UtilReader.class); 42 43 public static final String CONFIG_UTILS = "utils"; 44 45 46 public static final String PUBLIC_ID_UTIL_1_0 = "-//MMBase//DTD util config 1.0//EN"; 47 48 public static final String DTD_UTIL_1_0 = "util_1_0.dtd"; 49 50 51 public static final String PUBLIC_ID_UTIL = PUBLIC_ID_UTIL_1_0; 52 53 public static final String DTD_UTIL = DTD_UTIL_1_0; 54 55 59 public static void registerPublicIDs() { 60 XMLEntityResolver.registerPublicID(PUBLIC_ID_UTIL_1_0, DTD_UTIL_1_0, UtilReader.class); 61 } 62 63 private static final Map utilReaders = new HashMap(); 65 70 71 public static UtilReader get(String fileName) { 72 UtilReader utilReader = (UtilReader) utilReaders.get(fileName); 73 if (utilReader == null) { 74 synchronized(utilReaders) { 75 utilReader = new UtilReader(fileName); 76 utilReaders.put(fileName, utilReader); 77 } 78 } 79 return utilReader; 80 } 81 82 static { 83 DocumentReader.utilProperties = UtilReader.get("documentreader.xml").getProperties(); 85 86 } 87 88 private class UtilFileWatcher extends ResourceWatcher { 89 private ResourceWatcher wrappedWatcher; 90 public UtilFileWatcher(ResourceWatcher f) { 91 super(); wrappedWatcher = f; 93 } 94 95 public void onChange(String f) { 96 readProperties(f); 97 if (wrappedWatcher != null) { 98 wrappedWatcher.onChange(f); 99 } 100 } 101 } 102 103 private final Map properties = new HashMap(); 104 private final ResourceWatcher watcher; 105 private final String file; 106 107 108 114 public UtilReader(String fileName) { 115 file = CONFIG_UTILS + "/" + fileName; 116 readProperties(file); 117 watcher = new UtilFileWatcher(null); 118 watcher.add(file); 119 watcher.start(); 120 121 } 122 131 public UtilReader(String fileName, ResourceWatcher w) { 132 file = CONFIG_UTILS + "/" + fileName; 133 readProperties(file); 134 watcher = new UtilFileWatcher(w); 135 watcher.add(file); 136 watcher.start(); 137 138 } 139 145 public UtilReader(String resourceName, final Runnable onChange) { 146 this(resourceName, new ResourceWatcher() { 147 public void onChange(String name) { 148 onChange.run(); 149 } 150 } 151 ); 152 } 153 154 public void finalize() { 155 if (watcher != null) watcher.exit(); 156 } 157 158 161 public PropertiesMap getProperties() { 162 return new PropertiesMap(properties); 163 } 164 165 170 public boolean resourceAvailable() { 171 try { 172 return ResourceLoader.getConfigurationRoot().getResource(file).openConnection().getDoInput(); 173 } catch (IOException io) { 174 return false; 175 } 176 } 177 178 protected void readProperties(String s) { 179 properties.clear(); 180 181 ResourceLoader configLoader = ResourceLoader.getConfigurationRoot(); 182 List configList = configLoader.getResourceList(s); 183 Iterator configs = configList.iterator(); 184 while (configs.hasNext()) { 185 URL url = (URL ) configs.next(); 186 org.xml.sax.InputSource is; 187 try { 188 is = ResourceLoader.getInputSource(url); 189 } catch (IOException ioe) { 190 log.debug(ioe.getMessage() + " for " + url); 192 continue; 193 } 194 if (is != null) { 195 log.debug("Reading " + url); 196 DocumentReader reader = new DocumentReader(is, UtilReader.class); 197 Element e = reader.getElementByPath("util.properties"); 198 if (e != null) { 199 for (Iterator iter = reader.getChildElements(e, "property"); iter.hasNext();) { 200 Element p = (Element ) iter.next(); 201 String name = reader.getElementAttributeValue(p, "name"); 202 String type = reader.getElementAttributeValue(p, "type"); 203 if (type.equals("map")) { 204 Collection entryList = new ArrayList(); 205 206 for (Iterator entriesIter = reader.getChildElements(p, "entry"); entriesIter.hasNext();) { 207 Element entry = (Element ) entriesIter.next(); 208 String key = null; 209 String value = null; 210 211 for (Iterator en = reader.getChildElements(entry, "*"); en.hasNext();) { 212 Element keyorvalue = (Element ) en.next(); 213 if (keyorvalue.getTagName().equals("key")) { 214 key = reader.getElementValue(keyorvalue); 215 } else { 216 value = reader.getElementValue(keyorvalue); 217 } 218 } 219 if (key != null && value != null) { 220 entryList.add(new Entry(key, value)); 221 } 222 } 223 if (properties.containsKey(name)) { 224 log.service("Property '" + name + "'(" + entryList + "') of " + url + " is shadowed"); 225 } else { 226 properties.put(name, entryList); 227 } 228 } else { 229 String value = reader.getElementValue(p); 230 if (properties.containsKey(name)) { 231 log.service("Property '" + name + "'(" + value + "') of " + url + " is shadowed"); 232 } else { 233 properties.put(name, value); 234 } 235 } 236 } 237 } 238 } else { 239 log.debug("Resource " + s + " does not exist"); 240 } 241 } 242 if (properties.size() == 0) { 243 log.service("No properties read from " + configList); 244 } else { 245 log.service("Read " + properties.keySet() + " from " + configList); 246 } 247 } 248 249 255 256 public static class PropertiesMap extends AbstractMap { 257 258 private final Map wrappedMap; 259 260 263 public PropertiesMap() { 264 wrappedMap = new HashMap(); 265 } 266 267 270 public PropertiesMap(Map map) { 271 wrappedMap = map; 272 } 273 276 public Set entrySet() { 277 return new EntrySet(); 278 279 } 280 281 284 public Object getProperty(Object key, Object defaultValue) { 285 Object result = get(key); 286 return result == null ? defaultValue : result; 287 } 288 289 private class EntrySet extends AbstractSet { 290 EntrySet() {} 291 public int size() { 292 return PropertiesMap.this.wrappedMap.size(); 293 } 294 public Iterator iterator() { 295 return new EntrySetIterator(); 296 } 297 } 298 private class EntrySetIterator implements Iterator { 299 private Iterator i; 300 EntrySetIterator() { 301 i = PropertiesMap.this.wrappedMap.entrySet().iterator(); 302 } 303 public boolean hasNext() { 304 return i.hasNext(); 305 } 306 public Object next() { 307 return i.next(); 308 } 309 public void remove() { 310 throw new UnsupportedOperationException ("Unmodifiable"); 311 } 312 } 313 } 314 315 } 316 | Popular Tags |