Commits

Anonymous committed 915c168

* added an API to get a component using its enabler interface instead of using initialize()

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

  • Participants
  • Parent commits 1d18417

Comments (0)

Files changed (3)

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

     void dispose();
 
     void initializeObject(Object obj);
+
+    Object getComponent(Class enablerType);
 }

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

         //        }
     }
 
+    public Object getComponent(Class enablerType) {
+        DefaultComponentManager dcm = this;
+
+        // loop all the DCMs and get the one that holds this enabler
+        Class resource = null;
+        while (dcm != null) {
+            resource = (Class) dcm.enablers.get(enablerType);
+
+            if (resource != null) {
+                break;
+            }
+
+            dcm = dcm.fallback;
+        }
+
+        if (resource == null)
+        {
+            // this is an unknown resource, return null;
+            return null;
+        }
+
+        // now that we have the DCM and the resource class, we can set it up
+        try {
+            ResourceEnablerPair pair = setupAndOptionallyCreateResource(dcm, resource);
+            return pair.resource;
+        } catch (Exception e) {
+            String message = "Could not load resource with enabler " + enablerType;
+            log.error(message, e);
+            throw new RuntimeException(message);
+        }
+    }
+
     private Map getResourceDependencies(Class resourceClass) {
         List interfaces = new ArrayList();
         addAllInterfaces(resourceClass, interfaces);
                 DefaultComponentManager newDcm = (DefaultComponentManager) mapEntry.getValue();
 
                 try {
-                    Object newResource = newDcm.resourceInstances.get(depResource);
-
-                    if (newResource == null) {
-                        newResource = ObjectFactory.getObjectFactory().buildBean(depResource);
-                    }
-
-                    Class enabler = loadResource(newResource, depResource, newDcm);
-                    setupResource(resource, enabler, newResource);
+                    ResourceEnablerPair pair = setupAndOptionallyCreateResource(newDcm, depResource);
+                    setupResource(resource, pair.enabler, pair.resource);
                 } catch (Exception e) {
                     e.printStackTrace();
 
         return enabler;
     }
 
+    private ResourceEnablerPair setupAndOptionallyCreateResource(DefaultComponentManager newDcm, Class depResource) throws Exception {
+        ResourceEnablerPair pair = new ResourceEnablerPair();
+        Object newResource = newDcm.resourceInstances.get(depResource);
+
+        if (newResource == null) {
+            newResource = ObjectFactory.getObjectFactory().buildBean(depResource);
+        }
+        pair.resource = newResource;
+
+        Class enabler = loadResource(newResource, depResource, newDcm);
+        pair.enabler = enabler;
+        return pair;
+    }
+
     private void setupResource(Object resource, Class enabler, Object newResource) {
         if (enabler == null) {
             return;
             }
         }
     }
+
+    class ResourceEnablerPair {
+        Class enabler;
+        Object resource;
+    }
 }

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

         public void initializeObject(Object component) {
             initialized.add(component);
         }
+
+        public Object getComponent(Class enablerType) {
+            return null;
+        }
     }
 }