You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@beehive.apache.org by cs...@apache.org on 2006/10/30 22:46:49 UTC

svn commit: r469282 - /beehive/trunk/samples/controls-spring/src/org/apache/beehive/samples/spring/control/AdultImpl.java

Author: cschoett
Date: Mon Oct 30 13:46:49 2006
New Revision: 469282

URL: http://svn.apache.org/viewvc?view=rev&rev=469282
Log:
Fix for BEEHIVE-919.  This exception was being caused because the Adult control was attempting to add a child control which was already present with the same control ID.  This was occuring for children of the 'Dean' control bean, which do NOT have bean definitions in the application-context file.  

All other beans do have entries in the application-context and their control ID's are injected via constructor at creation time.  The children of 'Dean' don't get a control ID set at construction time so when the control bean is added to the control context, an ID is autogenerated by the context for them.  This ID is not the same as the names in Dean's child list, hence the error.

There were a couple of options on how to fix this bug:

1) Add a bean descriptor to the application-context file for each of Dean's children.
2) Modify the Adult control to map child names -> control ids.

I chose option 2 as it seems to best fit with the original intent of the spring integration sample.


Modified:
    beehive/trunk/samples/controls-spring/src/org/apache/beehive/samples/spring/control/AdultImpl.java

Modified: beehive/trunk/samples/controls-spring/src/org/apache/beehive/samples/spring/control/AdultImpl.java
URL: http://svn.apache.org/viewvc/beehive/trunk/samples/controls-spring/src/org/apache/beehive/samples/spring/control/AdultImpl.java?view=diff&rev=469282&r1=469281&r2=469282
==============================================================================
--- beehive/trunk/samples/controls-spring/src/org/apache/beehive/samples/spring/control/AdultImpl.java (original)
+++ beehive/trunk/samples/controls-spring/src/org/apache/beehive/samples/spring/control/AdultImpl.java Mon Oct 30 13:46:49 2006
@@ -18,11 +18,11 @@
  *
  * $Header:$
  */
-import java.lang.annotation.Retention;
-import java.lang.annotation.RetentionPolicy;
+import java.util.HashMap;
 
 import org.apache.beehive.controls.api.bean.Controls;
 import org.apache.beehive.controls.api.bean.ControlImplementation;
+import org.apache.beehive.controls.api.bean.ControlBean;
 import org.apache.beehive.controls.api.context.Context;
 import org.apache.beehive.controls.api.context.ControlBeanContext;
 import org.apache.beehive.controls.api.events.EventHandler;
@@ -31,22 +31,59 @@
 public class AdultImpl extends PersonImpl implements Person, java.io.Serializable
 {
     @Context ControlBeanContext context;
+    private HashMap<String, String> _childIdMap;
 
-    @EventHandler(field="context", 
+    @EventHandler(field="context",
                   eventSet=ControlBeanContext.LifeCycle.class, eventName="onCreate")
     public void onCreate()
     {
         Attributes attributes = context.getControlPropertySet(Person.Attributes.class);
         if (attributes.age() > 0 && attributes.age() <= 17)
             throw new RuntimeException("Person is a minor, not an adult!");
+
+        _childIdMap = new HashMap<String,String>();
     }
 
-    public PersonBean getChild(String name) 
-    { 
-        PersonBean child = (PersonBean)context.getBean(name);
+    /**
+     * This method attempts to find a child control using a child name from the children property array
+     * of this control.  In some cases a value from the children array property array may not be the
+     * same as the actual control ID of the control.
+     *
+     * Specifically for the children of 'Dean', since a specific bean definition is not provided in the
+     * applicationContext.xml for either child, the SpringControlFactory will instantiate a bean with a
+     * Spring bean id of  'org.apache.beehive.samples.spring.control.PersonBean' for this bean there is
+     * no constructor parameter specifing the ID of the control to create.  When a control ID is not
+     * provided the control framework autogenerates the ID. The autogenerated ID is not the same as the
+     * 'name' parameter passed into this method, in order to lookup the child control correctly it is
+     * necessary to map the value of the 'name' parameter to the actual control ID.
+     */
+    public PersonBean getChild(String name)
+    {
+        PersonBean child;
+        if (_childIdMap.containsKey(name))
+            child = (PersonBean)context.getBean(_childIdMap.get(name));
+        else
+            child = (PersonBean)context.getBean(name);
+
         if (child == null)
+        {
             child = Controls.instantiate(PersonBean.class, null, context, name);
+            _childIdMap.put(name, getLocalControlID(child.getControlID()));
+        }
         return child;
+    }
+
+    /**
+     * Convert a qualified controlID to a local controlID.  Qualified controlIDs look
+     * like: 'root/Dean/Grant', the local id is 'Grant'
+     */
+    private String getLocalControlID(String qualifiedControlID)
+    {
+        int i = qualifiedControlID.lastIndexOf(ControlBean.IDSeparator);
+        if (i > -1)
+            return qualifiedControlID.substring(i+1);
+
+        return qualifiedControlID;
     }
 }