crafterm 2002/07/09 03:40:26
Modified: fortress/src/java/org/apache/excalibur/fortress/lookup
FortressComponentManager.java
FortressComponentSelector.java
FortressServiceManager.java
Log:
Refactored Component/Service Manager and Component/Service Selector
classes to remove duplicated code.
ComponentManager is now implemented in terms of a ServiceManager, and
likewise for ComponentSelector.
Revision Changes Path
1.4 +12 -82
jakarta-avalon-excalibur/fortress/src/java/org/apache/excalibur/fortress/lookup/FortressComponentManager.java
Index: FortressComponentManager.java
===================================================================
RCS file:
/home/cvs/jakarta-avalon-excalibur/fortress/src/java/org/apache/excalibur/fortress/lookup/FortressComponentManager.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- FortressComponentManager.java 8 Jul 2002 11:58:58 -0000 1.3
+++ FortressComponentManager.java 9 Jul 2002 10:40:26 -0000 1.4
@@ -16,6 +16,7 @@
import org.apache.avalon.framework.service.ServiceManager;
import org.apache.excalibur.fortress.Container;
import org.apache.excalibur.fortress.handler.ComponentHandler;
+import org.apache.excalibur.fortress.lookup.FortressServiceManager;
/**
* This is the Default ComponentManager for the Container. It provides
@@ -23,13 +24,12 @@
* the references.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Berin Loritsch</a>
+ * @author <a href="mailto:[EMAIL PROTECTED]">Marcus Crafter</a>
* @version CVS $Revision$ $Date$
*/
public class FortressComponentManager implements ComponentManager
{
- private final Container m_components;
- private final BucketMap m_used;
- private final ComponentManager m_parent;
+ private final ServiceManager m_proxy;
/**
* This constructor is for a ContainerComponentManager with no parent
@@ -53,103 +53,33 @@
*/
public FortressComponentManager( final Container container, final
ComponentManager parent )
{
- m_parent = parent;
- m_components = container;
- m_used = new BucketMap();
+ m_proxy = new FortressServiceManager( container, parent );
}
public Component lookup( String role )
throws ComponentException
{
- Object temp = null;
-
try
{
- temp = m_components.get( role, null );
- }
- catch( ServiceException ce )
- {
- /* Logic is thus:
- * If we have the component and the get threw an exception, we need to
report that.
- * Otherwise, do the lookup on our parent
- * Otherwise, just throw the exception
- */
- if( m_components.has( role, null ) )
- {
- throw new ComponentException( role, ce.getMessage(), ce);
-
- }
- else if( null != m_parent )
- {
- return m_parent.lookup( role );
- }
- else
- {
- throw new ComponentException( role, ce.getMessage(), ce);
- }
- }
-
- if( temp instanceof ComponentSelector )
- {
- return (Component)temp;
- }
-
- if( !( temp instanceof ComponentHandler ) )
- {
- throw new ComponentException( role, "Invalid entry in component
manager" );
+ return ( Component ) m_proxy.lookup( role );
}
-
- ComponentHandler handler = (ComponentHandler)temp;
-
- final Component component;
-
- try
+ catch ( ClassCastException e )
{
- if( !handler.isInitialized() )
- {
- handler.initialize();
- }
-
- component = (Component)handler.get();
+ throw new ComponentException( role, "FortressComponentManager only
supports Components", e );
}
- catch( ComponentException ce )
+ catch ( ServiceException e )
{
- throw ce; // rethrow
+ throw new ComponentException( role, "Exception thrown during lookup", e
);
}
- catch( Exception e )
- {
- throw new ComponentException( role, "Could not return a reference to
the Component", e );
- }
-
- m_used.put( component, handler );
-
- return component;
}
public boolean hasComponent( String role )
{
- final boolean hasComponent = m_components.has( role, null );
-
- if( !hasComponent && null != m_parent )
- {
- return m_parent.hasComponent( role );
- }
-
- return hasComponent;
+ return m_proxy.hasService( role );
}
public void release( Component component )
{
- final ComponentHandler handler;
-
- handler = (ComponentHandler)m_used.remove( component );
-
- if( null == handler && null != m_parent )
- {
- m_parent.release( component );
- return;
- }
-
- handler.put( component );
+ m_proxy.release( component );
}
}
1.3 +13 -46
jakarta-avalon-excalibur/fortress/src/java/org/apache/excalibur/fortress/lookup/FortressComponentSelector.java
Index: FortressComponentSelector.java
===================================================================
RCS file:
/home/cvs/jakarta-avalon-excalibur/fortress/src/java/org/apache/excalibur/fortress/lookup/FortressComponentSelector.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- FortressComponentSelector.java 8 Jul 2002 11:58:58 -0000 1.2
+++ FortressComponentSelector.java 9 Jul 2002 10:40:26 -0000 1.3
@@ -12,8 +12,10 @@
import org.apache.avalon.framework.component.ComponentException;
import org.apache.avalon.framework.component.ComponentSelector;
import org.apache.avalon.framework.service.ServiceException;
+import org.apache.avalon.framework.service.ServiceSelector;
import org.apache.excalibur.fortress.Container;
import org.apache.excalibur.fortress.handler.ComponentHandler;
+import org.apache.excalibur.fortress.lookup.FortressServiceSelector;
/**
* This is the Default ComponentSelector for the Container. It provides
@@ -21,79 +23,44 @@
* the references.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Berin Loritsch</a>
+ * @author <a href="mailto:[EMAIL PROTECTED]">Marcus Crafter</a>
* @version CVS $Revision$ $Date$
*/
public class FortressComponentSelector implements ComponentSelector
{
+ private final ServiceSelector m_proxy;
private final String m_role;
- private final Container m_components;
- private final BucketMap m_used;
public FortressComponentSelector( final Container container, final String role )
{
+ m_proxy = new FortressServiceSelector( container, role );
m_role = role;
- m_components = container;
- m_used = new BucketMap();
}
public Component select( final Object hint )
throws ComponentException
{
- if( null == hint )
- {
- throw new IllegalArgumentException( "hint cannot be null" );
- }
-
- ComponentHandler handler = null;
try
{
- handler = (ComponentHandler)m_components.get( m_role, hint );
- } catch (ServiceException se)
- {
- throw new ComponentException( hint.toString(), se.getMessage(), se);
+ return (Component) m_proxy.select( hint );
}
-
- if( null == handler )
+ catch( ClassCastException e )
{
- throw new ComponentException( m_role + "/" + hint.toString(), "The hint
does not exist in the ComponentSelector" );
+ throw new ComponentException( m_role, "FortressComponentSelector only
supports Components", e );
}
-
- final Component component;
-
- try
+ catch( ServiceException e )
{
- if( !handler.isInitialized() )
- {
- handler.initialize();
- }
-
- component = (Component)handler.get();
+ throw new ComponentException( m_role, "Exception thrown during select",
e );
}
- catch( ComponentException ce )
- {
- throw ce; // rethrow
- }
- catch( Exception e )
- {
- throw new ComponentException( m_role + "/" + hint.toString(), "Could
not return a reference to the Component", e );
- }
-
- m_used.put( component, handler );
-
- return component;
}
public boolean hasComponent( Object hint )
{
- return m_components.has( m_role, hint );
+ return m_proxy.isSelectable( hint );
}
public void release( Component component )
{
- final ComponentHandler handler;
-
- handler = (ComponentHandler)m_used.remove( component );
-
- handler.put( component );
+ m_proxy.release( component );
}
}
1.3 +3 -2
jakarta-avalon-excalibur/fortress/src/java/org/apache/excalibur/fortress/lookup/FortressServiceManager.java
Index: FortressServiceManager.java
===================================================================
RCS file:
/home/cvs/jakarta-avalon-excalibur/fortress/src/java/org/apache/excalibur/fortress/lookup/FortressServiceManager.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- FortressServiceManager.java 8 Jul 2002 11:58:58 -0000 1.2
+++ FortressServiceManager.java 9 Jul 2002 10:40:26 -0000 1.3
@@ -9,6 +9,7 @@
import org.apache.avalon.excalibur.collections.BucketMap;
import org.apache.avalon.framework.component.ComponentManager;
+import org.apache.avalon.framework.component.ComponentSelector;
import org.apache.avalon.framework.service.ServiceException;
import org.apache.avalon.framework.service.ServiceManager;
import org.apache.avalon.framework.service.ServiceSelector;
@@ -89,7 +90,7 @@
}
}
- if( temp instanceof ServiceSelector )
+ if( temp instanceof ServiceSelector || temp instanceof ComponentSelector )
{
return temp;
}
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>