Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Scans the class path for jars that contain the provided package
- */
- class EagerBinder {
- private static final Logger log = Logger.getLogger(EagerBinder.class
- .getName());
- private final AbstractBindings bindings;
- /**
- * Create eager binder.
- *
- * @param bindings
- * bindings to bind to
- */
- public EagerBinder(AbstractBindings bindings) {
- this.bindings = bindings;
- }
- /**
- * Perform classpath scanning and binding.
- *
- * @param scanPackage
- * root package to scan classes in, for instance com.edgenuity
- */
- public void scanAndBind(String scanPackage) {
- ClassLoader classLoader = getClassLoader();
- try {
- for (Enumeration<URL> resources = classLoader
- .getResources((scanPackage != null) ? scanPackage.replace(
- '.', '/') : "/"); resources.hasMoreElements();) {
- URL url = resources.nextElement();
- String s = url.toString();
- log.config("scanning " + s);
- if (s.startsWith("jar:")) {
- s = s.substring(4, s.indexOf(".jar") + 4);
- File f = new File(s);
- assert (f.exists());
- ZipFile zip = new ZipFile(new File(new URL(s).toURI()));
- try {
- for (Enumeration<? extends ZipEntry> entries = zip
- .entries(); entries.hasMoreElements();) {
- ZipEntry entry = (ZipEntry) entries.nextElement();
- if (entry.getName().endsWith(".class")) {
- InputStream is = zip.getInputStream(entry);
- processClass(is);
- }
- }
- } finally {
- zip.close();
- }
- } else if (s.startsWith("file:")) {
- traverse(new File(url.toURI()));
- } // else ignore
- }
- } catch (IOException e) {
- throw new RuntimeException(e);
- } catch (URISyntaxException e) {
- throw new RuntimeException(e);
- }
- }
- private void processClass(InputStream is) throws IOException {
- ClassReader reader = new ClassReader(is);
- reader.accept(new EmptyVisitor() {
- private String owner;
- private Class<?> ownerType;
- private Class<?> annotationType;
- @Override
- public void visit(final int version, final int access,
- final String name, final String signature,
- final String superName, final String[] interfaces) {
- owner = name;
- }
- private void setOwnerType() {
- try {
- ownerType = getClassLoader().loadClass(
- owner.replace('/', '.'));
- } catch (ClassNotFoundException e) {
- throw new RuntimeException(e);
- }
- }
- @Override
- public AnnotationVisitor visitAnnotation(String desc,
- boolean visible) {
- if ("Lcom/google/inject/Singleton;".equals(desc)) {
- annotationType = Singleton.class;
- setOwnerType();
- if (!bindings.isBound(ownerType)
- && !bindings.isOnEagerBindingBlackList(ownerType)) {
- bindings.bind(ownerType).asEagerSingleton();
- log.config("bound " + ownerType + " as a singleton");
- }
- } else if ("Lcom/google/inject/ImplementedBy;".equals(desc)) {
- annotationType = ImplementedBy.class;
- setOwnerType();
- } else if ("Lcom/google/inject/ProvidedBy;".equals(desc)) {
- annotationType = ProvidedBy.class;
- setOwnerType();
- } else {
- annotationType = null;
- }
- return this;
- }
- @SuppressWarnings("unchecked")
- @Override
- public void visit(String name, Object value) {
- if (annotationType != null) {
- log.config(owner + ", " + annotationType + ", " + name
- + "=" + value);
- if (annotationType == ProvidedBy.class) {
- if (!bindings.isBound(ownerType)
- && !bindings
- .isOnEagerBindingBlackList(ownerType)) {
- Class provider = loadClass((com.teachscape.esb.asm.Type) value);
- ScopedBindingBuilder b = bindings.bind(ownerType)
- .toProvider(provider);
- if (provider.getAnnotation(Singleton.class) != null) {
- b.asEagerSingleton();
- log.config("bound " + ownerType.getName()
- + " to provider " + provider);
- } else {
- log.config("bound " + ownerType.getName()
- + " to provider " + provider
- + " as a singleton");
- }
- }
- } else if (annotationType == ImplementedBy.class) {
- if (ownerType.isInterface()
- && !bindings.isBound(ownerType)
- && !bindings
- .isOnEagerBindingBlackList(ownerType)) {
- Class implementation = loadClass((com.teachscape.esb.asm.Type) value);
- ScopedBindingBuilder b = bindings.bind(ownerType)
- .to(implementation);
- if (implementation.getAnnotation(Singleton.class) != null) {
- b.asEagerSingleton();
- log.config("bound " + ownerType.getName()
- + " to impl " + implementation);
- } else {
- log.config("bound " + ownerType.getName()
- + " to impl " + implementation
- + " as a singleton");
- }
- }
- }
- }
- }
- }, 0);
- }
- private void traverse(File file) {
- if (file.isDirectory()) {
- for (File child : file.listFiles()) {
- traverse(child);
- }
- }
- if (file.getName().endsWith(".class")) {
- try {
- InputStream is = new FileInputStream(file);
- try {
- processClass(is);
- } finally {
- is.close();
- }
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- }
- }
- private ClassLoader getClassLoader() {
- ClassLoader classLoader = Thread.currentThread()
- .getContextClassLoader();
- if (classLoader == null) {
- classLoader = EagerBinder.class.getClassLoader();
- }
- return classLoader;
- }
- private Class<?> loadClass(com.teachscape.esb.asm.Type type) {
- try {
- return getClassLoader().loadClass(type.getClassName());
- } catch (ClassNotFoundException e) {
- throw new RuntimeException(e);
- }
- }
- }
- /**
- * Configuration for dependency injection through {@link Dependencies}.
- */
- abstract class AbstractBindings extends AbstractModule {
- /**
- * Whether the provided type is already bound (managed) by Guice.
- *
- * @param type
- * the type to check
- * @return true if bound, false otherwise
- */
- public abstract boolean isBound(Class<?> type);
- /**
- * Whether the provided type should be ignored for eager loading if that is
- * turned on.
- *
- * @param type
- * @return true if the type should be ignore, false otherwise
- * @see EagerBinder
- */
- public abstract boolean isOnEagerBindingBlackList(Class<?> type);
- @Override
- public <T> AnnotatedBindingBuilder<T> bind(Class<T> clazz) {
- return super.bind(clazz);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement