Created
May 13, 2013 02:10
-
-
Save carlosspohr/5565773 to your computer and use it in GitHub Desktop.
Basic class scan for external logic's with Interface --> Implementation with @foo.bla.Component. Think a better way for this scan.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package br.inf.carlos.dindin.components.scan; | |
| import java.io.IOException; | |
| import java.net.URL; | |
| import java.util.Map; | |
| import java.util.Set; | |
| import org.apache.log4j.Logger; | |
| import org.scannotation.AnnotationDB; | |
| import org.scannotation.WarUrlFinder; | |
| import br.com.caelum.vraptor.ComponentRegistry; | |
| import br.com.caelum.vraptor.ioc.guice.GuiceProvider; | |
| import br.inf.carlos.base.cdi.provider.Component; | |
| public class ExternalComponentScanning extends GuiceProvider{ | |
| private static final Logger logger = Logger.getLogger(ExternalComponentScanning.class); | |
| private void register(ComponentRegistry registry, Set<String> keySet) throws ClassNotFoundException{ | |
| if(keySet == null){ | |
| return; | |
| } | |
| for (String clazz : keySet) { | |
| Class<?> componentClass = Class.forName(clazz); | |
| Class<?>[] interfaces = componentClass.getInterfaces(); | |
| if(interfaces == null || interfaces.length == 0){ | |
| logger.warn("No interfaces found for class " + componentClass.getName() + ". Only class will be provided by Container."); | |
| registry.register(componentClass, componentClass); | |
| } | |
| else{ | |
| logger.info("Providing " + interfaces[0].getName() + " for class " + componentClass.getName() + "."); | |
| registry.register(interfaces[0], componentClass); | |
| } | |
| } | |
| } | |
| @Override | |
| protected void registerCustomComponents(ComponentRegistry registry) { | |
| try { | |
| URL[] urls = WarUrlFinder.findWebInfLibClasspaths(this.context); | |
| AnnotationDB db = new AnnotationDB(); | |
| db.setScanClassAnnotations(true); | |
| db.scanArchives(urls); | |
| Map<String, Set<String>> index = db.getAnnotationIndex(); | |
| this.register(registry, index.get(Component.class.getName())); | |
| } catch (IOException e) { | |
| e.printStackTrace(); | |
| } catch (ClassNotFoundException e) { | |
| e.printStackTrace(); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment