Commits

Anonymous committed 1088f46

XW-240: Add the ability to register instances of a component type with the ComponentManager. Also add the ability to get an instance of a component from the ComponentManager.

git-svn-id: http://svn.opensymphony.com/svn/xwork/trunk@369e221344d-f017-0410-9bd5-d282ab1896d7

  • Participants
  • Parent commits 2fe9e26

Comments (0)

Files changed (3)

File src/java/com/opensymphony/xwork/interceptor/component/ComponentManager.java

     void dispose();
 
     void initializeObject(Object obj);
+
+    void registerInstance(Class componentType, Object instance);
+
+    Object getComponentInstance(Class componentType);
 }

File src/java/com/opensymphony/xwork/interceptor/component/DefaultComponentManager.java

         //        }
     }
 
+    public void registerInstance(Class componentType, Object instance) {
+        if (!componentType.isInstance(instance)) {
+            throw new IllegalArgumentException("The object " + instance + " is not an instance of " + componentType.getName());
+        }
+
+        loadResource(instance, componentType, this);
+    }
+
+    public Object getComponentInstance(Class componentType) {
+        DefaultComponentManager dcm = this;
+
+        // loop all the DCMs and get the one that holds this enabler
+        Class enablerType = null;
+
+        while (dcm != null) {
+            enablerType = (Class) dcm.enablers2.get(componentType);
+
+            if (enablerType != null) {
+                break;
+            }
+
+            dcm = dcm.fallback;
+        }
+
+        if (enablerType == null) {
+            // this is an unknown component type, return null
+            return null;
+        }
+
+        try {
+            ResourceEnablerPair pair = setupAndOptionallyCreateResource(dcm, componentType);
+
+            return pair.resource;
+        } catch (Exception e) {
+            String message = "Could not load resource of type " + componentType;
+            log.error(message, e);
+            throw new RuntimeException(message);
+        }
+    }
+
     private Map getResourceDependencies(Class resourceClass) {
         List interfaces = new ArrayList();
         addAllInterfaces(resourceClass, interfaces);

File src/test/com/opensymphony/xwork/interceptor/component/ComponentConfigurationTest.java

             enablers.add(enablerType);
         }
 
-        public void addResource(Class resource) {
-        }
-
         public void dispose() {
         }
 
         public void initializeObject(Object component) {
             initialized.add(component);
         }
+
+        public void registerInstance(Class componentType, Object instance) {
+
+        }
+
+        public Object getComponentInstance(Class componentType) {
+            return null; 
+        }
     }
 }