avalon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dona...@apache.org
Subject cvs commit: jakarta-avalon-excalibur/component/src/java/org/apache/avalon/excalibur/component DefaultComponentFactory.java
Date Fri, 12 Jul 2002 10:56:51 GMT
donaldp     2002/07/12 03:56:50

  Modified:    component/src/java/org/apache/avalon/excalibur/component
                        DefaultComponentFactory.java
  Log:
  Preliminary support for Serviceable
  
  Revision  Changes    Path
  1.5       +95 -4     jakarta-avalon-excalibur/component/src/java/org/apache/avalon/excalibur/component/DefaultComponentFactory.java
  
  Index: DefaultComponentFactory.java
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon-excalibur/component/src/java/org/apache/avalon/excalibur/component/DefaultComponentFactory.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- DefaultComponentFactory.java	13 Jun 2002 17:24:50 -0000	1.4
  +++ DefaultComponentFactory.java	12 Jul 2002 10:56:50 -0000	1.5
  @@ -28,6 +28,9 @@
   import org.apache.avalon.framework.parameters.Parameterizable;
   import org.apache.avalon.framework.parameters.Parameters;
   import org.apache.avalon.framework.thread.ThreadSafe;
  +import org.apache.avalon.framework.service.ServiceManager;
  +import org.apache.avalon.framework.service.ServiceException;
  +import org.apache.avalon.framework.service.Serviceable;
   
   /**
    * Factory for Avalon components.
  @@ -161,13 +164,24 @@
               ( (Contextualizable)component ).contextualize( m_context );
           }
   
  -        ComponentManager proxy = null;
  +        Object proxy = null;
   
           if( component instanceof Composable )
           {
               // wrap the real CM with a proxy, see below for more info
  -            proxy = new ComponentManagerProxy( m_componentManager );
  -            ( (Composable)component ).compose( proxy );
  +            final ComponentManagerProxy manager =
  +                new ComponentManagerProxy( m_componentManager );
  +            ( (Composable)component ).compose( manager );
  +            proxy = manager;
  +        }
  +
  +        if( component instanceof Serviceable )
  +        {
  +            // wrap the real CM with a proxy, see below for more info
  +            final ServiceManagerProxy manager =
  +                new ServiceManagerProxy( m_componentManager );
  +            ( (Serviceable)component ).service( manager );
  +            proxy = manager;
           }
   
           if( component instanceof RoleManageable )
  @@ -287,6 +301,11 @@
               ( (ComponentManagerProxy)m_components.get( component ) ).releaseAll();
           }
   
  +        if( component instanceof Serviceable )
  +        {
  +            ( (ServiceManagerProxy)m_components.get( component ) ).releaseAll();
  +        }
  +
           m_components.remove( component );
       }
   
  @@ -336,6 +355,78 @@
           }
   
           private synchronized void removeUnreleased( Component component )
  +        {
  +            m_unreleased.remove( component );
  +        }
  +
  +        /**
  +         * Releases all components that have been looked up through this
  +         * <code>ComponentLocator</code>, that have not yet been released
  +         * via user code.
  +         */
  +        private void releaseAll()
  +        {
  +            Component[] unreleased;
  +
  +            synchronized( this )
  +            {
  +                unreleased = new Component[ m_unreleased.size() ];
  +                m_unreleased.toArray( unreleased );
  +            }
  +
  +            for( int i = 0; i < unreleased.length; i++ )
  +            {
  +                release( unreleased[ i ] );
  +            }
  +        }
  +    }
  +
  +    private static class ServiceManagerProxy
  +        implements ServiceManager
  +    {
  +        private final ComponentManager m_realManager;
  +        private final Collection m_unreleased = new ArrayList();
  +
  +        ServiceManagerProxy( final ComponentManager manager )
  +        {
  +            m_realManager = manager;
  +        }
  +
  +        public Object lookup( final String role )
  +            throws ServiceException
  +        {
  +            try
  +            {
  +                final Object component = m_realManager.lookup( role );
  +                addUnreleased( component );
  +                return component;
  +            }
  +            catch( ComponentException e )
  +            {
  +                throw new ServiceException( e.getRole(),
  +                                            e.getMessage(),
  +                                            e );
  +            }
  +        }
  +
  +        public boolean hasService( final String role )
  +        {
  +            return m_realManager.hasComponent( role );
  +        }
  +
  +        public void release( final Object component )
  +        {
  +            removeUnreleased( component );
  +
  +            m_realManager.release( (Component)component );
  +        }
  +
  +        private synchronized void addUnreleased( final Object component )
  +        {
  +            m_unreleased.add( component );
  +        }
  +
  +        private synchronized void removeUnreleased( final Object component )
           {
               m_unreleased.remove( component );
           }
  
  
  

--
To unsubscribe, e-mail:   <mailto:avalon-cvs-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:avalon-cvs-help@jakarta.apache.org>


Mime
View raw message