From 8ff809120d6f9a27626618ba997563c6744c80b9 Mon Sep 17 00:00:00 2001 From: Emerson Oliveira Date: Mon, 1 Apr 2013 14:01:43 -0300 Subject: [PATCH] Migração dos métodos de carregamento de resource para o utilitário Reflections --- impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/bootstrap/AbstractLifecycleBootstrap.java | 3 +-- impl/core/src/main/java/br/gov/frameworkdemoiselle/util/Reflections.java | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/bootstrap/AbstractLifecycleBootstrap.java b/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/bootstrap/AbstractLifecycleBootstrap.java index 3549680..dc18a1e 100644 --- a/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/bootstrap/AbstractLifecycleBootstrap.java +++ b/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/bootstrap/AbstractLifecycleBootstrap.java @@ -57,7 +57,6 @@ import org.slf4j.Logger; import br.gov.frameworkdemoiselle.DemoiselleException; import br.gov.frameworkdemoiselle.annotation.ViewScoped; -import br.gov.frameworkdemoiselle.internal.configuration.ConfigurationLoader; import br.gov.frameworkdemoiselle.internal.context.Contexts; import br.gov.frameworkdemoiselle.internal.context.AbstractCustomContext; import br.gov.frameworkdemoiselle.internal.context.ThreadLocalContext; @@ -145,7 +144,7 @@ public abstract class AbstractLifecycleBootstrap implement AnnotatedMethodProcessor processor = iter.next(); try { - ClassLoader classLoader = ConfigurationLoader.getClassLoaderForClass(processor.getAnnotatedMethod() + ClassLoader classLoader = Reflections.getClassLoaderForClass(processor.getAnnotatedMethod() .getDeclaringType().getJavaClass().getCanonicalName()); if (Thread.currentThread().getContextClassLoader().equals(classLoader)) { diff --git a/impl/core/src/main/java/br/gov/frameworkdemoiselle/util/Reflections.java b/impl/core/src/main/java/br/gov/frameworkdemoiselle/util/Reflections.java index 711295d..7cfda32 100644 --- a/impl/core/src/main/java/br/gov/frameworkdemoiselle/util/Reflections.java +++ b/impl/core/src/main/java/br/gov/frameworkdemoiselle/util/Reflections.java @@ -36,16 +36,20 @@ */ package br.gov.frameworkdemoiselle.util; +import java.io.FileNotFoundException; import java.lang.reflect.Field; import java.lang.reflect.Member; import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; +import java.net.URL; import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import br.gov.frameworkdemoiselle.internal.configuration.ConfigurationLoader; + public final class Reflections { private Reflections() { @@ -158,4 +162,35 @@ public final class Reflections { public static boolean isOfType(Class clazz, Class type) { return type.isAssignableFrom(clazz) && clazz != type; } + + public static ClassLoader getClassLoaderForClass(final String canonicalName) throws FileNotFoundException { + return Reflections.getClassLoaderForResource(canonicalName.replaceAll("\\.", "/") + ".class"); + } + + public static ClassLoader getClassLoaderForResource(final String resource) throws FileNotFoundException { + final String stripped = resource.startsWith("/") ? resource.substring(1) : resource; + + URL url = null; + ClassLoader result = Thread.currentThread().getContextClassLoader(); + + if (result != null) { + url = result.getResource(stripped); + } + + if (url == null) { + result = ConfigurationLoader.class.getClassLoader(); + url = ConfigurationLoader.class.getClassLoader().getResource(stripped); + } + + if (url == null) { + result = null; + } + + return result; + } + + public static URL getResourceAsURL(final String resource) throws FileNotFoundException { + ClassLoader classLoader = getClassLoaderForResource(resource); + return classLoader != null ? classLoader.getResource(resource) : null; + } } -- libgit2 0.21.2