I've had this in my tree since the weekend and I wanted to get rid of
it.  So, I'm checking it in.

This implements some of the missing methods from beancontext.  I stuck
to the easier ones.  I must say, this particular package has just
about the worst documentation I've seen in the JDK.

Tom

2006-04-07  Tom Tromey  <[EMAIL PROTECTED]>

        * java/beans/beancontext/BeanContextMembershipEvent.java
        (serialVersionUID): New field.
        * java/beans/beancontext/BeanContextServicesSupport.java
        (addBeanContextServicesListener): Synchronize.
        (addService): Implemented.
        (createBCSChild): Implemented.
        (BCSSChild): Added arguments.
        (fireServiceAdded): Implemented.
        (fireServiceRevoked): Implemented.
        (getCurrentServiceSelectors): Implemented.
        (hasService): Implemented.
        (removeBeanContextServicesListener): Implemented.
        (serviceAvailable): Implemented.
        (serviceRevoked): Implemented.
        * java/beans/beancontext/BeanContextSupport.java (BCSChild): Added
        arguments.
        (createBCSChild): Implemented.
        (BeanContextSupport): 
        (addBeanContextMembershipListener): Synchronize.
        (fireChildrenAdded): Implemented.
        (fireChildrenRemoved): Implemented.
        (BeanContextSupport): Use default locale.
        (isEmpty): Implemented.
        (isDesignTime): Implemented.
        (size): Implemented.
        (toArray): Synchronized.
        (toArray): Likewise.
        (iterator): Likewise.
        (BCSIterator): Implemented.
        (bcsChildren): Implemented.
        (validatePendingAdd): Implemented.
        (validatePendingRemove): Likewise.
        (childJustAddedHook): Implemented.
        (childJustRemovedHook): Likewise.
        (classEquals): Likewise.
        (toArray): Mark as stub.
        (setDesignTime): Implemented.
        (copyChildren): Implemented.
        (containsKey): Implemented.
        (contains): Likewise.
        (containsAll): Likewise.
        (getResource): Implemented.
        (getResourceAsStream): Likewise.
        (removeBeanContextMembershipListener): Likewise.
        * java/beans/beancontext/BeanContextServiceRevokedEvent.java
        (serialVersionUID): New field.
        * java/beans/beancontext/BeanContextServiceAvailableEvent.java
        (serialVersionUID): New field.
        * java/beans/beancontext/BeanContext.java (instantiateChild): Javadoc
        fix.

Index: java/beans/beancontext/BeanContext.java
===================================================================
RCS file: /cvsroot/classpath/classpath/java/beans/beancontext/BeanContext.java,v
retrieving revision 1.7
diff -u -r1.7 BeanContext.java
--- java/beans/beancontext/BeanContext.java     2 Jul 2005 20:32:37 -0000       
1.7
+++ java/beans/beancontext/BeanContext.java     7 Apr 2006 16:22:18 -0000
@@ -198,7 +198,7 @@
         * @return the created Bean
         *
         * @see 
java.beans.Beans#instantiate(java.lang.ClassLoader,java.lang.String)
-        * @see 
java.beans.Beans#instantiate(java.lang.ClassLoader,java.lang.String,java.lang.BeanContext)
+        * @see 
java.beans.Beans#instantiate(java.lang.ClassLoader,java.lang.String,java.beans.beancontext.BeanContext)
         * @exception IOException if there is an I/O problem during
         *            instantiation.
         * @exception ClassNotFoundException if a serialized Bean's class
Index: java/beans/beancontext/BeanContextMembershipEvent.java
===================================================================
RCS file: 
/cvsroot/classpath/classpath/java/beans/beancontext/BeanContextMembershipEvent.java,v
retrieving revision 1.6
diff -u -r1.6 BeanContextMembershipEvent.java
--- java/beans/beancontext/BeanContextMembershipEvent.java      2 Jul 2005 
20:32:37 -0000       1.6
+++ java/beans/beancontext/BeanContextMembershipEvent.java      7 Apr 2006 
16:22:18 -0000
@@ -52,7 +52,9 @@
  * @see java.beans.beancontext.BeanContextMembershipListener
  */
 public class BeanContextMembershipEvent extends BeanContextEvent {
-       /**
+        private static final long serialVersionUID = 3499346510334590959L;
+
+        /**
         * The children that were added or removed.
         */
        protected Collection children;
Index: java/beans/beancontext/BeanContextServiceAvailableEvent.java
===================================================================
RCS file: 
/cvsroot/classpath/classpath/java/beans/beancontext/BeanContextServiceAvailableEvent.java,v
retrieving revision 1.5
diff -u -r1.5 BeanContextServiceAvailableEvent.java
--- java/beans/beancontext/BeanContextServiceAvailableEvent.java        2 Jul 
2005 20:32:37 -0000       1.5
+++ java/beans/beancontext/BeanContextServiceAvailableEvent.java        7 Apr 
2006 16:22:18 -0000
@@ -49,7 +49,9 @@
  */
 
 public class BeanContextServiceAvailableEvent extends BeanContextEvent {
-       /**
+       private static final long serialVersionUID = -5333985775656400778L;
+
+       /**
         * The <code>Class</code> representing the service which is now
         * available.
         */
Index: java/beans/beancontext/BeanContextServiceRevokedEvent.java
===================================================================
RCS file: 
/cvsroot/classpath/classpath/java/beans/beancontext/BeanContextServiceRevokedEvent.java,v
retrieving revision 1.6
diff -u -r1.6 BeanContextServiceRevokedEvent.java
--- java/beans/beancontext/BeanContextServiceRevokedEvent.java  2 Jul 2005 
20:32:37 -0000       1.6
+++ java/beans/beancontext/BeanContextServiceRevokedEvent.java  7 Apr 2006 
16:22:18 -0000
@@ -47,7 +47,9 @@
  */
 
 public class BeanContextServiceRevokedEvent extends BeanContextEvent {
-       /**
+       private static final long serialVersionUID = -1295543154724961754L;
+
+       /**
         * The <code>Class</code> representing the service which is now
         * available.
         */
Index: java/beans/beancontext/BeanContextServicesSupport.java
===================================================================
RCS file: 
/cvsroot/classpath/classpath/java/beans/beancontext/BeanContextServicesSupport.java,v
retrieving revision 1.9
diff -u -r1.9 BeanContextServicesSupport.java
--- java/beans/beancontext/BeanContextServicesSupport.java      23 Mar 2006 
00:30:14 -0000      1.9
+++ java/beans/beancontext/BeanContextServicesSupport.java      7 Apr 2006 
16:22:18 -0000
@@ -65,8 +65,9 @@
   {
     private static final long serialVersionUID = -3263851306889194873L;
 
-    private BCSSChild()
+    BCSSChild(Object targetChild, Object peer)
     {
+      super(targetChild, peer);
     }
   }
 
@@ -166,22 +167,33 @@
   public void addBeanContextServicesListener
     (BeanContextServicesListener listener)
   {
-    if (! bcsListeners.contains(listener))
-      bcsListeners.add(listener);
+    synchronized (bcsListeners)
+      {
+        if (! bcsListeners.contains(listener))
+          bcsListeners.add(listener);
+      }
   }
 
-  public boolean addService (Class serviceClass, BeanContextServiceProvider 
bcsp)
-    throws NotImplementedException
+  public boolean addService (Class serviceClass,
+                             BeanContextServiceProvider bcsp)
   {
-    throw new Error ("Not implemented");
+    return addService(serviceClass, bcsp, true);
   }
 
   protected boolean addService (Class serviceClass,
                                 BeanContextServiceProvider bcsp,
                                 boolean fireEvent)
-    throws NotImplementedException
   {
-    throw new Error ("Not implemented");
+    synchronized (services)
+      {
+        if (services.containsKey(serviceClass))
+          return false;
+        services.put(serviceClass, bcsp);
+        if (bcsp instanceof Serializable)
+          ++serializable;
+        fireServiceAdded(serviceClass);
+        return true;
+      }
   }
   
   protected void bcsPreDeserializationHook (ObjectInputStream ois)
@@ -205,9 +217,8 @@
 
   protected BeanContextSupport.BCSChild createBCSChild (Object targetChild,
                                                         Object peer)
-    throws NotImplementedException
   {
-    throw new Error ("Not implemented");
+    return new BCSSChild(targetChild, peer);
   }
 
   protected BeanContextServicesSupport.BCSSServiceProvider
@@ -218,28 +229,44 @@
   }
 
   protected final void fireServiceAdded (BeanContextServiceAvailableEvent 
bcssae)
-    throws NotImplementedException
   {
-    throw new Error ("Not implemented");
+    synchronized (bcsListeners)
+      {
+        int size = bcsListeners.size();
+        for (int i = 0; i < size; ++i)
+          {
+            BeanContextServicesListener bcsl
+              = (BeanContextServicesListener) bcsListeners.get(i);
+            bcsl.serviceAvailable(bcssae);
+          }
+      }
   }
 
   protected final void fireServiceAdded (Class serviceClass)
-    throws NotImplementedException
   {
-    throw new Error ("Not implemented");
+    fireServiceAdded(new BeanContextServiceAvailableEvent(this,
+                                                          serviceClass));
   }
 
   protected final void fireServiceRevoked(BeanContextServiceRevokedEvent event)
-    throws NotImplementedException
   {
-    throw new Error ("Not implemented");
+    synchronized (bcsListeners)
+      {
+        int size = bcsListeners.size();
+        for (int i = 0; i < size; ++i)
+          {
+            BeanContextServicesListener bcsl
+              = (BeanContextServicesListener) bcsListeners.get(i);
+            bcsl.serviceRevoked(event);
+          }
+      }
   }
 
   protected final void fireServiceRevoked (Class serviceClass,
                                            boolean revokeNow)
-    throws NotImplementedException
   {
-    throw new Error ("Not implemented");
+    fireServiceRevoked(new BeanContextServiceRevokedEvent(this, serviceClass,
+                                                          revokeNow));
   }
 
   public BeanContextServices getBeanContextServicesPeer ()
@@ -256,15 +283,22 @@
   }
 
   public Iterator getCurrentServiceClasses ()
-    throws NotImplementedException
   {
-    throw new Error ("Not implemented");
+    synchronized (services)
+      {
+        return services.keySet().iterator();
+      }
   }
 
   public Iterator getCurrentServiceSelectors (Class serviceClass)
-    throws NotImplementedException
   {
-    throw new Error ("Not implemented");
+    synchronized (services)
+      {
+        // FIXME: what if service does not exist?  Must write a test.
+        BeanContextServiceProvider bcsp
+          = (BeanContextServiceProvider) services.get(serviceClass);
+        return bcsp.getCurrentServiceSelectors(this, serviceClass);
+      }
   }
 
   public Object getService (BeanContextChild child, Object requestor,
@@ -276,9 +310,11 @@
   }
 
   public boolean hasService (Class serviceClass)
-    throws NotImplementedException
   {
-    throw new Error ("Not implemented");
+    synchronized (services)
+      {
+        return services.containsKey(serviceClass);
+      }
   }
 
   public void initialize ()
@@ -311,10 +347,12 @@
   public void removeBeanContextServicesListener
     (BeanContextServicesListener listener)
   {
-    int index = bcsListeners.indexOf(listener);
-
-    if (index > -1)
-      bcsListeners.remove(index);
+    synchronized (bcsListeners)
+      {
+        int index = bcsListeners.indexOf(listener);
+        if (index > -1)
+          bcsListeners.remove(index);
+      }
   }
 
   public void revokeService (Class serviceClass, BeanContextServiceProvider 
bcsp,
@@ -325,14 +363,36 @@
   }
 
   public void serviceAvailable (BeanContextServiceAvailableEvent bcssae)
-    throws NotImplementedException
   {
-    throw new Error ("Not implemented");
+    synchronized (services)
+      {
+        Class klass = bcssae.getServiceClass();
+        if (services.containsKey(klass))
+          return;
+        Iterator it = bcsChildren();
+        while (it.hasNext())
+          {
+            Object obj = it.next();
+            if (obj instanceof BeanContextServices)
+              ((BeanContextServices) obj).serviceAvailable(bcssae);
+          }
+      }
   }
 
   public void serviceRevoked (BeanContextServiceRevokedEvent bcssre)
-    throws NotImplementedException
   {
-    throw new Error ("Not implemented");
+    synchronized (services)
+      {
+        Class klass = bcssre.getServiceClass();
+        if (services.containsKey(klass))
+          return;
+        Iterator it = bcsChildren();
+        while (it.hasNext())
+          {
+            Object obj = it.next();
+            if (obj instanceof BeanContextServices)
+              ((BeanContextServices) obj).serviceRevoked(bcssre);
+          }
+      }
   }
 }
