I also revisited my code now. :-P
Changes:
* if not logger is supplied, it wraps all errors as
IllegalArgumentException (fail-fast, as requested by Russell)
* supports loading properties from existing Propeties/Dictionary object
* extends MapSymbolProvider - so it delegates all case sensitivity
issues to that class instead of handling those itself. So it becomes a
mere adapter to convert from Properties to Map.
As I'm still lazy and you didn't like me overwriting your code, I will
just attach it here. If you want, you can overwrite it yourself in wiki,
Rgds,
Neeme
Ulrich Stärk wrote:
Neeme Praks schrieb:
PS. Sorry about the curly braces style in the refactored class - my
eclipse autoformatted it to my coding style and I was too lazy to
change it back
Please don't be too lazy next time... I revised it a bit because I
personally don't like to have several initialization methods (you called
them load) for something as simple as loading a properties file. Also
all Exceptions are now re-thrown as runtime exceptions so that Tapestry
will fail to start when the properties file can not be found or loaded.
Cheers,
Uli
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Collections;
import java.util.Dictionary;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import org.apache.tapestry5.ioc.internal.services.MapSymbolProvider;
import org.slf4j.Logger;
public class PropertiesFileSymbolProvider extends MapSymbolProvider {
public PropertiesFileSymbolProvider(Logger logger, String resourceName,
boolean loadFromClassPath) {
super(convert(logger, resourceName, loadFromClassPath));
}
public PropertiesFileSymbolProvider(Logger logger, URL url) {
super(convert(logger, url));
}
public PropertiesFileSymbolProvider(Logger logger, InputStream in) {
super(convert(logger, in));
}
public PropertiesFileSymbolProvider(Logger logger, Dictionary<?, ?> properties) {
super(convert(properties));
}
private static Map<String, String> convert(Logger logger, String resourceName,
boolean loadFromClassPath) {
try {
if (loadFromClassPath) {
URL url = ClassLoader.getSystemResource(resourceName);
if (url == null)
throw new FileNotFoundException(
"properties file resource not found on classpath: " + resourceName);
return convert(logger, url);
} else {
return convert(logger, new FileInputStream(resourceName));
}
} catch (IOException e) {
return handleError(logger, e, "error while loading '" + resourceName + "' " +
(loadFromClassPath ? "from classpath" : "from filesystem"));
}
}
private static Map<String, String> convert(Logger logger, URL url) {
if (url == null) throw new IllegalArgumentException("URL is required!");
try {
return convert(logger, url.openStream());
} catch (IOException e) {
return handleError(logger, e, "error while opening URL '" + url + "'");
}
}
private static Map<String, String> convert(Logger logger, InputStream in) {
if (in == null) throw new IllegalArgumentException("input stream is required!");
Properties properties = new Properties();
try {
properties.load(in);
return convert(properties);
} catch (IOException e) {
return handleError(logger, e, "error while loading properties file");
}
}
private static Map<String, String> convert(Dictionary<?, ?> properties) {
if (properties == null) throw new IllegalArgumentException("properties is required!");
HashMap<String, String> map = new HashMap<String, String>(properties.size());
for (Enumeration<?> en = properties.keys(); en.hasMoreElements();) {
Object key = en.nextElement();
Object value = properties.get(key);
map.put(key != null ? key.toString() : null,
value != null ? value.toString() : null);
}
return map;
}
private static Map<String, String> handleError(Logger logger, Exception e, String msg) {
if (logger != null) {
logger.error(msg, e);
return Collections.emptyMap();
} else {
throw new IllegalArgumentException(msg, e);
}
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]