[jbosscache-commits] JBoss Cache SVN: r5191 - in core/trunk/src: test/java/org/jboss/cache/factories and 1 other directory.

jbosscache-commits at lists.jboss.org jbosscache-commits at lists.jboss.org
Tue Jan 22 09:56:16 EST 2008


Author: manik.surtani at jboss.com
Date: 2008-01-22 09:56:16 -0500 (Tue, 22 Jan 2008)
New Revision: 5191

Modified:
   core/trunk/src/main/java/org/jboss/cache/factories/ComponentRegistry.java
   core/trunk/src/test/java/org/jboss/cache/factories/ComponentRegistryFunctionalTest.java
   core/trunk/src/test/java/org/jboss/cache/factories/ComponentRegistryUnitTest.java
Log:
Simplified component registry

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-22 14:42:59 UTC (rev 5190)
+++ core/trunk/src/main/java/org/jboss/cache/factories/ComponentRegistry.java	2008-01-22 14:56:16 UTC (rev 5191)
@@ -172,7 +172,7 @@
       // make sure any other omponents that have inadvertently been stopped are now restarted.
       if (old != null)
       {
-         for (Component comp : componentLookup.values())
+         for (Component comp : old.dependencyFor)
          {
             if (comp.state != stateToMoveTo) comp.changeState(stateToMoveTo);
          }
@@ -182,11 +182,11 @@
    protected void addComponentDependencies(Component c, boolean firstTimeAdded)
    {
       // build any dependent components if necessary
-      for (Dependency d : c.dependencies)
+      for (Component d : c.dependencies)
       {
          getOrCreateComponent(d.name, d.type);
          Component dependencyComponent = componentLookup.get(d.name);
-         if (dependencyComponent != null) dependencyComponent.dependencyFor.add(c.asDependency());
+         if (dependencyComponent != null) dependencyComponent.dependencyFor.add(c);
       }
 
       if (firstTimeAdded)
@@ -194,7 +194,7 @@
          // loop through all other components already registered and make sure the current component's dependencyFor map is accurate
          for (Component other : componentLookup.values())
          {
-            if (other.dependencies.contains(c.asDependency())) c.dependencyFor.add(other.asDependency());
+            if (other.dependencies.contains(c)) c.dependencyFor.add(other);
          }
       }
 
@@ -277,7 +277,7 @@
          component = getFromConfiguration(componentClass);
          boolean attemptedFactoryConstruction = false;
 
-         if (component == null && isNonBootstrap(componentClass))
+         if (component == null && isNonBootstrapClass(componentClass))
          {
             // create this component and add it to the registry
             ComponentFactory factory = getFactory(componentClass);
@@ -307,7 +307,7 @@
       registerComponent(componentName, NULL_COMPONENT, type);
    }
 
-   private boolean isNonBootstrap(Class<?> componentClass)
+   private boolean isNonBootstrapClass(Class<?> componentClass)
    {
       return !(componentClass.equals(CacheSPI.class) || componentClass.equals(CacheImpl.class) || componentClass.equals(Cache.class)
             || componentClass.equals(ComponentRegistry.class) || componentClass.equals(Configuration.class));
@@ -451,7 +451,7 @@
    private <T> void performInjection(Method method, T target) throws IllegalAccessException, InvocationTargetException
    {
       Class[] parameterTypes = method.getParameterTypes();
-      List<Dependency> componentsToInject = getDeclaredDependencies(method);
+      List<Component> componentsToInject = getDeclaredDependencies(method);
 
       Object[] parameters = new Object[parameterTypes.length];
 
@@ -481,15 +481,17 @@
       return null;
    }
 
-   private List<Dependency> getDeclaredDependencies(Method method)
+   private List<Component> getDeclaredDependencies(Method method)
    {
-      List<Dependency> dependencies = new LinkedList<Dependency>();
+      List<Component> dependencies = new LinkedList<Component>();
       Class[] parameterTypes = method.getParameterTypes();
       Annotation[][] annotationsOnParams = method.getParameterAnnotations();
       for (int i = 0; i < parameterTypes.length; i++)
       {
          String componentName = extractComponentName(annotationsOnParams[i]);
-         Dependency d = new Dependency(componentName == null ? parameterTypes[i].getName() : componentName, parameterTypes[i]);
+         String componentNameToUse = componentName == null ? parameterTypes[i].getName() : componentName;
+         // if the component exists in the lookup, use it.  Otherwise use a placeholder which will be constructed later, on demand.
+         Component d = componentLookup.containsKey(componentNameToUse) ? componentLookup.get(componentNameToUse) : new Component(componentNameToUse, parameterTypes[i]);
          dependencies.add(d);
       }
       return dependencies;
@@ -615,11 +617,6 @@
     */
    public void wire()
    {
-      if (bootstrap != null && !bootstrap.isBootstrapped())
-      {
-         bootstrap.bootstrap();
-      }
-
       moveComponentsToState(WIRED);
    }
 
@@ -678,36 +675,6 @@
    }
 
    /**
-    * Represents a potentially unconstructed dependency.  A simple wrapper around a name and type pair.
-    */
-   class Dependency
-   {
-      String name;
-      Class<?> type;
-
-      public Dependency(String name, Class type)
-      {
-         this.name = name;
-         this.type = type;
-      }
-
-      public int hashCode()
-      {
-         return 31 * name.hashCode();
-      }
-
-      public boolean equals(Object other)
-      {
-         return other instanceof Dependency && name.equals(((Dependency) other).name);
-      }
-
-      public String toString()
-      {
-         return "Dependency (name = " + name + ")";
-      }
-   }
-
-   /**
     * Represents a component in the registry, along with state and dependencies.
     */
    class Component
@@ -716,11 +683,16 @@
       String name;
       Class type;
       State state = CONSTRUCTED;
-      Set<Dependency> dependencies = new HashSet<Dependency>(3);
-      Set<Dependency> dependencyFor = new HashSet<Dependency>(3);
-      Dependency me;
+      Set<Component> dependencies = new HashSet<Component>(3);
+      Set<Component> dependencyFor = new HashSet<Component>(3);
       boolean deepRecursionDetector = false;
 
+      public Component(String name, Class type)
+      {
+         this.name = name;
+         this.type = type;
+      }
+
       /**
        * Constructs a Component out of an instance and a name.  Scans instance for dependencies and populates
        * the "dependencies" and "dependencyFor" collections.
@@ -730,9 +702,8 @@
        */
       public Component(String name, Object instance, Class type)
       {
+         this(name, type);
          this.instance = instance;
-         this.name = name;
-         this.type = type;
 
          // now scan the instance for all dependencies.
          List<Method> injectionMethods = ReflectionUtil.getAllMethods(instance.getClass(), Inject.class);
@@ -742,15 +713,6 @@
       }
 
       /**
-       * @return a {@link org.jboss.cache.factories.ComponentRegistry.Dependency} object that represents the current component
-       */
-      Dependency asDependency()
-      {
-         if (me == null) me = new Dependency(name, type);
-         return me;
-      }
-
-      /**
        * Changes the state of a component - along with all dependent components - to a new state.  This method is recursion
        * and cyclic dependency safe.
        *
@@ -773,7 +735,7 @@
             }
 
             // now we update the state of dependent components accordingly.
-            Set<Dependency> dependentComponents = new HashSet<Dependency>();
+            Set<Component> dependentComponents = new HashSet<Component>();
             Set<Component> shallowCyclic = new HashSet<Component>();
 
             if (increase)
@@ -789,36 +751,39 @@
 
             // switch on the deep recursion detector
             deepRecursionDetector = true;
-            for (Dependency d : dependentComponents)
+            for (Component d : dependentComponents)
             {
-               Component c = componentLookup.get(d.name);
-//               if (c == null && increase)
-//               {
-//                  if (log.isTraceEnabled()) log.trace(name + " depends on " + d.name + " (type "+d.type+") but it does not exist in the registry, attempting to create.");
-//                  getOrCreateComponent(d.name, d.type);
-//                  c = componentLookup.get(d.name);
-//               }
-               if (c != null)
+               if (d.instance == null)
                {
-                  if (isShallowCyclic(c))
+                  // this is a "hollow" component that has not been constructed yet.  Another "constructed" version probably exists in the
+                  // componentLookup.  Make sure we replace this.
+                  Component c = componentLookup.get(d.name);
+                  if (increase)
                   {
-                     // don't process shallow cyclic deps here - shoud do that after we set our state.
-                     shallowCyclic.add(c);
+                     dependencies.remove(d);
+                     dependencies.add(c);
                   }
                   else
                   {
-                     // of we are "moving up" - only do this if the component is lower than what is needed.
-                     if ((increase && newState.isGreaterThan(c.state)) || (!increase && newState.isLessThan(c.state)))
-                     {
-                        c.changeState(newState);
-                     }
-
+                     dependencyFor.remove(d);
+                     dependencies.add(c);
                   }
+                  d = c;
                }
+
+               if (isShallowCyclic(d))
+               {
+                  // don't process shallow cyclic deps here - shoud do that after we set our state.
+                  shallowCyclic.add(d);
+               }
                else
                {
-                  if (log.isTraceEnabled())
-                     log.trace(name + " depends on " + d.name + " (type " + d.type + ") but it does not exist in the registry");
+                  // of we are "moving up" - only do this if the component is lower than what is needed.
+                  if ((increase && newState.isGreaterThan(d.state)) || (!increase && newState.isLessThan(d.state)))
+                  {
+                     d.changeState(newState);
+                  }
+
                }
             }
 
@@ -861,7 +826,7 @@
        */
       private boolean isShallowCyclic(Component c)
       {
-         return (dependencies.contains(c.asDependency()) && c.dependencies.contains(asDependency()));
+         return (dependencies.contains(c) && c.dependencies.contains(this));
       }
 
       /**
@@ -915,10 +880,24 @@
          }
       }
 
+      @Override
       public String toString()
       {
          return "Component (name = " + name + ", state = " + state + ")";
       }
+
+      @Override
+      public int hashCode()
+      {
+         return 31 * name.hashCode();
+      }
+
+      @Override
+      public boolean equals(Object other)
+      {
+         return other instanceof Component && name.equals(((Component) other).name);
+      }
+
    }
 
    class Bootstrap

Modified: core/trunk/src/test/java/org/jboss/cache/factories/ComponentRegistryFunctionalTest.java
===================================================================
--- core/trunk/src/test/java/org/jboss/cache/factories/ComponentRegistryFunctionalTest.java	2008-01-22 14:42:59 UTC (rev 5190)
+++ core/trunk/src/test/java/org/jboss/cache/factories/ComponentRegistryFunctionalTest.java	2008-01-22 14:56:16 UTC (rev 5191)
@@ -59,18 +59,18 @@
       for (ComponentRegistry.Component component : cr.componentLookup.values())
       {
          // test that this component appears in all dependencies' dependencyFor collection.
-         for (ComponentRegistry.Dependency dep : component.dependencies)
+         for (ComponentRegistry.Component dep : component.dependencies)
          {
-            assert cr.componentLookup.get(dep.name).dependencyFor.contains(component.asDependency()) : "Dependency " + dep.name + " does not have component " + component.name + " in it's dependencyFor collection.";
+            assert cr.componentLookup.get(dep.name).dependencyFor.contains(component) : "Dependency " + dep.name + " does not have component " + component.name + " in it's dependencyFor collection.";
          }
       }
 
       for (ComponentRegistry.Component component : cr.componentLookup.values())
       {
          // test that this component appears in all dependencies' dependencyFor collection.
-         for (ComponentRegistry.Dependency dep : component.dependencyFor)
+         for (ComponentRegistry.Component dep : component.dependencyFor)
          {
-            assert cr.componentLookup.get(dep.name).dependencies.contains(component.asDependency()) : "Dependency " + dep.name + " does not have component " + component.name + " in it's dependencies collection.";
+            assert cr.componentLookup.get(dep.name).dependencies.contains(component) : "Dependency " + dep.name + " does not have component " + component.name + " in it's dependencies collection.";
          }
       }
    }

Modified: core/trunk/src/test/java/org/jboss/cache/factories/ComponentRegistryUnitTest.java
===================================================================
--- core/trunk/src/test/java/org/jboss/cache/factories/ComponentRegistryUnitTest.java	2008-01-22 14:42:59 UTC (rev 5190)
+++ core/trunk/src/test/java/org/jboss/cache/factories/ComponentRegistryUnitTest.java	2008-01-22 14:56:16 UTC (rev 5191)
@@ -34,9 +34,9 @@
       ComponentRegistry.Component c3 = cr.componentLookup.get("c3");
 
       // add some dependencies
-      ComponentRegistry.Dependency d1 = cr.new Dependency("c1", null);
-      ComponentRegistry.Dependency d2 = cr.new Dependency("c2", null);
-      ComponentRegistry.Dependency d3 = cr.new Dependency("c3", null);
+      ComponentRegistry.Component d1 = cr.new Component("c1", null);
+      ComponentRegistry.Component d2 = cr.new Component("c2", null);
+      ComponentRegistry.Component d3 = cr.new Component("c3", null);
 
       // c1 depends on c2
       // c3 depends on c1




More information about the jbosscache-commits mailing list