CVS: plexus-container/src/java/org/apache/plexus/service/repository DefaultComponentRepository.java,1.5,1.6

[email protected] Thu, 12 Jun 2003 13:04:12 -0500
Newsgroups gmane.comp.java.plexus.devel
Message-ID <[email protected]>
Update of /cvsroot/plexus/plexus-container/src/java/org/apache/plexus/service/repository
In directory eng.werken.com:/tmp/cvs-serv18191/src/java/org/apache/plexus/service/repository

Modified Files:
	DefaultComponentRepository.java 
Log Message:
Two changes:
                                                                                                        
   - Added synchronized keyword to various public methods as there was
     not synchronization being done.  This would eventually lead to an
     inconsistent state and/or ConcurrentMoficationExceptions.
                                                                                                        
   - disposeAllComponents() actually works now.  When the container is
     shutting down, it releases any unreleased components so that they
     can be cleanly shutdown.



Index: DefaultComponentRepository.java
===================================================================
RCS file: /cvsroot/plexus/plexus-container/src/java/org/apache/plexus/service/repository/DefaultComponentRepository.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -d -r1.5 -r1.6
--- DefaultComponentRepository.java	12 Jun 2003 16:53:44 -0000	1.5
+++ DefaultComponentRepository.java	12 Jun 2003 18:04:05 -0000	1.6
@@ -264,7 +264,7 @@
     // Service lookup methods
     // ----------------------------------------------------------------------
 
-    public Object lookup( String key )
+    public synchronized Object lookup( String key )
         throws ServiceException
     {
         // Attempt to lookup the componentManager by key.
@@ -320,7 +320,7 @@
         return component;
     }
 
-    public Object lookup( String role, String id )
+    public synchronized Object lookup( String role, String id )
         throws ServiceException
     {
         return lookup( role + id );
@@ -329,12 +329,12 @@
     /**
      * @see org.apache.avalon.framework.service.ServiceManager#hasService(java.lang.String)
      */
-    public boolean hasService( String role )
+    public synchronized boolean hasService( String role )
     {
         return getComponentDescriptors().containsKey( role );
     }
 
-    public boolean hasService( String role, String id )
+    public synchronized boolean hasService( String role, String id )
     {
         return getComponentDescriptors().containsKey( role + id );
     }
@@ -344,7 +344,7 @@
      *
      * @see org.apache.avalon.framework.service.ServiceManager#release(java.lang.Object)
      */
-    public void release( Object component )
+    public synchronized void release( Object component )
     {
         ComponentHousing housing = (ComponentHousing) getComponentHousings().get( component );
 
@@ -382,9 +382,14 @@
     /**
      * @see org.apache.avalon.framework.activity.Disposable#dispose()
      */
-    public void dispose()
+    public synchronized void dispose()
     {
         disposeAllComponents();
+
+        // Making sure that everything is finally released.
+        // System.out.println("DEBUG>> descriptors = " + componentDescriptors.size());
+        // System.out.println("DEBUG>> managers    = " + componentManagers.size());
+        // System.out.println("DEBUG>> housings    = " + componentHousings.size());
     }
 
     /**
@@ -392,13 +397,16 @@
      */
     protected void disposeAllComponents()
     {
-        Set roles = getComponentManagers().keySet();
+        // Use an array to get the list of components; otherwise we'll
+        // end up with a ConcurrentModificationException if we use an
+        // Iterator to cycle through the set because release() makes
+        // changes to the set as well.
 
-        for ( Iterator i = roles.iterator(); i.hasNext(); )
+        Object components[] = getComponentHousings().keySet().toArray();
+
+        for ( int i = 0; i < components.length; i++ )
         {
-            String eachRole = (String) i.next();
-            //disposeComponents( (Collection) getComponentManagers().get( eachRole ) );
-            i.remove();
+            release( components[ i ] );
         }
     }