bloritsch 01/04/20 13:48:36 Modified: src/java/org/apache/excalibur/component DefaultComponentHandler.java DefaultComponentManager.java DefaultComponentSelector.java src/java/org/apache/excalibur/datasource JdbcConnectionPool.java Added: src/java/org/apache/excalibur/component ComponentHandler.java PoolableComponentHandler.java ThreadSafeComponentHandler.java Log: Fixes for ComponentManagement architecture and fix for JdbcConnectionPool so that it checks to see if the connection has timed out before returning the instance. Revision Changes Path 1.2 +15 -167 jakarta-avalon/src/java/org/apache/excalibur/component/DefaultComponentHandler.java Index: DefaultComponentHandler.java =================================================================== RCS file: /home/cvs/jakarta-avalon/src/java/org/apache/excalibur/component/DefaultComponentHandler.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- DefaultComponentHandler.java 2001/04/18 13:16:36 1.1 +++ DefaultComponentHandler.java 2001/04/20 20:48:34 1.2 @@ -15,8 +15,6 @@ import org.apache.avalon.configuration.Configuration; import org.apache.avalon.context.Context; import org.apache.avalon.logger.AbstractLoggable; -import org.apache.avalon.thread.ThreadSafe; -import org.apache.excalibur.pool.Poolable; import org.apache.log.Logger; /** @@ -24,33 +22,14 @@ * and destroyed correctly. * * @author <a href="mailto:[EMAIL PROTECTED]">Berin Loritsch</a> - * @version CVS $Revision: 1.1 $ $Date: 2001/04/18 13:16:36 $ + * @version CVS $Revision: 1.2 $ $Date: 2001/04/20 20:48:34 $ */ class DefaultComponentHandler - extends AbstractLoggable - implements Initializable, Disposable + extends ComponentHandler { - /** Indicates that the Handler is holding a <code>ThreadSafe</code> Component */ - private final static int THREADSAFE = 0; - - /** Indicates that the Handler is holding a <code>Poolable</code> Component */ - private final static int POOLABLE = 1; - - /** Indicates that the Handler is holding a <code>SingleThreaded</code> Component */ - private final static int SINGLETHREADED = 2; - /** The instance of the ComponentFactory that creates and disposes of the Component */ - private DefaultComponentFactory m_factory; - - /** The pool of components for <code>Poolable</code> Components */ - private DefaultComponentPool m_pool; + private final DefaultComponentFactory m_factory; - /** The instance of the Component for <code>ThreadSafe</code> Components */ - private Component m_instance; - - /** The type of the Component: THREADSAFE, POOLABLE, or SINGLETHREADED */ - private final int m_type; - /** State management boolean stating whether the Handler is initialized or not */ private boolean m_initialized = false; @@ -62,7 +41,7 @@ * whether a Component is ThreadSafe, Poolable, or SingleThreaded. * It falls back to SingleThreaded if not specified. */ - DefaultComponentHandler( final Class componentClass, + protected DefaultComponentHandler( final Class componentClass, final Configuration config, final ComponentManager manager, final Context context, @@ -70,48 +49,14 @@ throws Exception { m_factory = new DefaultComponentFactory( componentClass, config, manager, context, roles ); - - if( Poolable.class.isAssignableFrom( componentClass ) ) - { - m_pool = new DefaultComponentPool( m_factory ); - m_type = POOLABLE; - } - else if( ThreadSafe.class.isAssignableFrom( componentClass ) ) - { - m_type = THREADSAFE; - } - else - { - m_type = SINGLETHREADED; - } } /** - * Create a ComponentHandler that takes care of hiding the details of - * whether a Component is ThreadSafe, Poolable, or SingleThreaded. - * It falls back to SingleThreaded if not specified. - */ - DefaultComponentHandler( final Component component ) - throws Exception - { - m_type = THREADSAFE; - m_instance = component; - } - - /** * Sets the logger that the ComponentHandler will use. */ public void setLogger( final Logger logger ) { - if( null != m_factory ) - { - m_factory.setLogger( logger ); - } - - if( null != m_pool ) - { - m_pool.setLogger( logger ); - } + m_factory.setLogger( logger ); super.setLogger( logger ); } @@ -123,39 +68,7 @@ { if( m_initialized ) return; - switch( m_type ) - { - case THREADSAFE: - try - { - if( null == m_instance ) - { - m_instance = (Component)m_factory.newInstance(); - } - } - catch( final Exception e ) - { - getLogger().error( "Cannot use component: " + - m_factory.getCreatedClass().getName(), e ); - } - break; - - case POOLABLE: - try - { - m_pool.init(); - } - catch( Exception e ) - { - getLogger().error( "Cannot use component: " + m_factory.getCreatedClass().getName(), e ); - } - break; - - default: - // Nothing to do for SingleThreaded Components - break; - } - + getLogger().debug("ComponentHandler initialized for: " + this.m_factory.getCreatedClass().getName()); m_initialized = true; } @@ -174,25 +87,8 @@ { throw new IllegalStateException( "You cannot get a component from a disposed holder" ); } - - Component component = null; - switch( m_type ) - { - case THREADSAFE: - component = m_instance; - break; - - case POOLABLE: - component = (Component)m_pool.get(); - break; - - default: - component = (Component)m_factory.newInstance(); - break; - } - - return component; + return (Component)m_factory.newInstance(); } /** @@ -210,27 +106,14 @@ throw new IllegalStateException( "You cannot put a component in a disposed holder" ); } - switch( m_type ) + try { - case THREADSAFE: - // Nothing to do for ThreadSafe Components - break; - - case POOLABLE: - m_pool.put( (Poolable)component ); - break; - - default: - try - { - m_factory.decommission( component ); - } - catch( final Exception e ) - { - getLogger().warn( "Error decommissioning component: " + - m_factory.getCreatedClass().getName(), e); - } - break; + m_factory.decommission( component ); + } + catch( final Exception e ) + { + getLogger().warn( "Error decommissioning component: " + + m_factory.getCreatedClass().getName(), e); } } @@ -243,47 +126,12 @@ try { - switch( m_type ) - { - case THREADSAFE: - if( null != m_factory ) - { - m_factory.decommission( m_instance ); - } - else - { - if( m_instance instanceof Stoppable ) - { - ((Stoppable)m_instance).stop(); - } - - if( m_instance instanceof Disposable ) - { - ((Disposable)m_instance).dispose(); - } - } - m_instance = null; - break; - - case POOLABLE: - if( m_pool instanceof Disposable ) - { - ((Disposable)m_pool).dispose(); - } - - m_pool = null; - break; - - default: - // do nothing here - break; - } + // do nothing here if( m_factory instanceof Disposable ) { ((Disposable)m_factory).dispose(); } - m_factory = null; } catch( final Exception e ) { 1.2 +35 -18 jakarta-avalon/src/java/org/apache/excalibur/component/DefaultComponentManager.java Index: DefaultComponentManager.java =================================================================== RCS file: /home/cvs/jakarta-avalon/src/java/org/apache/excalibur/component/DefaultComponentManager.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- DefaultComponentManager.java 2001/04/18 13:16:36 1.1 +++ DefaultComponentManager.java 2001/04/20 20:48:34 1.2 @@ -32,7 +32,7 @@ * * @author <a href="mailto:[EMAIL PROTECTED]">Berin Loritsch</a> * @author <a href="mailto:[EMAIL PROTECTED]">Paul Russell</a> - * @version CVS $Revision: 1.1 $ $Date: 2001/04/18 13:16:36 $ + * @version CVS $Revision: 1.2 $ $Date: 2001/04/20 20:48:34 $ */ public class DefaultComponentManager extends AbstractLoggable @@ -87,10 +87,18 @@ while( keys.hasNext() ) { final Object key = keys.next(); - final DefaultComponentHandler handler = - (DefaultComponentHandler)m_componentHandlers.get( key ); + final ComponentHandler handler = + (ComponentHandler)m_componentHandlers.get( key ); - handler.dispose(); + try + { + handler.dispose(); + } + catch (Exception e) + { + getLogger().debug("Caught an exception trying to dispose of the component handler.", e); + } + keyList.add( key ); } @@ -127,7 +135,7 @@ throw new ComponentException( message ); } - DefaultComponentHandler handler = (DefaultComponentHandler)m_componentHandlers.get( role ); + ComponentHandler handler = (ComponentHandler)m_componentHandlers.get( role ); // Retrieve the instance of the requested component if( null == handler ) @@ -144,11 +152,11 @@ final Configuration configuration = new DefaultConfiguration( "", "-" ); handler = - new DefaultComponentHandler( componentClass, - configuration, - this, - m_context, - m_roles ); + ComponentHandler.getComponentHandler( componentClass, + configuration, + this, + m_context, + m_roles ); handler.setLogger( getLogger() ); handler.init(); @@ -177,10 +185,9 @@ } catch( final IllegalStateException ise ) { - handler.init(); - try { + handler.init(); component = handler.get(); } catch( final Exception e ) @@ -279,12 +286,20 @@ { if( null == component ) return; - final DefaultComponentHandler handler = - (DefaultComponentHandler)m_componentMapping.get( component ); + final ComponentHandler handler = + (ComponentHandler)m_componentMapping.get( component ); if( null != handler ) { - handler.put( component ); + try + { + handler.put( component ); + } + catch (Exception e) + { + getLogger().debug("Error trying to release component.", e); + } + m_componentMapping.remove( component ); } } @@ -301,9 +316,11 @@ { try { - final DefaultComponentHandler handler = - new DefaultComponentHandler( component, configuration, this, m_context, m_roles ); + getLogger().debug("Attempting to get Handler for: " + role); + final ComponentHandler handler = + ComponentHandler.getComponentHandler( component, configuration, this, m_context, m_roles ); + getLogger().debug("Handler type = " + handler.getClass().getName()); handler.setLogger( getLogger() ); m_componentHandlers.put( role, handler ); } @@ -321,7 +338,7 @@ { try { - DefaultComponentHandler handler = new DefaultComponentHandler( (Component)instance ); + ComponentHandler handler = ComponentHandler.getComponentHandler( (Component)instance ); handler.setLogger( getLogger() ); m_componentHandlers.put( role, handler ); } 1.2 +25 -16 jakarta-avalon/src/java/org/apache/excalibur/component/DefaultComponentSelector.java Index: DefaultComponentSelector.java =================================================================== RCS file: /home/cvs/jakarta-avalon/src/java/org/apache/excalibur/component/DefaultComponentSelector.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- DefaultComponentSelector.java 2001/04/18 13:16:36 1.1 +++ DefaultComponentSelector.java 2001/04/20 20:48:34 1.2 @@ -33,7 +33,7 @@ * * @author <a href="mailto:[EMAIL PROTECTED]">Berin Loritsch</a> * @author <a href="mailto:[EMAIL PROTECTED]">Paul Russell</a> - * @version CVS $Revision: 1.1 $ $Date: 2001/04/18 13:16:36 $ + * @version CVS $Revision: 1.2 $ $Date: 2001/04/20 20:48:34 $ */ public class DefaultComponentSelector extends AbstractLoggable @@ -121,10 +121,15 @@ while( keys.hasNext() ) { Object key = keys.next(); - DefaultComponentHandler handler = - (DefaultComponentHandler)m_componentHandlers.get( key ); + ComponentHandler handler = + (ComponentHandler)m_componentHandlers.get( key ); - handler.dispose(); + try { + handler.dispose(); + } catch (Exception e) { + getLogger().debug("Caught an exception disposing of component handler.", e); + } + keyList.add( key ); } @@ -159,7 +164,7 @@ throw new ComponentException( message ); } - DefaultComponentHandler handler = (DefaultComponentHandler)m_componentHandlers.get( hint ); + ComponentHandler handler = (ComponentHandler)m_componentHandlers.get( hint ); // Retrieve the instance of the requested component if( null == handler ) @@ -259,12 +264,16 @@ { if( null == component ) return; - final DefaultComponentHandler handler = - (DefaultComponentHandler)m_componentMapping.get( component ); + final ComponentHandler handler = + (ComponentHandler)m_componentMapping.get( component ); if( null == handler ) return; - handler.put( component ); + try { + handler.put( component ); + } catch (Exception e) { + getLogger().debug("Error trying to release component", e); + } m_componentMapping.remove( component ); } @@ -281,12 +290,12 @@ { try { - final DefaultComponentHandler handler = - new DefaultComponentHandler( component, - configuration, - m_componentManager, - m_context, - m_roles ); + final ComponentHandler handler = + ComponentHandler.getComponentHandler( component, + configuration, + m_componentManager, + m_context, + m_roles ); handler.setLogger( getLogger() ); handler.init(); @@ -310,8 +319,8 @@ { try { - final DefaultComponentHandler handler = - new DefaultComponentHandler( (Component)instance ); + final ComponentHandler handler = + ComponentHandler.getComponentHandler( (Component)instance ); handler.setLogger( getLogger() ); handler.init(); m_componentHandlers.put( hint, handler ); 1.1 jakarta-avalon/src/java/org/apache/excalibur/component/ComponentHandler.java Index: ComponentHandler.java =================================================================== /* * Copyright (C) The Apache Software Foundation. All rights reserved. * * This software is published under the terms of the Apache Software License * version 1.1, a copy of which has been included with this distribution in * the LICENSE file. */ package org.apache.excalibur.component; import org.apache.avalon.logger.AbstractLoggable; import org.apache.avalon.thread.ThreadSafe; import org.apache.avalon.thread.SingleThreaded; import org.apache.avalon.Initializable; import org.apache.avalon.Disposable; import org.apache.avalon.component.Component; import org.apache.avalon.configuration.Configuration; import org.apache.avalon.component.ComponentManager; import org.apache.avalon.context.Context; import org.apache.excalibur.pool.Poolable; /** * The DefaultComponentHandler to make sure components are initialized * and destroyed correctly. * * @author <a href="mailto:[EMAIL PROTECTED]">Berin Loritsch</a> * @version CVS $Revision: 1.1 $ $Date: 2001/04/20 20:48:33 $ */ public abstract class ComponentHandler extends AbstractLoggable implements Initializable, Disposable { public static ComponentHandler getComponentHandler( final Class componentClass, final Configuration config, final ComponentManager manager, final Context context, final RoleManager roles ) throws Exception { int numInterfaces = 0; if (SingleThreaded.class.isAssignableFrom(componentClass)) { numInterfaces++; } if (ThreadSafe.class.isAssignableFrom(componentClass)) { numInterfaces++; } if (Poolable.class.isAssignableFrom(componentClass)) { numInterfaces++; } if (numInterfaces > 1) { throw new Exception("[CONFLICT] lifestyle interfaces: " + componentClass.getName()); } if (Poolable.class.isAssignableFrom(componentClass)) { return new PoolableComponentHandler(componentClass, config, manager, context, roles); } else if (ThreadSafe.class.isAssignableFrom(componentClass)) { return new ThreadSafeComponentHandler(componentClass, config, manager, context, roles); } else // This is a SingleThreaded component { return new DefaultComponentHandler(componentClass, config, manager, context, roles); } } public static ComponentHandler getComponentHandler( final Component componentInstance ) throws Exception { int numInterfaces = 0; if (SingleThreaded.class.isAssignableFrom(componentInstance.getClass())) { numInterfaces++; } if (ThreadSafe.class.isAssignableFrom(componentInstance.getClass())) { numInterfaces++; } if (Poolable.class.isAssignableFrom(componentInstance.getClass())) { numInterfaces++; } if (numInterfaces > 1) { throw new Exception("[CONFLICT] lifestyle interfaces: " + componentInstance.getClass().getName()); } return new ThreadSafeComponentHandler(componentInstance); } public abstract Component get() throws Exception; public abstract void put(Component component) throws Exception; } 1.1 jakarta-avalon/src/java/org/apache/excalibur/component/PoolableComponentHandler.java Index: PoolableComponentHandler.java =================================================================== /* * Copyright (C) The Apache Software Foundation. All rights reserved. * * This software is published under the terms of the Apache Software License * version 1.1, a copy of which has been included with this distribution in * the LICENSE file. */ package org.apache.excalibur.component; import org.apache.excalibur.pool.Poolable; import org.apache.avalon.Disposable; import org.apache.avalon.component.Component; import org.apache.avalon.component.ComponentManager; import org.apache.avalon.configuration.Configuration; import org.apache.avalon.context.Context; import org.apache.log.Logger; /** * The PoolableComponentHandler to make sure components are initialized * and destroyed correctly. * * @author <a href="mailto:[EMAIL PROTECTED]">Berin Loritsch</a> * @version CVS $Revision: 1.1 $ $Date: 2001/04/20 20:48:34 $ */ public class PoolableComponentHandler extends ComponentHandler { /** The instance of the ComponentFactory that creates and disposes of the Component */ private final DefaultComponentFactory m_factory; /** The pool of components for <code>Poolable</code> Components */ private final DefaultComponentPool m_pool; /** State management boolean stating whether the Handler is initialized or not */ private boolean m_initialized = false; /** State management boolean stating whether the Handler is disposed or not */ private boolean m_disposed = false; /** * Create a ComponentHandler that takes care of hiding the details of * whether a Component is ThreadSafe, Poolable, or SingleThreaded. * It falls back to SingleThreaded if not specified. */ protected PoolableComponentHandler( final Class componentClass, final Configuration config, final ComponentManager manager, final Context context, final RoleManager roles ) throws Exception { m_factory = new DefaultComponentFactory( componentClass, config, manager, context, roles ); m_pool = new DefaultComponentPool( m_factory ); } /** * Sets the logger that the ComponentHandler will use. */ public void setLogger( final Logger logger ) { m_factory.setLogger( logger ); m_pool.setLogger( logger ); super.setLogger( logger ); } /** * Initialize the ComponentHandler. */ public void init() { if( m_initialized ) return; try { m_pool.init(); } catch( Exception e ) { getLogger().error( "Cannot use component: " + m_factory.getCreatedClass().getName(), e ); } getLogger().debug("ComponentHandler initialized for: " + this.m_factory.getCreatedClass().getName()); m_initialized = true; } /** * Get a reference of the desired Component */ public Component get() throws Exception { if( ! m_initialized ) { throw new IllegalStateException( "You cannot get a component from an uninitialized holder." ); } if( m_disposed ) { throw new IllegalStateException( "You cannot get a component from a disposed holder" ); } return (Component)m_pool.get(); } /** * Return a reference of the desired Component */ public void put( final Component component ) { if( !m_initialized ) { throw new IllegalStateException( "You cannot put a component in an uninitialized holder." ); } if( m_disposed ) { throw new IllegalStateException( "You cannot put a component in a disposed holder" ); } m_pool.put( (Poolable)component ); } /** * Dispose of the ComponentHandler and any associated Pools and Factories. */ public void dispose() { m_disposed = true; try { if( m_pool instanceof Disposable ) { ((Disposable)m_pool).dispose(); } if( m_factory instanceof Disposable ) { ((Disposable)m_factory).dispose(); } } catch( final Exception e ) { getLogger().warn( "Error decommissioning component: " + m_factory.getCreatedClass().getName(), e ); } } } 1.1 jakarta-avalon/src/java/org/apache/excalibur/component/ThreadSafeComponentHandler.java Index: ThreadSafeComponentHandler.java =================================================================== /* * Copyright (C) The Apache Software Foundation. All rights reserved. * * This software is published under the terms of the Apache Software License * version 1.1, a copy of which has been included with this distribution in * the LICENSE file. */ package org.apache.excalibur.component; import org.apache.avalon.component.Component; import org.apache.avalon.configuration.Configuration; import org.apache.avalon.component.ComponentManager; import org.apache.avalon.context.Context; import org.apache.avalon.Stoppable; import org.apache.avalon.Disposable; import org.apache.log.Logger; /** * The ThreadSafeComponentHandler to make sure components are initialized * and destroyed correctly. * * @author <a href="mailto:[EMAIL PROTECTED]">Berin Loritsch</a> * @version CVS $Revision: 1.1 $ $Date: 2001/04/20 20:48:34 $ */ public class ThreadSafeComponentHandler extends ComponentHandler { private Component m_instance; private final DefaultComponentFactory m_factory; private boolean m_initialized = false; private boolean m_disposed = false; /** * Create a ComponentHandler that takes care of hiding the details of * whether a Component is ThreadSafe, Poolable, or SingleThreaded. * It falls back to SingleThreaded if not specified. */ protected ThreadSafeComponentHandler( final Class componentClass, final Configuration config, final ComponentManager manager, final Context context, final RoleManager roles ) throws Exception { m_factory = new DefaultComponentFactory( componentClass, config, manager, context, roles ); } /** * Create a ComponentHandler that takes care of hiding the details of * whether a Component is ThreadSafe, Poolable, or SingleThreaded. * It falls back to SingleThreaded if not specified. */ protected ThreadSafeComponentHandler( final Component component ) throws Exception { m_instance = component; m_factory = null; } public void setLogger(Logger log) { if (m_factory != null) { m_factory.setLogger(log); } super.setLogger(log); } /** * Initialize the ComponentHandler. */ public void init() throws Exception { if( m_initialized ) return; if (m_instance == null) { m_instance = (Component) this.m_factory.newInstance(); } if (this.m_factory != null) { getLogger().debug("ComponentHandler initialized for: " + this.m_factory.getCreatedClass().getName()); } else { getLogger().debug("ComponentHandler initialized for: " + this.m_instance.getClass().getName()); } m_initialized = true; } /** * Get a reference of the desired Component */ public final Component get() throws Exception { if( ! m_initialized ) { throw new IllegalStateException( "You cannot get a component from an uninitialized holder." ); } if( m_disposed ) { throw new IllegalStateException( "You cannot get a component from a disposed holder" ); } return m_instance; } /** * Return a reference of the desired Component */ public void put( final Component component ) { if( !m_initialized ) { throw new IllegalStateException( "You cannot put a component in an uninitialized holder." ); } if( m_disposed ) { throw new IllegalStateException( "You cannot put a component in a disposed holder" ); } } /** * Dispose of the ComponentHandler and any associated Pools and Factories. */ public void dispose() { m_disposed = true; try { if( null != m_factory ) { m_factory.decommission( m_instance ); } else { if( m_instance instanceof Stoppable ) { ((Stoppable)m_instance).stop(); } if( m_instance instanceof Disposable ) { ((Disposable)m_instance).dispose(); } } m_instance = null; } catch( final Exception e ) { getLogger().warn( "Error decommissioning component: " + m_factory.getCreatedClass().getName(), e ); } } } 1.3 +6 -1 jakarta-avalon/src/java/org/apache/excalibur/datasource/JdbcConnectionPool.java Index: JdbcConnectionPool.java =================================================================== RCS file: /home/cvs/jakarta-avalon/src/java/org/apache/excalibur/datasource/JdbcConnectionPool.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- JdbcConnectionPool.java 2001/04/18 13:15:48 1.2 +++ JdbcConnectionPool.java 2001/04/20 20:48:35 1.3 @@ -26,7 +26,7 @@ * thread to manage the number of SQL Connections. * * @author <a href="mailto:[EMAIL PROTECTED]">Berin Loritsch</a> - * @version CVS $Revision: 1.2 $ $Date: 2001/04/18 13:15:48 $ + * @version CVS $Revision: 1.3 $ $Date: 2001/04/20 20:48:35 $ */ public class JdbcConnectionPool extends AbstractLoggable @@ -147,6 +147,11 @@ else { obj = (Poolable)m_ready.remove( 0 ); + if (((Connection)obj).isClosed()) + { + ((JdbcConnection)obj).recycle(); + obj = this.createJdbcConnection(); + } m_active.add( obj ); } --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]