avalon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mcconn...@apache.org
Subject cvs commit: jakarta-avalon-excalibur/assembly/src/java/org/apache/excalibur/merlin/container ContainerService.java ContainerFactory.java
Date Mon, 12 Aug 2002 01:55:44 GMT
mcconnell    2002/08/11 18:55:44

  Added:       assembly/src/java/org/apache/excalibur/merlin/container
                        ContainerService.java ContainerFactory.java
  Log:
  Utility classes supporting container creation.
  
  Revision  Changes    Path
  1.1                  jakarta-avalon-excalibur/assembly/src/java/org/apache/excalibur/merlin/container/ContainerService.java
  
  Index: ContainerService.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.txt file.
   */
  
  package org.apache.excalibur.merlin.container;
  
  import java.util.Map;
  import org.apache.avalon.framework.context.Context;
  import org.apache.avalon.framework.context.DefaultContext;
  import org.apache.excalibur.merlin.assembly.DefaultLoggerManager;
  import org.apache.excalibur.merlin.assembly.ContainerManager;
  import org.apache.excalibur.merlin.assembly.AssemblyException;
  import org.apache.excalibur.merlin.model.ContainerDescriptor;
  import org.apache.excalibur.merlin.model.Resource;
  
  /**
   * Utility class that provides support for the creation of an initial 
   * container resource instance.
   * @see ContainerFactory
   * @see Container
   * @see Resource
   * @author <a href="mailto:mcconnell@apache.org">Stephen McConnell</a>
   * @version $Revision: 1.1 $ $Date: 2002/08/12 01:55:44 $
   */
  public class ContainerService
  {
     /**
      * The container resource.
      */
      private DefaultLoggerManager m_logging;
     
     /**
      * The container descriptor.
      */
      private ContainerDescriptor m_descriptor;
  
     /**
      * The container manager.
      */
      private ContainerManager m_manager;
     
     /**
      * Subsidiary services.
      */
      private Map m_services;
     
     /**
      * The container resource.
      */
      private Resource m_resource;
     
     /**
      * Creation of a new container service.
      * @param descriptor the container descriptor
      * @param manager the container manager
      */
      public ContainerService( 
                       DefaultLoggerManager logging,
                       ContainerDescriptor descriptor, 
                       ContainerManager manager, 
                       Map services )
      {
          if( descriptor == null ) 
            throw new NullPointerException( "descriptor" );
  
          if( manager == null ) 
            throw new NullPointerException( "manager" );
  
          if( logging == null ) 
            throw new NullPointerException( "logging" );
  
          if( services == null ) 
            throw new NullPointerException( "services" );
  
          m_descriptor = descriptor;
          m_manager = manager;
          m_logging = logging;
          m_services = services;
      }
  
     /**
      * Return the container descriptor.
      * @return the container descriptor
      */
      public ContainerDescriptor getDescriptor()
      {
          return m_descriptor;
      }
  
     /**
      * Return the container manager.
      * @return the container manager
      */
      public ContainerManager getManager()
      {
          return m_manager;
      }
  
     /**
      * Returns the container as a resource reference.
      * @return the container as a resource reference
      */
      public Resource getResource()
        throws AssemblyException
      {
          return getResource( null );
      }
  
     /**
      * Returns the container as a resource reference.
      * @param listener a state listener to assign to the container
      * @return the container as a resource reference
      */
      public Resource getResource( StateListener listener )
        throws AssemblyException
      {
          return getResource( listener, null );
      }
  
     /**
      * Returns the container as a resource reference.
      * @param stateListener a state listener to assign to the container
      * @param containerListener a container listener to assign to the container
      * @return the container as a resource reference
      */
      public Resource getResource( StateListener stateListener, ContainerListener containerListener
)
        throws AssemblyException
      {
          //
          // create the container context
          //
  
          try
          {
              DefaultContext context = new DefaultContext();
              context.put( Container.MANAGER_KEY, m_manager );
              context.put( Container.DESCRIPTOR_KEY, m_descriptor );
              context.put( Container.LOGGING_KEY, m_logging );
              context.put( Container.SERVICES_KEY, m_services );
              if( stateListener != null )
              {
                  context.put( Container.STATE_LISTENER_KEY, stateListener );
              }
              if( containerListener != null )
              {
                  context.put( Container.CONTAINER_LISTENER_KEY, containerListener );
              }
  
              //
              // assemble the descriptor to ensure that all dependecies are 
              // resolved and return a reference to the container
              //
  
              return m_manager.assembleProfile( m_descriptor, context );
          }
          catch( Throwable e )
          {
              final String error = "Error during assembly of subsidiary container: " + m_manager.getPath();
              throw new AssemblyException( error, e );
          }
      }
  
      //ContainerService getService( ContainerDescriptor key )
      //{
      //    return (ContainerService) m_services.get( key );
      //}
  }
  
  
  
  1.1                  jakarta-avalon-excalibur/assembly/src/java/org/apache/excalibur/merlin/container/ContainerFactory.java
  
  Index: ContainerFactory.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.txt file.
   */
  
  package org.apache.excalibur.merlin.container;
  
  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.Map;
  import java.util.List;
  import java.util.LinkedList;
  import java.util.Hashtable;
  import java.util.Properties;
  import java.util.ArrayList;
  import java.util.Vector;
  import java.util.Iterator;
  import java.util.jar.Attributes;
  import java.util.jar.Manifest;
  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.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.activity.Suspendable;
  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.parameters.Parameters;
  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.Version;
  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.avalon.excalibur.logger.LoggerManager;
  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.ReferenceDescriptor;
  import org.apache.excalibur.merlin.assembly.TypeManager;
  import org.apache.excalibur.merlin.assembly.KernelManager;
  import org.apache.excalibur.merlin.assembly.TypeException;
  import org.apache.excalibur.merlin.assembly.ContainerManager;
  import org.apache.excalibur.merlin.assembly.DefaultLoggerManager;
  import org.apache.excalibur.merlin.assembly.DependencyGraph;
  import org.apache.excalibur.merlin.model.ContainerDescriptor;
  import org.apache.excalibur.merlin.model.ClasspathDescriptor;
  import org.apache.excalibur.merlin.model.Profile;
  import org.apache.excalibur.merlin.model.Resource;
  import org.apache.excalibur.merlin.model.ContextDirective;
  import org.apache.excalibur.merlin.model.CategoriesDescriptor;
  import org.apache.excalibur.merlin.model.builder.XMLContainerUtil;
  import org.apache.excalibur.merlin.model.builder.XMLContainerCreator;
  import org.apache.excalibur.merlin.model.builder.ContainerBuilder;
  import org.apache.excalibur.merlin.Controller;
  import org.apache.excalibur.configuration.ConfigurationUtil;
  
  /**
   * Utility class that provides support for the creation of subsidiary containers
   * based on configuration criteria or meta-data.
   *
   * @author <a href="mailto:mcconnell@apache.org">Stephen McConnell</a>
   * @version $Revision: 1.1 $ $Date: 2002/08/12 01:55:44 $
   */
  public class ContainerFactory extends AbstractLogEnabled
  {
      //=======================================================================
      // static
      //=======================================================================
  
      private static final Resources REZ =
          ResourceManager.getPackageResources( ContainerFactory.class );
  
      //=======================================================================
      // state
      //=======================================================================
  
     /**
      * Utility class that provides support for marshalling an XML configuration
      * into meta-data and meta-info instances.
      */
      private XMLContainerCreator m_creator = new XMLContainerCreator();
  
     /**
      * Constructor supplied reference to the type manager.
      */
      private ContainerManager m_manager;
  
     /**
      * The logging manager to pass to the created container.
      */
      private DefaultLoggerManager m_logging;
  
      //=======================================================================
      // constructor
      //=======================================================================
  
     /**
      * Creation of a container factory that supports the creation of 
      * subsidiary containers.
      *
      * @param manager the context type manager
      * @param logging the logging manager
      */
      public ContainerFactory( 
                          ContainerManager manager, 
                          DefaultLoggerManager logging )
         
      {
          if( manager == null )
            throw new NullPointerException("manager");
  
          if( logging == null )
            throw new NullPointerException("logging");
  
          m_manager = manager;
          m_logging = logging;
      }
  
     /**
      * Utility to create an subsidiary container manager.
      *
      * @param config the subsidiary container configuration
      * @return the resource holding the container 
      *
      * @exception Exception is an error occurs
      */
      public ContainerService build( 
                           ContainerDescriptor descriptor, 
                           ClasspathDescriptor classpath ) 
        throws Exception
      {
          if( descriptor == null )
            throw new NullPointerException( "descriptor" );
  
          if( classpath == null )
            throw new NullPointerException( "classpath" );
  
          final ContainerManager manager = 
            m_manager.createContainerManager( descriptor, classpath );
          return new ContainerService( m_logging, descriptor, manager, new Hashtable() );
      }
  
  
     /**
      * Create a container descriptor and associated manager using a supplied parent
      * manager and container configuration.
      * @param parent the parent manager
      * @param config the container configuration
      * @return the fully populated <code>ContainerDescriptor</code>
      * @exception Exception if an error occurs
      */
      public ContainerService build( Configuration config )
        throws Exception
      {
  
          final String name = 
            config.getAttribute( "name" );
  
          final String classname = 
            config.getAttribute( "class", DefaultContainer.class.getName() );
  
          try
          {
              //
              // create the empty container descriptor
              //
  
              Type type = m_manager.getType( classname );
              ContainerDescriptor descriptor = 
                m_creator.createContainerDescriptor( type, config );
  
              //
              // create the container manager
              //
  
              final ClasspathDescriptor classpath = 
                m_creator.createClasspathDescriptor( config.getChild("classpath") );
  
              final ContainerManager manager = 
                m_manager.createContainerManager( descriptor, classpath );
  
              //
              // populate the descriptor with component profiles
              //
  
              Configuration[] components = config.getChildren("component");
              for( int i=0; i<components.length; i++ )
              {
                  final Configuration component = components[i];
                  final String c = component.getAttribute("class");
                  try
                  {
                      final Type t = manager.getType( c );
                      final Profile profile = m_creator.createProfile( t, component );
                      descriptor.addComponent( profile );
                      manager.addProfile( profile );
                  }
                  catch( TypeException e )
                  {
                      final String warning =
                        "Ignoring component declaration at " 
                        + component.getLocation() 
                        + " due to unknown type: " + c;
                      getLogger().warn( warning );
                  }
              }
  
              //
              // populate the descriptor with subsidiary container descriptors
              //
  
              Map map = new Hashtable();
              Configuration[] containers = config.getChildren("container");
              for( int i=0; i<containers.length; i++ )
              {
                  final Configuration container = containers[i];
                  ContainerFactory factory = new ContainerFactory( manager, m_logging );
                  if( m_manager instanceof KernelManager )
                  {
                      factory.enableLogging( getLogger() );
                  }
                  else
                  {
                      factory.enableLogging( getLogger().getChildLogger( descriptor.getName()
) );
                  }
  
                  ContainerService service = factory.build( container );
                  ContainerDescriptor d = service.getDescriptor();
                  descriptor.addContainer( d );
                  map.put( d, service );
              }
  
              return new ContainerService( m_logging, descriptor, manager, map );
  
          }
          catch( Throwable e )
          {
              final String error = "Error building container descriptor.";
              throw new ContainerException( error, e );
          }
      }
  }
  
  
  

--
To unsubscribe, e-mail:   <mailto:avalon-cvs-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:avalon-cvs-help@jakarta.apache.org>


Mime
View raw message