Index: java/beans/beancontext/BeanContextSupport.java
===================================================================
RCS file: 
/cvsroot/classpath/classpath/java/beans/beancontext/BeanContextSupport.java,v
retrieving revision 1.8
diff -u -r1.8 BeanContextSupport.java
--- java/beans/beancontext/BeanContextSupport.java      23 Mar 2006 00:30:14 
-0000      1.8
+++ java/beans/beancontext/BeanContextSupport.java      7 Apr 2006 16:22:18 
-0000
@@ -40,6 +40,7 @@
 
 import gnu.classpath.NotImplementedException;
 
+import java.beans.DesignMode;
 import java.beans.PropertyChangeEvent;
 import java.beans.PropertyChangeListener;
 import java.beans.PropertyVetoException;
@@ -87,27 +88,35 @@
   {
     private static final long serialVersionUID = -5815286101609939109L;
 
-    BCSChild()
+    private Object targetChild;
+    private Object peer;
+
+    BCSChild(Object targetChild, Object peer)
     {
+      this.targetChild = targetChild;
+      this.peer = peer;
     }
   }
 
   protected static final class BCSIterator implements Iterator
   {
-    BCSIterator()
+    private Iterator child;
+
+    BCSIterator(Iterator child)
     {
+      this.child = child;
     }
 
     public boolean hasNext ()
       throws NotImplementedException
     {
-      throw new Error ("Not implemented");
+      return child.hasNext();
     }
 
     public Object next ()
       throws NotImplementedException
     {
-      throw new Error ("Not implemented");
+      return child.next();
     }
 
     public void remove ()
@@ -164,7 +173,9 @@
   public BeanContextSupport (BeanContext peer, Locale lcle, boolean dtime,
                              boolean visible)
   {
-    locale = lcle;
+    super(peer);
+
+    locale = lcle == null ? Locale.getDefault() : lcle;
     designTime = dtime;
     okToUseGui = visible;
 
@@ -176,24 +187,38 @@
     if (targetChild == null)
       throw new IllegalArgumentException();
 
-    if (children.containsKey(targetChild))
-      return false;
-
-    // FIXME: The second argument is surely wrong.
-    children.put(targetChild, targetChild);
+    BCSChild child;
+    synchronized (children)
+      {
+        if (children.containsKey(targetChild)
+            || ! validatePendingAdd(targetChild))
+          return false;
+        child = createBCSChild(targetChild, beanContextChildPeer);
+        children.put(targetChild, child);
+      }
+    synchronized (targetChild)
+      {
+        childJustAddedHook(targetChild, child);
+      }
+    fireChildrenAdded(new BeanContextMembershipEvent(this,
+                                                     new Object[] { 
targetChild }));
     return true;
   }
 
   public boolean addAll (Collection c)
   {
+    // Intentionally throws an exception.
     throw new UnsupportedOperationException();
   }
 
   public void addBeanContextMembershipListener
     (BeanContextMembershipListener listener)
   {
-    if (! bcmListeners.contains(listener))
-      bcmListeners.add(listener);
+    synchronized (bcmListeners)
+      {
+        if (! bcmListeners.contains(listener))
+          bcmListeners.add(listener);
+      }
   }
 
   public boolean avoidingGui ()
@@ -203,9 +228,11 @@
   }
 
   protected Iterator bcsChildren ()
