Commit 5a14f56b676accba053b26bef0bce88e3fc12a09

Authored by Cleverson Sacramento
1 parent 21466017
Exists in master

Mais ajustes para busca correta dos recursos no classloader

impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/bootstrap/AbstractStrategyBootstrap.java
... ... @@ -36,6 +36,9 @@
36 36 */
37 37 package br.gov.frameworkdemoiselle.internal.bootstrap;
38 38  
  39 +import java.io.FileNotFoundException;
  40 +import java.net.URL;
  41 +
39 42 import javax.enterprise.event.Observes;
40 43 import javax.enterprise.inject.spi.BeforeBeanDiscovery;
41 44 import javax.enterprise.inject.spi.ProcessAnnotatedType;
... ... @@ -44,6 +47,7 @@ import org.apache.commons.configuration.Configuration;
44 47 import org.apache.commons.configuration.PropertiesConfiguration;
45 48  
46 49 import br.gov.frameworkdemoiselle.configuration.ConfigurationException;
  50 +import br.gov.frameworkdemoiselle.internal.configuration.ConfigurationLoader;
47 51 import br.gov.frameworkdemoiselle.util.Reflections;
48 52 import br.gov.frameworkdemoiselle.util.Strings;
49 53  
... ... @@ -91,7 +95,8 @@ public abstract class AbstractStrategyBootstrap<T, D extends T> extends Abstract
91 95 String key = null;
92 96  
93 97 try {
94   - Configuration config = new PropertiesConfiguration("demoiselle.properties");
  98 + URL url = ConfigurationLoader.getResourceAsURL("demoiselle.properties");
  99 + Configuration config = new PropertiesConfiguration(url);
95 100 canonicalName = config.getString(getConfigKey(), getDefaultClass().getCanonicalName());
96 101  
97 102 ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
... ... @@ -108,6 +113,9 @@ public abstract class AbstractStrategyBootstrap<T, D extends T> extends Abstract
108 113 } catch (ClassCastException cause) {
109 114 key = Strings.getString("{0}-class-must-be-of-type", typeName);
110 115 throw new ConfigurationException(getBundle().getString(key, canonicalName, getType()));
  116 +
  117 + } catch (FileNotFoundException e) {
  118 + throw new ConfigurationException(getBundle().getString("file-not-found", "demoiselle.properties"));
111 119 }
112 120  
113 121 return result;
... ...
impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/configuration/ConfigurationLoader.java
... ... @@ -36,9 +36,11 @@
36 36 */
37 37 package br.gov.frameworkdemoiselle.internal.configuration;
38 38  
  39 +import java.io.FileNotFoundException;
39 40 import java.io.Serializable;
40 41 import java.lang.reflect.Field;
41 42 import java.lang.reflect.Method;
  43 +import java.net.URL;
42 44 import java.util.HashSet;
43 45 import java.util.Iterator;
44 46 import java.util.Properties;
... ... @@ -49,7 +51,6 @@ import javax.validation.constraints.NotNull;
49 51  
50 52 import org.apache.commons.configuration.PropertiesConfiguration;
51 53 import org.apache.commons.configuration.SystemConfiguration;
52   -import org.apache.commons.configuration.XMLConfiguration;
53 54 import org.slf4j.Logger;
54 55  
55 56 import br.gov.frameworkdemoiselle.annotation.Ignore;
... ... @@ -208,17 +209,21 @@ public class ConfigurationLoader implements Serializable {
208 209 org.apache.commons.configuration.Configuration config = null;
209 210  
210 211 try {
  212 + URL url;
  213 +
211 214 switch (type) {
212 215 case SYSTEM:
213 216 config = new SystemConfiguration();
214 217 break;
215 218  
216 219 case PROPERTIES:
217   - config = new PropertiesConfiguration(resource + ".properties");
  220 + url = getResourceAsURL(resource + ".properties");
  221 + config = new PropertiesConfiguration(url);
218 222 break;
219 223  
220 224 case XML:
221   - config = new XMLConfiguration(resource + ".xml");
  225 + url = getResourceAsURL(resource + ".xml");
  226 + config = new PropertiesConfiguration(url);
222 227 break;
223 228  
224 229 default:
... ... @@ -301,4 +306,28 @@ public class ConfigurationLoader implements Serializable {
301 306  
302 307 return value;
303 308 }
  309 +
  310 + public static URL getResourceAsURL(final String resource) throws FileNotFoundException {
  311 + final String stripped = resource.startsWith("/") ? resource.substring(1) : resource;
  312 +
  313 + URL url = null;
  314 + final ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
  315 +
  316 + if (classLoader != null) {
  317 + url = classLoader.getResource(stripped);
  318 + }
  319 +
  320 + if (url == null) {
  321 + url = ConfigurationLoader.class.getResource(stripped);
  322 + }
  323 + if (url == null) {
  324 + url = ConfigurationLoader.class.getClassLoader().getResource(stripped);
  325 + }
  326 +
  327 + if (url == null) {
  328 + throw new FileNotFoundException(resource + " not found.");
  329 + }
  330 +
  331 + return url;
  332 + }
304 333 }
... ...
impl/core/src/main/java/br/gov/frameworkdemoiselle/util/ResourceBundle.java
... ... @@ -40,6 +40,7 @@ import java.io.Serializable;
40 40 import java.lang.reflect.Method;
41 41 import java.util.Enumeration;
42 42 import java.util.Locale;
  43 +import java.util.MissingResourceException;
43 44 import java.util.Set;
44 45  
45 46 public class ResourceBundle extends java.util.ResourceBundle implements Serializable {
... ... @@ -47,17 +48,22 @@ public class ResourceBundle extends java.util.ResourceBundle implements Serializ
47 48 private static final long serialVersionUID = 1L;
48 49  
49 50 private String baseName;
50   -
  51 +
51 52 private transient java.util.ResourceBundle delegate;
52 53  
53 54 private final Locale locale;
54   -
  55 +
55 56 private java.util.ResourceBundle getDelegate() {
56   - if(delegate == null) {
57   - ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
58   - delegate = ResourceBundle.getBundle(baseName, locale, classLoader);
  57 + if (delegate == null) {
  58 + try {
  59 + ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
  60 + delegate = ResourceBundle.getBundle(baseName, locale, classLoader);
  61 +
  62 + } catch (MissingResourceException mre) {
  63 + delegate = ResourceBundle.getBundle(baseName, locale);
  64 + }
59 65 }
60   -
  66 +
61 67 return delegate;
62 68 }
63 69  
... ... @@ -65,7 +71,7 @@ public class ResourceBundle extends java.util.ResourceBundle implements Serializ
65 71 this.baseName = baseName;
66 72 this.locale = locale;
67 73 }
68   -
  74 +
69 75 @Override
70 76 public boolean containsKey(String key) {
71 77 return getDelegate().containsKey(key);
... ... @@ -104,6 +110,7 @@ public class ResourceBundle extends java.util.ResourceBundle implements Serializ
104 110 } catch (Exception cause) {
105 111 throw new RuntimeException(cause);
106 112 }
  113 +
107 114 return result;
108 115 }
109 116 }
... ...