mcconnell 2003/07/05 00:59:09
Modified: merlin/composition maven.xml
merlin/composition/src/java/org/apache/avalon/composition/model/impl
DefaultClassLoaderModel.java
DefaultTypeRepository.java
Log:
Incorporation of a generic scanning utility class.
Revision Changes Path
1.2 +5 -5 avalon-sandbox/merlin/composition/maven.xml
Index: maven.xml
===================================================================
RCS file: /home/cvs/avalon-sandbox/merlin/composition/maven.xml,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- maven.xml 4 Jul 2003 07:27:32 -0000 1.1
+++ maven.xml 5 Jul 2003 07:59:08 -0000 1.2
@@ -13,23 +13,23 @@
<ant:jar jarfile="${basedir}/target/test-classes/ext/test-a.jar"
manifest="${basedir}/src/test/etc/a.mf"
- includes="**/testa/*.class" basedir="${basedir}/target/test-classes">
+ includes="**/testa/*.*" basedir="${basedir}/target/test-classes">
</ant:jar>
<ant:jar jarfile="${basedir}/target/test-classes/ext/test-b.jar"
manifest="${basedir}/src/test/etc/b.mf"
- includes="**/testb/*.class" basedir="${basedir}/target/test-classes">
+ includes="**/testb/*.*" basedir="${basedir}/target/test-classes">
</ant:jar>
<ant:jar jarfile="${basedir}/target/test-classes/lib/test-c.jar"
manifest="${basedir}/src/test/etc/c.mf"
- includes="**/testc/*.class" basedir="${basedir}/target/test-classes">
+ includes="**/testc/*.*" basedir="${basedir}/target/test-classes">
</ant:jar>
<ant:jar
jarfile="${basedir}/target/test-classes/repository/test/jars/test-d.jar"
manifest="${basedir}/src/test/etc/d.mf"
- includes="**/testd/*.class" basedir="${basedir}/target/test-classes">
+ includes="**/testd/*.*" basedir="${basedir}/target/test-classes">
</ant:jar>
<ant:jar
jarfile="${basedir}/target/test-classes/repository/test/jars/test-e.jar"
manifest="${basedir}/src/test/etc/e.mf"
- includes="**/teste/*.class" basedir="${basedir}/target/test-classes">
+ includes="**/teste/*.*" basedir="${basedir}/target/test-classes">
</ant:jar>
</postGoal>
1.3 +128 -59
avalon-sandbox/merlin/composition/src/java/org/apache/avalon/composition/model/impl/DefaultClassLoaderModel.java
Index: DefaultClassLoaderModel.java
===================================================================
RCS file:
/home/cvs/avalon-sandbox/merlin/composition/src/java/org/apache/avalon/composition/model/impl/DefaultClassLoaderModel.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- DefaultClassLoaderModel.java 4 Jul 2003 15:24:55 -0000 1.2
+++ DefaultClassLoaderModel.java 5 Jul 2003 07:59:09 -0000 1.3
@@ -62,7 +62,9 @@
import org.apache.avalon.composition.model.ClassLoaderModel;
import org.apache.avalon.composition.model.SystemContext;
import org.apache.avalon.composition.model.TypeRepository;
+import org.apache.avalon.composition.model.ServiceRepository;
import org.apache.avalon.composition.repository.Repository;
+import org.apache.avalon.composition.util.ExceptionHelper;
import org.apache.avalon.extension.Extension;
import org.apache.avalon.extension.manager.ExtensionManager;
import org.apache.avalon.extension.manager.OptionalPackage;
@@ -138,6 +140,8 @@
private final DefaultTypeRepository m_types;
+ private final DefaultServiceRepository m_services;
+
//==============================================================
// constructor
//==============================================================
@@ -155,49 +159,11 @@
* primative classpath entries and resource directives
*/
protected DefaultClassLoaderModel(
- final Logger logger, final SystemContext system, ClassLoader classLoader,
final ClassLoaderDirective directive )
+ final Logger logger, final SystemContext system,
+ ClassLoader classLoader, final ClassLoaderDirective directive )
throws Exception
{
- if( logger == null )
- {
- throw new NullPointerException( "logger" );
- }
- if( system == null )
- {
- throw new NullPointerException( "system" );
- }
- if( classLoader == null )
- {
- throw new NullPointerException( "classLoader" );
- }
- if( directive == null )
- {
- throw new NullPointerException( "directive" );
- }
-
- m_logger = logger;
- m_extension = new DefaultExtensionManager(
- directive.getLibrary().getOptionalExtensionDirectories(
system.getBaseDirectory() ) );
- m_manager = new PackageManager( m_extension );
- m_parent = null;
-
- //
- // generate the primitive classpath
- //
-
- m_classpath = createClassPath( system, directive );
- m_packages = buildOptionalPackages( m_classpath );
- m_urls = buildQualifiedClassPath();
- m_classLoader = new URLClassLoader(
- m_urls, classLoader );
-
- //
- // create a type repository
- //
-
- m_types = new DefaultTypeRepository( m_classLoader );
- m_types.enableLogging( logger.getChildLogger( "type" ) );
-
+ this( logger, system, classLoader, null, directive );
}
/**
@@ -217,6 +183,26 @@
ClassLoaderModel parent, ClassLoaderDirective directive )
throws Exception
{
+ this( logger, system, parent.getClassLoader(), parent, directive );
+ }
+
+ /**
+ * Creation of a new classloader model. The model associated a
+ * repository, a base directory and a classloader directive
+ * enabling the creation of a fully populated classpath.
+ *
+ * @param logger the assigned logging channel
+ * @param system the system context
+ * @param classloader the parent classloader
+ * @param parent the parent classloader model
+ * @param directive the classloader directive containing the
+ * primative classpath entries and resource directives
+ */
+ private DefaultClassLoaderModel(
+ final Logger logger, final SystemContext system,
+ ClassLoader classLoader, ClassLoaderModel parent, ClassLoaderDirective
directive )
+ throws Exception
+ {
if( logger == null )
{
throw new NullPointerException( "logger" );
@@ -225,9 +211,9 @@
{
throw new NullPointerException( "system" );
}
- if( parent == null )
+ if( classLoader == null )
{
- throw new NullPointerException( "parent" );
+ throw new NullPointerException( "classLoader" );
}
if( directive == null )
{
@@ -237,11 +223,22 @@
m_logger = logger;
m_parent = parent;
- DefaultExtensionManager local =
- new DefaultExtensionManager(
- directive.getLibrary().getOptionalExtensionDirectories(
system.getBaseDirectory() ) );
- m_extension = new DelegatingExtensionManager(
- new ExtensionManager[]{ parent.getExtensionManager(), local } );
+ if( parent != null )
+ {
+ DefaultExtensionManager local =
+ new DefaultExtensionManager(
+ directive.getLibrary().getOptionalExtensionDirectories(
+ system.getBaseDirectory() ) );
+ m_extension = new DelegatingExtensionManager(
+ new ExtensionManager[]{ parent.getExtensionManager(), local } );
+ }
+ else
+ {
+ m_extension = new DefaultExtensionManager(
+ directive.getLibrary().getOptionalExtensionDirectories(
+ system.getBaseDirectory() ) );
+ }
+
m_manager = new PackageManager( m_extension );
//
@@ -249,21 +246,46 @@
//
m_classpath = createClassPath( system, directive );
- m_packages = buildOptionalPackages(
- m_classpath, parent.getOptionalPackages( true ) );
+ if( parent != null )
+ {
+ m_packages = buildOptionalPackages(
+ m_classpath, parent.getOptionalPackages( true ) );
+ }
+ else
+ {
+ m_packages = buildOptionalPackages( m_classpath );
+ }
+
m_urls = buildQualifiedClassPath();
- m_classLoader = new URLClassLoader(
- m_urls, parent.getClassLoader() );
+ m_classLoader = new URLClassLoader( m_urls, classLoader );
//
// create a type repository
//
- m_types =
- new DefaultTypeRepository(
- m_classLoader, parent.getTypeRepository() );
- m_types.enableLogging( logger.getChildLogger( "type" ) );
+ ArrayList types = new ArrayList();
+ ArrayList services = new ArrayList();
+
+ Scanner scanner = new Scanner( classLoader );
+ scanner.enableLogging( logger.getChildLogger( "scanner" ) );
+ scanner.scan( m_urls, types, services );
+
+ if( parent != null )
+ {
+ m_types = new DefaultTypeRepository(
+ m_classLoader, parent.getTypeRepository(), types );
+ m_services = new DefaultServiceRepository(
+ m_classLoader, parent.getServiceRepository(), services );
+ }
+ else
+ {
+ m_types = new DefaultTypeRepository( m_classLoader, types );
+ m_services = new DefaultServiceRepository( m_classLoader, services );
+ }
+ m_types.enableLogging( logger.getChildLogger( "types" ) );
+ m_services.enableLogging( logger.getChildLogger( "services" ) );
+ m_types.initialize();
}
//==============================================================
@@ -282,6 +304,16 @@
}
/**
+ * Return the classloader model service repository.
+ *
+ * @return the repository
+ */
+ public ServiceRepository getServiceRepository()
+ {
+ return m_services;
+ }
+
+ /**
* Return the optional extensions manager.
* @return the extension manager
*/
@@ -357,6 +389,40 @@
// private implementation
//==============================================================
+ private Logger getLogger()
+ {
+ return m_logger;
+ }
+/*
+ private static void setUpRepositories(
+ ClassLoader classLoader, ClassLoaderModel parent, Logger logger, URL[] urls )
+ throws Exception
+ {
+ ArrayList types = new ArrayList();
+ ArrayList services = new ArrayList();
+
+ Scanner scanner = new Scanner( classLoader );
+ scanner.enableLogging( logger.getChildLogger( "scanner" ) );
+ scanner.scan( urls, types, services );
+
+ if( parent != null )
+ {
+ m_types = new DefaultTypeRepository(
+ m_classLoader, parent.getTypeRepository(), types );
+ m_services = new DefaultServiceRepository(
+ m_classLoader, parent.getServiceRepository(), services );
+ }
+ else
+ {
+ m_types = new DefaultTypeRepository( m_classLoader, types );
+ m_services = new DefaultServiceRepository( m_classLoader, services );
+ }
+
+ m_types.enableLogging( logger.getChildLogger( "types" ) );
+ m_services.enableLogging( logger.getChildLogger( "services" ) );
+
+ }
+*/
/**
* Build the fully qulalified classpath including extension jar files
* resolved relative to the classpath directives in the meta-data.
@@ -480,7 +546,8 @@
return consolidate( packages, established );
}
- private OptionalPackage[] consolidate( OptionalPackage[] includes,
OptionalPackage[] excludes )
+ private OptionalPackage[] consolidate(
+ OptionalPackage[] includes, OptionalPackage[] excludes )
{
ArrayList list = new ArrayList();
for( int i=0; i<includes.length; i++ )
@@ -499,7 +566,8 @@
list.add( inc );
}
}
- return (OptionalPackage[]) list.toArray( new OptionalPackage[0] );
+ return (OptionalPackage[]) list.toArray(
+ new OptionalPackage[0] );
}
private void addToClassPath( List list, File[] files )
@@ -557,4 +625,5 @@
}
return (Manifest[]) manifests.toArray( new Manifest[0] );
}
+
}
1.2 +50 -132
avalon-sandbox/merlin/composition/src/java/org/apache/avalon/composition/model/impl/DefaultTypeRepository.java
Index: DefaultTypeRepository.java
===================================================================
RCS file:
/home/cvs/avalon-sandbox/merlin/composition/src/java/org/apache/avalon/composition/model/impl/DefaultTypeRepository.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- DefaultTypeRepository.java 4 Jul 2003 07:27:39 -0000 1.1
+++ DefaultTypeRepository.java 5 Jul 2003 07:59:09 -0000 1.2
@@ -54,12 +54,16 @@
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Iterator;
+import java.util.List;
import org.apache.avalon.composition.model.TypeRepository;
import org.apache.avalon.composition.model.TypeException;
+import org.apache.avalon.composition.model.TypeRuntimeException;
import org.apache.avalon.composition.model.TypeDuplicateException;
import org.apache.avalon.composition.model.TypeUnknownException;
+import org.apache.avalon.composition.util.ExceptionHelper;
import org.apache.avalon.framework.logger.AbstractLogEnabled;
+import org.apache.avalon.framework.activity.Initializable;
import org.apache.avalon.meta.info.DependencyDescriptor;
import org.apache.avalon.meta.info.ReferenceDescriptor;
import org.apache.avalon.meta.info.ServiceDescriptor;
@@ -75,25 +79,10 @@
* @author <a href="mailto:[EMAIL PROTECTED]">Avalon Development Team</a>
* @version $Revision$ $Date$
*/
-class DefaultTypeRepository extends AbstractLogEnabled implements TypeRepository
+class DefaultTypeRepository extends AbstractLogEnabled implements TypeRepository,
Initializable
{
- //==============================================================
- // static
- //==============================================================
-
- /**
- * The type builder.
- */
- private static final TypeBuilder DEFAULT_BUILDER = new TypeBuilder();
- //==============================================================
- // state
- //==============================================================
-
- /**
- * The classloader supplied to the manager.
- */
- private ClassLoader m_classloader;
+ private final ClassLoader m_classloader;
/**
* The parent type manager (may be null)
@@ -105,149 +94,79 @@
*/
private final Hashtable m_types = new Hashtable();
+ private final List m_typesList;
+
//==============================================================
// constructor
//==============================================================
/**
* Creation of a new root type manager.
- * @param classloader the classloder to use
- * @exception NullPointerException if the classloader is null
+ * @param types the list of types local to the repository
+ * @exception NullPointerException if the type list is null
*/
- public DefaultTypeRepository( ClassLoader classloader ) throws
NullPointerException
+ public DefaultTypeRepository( ClassLoader classloader, List types )
{
- this( classloader, null );
+ this( classloader, null, types );
}
/**
* Creation of a new type manager.
- * @param classloader the classloder to use
* @param parent the parent type manager
- * @exception NullPointerException if the classloader is null
+ * @param types the list of types local to the repository
+ * @exception NullPointerException if the type list is null
*/
- public DefaultTypeRepository( ClassLoader classloader, TypeRepository parent )
- throws NullPointerException
+ public DefaultTypeRepository( ClassLoader classloader, TypeRepository parent,
List types )
{
- if( classloader == null )
+ if( types == null )
{
- throw new NullPointerException( "classloader" );
+ throw new NullPointerException( "types" );
}
- m_classloader = classloader;
m_parent = parent;
+ m_classloader = classloader;
+ m_typesList = types;
}
//==============================================================
- // DefaultTypeRepository
+ // lifecycle
//==============================================================
- /**
- * Create a new type instance. The type instance returned is not
- * registered with the manager. To register the type the client
- * must explicity declare the type using the [EMAIL PROTECTED] #addType}
- * operation.
- *
- * @param clazz the component implementation class
- * @return the component type
- * @exception TypeException is a type creation error occurs
- * @see #addType
- */
- public Type createType( Class clazz ) throws TypeException
- {
- if( clazz == null )
- {
- throw new NullPointerException( "clazz" );
- }
-
- try
- {
- return DEFAULT_BUILDER.build( clazz.getName(), clazz.getClassLoader() );
- }
- catch( Throwable e )
- {
- final String error =
- "Could not register a type relative to the path: "
- + clazz.getName()
- + " due to a type build error.";
- throw new TypeException( error, e );
- }
- }
-
- /**
- * Create a type instance based on a supplied classname and classloader.
- *
- * @param classname the component implementation classname
- * @return the component type
- * @exception TypeException is a type creation error occurs
- */
- public Type createType( String classname ) throws TypeException
- {
- if( classname == null )
- {
- throw new NullPointerException( "classname" );
- }
-
- try
- {
- Class clazz = m_classloader.loadClass( classname );
- return createType( clazz );
- }
- catch( Throwable e )
- {
- final String error =
- "Unexpected error while attempting to build a type from the
classname: "
- + classname;
- throw new TypeException( error, e );
- }
- }
-
- /**
- * Add a type to the manager.
- * @param type the component type description.
- * @exception TypeDuplicateException if the supplied type is already registered
- * @exception TypeException if a type verification failure occurs
- * @see #createType
- */
- public void addType( Type type ) throws TypeDuplicateException, TypeException
+ public void initialize() throws Exception
{
- if( type == null )
- {
- throw new NullPointerException( "type" );
- }
-
- if( getLogger() == null )
+ if( getLogger().isDebugEnabled() )
{
- throw new IllegalStateException( "logging" );
+ getLogger().debug( "type count: " + m_typesList.size() );
}
-
- final String classname = type.getInfo().getClassname();
-
- try
- {
- type = getType( classname );
- //throw new TypeDuplicateException( classname );
- return;
- } catch( TypeUnknownException ute )
+ Iterator iterator = m_typesList.iterator();
+ while( iterator.hasNext() )
{
+ Type type = (Type) iterator.next();
+ final String classname = type.getInfo().getClassname();
try
{
verify( type );
- } catch( Throwable e )
+ }
+ catch( Throwable e )
{
- final String error =
- "Could not register the type: " + classname
- + " due to a verification failure.";
- throw new TypeException( error, e );
+ if( getLogger().isWarnEnabled() )
+ {
+ final String error =
+ "Ignoring type due to verification error: " + classname;
+ getLogger().warn( ExceptionHelper.packException(
+ error, e, getLogger().isDebugEnabled() ) );
+ }
}
-
if( getLogger().isDebugEnabled() )
{
- getLogger().debug( "add: " + type );
+ getLogger().debug( "add: " + classname );
}
-
m_types.put( classname, type );
}
}
+ //==============================================================
+ // DefaultTypeRepository
+ //==============================================================
/**
* Locate a [EMAIL PROTECTED] Type} instances associated with the
* supplied implementation classname.
@@ -261,7 +180,6 @@
{
throw new NullPointerException( "clazz" );
}
-
return getType( clazz.getName() );
}
@@ -285,11 +203,13 @@
if( m_parent != null )
{
return m_parent.getType( classname );
- } else
+ }
+ else
{
throw new TypeUnknownException( classname );
}
}
+
return type;
}
@@ -388,7 +308,7 @@
* @param type the component type
* @return an array of classes represnting the type's service interfaces
*/
- private Class[] getServiceClasses( Type type ) throws TypeException
+ private Class[] getServiceClasses( Type type )
{
ArrayList list = new ArrayList();
ServiceDescriptor[] services = type.getServices();
@@ -423,8 +343,7 @@
try
{
return m_classloader.loadClass( classname );
- }
- catch( Throwable e )
+ } catch( Throwable e )
{
final String error =
"Could not load implementation class for component type: "
@@ -437,21 +356,20 @@
* Returns the service type implementation class.
* @param service the service type descriptor
* @return the class implementing the service type
- * @exception TypeException if the class could not be loaded
+ * @exception TypeRuntimeException if a classloader error occurs
*/
- private Class getServiceClass( ServiceDescriptor service ) throws TypeException
+ private Class getServiceClass( ServiceDescriptor service ) throws
TypeRuntimeException
{
final String classname = service.getReference().getClassname();
try
{
return m_classloader.loadClass( classname );
- }
- catch( Throwable e )
+ } catch( Throwable e )
{
final String error =
"Could not load implementation class for service type: "
+ classname;
- throw new TypeException( error, e );
+ throw new TypeRuntimeException( error, e );
}
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]