-    throws NotImplementedException
   {
-    throw new Error ("Not implemented");
+    synchronized (children)
+      {
+        return new BCSIterator(children.values().iterator());
+      }
   }
 
   protected void bcsPreDeserializationHook (ObjectInputStream ois)
@@ -227,58 +254,67 @@
   }
 
   protected void childJustAddedHook (Object child, BeanContextSupport.BCSChild 
bcsc)
-    throws NotImplementedException
   {
-    throw new Error ("Not implemented");
+    // Do nothing in the base class.
   }
 
   protected void childJustRemovedHook (Object child, 
BeanContextSupport.BCSChild bcsc)
-    throws NotImplementedException
   {
-    throw new Error ("Not implemented");
+    // Do nothing in the base class.
   }
 
   protected static final boolean classEquals (Class first, Class second)
-    throws NotImplementedException
   {
-    throw new Error ("Not implemented");
+    // Lame function!
+    return (first == second || first.getName().equals(second.getName()));
   }
 
   public void clear ()
   {
-    // This is probably the right thing to do.
+    // This is the right thing to do.
     // The JDK docs are really bad here.
     throw new UnsupportedOperationException();
   }
 
   public boolean contains (Object o)
-    throws NotImplementedException
   {
-    throw new Error ("Not implemented");
+    synchronized (children)
+      {
+        return children.containsKey(o);
+      }
   }
 
   public boolean containsAll (Collection c)
