diff --git a/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/bootstrap/AbstractStrategyBootstrap.java b/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/bootstrap/AbstractStrategyBootstrap.java index 8c744b4..9ac2085 100644 --- a/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/bootstrap/AbstractStrategyBootstrap.java +++ b/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/bootstrap/AbstractStrategyBootstrap.java @@ -36,6 +36,9 @@ */ package br.gov.frameworkdemoiselle.internal.bootstrap; +import java.io.FileNotFoundException; +import java.net.URL; + import javax.enterprise.event.Observes; import javax.enterprise.inject.spi.BeforeBeanDiscovery; import javax.enterprise.inject.spi.ProcessAnnotatedType; @@ -44,6 +47,7 @@ import org.apache.commons.configuration.Configuration; import org.apache.commons.configuration.PropertiesConfiguration; import br.gov.frameworkdemoiselle.configuration.ConfigurationException; +import br.gov.frameworkdemoiselle.internal.configuration.ConfigurationLoader; import br.gov.frameworkdemoiselle.util.Reflections; import br.gov.frameworkdemoiselle.util.Strings; @@ -91,7 +95,8 @@ public abstract class AbstractStrategyBootstrap extends Abstract String key = null; try { - Configuration config = new PropertiesConfiguration("demoiselle.properties"); + URL url = ConfigurationLoader.getResourceAsURL("demoiselle.properties"); + Configuration config = new PropertiesConfiguration(url); canonicalName = config.getString(getConfigKey(), getDefaultClass().getCanonicalName()); ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); @@ -108,6 +113,9 @@ public abstract class AbstractStrategyBootstrap extends Abstract } catch (ClassCastException cause) { key = Strings.getString("{0}-class-must-be-of-type", typeName); throw new ConfigurationException(getBundle().getString(key, canonicalName, getType())); + + } catch (FileNotFoundException e) { + throw new ConfigurationException(getBundle().getString("file-not-found", "demoiselle.properties")); } return result; diff --git a/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/configuration/ConfigurationLoader.java b/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/configuration/ConfigurationLoader.java index 07d7470..bfc9e65 100644 --- a/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/configuration/ConfigurationLoader.java +++ b/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/configuration/ConfigurationLoader.java @@ -36,9 +36,11 @@ */ package br.gov.frameworkdemoiselle.internal.configuration; +import java.io.FileNotFoundException; import java.io.Serializable; import java.lang.reflect.Field; import java.lang.reflect.Method; +import java.net.URL; import java.util.HashSet; import java.util.Iterator; import java.util.Properties; @@ -49,7 +51,6 @@ import javax.validation.constraints.NotNull; import org.apache.commons.configuration.PropertiesConfiguration; import org.apache.commons.configuration.SystemConfiguration; -import org.apache.commons.configuration.XMLConfiguration; import org.slf4j.Logger; import br.gov.frameworkdemoiselle.annotation.Ignore; @@ -208,17 +209,21 @@ public class ConfigurationLoader implements Serializable { org.apache.commons.configuration.Configuration config = null; try { + URL url; + switch (type) { case SYSTEM: config = new SystemConfiguration(); break; case PROPERTIES: - config = new PropertiesConfiguration(resource + ".properties"); + url = getResourceAsURL(resource + ".properties"); + config = new PropertiesConfiguration(url); break; case XML: - config = new XMLConfiguration(resource + ".xml"); + url = getResourceAsURL(resource + ".xml"); + config = new PropertiesConfiguration(url); break; default: @@ -301,4 +306,28 @@ public class ConfigurationLoader implements Serializable { return value; } + + public static URL getResourceAsURL(final String resource) throws FileNotFoundException { + final String stripped = resource.startsWith("/") ? resource.substring(1) : resource; + + URL url = null; + final ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); + + if (classLoader != null) { + url = classLoader.getResource(stripped); + } + + if (url == null) { + url = ConfigurationLoader.class.getResource(stripped); + } + if (url == null) { + url = ConfigurationLoader.class.getClassLoader().getResource(stripped); + } + + if (url == null) { + throw new FileNotFoundException(resource + " not found."); + } + + return url; + } } diff --git a/impl/core/src/main/java/br/gov/frameworkdemoiselle/util/ResourceBundle.java b/impl/core/src/main/java/br/gov/frameworkdemoiselle/util/ResourceBundle.java index f2de5eb..af9f77d 100644 --- a/impl/core/src/main/java/br/gov/frameworkdemoiselle/util/ResourceBundle.java +++ b/impl/core/src/main/java/br/gov/frameworkdemoiselle/util/ResourceBundle.java @@ -40,6 +40,7 @@ import java.io.Serializable; import java.lang.reflect.Method; import java.util.Enumeration; import java.util.Locale; +import java.util.MissingResourceException; import java.util.Set; public class ResourceBundle extends java.util.ResourceBundle implements Serializable { @@ -47,17 +48,22 @@ public class ResourceBundle extends java.util.ResourceBundle implements Serializ private static final long serialVersionUID = 1L; private String baseName; - + private transient java.util.ResourceBundle delegate; private final Locale locale; - + private java.util.ResourceBundle getDelegate() { - if(delegate == null) { - ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); - delegate = ResourceBundle.getBundle(baseName, locale, classLoader); + if (delegate == null) { + try { + ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); + delegate = ResourceBundle.getBundle(baseName, locale, classLoader); + + } catch (MissingResourceException mre) { + delegate = ResourceBundle.getBundle(baseName, locale); + } } - + return delegate; } @@ -65,7 +71,7 @@ public class ResourceBundle extends java.util.ResourceBundle implements Serializ this.baseName = baseName; this.locale = locale; } - + @Override public boolean containsKey(String key) { return getDelegate().containsKey(key); @@ -104,6 +110,7 @@ public class ResourceBundle extends java.util.ResourceBundle implements Serializ } catch (Exception cause) { throw new RuntimeException(cause); } + return result; } } -- libgit2 0.21.2