Commit 5a14f56b676accba053b26bef0bce88e3fc12a09
1 parent
21466017
Exists in
master
Mais ajustes para busca correta dos recursos no classloader
Showing
3 changed files
with
55 additions
and
11 deletions
Show diff stats
impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/bootstrap/AbstractStrategyBootstrap.java
@@ -36,6 +36,9 @@ | @@ -36,6 +36,9 @@ | ||
36 | */ | 36 | */ |
37 | package br.gov.frameworkdemoiselle.internal.bootstrap; | 37 | package br.gov.frameworkdemoiselle.internal.bootstrap; |
38 | 38 | ||
39 | +import java.io.FileNotFoundException; | ||
40 | +import java.net.URL; | ||
41 | + | ||
39 | import javax.enterprise.event.Observes; | 42 | import javax.enterprise.event.Observes; |
40 | import javax.enterprise.inject.spi.BeforeBeanDiscovery; | 43 | import javax.enterprise.inject.spi.BeforeBeanDiscovery; |
41 | import javax.enterprise.inject.spi.ProcessAnnotatedType; | 44 | import javax.enterprise.inject.spi.ProcessAnnotatedType; |
@@ -44,6 +47,7 @@ import org.apache.commons.configuration.Configuration; | @@ -44,6 +47,7 @@ import org.apache.commons.configuration.Configuration; | ||
44 | import org.apache.commons.configuration.PropertiesConfiguration; | 47 | import org.apache.commons.configuration.PropertiesConfiguration; |
45 | 48 | ||
46 | import br.gov.frameworkdemoiselle.configuration.ConfigurationException; | 49 | import br.gov.frameworkdemoiselle.configuration.ConfigurationException; |
50 | +import br.gov.frameworkdemoiselle.internal.configuration.ConfigurationLoader; | ||
47 | import br.gov.frameworkdemoiselle.util.Reflections; | 51 | import br.gov.frameworkdemoiselle.util.Reflections; |
48 | import br.gov.frameworkdemoiselle.util.Strings; | 52 | import br.gov.frameworkdemoiselle.util.Strings; |
49 | 53 | ||
@@ -91,7 +95,8 @@ public abstract class AbstractStrategyBootstrap<T, D extends T> extends Abstract | @@ -91,7 +95,8 @@ public abstract class AbstractStrategyBootstrap<T, D extends T> extends Abstract | ||
91 | String key = null; | 95 | String key = null; |
92 | 96 | ||
93 | try { | 97 | try { |
94 | - Configuration config = new PropertiesConfiguration("demoiselle.properties"); | 98 | + URL url = ConfigurationLoader.getResourceAsURL("demoiselle.properties"); |
99 | + Configuration config = new PropertiesConfiguration(url); | ||
95 | canonicalName = config.getString(getConfigKey(), getDefaultClass().getCanonicalName()); | 100 | canonicalName = config.getString(getConfigKey(), getDefaultClass().getCanonicalName()); |
96 | 101 | ||
97 | ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); | 102 | ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); |
@@ -108,6 +113,9 @@ public abstract class AbstractStrategyBootstrap<T, D extends T> extends Abstract | @@ -108,6 +113,9 @@ public abstract class AbstractStrategyBootstrap<T, D extends T> extends Abstract | ||
108 | } catch (ClassCastException cause) { | 113 | } catch (ClassCastException cause) { |
109 | key = Strings.getString("{0}-class-must-be-of-type", typeName); | 114 | key = Strings.getString("{0}-class-must-be-of-type", typeName); |
110 | throw new ConfigurationException(getBundle().getString(key, canonicalName, getType())); | 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 | return result; | 121 | return result; |
impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/configuration/ConfigurationLoader.java
@@ -36,9 +36,11 @@ | @@ -36,9 +36,11 @@ | ||
36 | */ | 36 | */ |
37 | package br.gov.frameworkdemoiselle.internal.configuration; | 37 | package br.gov.frameworkdemoiselle.internal.configuration; |
38 | 38 | ||
39 | +import java.io.FileNotFoundException; | ||
39 | import java.io.Serializable; | 40 | import java.io.Serializable; |
40 | import java.lang.reflect.Field; | 41 | import java.lang.reflect.Field; |
41 | import java.lang.reflect.Method; | 42 | import java.lang.reflect.Method; |
43 | +import java.net.URL; | ||
42 | import java.util.HashSet; | 44 | import java.util.HashSet; |
43 | import java.util.Iterator; | 45 | import java.util.Iterator; |
44 | import java.util.Properties; | 46 | import java.util.Properties; |
@@ -49,7 +51,6 @@ import javax.validation.constraints.NotNull; | @@ -49,7 +51,6 @@ import javax.validation.constraints.NotNull; | ||
49 | 51 | ||
50 | import org.apache.commons.configuration.PropertiesConfiguration; | 52 | import org.apache.commons.configuration.PropertiesConfiguration; |
51 | import org.apache.commons.configuration.SystemConfiguration; | 53 | import org.apache.commons.configuration.SystemConfiguration; |
52 | -import org.apache.commons.configuration.XMLConfiguration; | ||
53 | import org.slf4j.Logger; | 54 | import org.slf4j.Logger; |
54 | 55 | ||
55 | import br.gov.frameworkdemoiselle.annotation.Ignore; | 56 | import br.gov.frameworkdemoiselle.annotation.Ignore; |
@@ -208,17 +209,21 @@ public class ConfigurationLoader implements Serializable { | @@ -208,17 +209,21 @@ public class ConfigurationLoader implements Serializable { | ||
208 | org.apache.commons.configuration.Configuration config = null; | 209 | org.apache.commons.configuration.Configuration config = null; |
209 | 210 | ||
210 | try { | 211 | try { |
212 | + URL url; | ||
213 | + | ||
211 | switch (type) { | 214 | switch (type) { |
212 | case SYSTEM: | 215 | case SYSTEM: |
213 | config = new SystemConfiguration(); | 216 | config = new SystemConfiguration(); |
214 | break; | 217 | break; |
215 | 218 | ||
216 | case PROPERTIES: | 219 | case PROPERTIES: |
217 | - config = new PropertiesConfiguration(resource + ".properties"); | 220 | + url = getResourceAsURL(resource + ".properties"); |
221 | + config = new PropertiesConfiguration(url); | ||
218 | break; | 222 | break; |
219 | 223 | ||
220 | case XML: | 224 | case XML: |
221 | - config = new XMLConfiguration(resource + ".xml"); | 225 | + url = getResourceAsURL(resource + ".xml"); |
226 | + config = new PropertiesConfiguration(url); | ||
222 | break; | 227 | break; |
223 | 228 | ||
224 | default: | 229 | default: |
@@ -301,4 +306,28 @@ public class ConfigurationLoader implements Serializable { | @@ -301,4 +306,28 @@ public class ConfigurationLoader implements Serializable { | ||
301 | 306 | ||
302 | return value; | 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,6 +40,7 @@ import java.io.Serializable; | ||
40 | import java.lang.reflect.Method; | 40 | import java.lang.reflect.Method; |
41 | import java.util.Enumeration; | 41 | import java.util.Enumeration; |
42 | import java.util.Locale; | 42 | import java.util.Locale; |
43 | +import java.util.MissingResourceException; | ||
43 | import java.util.Set; | 44 | import java.util.Set; |
44 | 45 | ||
45 | public class ResourceBundle extends java.util.ResourceBundle implements Serializable { | 46 | public class ResourceBundle extends java.util.ResourceBundle implements Serializable { |
@@ -47,17 +48,22 @@ public class ResourceBundle extends java.util.ResourceBundle implements Serializ | @@ -47,17 +48,22 @@ public class ResourceBundle extends java.util.ResourceBundle implements Serializ | ||
47 | private static final long serialVersionUID = 1L; | 48 | private static final long serialVersionUID = 1L; |
48 | 49 | ||
49 | private String baseName; | 50 | private String baseName; |
50 | - | 51 | + |
51 | private transient java.util.ResourceBundle delegate; | 52 | private transient java.util.ResourceBundle delegate; |
52 | 53 | ||
53 | private final Locale locale; | 54 | private final Locale locale; |
54 | - | 55 | + |
55 | private java.util.ResourceBundle getDelegate() { | 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 | return delegate; | 67 | return delegate; |
62 | } | 68 | } |
63 | 69 | ||
@@ -65,7 +71,7 @@ public class ResourceBundle extends java.util.ResourceBundle implements Serializ | @@ -65,7 +71,7 @@ public class ResourceBundle extends java.util.ResourceBundle implements Serializ | ||
65 | this.baseName = baseName; | 71 | this.baseName = baseName; |
66 | this.locale = locale; | 72 | this.locale = locale; |
67 | } | 73 | } |
68 | - | 74 | + |
69 | @Override | 75 | @Override |
70 | public boolean containsKey(String key) { | 76 | public boolean containsKey(String key) { |
71 | return getDelegate().containsKey(key); | 77 | return getDelegate().containsKey(key); |
@@ -104,6 +110,7 @@ public class ResourceBundle extends java.util.ResourceBundle implements Serializ | @@ -104,6 +110,7 @@ public class ResourceBundle extends java.util.ResourceBundle implements Serializ | ||
104 | } catch (Exception cause) { | 110 | } catch (Exception cause) { |
105 | throw new RuntimeException(cause); | 111 | throw new RuntimeException(cause); |
106 | } | 112 | } |
113 | + | ||
107 | return result; | 114 | return result; |
108 | } | 115 | } |
109 | } | 116 | } |