-    throws NotImplementedException
   {
-    throw new Error ("Not implemented");
+    synchronized (children)
+      {
+        Iterator it = c.iterator();
+        while (it.hasNext())
+          if (! children.containsKey(it.next()))
+            return false;
+      }
+    return true;
   }
 
   public boolean containsKey (Object o)
-    throws NotImplementedException
   {
-    throw new Error ("Not implemented");
+    synchronized (children)
+      {
+        return children.containsKey(o);
+      }
   }
 
   protected final Object[] copyChildren ()
-    throws NotImplementedException
   {
-    throw new Error ("Not implemented");
+    synchronized (children)
+      {
+        return children.keySet().toArray();
+      }
   }
 
   protected BeanContextSupport.BCSChild createBCSChild (Object targetChild, 
Object peer)
-    throws NotImplementedException
   {
-    throw new Error ("Not implemented");
+    return new BCSChild(targetChild, peer);
   }
 
   protected final void deserialize (ObjectInputStream ois, Collection coll)
@@ -294,15 +330,31 @@
   }
 
   protected final void fireChildrenAdded (BeanContextMembershipEvent bcme)
-    throws NotImplementedException
   {
-    throw new Error ("Not implemented");
+    synchronized (bcmListeners)
+      {
+        Iterator it = bcmListeners.iterator();
+        while (it.hasNext())
+          {
+            BeanContextMembershipListener l
+              = (BeanContextMembershipListener) it.next();
+            l.childrenAdded(bcme);
+          }
+      }
   }
 
   protected final void fireChildrenRemoved (BeanContextMembershipEvent bcme)
-    throws NotImplementedException
   {
-    throw new Error ("Not implemented");
+    synchronized (bcmListeners)
+      {
+        Iterator it = bcmListeners.iterator();
+        while (it.hasNext())
+          {
+            BeanContextMembershipListener l
+            = (BeanContextMembershipListener) it.next();
+            l.childrenRemoved(bcme);
+          }
+      }
   }
 
   public BeanContext getBeanContextPeer ()
@@ -353,15 +405,21 @@
   }
 
   public URL getResource (String name, BeanContextChild bcc)
