mcconnell 2002/07/07 16:12:44
Modified: assembly/src/java/org/apache/excalibur/merlin/registry
Registry.java
Log:
Was DefaultRegistry - now Registry is base type to DefaultContainer
Revision Changes Path
1.7 +368 -32
jakarta-avalon-excalibur/assembly/src/java/org/apache/excalibur/merlin/registry/Registry.java
Index: Registry.java
===================================================================
RCS file:
/home/cvs/jakarta-avalon-excalibur/assembly/src/java/org/apache/excalibur/merlin/registry/Registry.java,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -r1.6 -r1.7
--- Registry.java 7 Jul 2002 04:28:31 -0000 1.6
+++ Registry.java 7 Jul 2002 23:12:44 -0000 1.7
@@ -8,61 +8,397 @@
package org.apache.excalibur.merlin.registry;
-import org.apache.excalibur.meta.info.DependencyDescriptor;
-import org.apache.excalibur.meta.info.ServiceDescriptor;
+import java.io.InputStream;
+import java.io.File;
+import java.io.IOException;
+import java.net.URL;
+import java.net.JarURLConnection;
+import java.net.URLClassLoader;
+import java.util.List;
+import java.util.LinkedList;
+import java.util.Hashtable;
+import java.util.Properties;
+import java.util.Vector;
+import java.util.Iterator;
+import java.util.ArrayList;
+import java.util.jar.Attributes;
+import java.util.jar.Manifest;
+import java.util.Enumeration;
+import java.security.Policy;
+import java.io.FileInputStream;
+
+import org.apache.avalon.excalibur.i18n.ResourceManager;
+import org.apache.avalon.excalibur.i18n.Resources;
+import org.apache.excalibur.configuration.ConfigurationUtil;
+import org.apache.avalon.framework.logger.Logger;
+import org.apache.avalon.framework.logger.LogEnabled;
+import org.apache.avalon.framework.logger.AbstractLogEnabled;
+import org.apache.avalon.framework.logger.AvalonFormatter;
+import org.apache.avalon.framework.logger.LogKitLogger;
+import org.apache.avalon.framework.activity.Initializable;
+import org.apache.avalon.framework.activity.Disposable;
+import org.apache.avalon.framework.activity.Executable;
+import org.apache.avalon.framework.activity.Startable;
+import org.apache.avalon.framework.CascadingRuntimeException;
+import org.apache.avalon.framework.configuration.Configuration;
+import org.apache.avalon.framework.configuration.DefaultConfiguration;
+import org.apache.avalon.framework.configuration.DefaultConfigurationBuilder;
+import org.apache.avalon.framework.configuration.ConfigurationException;
+import org.apache.avalon.framework.configuration.Configurable;
+import org.apache.avalon.framework.context.Context;
+import org.apache.avalon.framework.context.Contextualizable;
+import org.apache.avalon.framework.context.ContextException;
+import org.apache.avalon.framework.context.DefaultContext;
+import org.apache.avalon.framework.service.Serviceable;
+import org.apache.avalon.framework.service.ServiceManager;
+import org.apache.avalon.framework.service.DefaultServiceManager;
+import org.apache.avalon.framework.service.ServiceException;
+import org.apache.avalon.framework.parameters.Parameters;
+import org.apache.avalon.framework.Version;
+import org.apache.avalon.framework.ExceptionUtil;
+import org.apache.avalon.excalibur.extension.PackageRepository;
+import org.apache.avalon.excalibur.extension.Extension;
+import org.apache.avalon.excalibur.extension.OptionalPackage;
+import org.apache.avalon.excalibur.extension.DefaultPackageRepository;
+import org.apache.excalibur.meta.info.DefaultType;
import org.apache.excalibur.meta.info.Type;
+import org.apache.excalibur.meta.info.ServiceDescriptor;
+import org.apache.excalibur.meta.info.DependencyDescriptor;
+import org.apache.excalibur.meta.info.ServiceDesignator;
import org.apache.excalibur.meta.data.Profile;
+import org.apache.excalibur.meta.data.DefaultProfile;
+import org.apache.excalibur.meta.data.Association;
+import org.apache.excalibur.meta.verifier.AssemblyVerifier;
+import org.apache.excalibur.meta.verifier.MetaDataVerifier;
+import org.apache.excalibur.meta.verifier.VerifyException;
+import org.apache.excalibur.configuration.ContextFactory;
+import org.apache.log.output.io.StreamTarget;
+import org.apache.log.Hierarchy;
+import org.apache.log.Priority;
+
+import org.apache.excalibur.merlin.kernel.Map;
import org.apache.excalibur.merlin.kernel.Container;
+import org.apache.excalibur.merlin.kernel.ContainerClassLoader;
+import org.apache.excalibur.merlin.kernel.Verifiable;
/**
- * A service that provides support for location of components types.
+ * Provides support for the maintenance of a registry of
+ * component type definitions and profiles established within a classloader.
* @author <a href="mailto:[EMAIL PROTECTED]">Stephen McConnell</a>
* @version $Revision$ $Date$
*/
-public interface Registry
+public class Registry extends AbstractLogEnabled implements Contextualizable,
Configurable, Initializable, Executable, Disposable, Startable
{
+ //=======================================================================
+ // static
+ //=======================================================================
+
+ /**
+ * Context key used to locate the application classloader.
+ */
+ public static final String CLASSLOADER_KEY = "classloader";
+
+ /**
+ * Context key used to locate the application classloader.
+ */
+ public static final String CONTAINER_KEY = "container";
+
+ /**
+ * Context key used to locate the application classloader.
+ */
+ public static final String MAP_KEY = "map";
+
+ private static final Resources REZ =
+ ResourceManager.getPackageResources( Registry.class );
+
+ //=======================================================================
+ // state
+ //=======================================================================
+
+ /**
+ * The parent registry to this registry.
+ * (not currently in use - will come into effect as registry hierachies as
+ * dealt with).
+ */
+ private Container m_parent;
+
+ /**
+ * Classloader supplied by the parent.
+ */
+ private ContainerClassLoader m_classloader;
+
+ /**
+ * Internal class that maintains information about registered types.
+ */
+ private TypeRegistry m_types;
+
+ /**
+ * The supplied configuration.
+ */
+ private Configuration m_config;
+
+ /**
+ * Hashtable of all installed profiles keyed by profile name.
+ */
+ //private Hashtable m_profiles = new Hashtable();
+
+ /**
+ * Internal class that maintains information about profile types.
+ */
+ private ProfileRegistry m_profiles;
+
/**
- * Default role.
+ * The depenecy map supplied by the parent container.
*/
- static final String ROLE = Registry.class.getName();
+ private Map m_map;
+
+ //=======================================================================
+ // Contextualizable
+ //=======================================================================
/**
- * Returns an array of component profiles representing candidate component types
- * capable of acting as a service supplier relative to the supplied dependecy
descriptor.
- * @param dependency the dependency descriptor to evaluate
- * @return the set of profiles
+ * Service context from which the registry classloader is resolved.
+ * @param context a context value containing the key 'classloader'
*/
- //Profile[] getCandidateProfiles( DependencyDescriptor dependency );
+ public void contextualize( Context context ) throws ContextException
+ {
+ m_classloader = (ContainerClassLoader) context.get( CLASSLOADER_KEY );
+ m_map = (Map) context.get( MAP_KEY );
+ try
+ {
+ m_parent = (Container) context.get( CONTAINER_KEY );
+ }
+ catch( ContextException e )
+ {
+ // root container
+ }
+ }
+
+ //=======================================================================
+ // Configure
+ //=======================================================================
/**
- * Return a single preferred profile capable of supporting the supplied
dependency.
- * @param dependency a consumer component dependecy declaration
- * @return the preferred candidate supplier profile or null if not candidates
found
+ * Invoked by the container to establish the registry configuration.
+ * @param config a component configuration
*/
- //Profile getCandidateProfile( DependencyDescriptor dependency )
- // throws UnresolvedProviderException;
+ public void configure( Configuration config)
+ {
+ m_config = config;
+ getLogger().debug("registry configuration");
+ getLogger().debug( ConfigurationUtil.list( config ) );
+ }
+
+ //=======================================================================
+ // Initializable
+ //=======================================================================
/**
- * Test if the registry can resolve a request for a component with the supplied
classname
- * @param classname a component or service class name
- * @return TRUE if the registry can service the request
+ * Initalization of a <code>Registry</code> during which an application
+ * scoped classloader is created.
+ * @exception Exception if an error occurs during initialization.
*/
- //boolean hasComponentDefinition( String classname );
+ public void initialize() throws Exception
+ {
+ getLogger().debug("registry initialization");
+ m_types = new TypeRegistry( this, m_classloader );
+ m_types.enableLogging( getLogger().getChildLogger("types") );
+ String[] blocks = m_classloader.getComponentClassnames();
+
+ for( int i=0; i<blocks.length; i++ )
+ {
+ //
+ // register all of the the component providers implied by the classpath
+ // manifest declarations as potential component type without resolution
+ // of dependencies or other other constraints
+ //
+
+ final String classname = blocks[i].replace('/','.');
+ try
+ {
+ // initialize the component type defintions
+ m_types.register( classname );
+ }
+ catch( Throwable e )
+ {
+ final String error = "Ignoring component due to registration
error.";
+ getLogger().warn( error, e );
+ }
+ }
+
+ //
+ // we now have the complete set of components registered and we can proceed
+ // with the establishment of candidate profiles
+ //
+
+ Type[] types = m_types.getTypes();
+ try
+ {
+ Configuration[] explicit = m_config.getChildren( "component" );
+ m_profiles = new ProfileRegistry( explicit, m_types, m_classloader,
m_parent, m_map );
+ m_profiles.enableLogging( getLogger().getChildLogger( "profiles") );
+ int n = m_profiles.prepareProfiles();
+ getLogger().info("Profile count: " + n );
+ }
+ catch( Throwable e )
+ {
+ final String error = "Ignoring candidate due to profile error.";
+ getLogger().warn( error, e );
+ }
+
+ //
+ // Every available type has been expanded to a profile based on .profile
+ // declarations, or a default profile (*where no defaults are specified)
+ // We can now proceed with assembly.
+ //
+
+ try
+ {
+ m_profiles.assembleProfiles();
+ }
+ catch( Throwable e )
+ {
+ final String error = "Ignoring candidate due to profile error.";
+ getLogger().warn( error, e );
+ }
+ }
+
+ //=======================================================================
+ // Executable
+ //=======================================================================
/**
- * Returns an component defintion relative to the requested classname.
- * @param classname the class name of the componet defintion to locate or create
- * @return the corresponding component defintion
- * @exception AssemblyException if an error occurs during the construction of a
- * new component defintion
+ * Executes composition of a target component assembly options relative to a
target
+ * component classname within the scope of a classpath declaration containing
jar file
+ * references (refer contextualize), and generate a report to the logging
channel.
+ *
+ * @exception Exception if an error occurs during execution
*/
- //Type getComponentDefinition( String classname ) throws RegistryException;
+ public void execute() throws Exception
+ {
+ }
+
+ //======================================================================
+ // Startable
+ //======================================================================
+
+ public void start() throws Exception
+ {
+ m_profiles.start();
+ }
+
+ public void stop()
+ {
+ m_profiles.stop();
+ }
+
+
+ //=======================================================================
+ // Verifiable
+ //=======================================================================
+
+ /**
+ * Method invoked by a parent container to request type level validation of
+ * the container.
+ *
+ * @exception ValidationException if a validation failure occurs
+ */
+ public void verify() throws VerifyException
+ {
+ getLogger().debug("Map listing");
+ Profile[] startup = m_map.getStartupGraph();
+ doVerify( startup );
+ }
+
+ //=======================================================================
+ // Container
+ //=======================================================================
+
+ public Profile[] getProviders( ServiceDesignator designator )
+ {
+ Profile[] local = m_profiles.getProviders( designator );
+ if( m_parent != null )
+ {
+ Profile[] facilities = m_parent.getProviders( designator );
+ ArrayList list = new ArrayList();
+ for( int i=0; i<local.length; i++ )
+ {
+ list.add( local[i] );
+ }
+ for( int i=0; i<facilities.length; i++ )
+ {
+ list.add( facilities[i] );
+ }
+ return (Profile[]) list.toArray( new Profile[0] );
+ }
+ return local;
+ }
+
+ //=======================================================================
+ // implementation
+ //=======================================================================
+
+
+ private void doVerify( Profile[] assembly ) throws VerifyException
+ {
+ MetaDataVerifier mdv = new MetaDataVerifier();
+ for( int i=0; i<assembly.length; i++ )
+ {
+ try
+ {
+ mdv.verifyType( assembly[i], m_classloader );
+ }
+ catch( Throwable e )
+ {
+ getLogger().error("verification failure", e );
+ }
+ }
+ AssemblyVerifier verifier = new AssemblyVerifier();
+ verifier.enableLogging( getLogger().getChildLogger("verifier") );
+ getLogger().debug("commencing assembly verification");
+ verifier.verifyAssembly( assembly );
+ listProfiles( assembly );
+ }
+
+ private void listProfiles( Profile[] profiles )
+ {
+ getLogger().debug("listing profiles");
+ List reported = new LinkedList();
+ for( int i=0; i<profiles.length; i++ )
+ {
+ listProfile( i+1, profiles[i], m_map, reported );
+ }
+ }
+
+ private void listProfile( int n, Profile profile, Map map, List reported )
+ {
+ if( !reported.contains( profile ) )
+ {
+ reported.add( profile );
+ getLogger().debug( "" + n + ": " + profile.toString() );
+ Profile[] suppliers = map.getProviderGraph( profile );
+ for( int j=0; j<suppliers.length; j++ )
+ {
+ Profile supplier = suppliers[j];
+ getLogger().debug(" supplier: " + supplier );
+ }
+ Profile[] consumers = map.getConsumerGraph( profile );
+ for( int j=0; j<consumers.length; j++ )
+ {
+ Profile consumer = consumers[j];
+ getLogger().debug(" consumer: " + consumer.getName() );
+ }
+ }
+ }
+
+ //=======================================================================
+ // Disposable
+ //=======================================================================
/**
- * Returns an array of component definitions capable of acting as a service
supplier
- * relative to the supplied dependecy descriptor.
- * @param dependency the dependency descriptor to evaluate
- * @return the set of matching service descriptors capable of supporting the
dependency
+ * Disposal of this component.
*/
- //public Type[] getCandidateProviders( DependencyDescriptor dependency );
+ public void dispose()
+ {
+ }
}
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>