Lets make this lookup thing lazy:
String role;
try
{
role = (String)clazz.getField("ROLE");
}
catch( NoSuchFieldException nsfe )
{
// if DataSourceComponent.ROLE does not exist, use the
// class name. This means putting ROLE in the interface
// is not required, though supported.
role = clazz.getName();
}
Better yet, lets have an array of roles, in case a single
class/interface performs multiple roles:
String[] role;
try
{
role = (String[])DataSourceComponent.class.getField("ROLE");
}
catch( NoSuchFieldException nsfe )
{
role = new String[] { DataSourceComponent.class.getName() };
}
With regard to the lookup, we can easily have:
(DataSourceComponent)manager.lookup( MyClass.class );
(DataSourceComponent)manager.lookup( MyClass.ROLE );
(DataSourceComponent)manager.lookup( MyClass.class.getName() );
(DataSourceComponent)manager.lookup( "org.apache.avalon.excalibur.MyClass" )
;
String[] myroles = getSomeStringArraySomewhere();
(DataSourceComponent)manager.lookup( myclass );
Anyway, the complete code becomes:
ComponentManager {
//...
lookup( final Class clazz ) throws RoleNotFoundException
{
String[] role;
try
{
return lookup( (String[])clazz.getField("ROLE") );
}
catch( NoSuchFieldException nsfe )
{
return lookup( clazz.getName() );
}
}
lookup( final String role ) throws RoleNotFoundException
{
return lookup( new String[] { role } );
}
lookup( final String[] roles ) throws RoleNotFoundException
{
try
{
m_hashmap.get( roles );
}
catch( Exception e ) { throw new RoleNotFoundException( e ); }
}
//...
}
Did I just make everybody happy? =)
oh, a note on Component, ComponentManager and ComponentSelector:
you should read the design docs on those (written by Berin,
translated into slightly better readable English by me). The
concept is far from simple but essential to the framework.
LSD
PS: if anyone has an old TCL book lying around unused somewhere,
feel free to send it my way - I have to learn the language for
my new job...
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]