[jbosscache-commits] JBoss Cache SVN: r5133 - in core/trunk/src: main/java/org/jboss/cache/util/reflect and 1 other directories.

jbosscache-commits at lists.jboss.org jbosscache-commits at lists.jboss.org
Tue Jan 15 06:05:23 EST 2008


Author: manik.surtani at jboss.com
Date: 2008-01-15 06:05:23 -0500 (Tue, 15 Jan 2008)
New Revision: 5133

Removed:
   core/trunk/src/main/java/org/jboss/cache/util/reflect/ClasspathScanner.java
   core/trunk/src/test/java/org/jboss/cache/util/reflect/
Modified:
   core/trunk/src/main/java/org/jboss/cache/factories/ComponentRegistry.java
Log:
hard coded factory registration rather than using a classpath scanner

Modified: core/trunk/src/main/java/org/jboss/cache/factories/ComponentRegistry.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/factories/ComponentRegistry.java	2008-01-15 10:40:11 UTC (rev 5132)
+++ core/trunk/src/main/java/org/jboss/cache/factories/ComponentRegistry.java	2008-01-15 11:05:23 UTC (rev 5133)
@@ -16,7 +16,6 @@
 import org.jboss.cache.factories.annotations.Stop;
 import org.jboss.cache.invocation.RemoteCacheInvocationDelegate;
 import org.jboss.cache.util.BeanUtils;
-import org.jboss.cache.util.reflect.ClasspathScanner;
 import org.jboss.cache.util.reflect.ReflectionUtil;
 
 import java.lang.annotation.Annotation;