-    throws NotImplementedException
   {
-    throw new Error ("Not implemented");
+    if (! contains(bcc))
+      throw new IllegalArgumentException("argument not a child");
+    ClassLoader loader = bcc.getClass().getClassLoader();
+    return (loader == null ? ClassLoader.getSystemResource(name)
+            : loader.getResource(name));
   }
 
   public InputStream getResourceAsStream (String name, BeanContextChild bcc)
-    throws NotImplementedException
   {
-    throw new Error ("Not implemented");
+    if (! contains(bcc))
+      throw new IllegalArgumentException("argument not a child");
+    ClassLoader loader = bcc.getClass().getClassLoader();
+    return (loader == null ? ClassLoader.getSystemResourceAsStream(name)
+            : loader.getResourceAsStream(name));
   }
 
   protected void initialize ()
@@ -377,15 +435,16 @@
   }
 
   public boolean isDesignTime ()
-    throws NotImplementedException
   {
-    throw new Error ("Not implemented");
+    return designTime;
   }
 
   public boolean isEmpty ()
-    throws NotImplementedException
   {
-    throw new Error ("Not implemented");
+    synchronized (children)
+      {
+        return children.isEmpty();
+      }
   }
 
   public boolean isSerializing ()
@@ -396,7 +455,10 @@
 
   public Iterator iterator ()
   {
-    return children.keySet().iterator();
+    synchronized (children)
+      {
+        return children.keySet().iterator();
+      }
   }
 
   public boolean needsGui ()
@@ -439,17 +501,21 @@
 
   public boolean removeAll (Collection c)
   {
+    // Intentionally throws an exception.
     throw new UnsupportedOperationException();
   }
 
   public void removeBeanContextMembershipListener 
(BeanContextMembershipListener bcml)
-    throws NotImplementedException
   {
-    throw new Error ("Not implemented");
+    synchronized (bcmListeners)
+      {
+        bcmListeners.remove(bcml);
+      }
   }
 
   public boolean retainAll (Collection c)
   {
+    // Intentionally throws an exception.
     throw new UnsupportedOperationException();
   }
 
@@ -460,43 +526,58 @@
   }
 
   public void setDesignTime (boolean dtime)
-    throws NotImplementedException
   {
-    throw new Error ("Not implemented");
+    boolean save = designTime;
+    designTime = dtime;
+    firePropertyChange(DesignMode.PROPERTYNAME, Boolean.valueOf(save),
+                       Boolean.valueOf(dtime));
   }
 
   public void setLocale (Locale newLocale)
-    throws PropertyVetoException, NotImplementedException
+    throws PropertyVetoException
   {
-    throw new Error ("Not implemented");
+    if (newLocale == null || locale == newLocale)
+      return;
+    fireVetoableChange("locale", locale, newLocale);
+    Locale oldLocale = locale;
+    locale = newLocale;
+    firePropertyChange("locale", oldLocale, newLocale);
   }
 
   public int size ()
-    throws NotImplementedException
   {
-    throw new Error ("Not implemented");
+    synchronized (children)
+      {
+        return children.size();
+      }
   }
 
   public Object[] toArray ()
   {
-    return children.keySet().toArray();
+    synchronized (children)
+      {
+        return children.keySet().toArray();
+      }
   }
 
   public Object[] toArray(Object[] array)
+    throws NotImplementedException
   {
-    return children.keySet().toArray(array);
+    // This implementation is incorrect, I think.
+    synchronized (children)
+      {
+        return children.keySet().toArray(array);
+      }
   }
 
   protected boolean validatePendingAdd (Object targetChild)
-    throws NotImplementedException
   {
-    throw new Error ("Not implemented");
+    return true;
   }
 
   protected boolean validatePendingRemove (Object targetChild)
-    throws NotImplementedException
   {
-    throw new Error ("Not implemented");
+    return true;
   }
 
   public void vetoableChange (PropertyChangeEvent pce)

Reply via email to