Dear Wiki user, You have subscribed to a wiki page or wiki category on "Tapestry Wiki" for change notification.
The following page has been changed by UlrichStaerk: http://wiki.apache.org/tapestry/Tapestry5HowToReadSymbolsFromPropertiesFile The comment on the change is: Symbol lookups are now case insensitive ------------------------------------------------------------------------------ * SystemPropertiesSymbolProvider makes system properties (coming from your JVM) available as symbols = Implementation = - I wanted to have a SymbolProvider that looks for configuration symbols in a properties file either located somewhere on the filesystem or in the classpath. I therefore wrote a PropertiesFileSymbolProvider that implements SymbolProvider: + I wanted to have a SymbolProvider that looks for configuration symbols in a properties file either located somewhere on the filesystem or in the classpath. Additionally symbol lookups should be case insensitive (as everywhere else in Tapestry). I therefore wrote a PropertiesFileSymbolProvider that implements SymbolProvider: {{{ import java.io.FileInputStream; @@ -23, +23 @@ import java.io.IOException; import java.io.InputStream; import java.net.URL; + import java.util.Map; import java.util.Properties; + import org.apache.tapestry5.ioc.internal.util.CollectionFactory; import org.apache.tapestry5.ioc.services.SymbolProvider; import org.slf4j.Logger; public class PropertiesFileSymbolProvider implements SymbolProvider { - private Properties properties; + + private final Map<String, String> propertiesMap = CollectionFactory.newCaseInsensitiveMap(); /** * Instantiate a new PropertiesFileSymbolProvider using a given resource name @@ -41, +44 @@ */ public PropertiesFileSymbolProvider(Logger logger, String resourceName, boolean classPath) { - properties = new Properties(); - try { InputStream in; if (classPath) + { in = ClassLoader.getSystemResourceAsStream(resourceName); + + // ClassLoader.getSystemResourceAsStream() returns null if + // the resource cannot be found on the classpath + if (in == null) + throw new FileNotFoundException(); + } else in = new FileInputStream(resourceName); - - // ClassLoader.getSystemResourceAsStream() returns null if - // the resource cannot be found on the classpath - if (in == null) - throw new FileNotFoundException(); - properties.load(in); + initialize(logger, in); } catch (FileNotFoundException e) { String msg = "Could not find '" + resourceName + "'"; - - logger.error(msg); - - throw new IllegalArgumentException(msg, e); - } catch (IOException e) - { - String msg = "IOException while loading '" + resourceName + "': " + e.getMessage(); logger.error(msg); @@ -110, +106 @@ private void initialize(Logger logger, InputStream in) { - properties = new Properties(); + Properties properties = new Properties(); + try { properties.load(in); + + for(Object key : properties.keySet()) + { + propertiesMap.put((String)key, properties.getProperty((String)key)); + } } catch (IOException e) { String msg = "IOEception while loading properties: " + e.getMessage(); @@ -122, +124 @@ throw new IllegalArgumentException(msg, e); } - } public String valueForSymbol(String arg0) { - return properties.getProperty(arg0); + return propertiesMap.get(arg0); } } }}} --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