@@ -91,6 +90,28 @@
    }
 
    /**
+    * This is hard coded for now, since scanning the classpath for factories annotated with {@link org.jboss.cache.factories.annotations.DefaultFactoryFor}
+    * does not work with all class loaders.  This is a temporary solution until a more elegant one can be designed.
+    * <p/>
+    * BE SURE TO ADD ANY NEW FACTORY TYPES ANNOTATED WITH DefaultFactoryFor TO THIS SET!!
+    * <p/>
+    *
+    * @return set of known factory types.
+    */
+   private Set<Class<? extends ComponentFactory>> getHardcodedFactories()
+   {
+      Set<Class<? extends ComponentFactory>> s = new HashSet<Class<? extends ComponentFactory>>();
+      s.add(BuddyManagerFactory.class);
+      s.add(EmptyConstructorFactory.class);
+      s.add(InterceptorChainFactory.class);
+      s.add(LockTableFactory.class);
+      s.add(RuntimeConfigAwareFactory.class);
+      s.add(TransactionManagerFactory.class);
+      return s;
+   }
+
+
+   /**
     * Adds a singleton instance to the registry.  Note that if an instance of this component already exists in the
     * registry it is overwritten.  The instance is registered under type <tt>component.getClass()</tt>.  If the component
     * implements an interface or extends an abstract class, it may make more sense to use {@link #registerComponent(String, Object, Class)}
@@ -518,10 +539,8 @@
    {
       defaultFactories = new HashMap<Class, Class<? extends ComponentFactory>>();
 
-      ClasspathScanner scanner = new ClasspathScanner();
+      Set<Class<? extends ComponentFactory>> factories = getHardcodedFactories();
 
-      Set<Class<? extends ComponentFactory>> factories = scanner.scan(DefaultFactoryFor.class, ComponentFactory.class);
-
       for (Class<? extends ComponentFactory> factory : factories)
       {
          DefaultFactoryFor dFFAnnotation = factory.getAnnotation(DefaultFactoryFor.class);

Deleted: core/trunk/src/main/java/org/jboss/cache/util/reflect/ClasspathScanner.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/util/reflect/ClasspathScanner.java	2008-01-15 10:40:11 UTC (rev 5132)
+++ core/trunk/src/main/java/org/jboss/cache/util/reflect/ClasspathScanner.java	2008-01-15 11:05:23 UTC (rev 5133)
@@ -1,174 +0,0 @@
-package org.jboss.cache.util.reflect;
-
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-import org.jboss.cache.config.ConfigurationException;
-
-import java.io.File;
-import java.io.IOException;
-import java.lang.annotation.Annotation;
-import java.net.MalformedURLException;
-import java.net.URL;
-import java.net.URLClassLoader;
-import java.util.Collections;
-import java.util.Enumeration;
-import java.util.HashSet;
-import java.util.Set;
-import java.util.zip.ZipEntry;
-import java.util.zip.ZipFile;
-
-/**
- * Class for scanning archives and classpaths in the current JBoss Cache classpath for classes annotated with a given annotation.  Inspired by a similar class in
- * JBoss SEAM.
- *
- * @author Manik Surtani
- */
-public class ClasspathScanner
-{
-   private Log log = LogFactory.getLog(ClasspathScanner.class);
-   URLClassLoader classLoader;
-
-   /**
-    * Constructor with the type of annotation to scan for.
-    */
-   public ClasspathScanner()
-   {
-      classLoader = (URLClassLoader) ClasspathScanner.class.getClassLoader();
-   }
-
-   /**
-    * Scans the class path element that contains JBoss Cache for all classes that contain the annotation type this class is
-    * initialised with.  Note that this only scans CLASSES for the annotation; not methods, etc.
-    *
-    * @param annotationType the type of annotation to scan for.
-    * @param classType      the type of class to scan for.  Subclasses will be scanned, others will not.
-    * @return a set of Classes that contain the specified annotation on the class.
-    */
-   public <T> Set<Class<? extends T>> scan(Class<? extends Annotation> annotationType, Class<T> classType)
-   {
-      Set<Class<? extends T>> classes = Collections.emptySet();
-
-      try
-      {
-         // only scan the current ClassPath location that contains this file.  Could be a directory or a JAR file.
-         URL url = getURLPathFromClassLoader("org/jboss/cache/Cache.class");
-         String urlPath = url.getFile();
-         if (urlPath.endsWith("/"))
-         {
-            urlPath = urlPath.substring(0, urlPath.length() - 1);
-         }
-
-         if (log.isDebugEnabled()) log.debug("scanning: " + urlPath);
-         File file = new File(urlPath);
-         if (file.isDirectory())
-         {
-            classes = handleDirectory(file, null, classType, annotationType);
-         }
-         else
-         {
-            classes = handleArchive(file, classType, annotationType);
-         }
-      }
-      catch (IOException ioe)
-      {
-         log.fatal("could not read entries", ioe);
-      }
-      catch (ClassNotFoundException e)
-      {
-         log.fatal("Unable to load class", e);
-      }
-
-      return classes;
-   }
-
-   URL getURLPathFromClassLoader(String resourceName) throws MalformedURLException
-   {
-      URL u2 = classLoader.findResource(resourceName);
-      String u2String = u2.toString();
-
-      boolean isJar = u2String.startsWith("jar:") && (u2String.toLowerCase().contains(".jar!/") || u2String.toLowerCase().contains(".zip!/"));
-
-      for (URL u : classLoader.getURLs())
-      {
-         String originalUrlString = u.toString().replaceAll("\\/\\.\\/", "/");
-         String urlString = originalUrlString;
-         if (isJar) urlString = "jar:" + urlString + "!/";
-         if (u2String.startsWith(urlString)) return new URL(originalUrlString);
-      }
-
-      throw new ConfigurationException("Unable to find core JBoss Cache classes on classpath!");
-   }
-
-
-   private <T> Set<Class<? extends T>> handleArchive(File file, Class<T> classType, Class<? extends Annotation> annotationType) throws IOException, ClassNotFoundException
-   {
-      Set<Class<? extends T>> classesWithAnnotations = new HashSet<Class<? extends T>>();
-      ZipFile zip = new ZipFile(file);
-      Enumeration<? extends ZipEntry> entries = zip.entries();
-      while (entries.hasMoreElements())
-      {
-         ZipEntry entry = entries.nextElement();
-         String name = entry.getName();
-         Class<? extends T> c = handleItem(name, classType, annotationType);
-         if (c != null) classesWithAnnotations.add(c);
-      }
-
-      return classesWithAnnotations;
-   }
-
-   private <T> Set<Class<? extends T>> handleDirectory(File file, String path, Class<T> classType, Class<? extends Annotation> annotationType) throws IOException, ClassNotFoundException
-   {
-      Set<Class<? extends T>> classesWithAnnotations = new HashSet<Class<? extends T>>();
-      for (File child : file.listFiles())
-      {
-         String newPath = path == null ? child.getName() : path + '/' + child.getName();
-         if (child.isDirectory())
-         {
-            classesWithAnnotations.addAll(handleDirectory(child, newPath, classType, annotationType));
-         }
-         else
-         {
-            Class<? extends T> c = handleItem(newPath, classType, annotationType);
-            if (c != null)
-            {
-               classesWithAnnotations.add(c);
-            }
-         }
-      }
-
-      return classesWithAnnotations;
-   }
-
-   private <T> Class<? extends T> handleItem(String name, Class<T> classType, Class<? extends Annotation> annotationType) throws IOException, ClassNotFoundException
-   {
-      if (!name.endsWith(".class")) return null;
-
-      Class<? extends T> c = getClassFile(filenameToClassname(name), classType);
-      if (c != null && hasAnnotation(c, annotationType))
-      {
-         return c;
-      }
-      else
-      {
-         return null;
-      }
-   }
-
-   private <T> Class<? extends T> getClassFile(String name, Class<T> classType) throws IOException, ClassNotFoundException
-   {
-      Class c = classLoader.loadClass(name);
-      if (c != null && classType.isAssignableFrom(c)) return c;
-      else return null;
-   }
-
-   private boolean hasAnnotation(Class clazz, Class<? extends Annotation> annotationType)
-   {
-      return (clazz.isAnnotationPresent(annotationType));
-   }
-
-   private static String filenameToClassname(String filename)
-   {
-      return filename.substring(0, filename.lastIndexOf(".class")).replace('/', '.').replace('\\', '.');
-   }
-
-}




More information about the jbosscache-commits mailing list