Commit 8ff809120d6f9a27626618ba997563c6744c80b9
1 parent
0797a2d4
Exists in
master
Migração dos métodos de carregamento de resource para o utilitário
Reflections
Showing
2 changed files
with
36 additions
and
2 deletions
Show diff stats
impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/bootstrap/AbstractLifecycleBootstrap.java
| @@ -57,7 +57,6 @@ import org.slf4j.Logger; | @@ -57,7 +57,6 @@ import org.slf4j.Logger; | ||
| 57 | 57 | ||
| 58 | import br.gov.frameworkdemoiselle.DemoiselleException; | 58 | import br.gov.frameworkdemoiselle.DemoiselleException; |
| 59 | import br.gov.frameworkdemoiselle.annotation.ViewScoped; | 59 | import br.gov.frameworkdemoiselle.annotation.ViewScoped; |
| 60 | -import br.gov.frameworkdemoiselle.internal.configuration.ConfigurationLoader; | ||
| 61 | import br.gov.frameworkdemoiselle.internal.context.Contexts; | 60 | import br.gov.frameworkdemoiselle.internal.context.Contexts; |
| 62 | import br.gov.frameworkdemoiselle.internal.context.AbstractCustomContext; | 61 | import br.gov.frameworkdemoiselle.internal.context.AbstractCustomContext; |
| 63 | import br.gov.frameworkdemoiselle.internal.context.ThreadLocalContext; | 62 | import br.gov.frameworkdemoiselle.internal.context.ThreadLocalContext; |
| @@ -145,7 +144,7 @@ public abstract class AbstractLifecycleBootstrap<A extends Annotation> implement | @@ -145,7 +144,7 @@ public abstract class AbstractLifecycleBootstrap<A extends Annotation> implement | ||
| 145 | AnnotatedMethodProcessor<?> processor = iter.next(); | 144 | AnnotatedMethodProcessor<?> processor = iter.next(); |
| 146 | 145 | ||
| 147 | try { | 146 | try { |
| 148 | - ClassLoader classLoader = ConfigurationLoader.getClassLoaderForClass(processor.getAnnotatedMethod() | 147 | + ClassLoader classLoader = Reflections.getClassLoaderForClass(processor.getAnnotatedMethod() |
| 149 | .getDeclaringType().getJavaClass().getCanonicalName()); | 148 | .getDeclaringType().getJavaClass().getCanonicalName()); |
| 150 | 149 | ||
| 151 | if (Thread.currentThread().getContextClassLoader().equals(classLoader)) { | 150 | if (Thread.currentThread().getContextClassLoader().equals(classLoader)) { |
impl/core/src/main/java/br/gov/frameworkdemoiselle/util/Reflections.java
| @@ -36,16 +36,20 @@ | @@ -36,16 +36,20 @@ | ||
| 36 | */ | 36 | */ |
| 37 | package br.gov.frameworkdemoiselle.util; | 37 | package br.gov.frameworkdemoiselle.util; |
| 38 | 38 | ||
| 39 | +import java.io.FileNotFoundException; | ||
| 39 | import java.lang.reflect.Field; | 40 | import java.lang.reflect.Field; |
| 40 | import java.lang.reflect.Member; | 41 | import java.lang.reflect.Member; |
| 41 | import java.lang.reflect.Method; | 42 | import java.lang.reflect.Method; |
| 42 | import java.lang.reflect.Modifier; | 43 | import java.lang.reflect.Modifier; |
| 43 | import java.lang.reflect.ParameterizedType; | 44 | import java.lang.reflect.ParameterizedType; |
| 44 | import java.lang.reflect.Type; | 45 | import java.lang.reflect.Type; |
| 46 | +import java.net.URL; | ||
| 45 | import java.util.ArrayList; | 47 | import java.util.ArrayList; |
| 46 | import java.util.Arrays; | 48 | import java.util.Arrays; |
| 47 | import java.util.List; | 49 | import java.util.List; |
| 48 | 50 | ||
| 51 | +import br.gov.frameworkdemoiselle.internal.configuration.ConfigurationLoader; | ||
| 52 | + | ||
| 49 | public final class Reflections { | 53 | public final class Reflections { |
| 50 | 54 | ||
| 51 | private Reflections() { | 55 | private Reflections() { |
| @@ -158,4 +162,35 @@ public final class Reflections { | @@ -158,4 +162,35 @@ public final class Reflections { | ||
| 158 | public static boolean isOfType(Class<?> clazz, Class<?> type) { | 162 | public static boolean isOfType(Class<?> clazz, Class<?> type) { |
| 159 | return type.isAssignableFrom(clazz) && clazz != type; | 163 | return type.isAssignableFrom(clazz) && clazz != type; |
| 160 | } | 164 | } |
| 165 | + | ||
| 166 | + public static ClassLoader getClassLoaderForClass(final String canonicalName) throws FileNotFoundException { | ||
| 167 | + return Reflections.getClassLoaderForResource(canonicalName.replaceAll("\\.", "/") + ".class"); | ||
| 168 | + } | ||
| 169 | + | ||
| 170 | + public static ClassLoader getClassLoaderForResource(final String resource) throws FileNotFoundException { | ||
| 171 | + final String stripped = resource.startsWith("/") ? resource.substring(1) : resource; | ||
| 172 | + | ||
| 173 | + URL url = null; | ||
| 174 | + ClassLoader result = Thread.currentThread().getContextClassLoader(); | ||
| 175 | + | ||
| 176 | + if (result != null) { | ||
| 177 | + url = result.getResource(stripped); | ||
| 178 | + } | ||
| 179 | + | ||
| 180 | + if (url == null) { | ||
| 181 | + result = ConfigurationLoader.class.getClassLoader(); | ||
| 182 | + url = ConfigurationLoader.class.getClassLoader().getResource(stripped); | ||
| 183 | + } | ||
| 184 | + | ||
| 185 | + if (url == null) { | ||
| 186 | + result = null; | ||
| 187 | + } | ||
| 188 | + | ||
| 189 | + return result; | ||
| 190 | + } | ||
| 191 | + | ||
| 192 | + public static URL getResourceAsURL(final String resource) throws FileNotFoundException { | ||
| 193 | + ClassLoader classLoader = getClassLoaderForResource(resource); | ||
| 194 | + return classLoader != null ? classLoader.getResource(resource) : null; | ||
| 195 | + } | ||
| 161 | } | 196 | } |