avalon-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From f...@apache.org
Subject cvs commit: jakarta-avalon/proposal/4.0/src/java/org/apache/avalon/container AbstractCamelotDeployer.java AbstractContainer.java AbstractDeployer.java AbstractZipDeployer.java AvalonState.java CamelotUtil.java DefaultFactory.java DefaultLoader.java DefaultLocator.java DefaultLocatorRegistry.java DefaultRegistry.java DeployerUtil.java Info.java
Date Tue, 27 Feb 2001 08:42:31 GMT
fede        01/02/27 00:42:31

  Added:       proposal/4.0/src/java/org/apache/avalon/camelot
                        AbstractCamelotDeployer.java AbstractContainer.java
                        AbstractDeployer.java AbstractZipDeployer.java
                        AvalonState.java CamelotUtil.java
                        DefaultFactory.java DefaultLoader.java
                        DefaultLocator.java DefaultLocatorRegistry.java
                        DefaultRegistry.java DeployerUtil.java
  Removed:     proposal/4.0/src/java/org/apache/avalon/container
                        AbstractCamelotDeployer.java AbstractContainer.java
                        AbstractDeployer.java AbstractZipDeployer.java
                        AvalonState.java CamelotUtil.java
                        DefaultFactory.java DefaultLoader.java
                        DefaultLocator.java DefaultLocatorRegistry.java
                        DefaultRegistry.java DeployerUtil.java Info.java
  Log:
  split container and camelot
  
  Revision  Changes    Path
  1.1                  jakarta-avalon/proposal/4.0/src/java/org/apache/avalon/camelot/AbstractCamelotDeployer.java
  
  Index: AbstractCamelotDeployer.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 file.
   */
  package org.apache.avalon.camelot;
  
  import java.net.URL;
  import org.apache.avalon.component.ComponentManager;
  import org.apache.avalon.component.Composer;
  import org.apache.avalon.container.*;
  
  /**
   * This class deploys resources from camelot based system.
   *
   * @author <a href="mailto:donaldp@apache.org">Peter Donald</a>
   */
  public abstract class AbstractCamelotDeployer
      extends AbstractDeployer
      implements Composer
  {
      protected boolean                      m_deployToContainer;
      protected boolean                      m_deployToLocatorRegistry;
      protected boolean                      m_deployToInfoRegistry;
  
      protected LocatorRegistry              m_locatorRegistry;
      protected Container                    m_container;
      protected Registry                     m_infoRegistry;
      
      /**
       * Retrieve relevent services needed to deploy.
       *
       * @param componentManager the ComponentManager
       * @exception ComponentNotFoundException if an error occurs
       * @exception ComponentNotAccessibleException if an error occurs
       */
      public void compose( final ComponentManager componentManager )
      {
          try {
              if( m_deployToLocatorRegistry )
              {
                  m_locatorRegistry = (LocatorRegistry)componentManager.
                      lookup( "org.apache.avalon.container.LocatorRegistry" );
              }
      
              if( m_deployToInfoRegistry )
              {
                  m_infoRegistry = (Registry)componentManager.
                      lookup( "org.apache.avalon.container.Registry" );
              }
      
              if( m_deployToContainer )
              {
                  m_container = (Container)componentManager.
                      lookup( "org.apache.avalon.container.Container" );
              }
          } catch (Exception e) {
          }
      }
  
      protected void addEntry( final String name, final Entry entry )
          throws DeploymentException
      {
          try { m_container.add( name, entry ); }
          catch( final ContainerException ce )
          {
              throw new DeploymentException( "Error adding component to container", ce );
          }
  
          getLogger().debug( "Adding " + m_type + "Entry " + name + " as " + entry );
      }    
  
      protected void addLocator( final String name, final String classname, final URL url
)
          throws DeploymentException
      {
          final DefaultLocator locator = new DefaultLocator( classname, url );
          
          try { m_locatorRegistry.register( name, locator ); }
          catch( final RegistryException re )
          {
              throw new DeploymentException( "Error registering " + name + " due to " + re,
                                             re );
          }
          
          getLogger().debug( "Registered " + m_type + " " + name + " as " + classname );
      }
      
      protected void addInfo( final String name, final Info info )
          throws DeploymentException
      {
          try { m_infoRegistry.register( name, info ); }
          catch( final RegistryException re )
          {
              throw new DeploymentException( "Error registering " + name + " due to " + re,
                                             re );
          }
          
          getLogger().debug( "Registered Info " + m_type + " " + name );
      }
  }
  
  
  
  1.1                  jakarta-avalon/proposal/4.0/src/java/org/apache/avalon/camelot/AbstractContainer.java
  
  Index: AbstractContainer.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 file. 
   */
  package org.apache.avalon.camelot;
  
  import java.util.HashMap;
  import java.util.Iterator;
  import org.apache.avalon.AbstractLoggable;
  import org.apache.avalon.component.Component;
  import org.apache.avalon.container.*;
  
  /**
   * This contains it during execution and may provide certain 
   * facilities (like a thread per EJB etc).
   *
   * @author <a href="mailto:donaldp@apache.org">Peter Donald</a>
   */
  public abstract class AbstractContainer
      extends AbstractLoggable
      implements Component, Container
  {
      protected final HashMap          m_entries      = new HashMap();
      protected Class                  m_entryClass;
  
      /**
       * Add a component instance to container.
       *
       * @param entry the component entry
       */
      public void add( final String name, final Entry entry )
          throws ContainerException
      {
          checkEntry( name, entry );
          preAdd( name, entry );
          m_entries.put( name, entry );
          postAdd( name, entry );
      }
  
      /**
       * Remove a component instance from container.
       *
       * @param name the name of component
       */
      public void remove( final String name )
          throws ContainerException
      {
          final Entry entry = (Entry)m_entries.get( name );
  
          if( null == entry )
          {
              throw new ContainerException( "Component named " + name + " not contained" );
          }
  
          preRemove( name, entry );
          m_entries.remove( name );
          postRemove( name, entry );
      }
  
      /**
       * Retrieve Entry from container
       *
       * @param name the name of entry
       * @return the entry
       */
      public Entry getEntry( final String name )
          throws ContainerException
      {
          final Entry entry = (Entry)m_entries.get( name );
          
          if( null == entry )
          {
              throw new ContainerException( "Name " + name + " not contained" );
          }
          else
          {
              return entry;
          }
      }
  
      /**
       * List all names of entries in container.
       *
       * @return the list of all entries
       */
      public Iterator list()
      {
          return m_entries.keySet().iterator();
      }
  
      /**
       * This method is called before entry is added to give chance for 
       * sub-class to veto removal.
       *
       * @param name the name of entry
       * @param entry the entry
       * @exception ContainerException to stop removal of entry
       */
      protected void preAdd( final String name, final Entry entry )
          throws ContainerException
      {
      }
  
      /**
       * This method is called after entry is added to give chance for 
       * sub-class to do some cleanup.
       *
       * @param name the name of entry
       * @param entry the entry
       */
      protected void postAdd( final String name, final Entry entry )
      {
      }
      
      /**
       * This method is called before entry is removed to give chance for 
       * sub-class to veto removal.
       *
       * @param name the name of entry
       * @param entry the entry
       * @exception ContainerException to stop removal of entry
       */
      protected void preRemove( final String name, final Entry entry )
          throws ContainerException
      {
      }
  
      /**
       * This method is called after entry is removed to give chance for 
       * sub-class to do some cleanup.
       *
       * @param name the name of entry
       * @param entry the entry
       */
      protected void postRemove( final String name, final Entry entry )
      {
      }
      
      /**
       * List all entries in container.
       *
       * @return the list of all entries
       */
      protected Iterator listEntries()
      {
          return m_entries.values().iterator();
      }   
       
      protected void checkEntry( final String name, final Entry entry )
          throws ContainerException
      {
          if( null != m_entries.get( name ) )
          {
              throw new ContainerException( "Can not add component to container because "
+ 
                                            "entry already exists with name " + name );
          }
  
          if( !isValidName( name ) )
          {
              throw new ContainerException( "Can not add component to container because "
+ 
                                            "invalid name " + name );
          }
          
          if( !isValidEntry( entry ) )
          {
              throw new ContainerException( "Can not add component to container because "
+ 
                                            "invalid entry for " + name );
          }
  
          if( !m_entryClass.isAssignableFrom( entry.getClass() ) )
          {
              throw new ContainerException( "Only Entries of type " + m_entryClass.getName()
+
                                            " may be placed in container." );
          }
      }
  
      protected boolean isValidName( final String name )
          throws ContainerException
      {
          return true;
      }
  
      protected boolean isValidEntry( final Entry entry )
          throws ContainerException
      {
          return true;
      }
  }
  
  
  
  1.1                  jakarta-avalon/proposal/4.0/src/java/org/apache/avalon/camelot/AbstractDeployer.java
  
  Index: AbstractDeployer.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 file. 
   */
  package org.apache.avalon.camelot;
  
  import java.io.File;
  import java.io.FileOutputStream;
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.OutputStream;
  import java.net.URL;
  import java.util.HashMap;
  import org.apache.avalon.AbstractLoggable;
  import org.apache.avalon.component.Component;
  import org.apache.avalon.component.ComponentNotFoundException;
  import org.apache.aut.io.FileUtil;
  import org.apache.log.Logger;
  import org.apache.avalon.container.*;
  
  /**
   * A Deployer is responsible for taking a URL (ie a jar/war/ear) and deploying
   * it to a particular "location". "location" means different things for
   * different containers. For a servlet container it may mean the place to
   * mount servlet (ie /myapp --> /myapp/Cocoon.xml is mapping cocoon servlet to
   * /myapp context).
   *
   * @author <a href="mailto:donaldp@apache.org">Peter Donald</a>
   */
  public abstract class AbstractDeployer
      extends AbstractLoggable
      implements Deployer, Component
  {
      protected final HashMap                m_deployments  = new HashMap();
      protected boolean                      m_autoUndeploy;
      protected String                       m_type;
  
      public void deploy( final String location, final URL url )
          throws DeploymentException
      {
          checkDeployment( location, url );
          final File file = getFileFor( url );
  
          getLogger().info( "Deploying " + m_type + " file (" + file + ") as " + location
);
          deployFromFile( location, file );
      }
  
      protected void checkDeployment( final String location, final URL url )
          throws DeploymentException
      {
          if( null != m_deployments.get( location ) )
          {
              throw new DeploymentException( m_type + " already exists at " + location );
          }
          
          if( !isValidLocation( location ) )
          {
              throw new DeploymentException( "Invalid location (" + location + 
                                             ") for " + m_type );
          }
      }
  
      public void undeploy( final String location )
          throws DeploymentException
      {
          final Component component = (Component)m_deployments.get( location );
  
          if( null == component )
          {
              throw new DeploymentException( m_type + " does not exist at " + location );
          }
  
          final boolean canUndeploy = canUndeploy( component );
  
          if( !canUndeploy )
          {
              if( !m_autoUndeploy )
              {
                  //we are midstream but not allowed to automagically undeploy .. therefore
                  throw new DeploymentException( m_type + " not ready to undeploy at " + 
                                                 location );
              }
              else
              {
                  shutdownDeployment( component );
              }
          }
  
          //if everything has gone successful then remove application
          m_deployments.remove( location );
      }
  
      protected File getCacheLocationFor( final URL url )
          throws DeploymentException
      {
          throw new DeploymentException( "Unable to deploy non-local resources" );
      }
  
      protected File getFileFor( final URL url )
          throws DeploymentException
      {
          File file = null;
  
          if( url.getProtocol().equals( "file" ) )
          {
              file = new File( url.getFile() );
          }
          else
          {
              file = getCacheLocationFor( url );
              try { FileUtil.copyURLToFile( url, file ); }
              catch( final IOException ioe )
              {
                  throw new DeploymentException( "Failed attempting to copy from  " + url
+
                                                 " to local file cache " + file, ioe );
              }
          }
  
          file = file.getAbsoluteFile();
  
          if( !file.exists() )
          {
              throw new DeploymentException( "Could not find application archive at " + 
                                             file );
          }
  
          if( file.isDirectory() )
          {
              throw new DeploymentException( "Could not find application archive at " + 
                                             file + " as it is a directory." );
          }
  
          return file;
      }
  
      protected boolean isValidLocation( String location )
      {
          return true;
      }
  
      protected boolean canUndeploy( Component component )
          throws DeploymentException
      {
          return true;
      }
  
      protected void shutdownDeployment( Component component )
          throws DeploymentException
      {
      }
  
      protected abstract void deployFromFile( String location, File file )
          throws DeploymentException;
  }
  
  
  
  1.1                  jakarta-avalon/proposal/4.0/src/java/org/apache/avalon/camelot/AbstractZipDeployer.java
  
  Index: AbstractZipDeployer.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 file.
   */
  package org.apache.avalon.camelot;
  
  import java.io.File;
  import java.io.IOException;
  import java.net.MalformedURLException;
  import java.net.URL;
  import java.util.zip.ZipFile;
  import org.apache.avalon.component.Composer;
  import org.apache.avalon.container.*;
  
  /**
   * This class deploys a .zip file into a registry.
   *
   * @author <a href="mailto:donaldp@apache.org">Peter Donald</a>
   */
  public abstract class AbstractZipDeployer
      extends AbstractCamelotDeployer
      implements Composer
  {
      /**
       * Deploy a file.
       * Eventually this should be cached for performance reasons.
       *
       * @param location the location 
       * @param file the file
       * @exception DeploymentException if an error occurs
       */
      protected void deployFromFile( final String location, final File file )
          throws DeploymentException
      {
          final ZipFile zipFile = DeployerUtil.getZipFileFor( file );
  
          URL url = null;
          
          try
          {
              try { url = file.toURL(); }
              catch( final MalformedURLException mue ) 
              {
                  throw new DeploymentException( "Unable to form url", mue );
              }
              loadResources( zipFile, location, url );
          }
          finally
          {
              try { zipFile.close(); }
              catch( final IOException ioe ) {}
          }
      }
  
      /**
       * Overide this method to provide the actual functionality and 
       * deploy the resources from a zip file.
       *
       * @param zipFile the ZipFile
       * @param location the location that it was deployed to
       * @param url the url of deployment
       * @exception DeploymentException if an error occurs
       */
      protected abstract void loadResources( ZipFile zipFile, String location, URL url )
          throws DeploymentException;
  }
  
  
  
  1.1                  jakarta-avalon/proposal/4.0/src/java/org/apache/avalon/camelot/AvalonState.java
  
  Index: AvalonState.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 file. 
   */
  package org.apache.avalon.camelot;
  import org.apache.avalon.container.*;
  
  public final class AvalonState
      extends State
  {
      public final static AvalonState ERROR          = new AvalonState( "ERROR", -10 );
  
      public final static AvalonState BASE           = new AvalonState( "BASE", 0 );
  
      public final static AvalonState CREATED        = new AvalonState( "CREATED", 5 );
      public final static AvalonState LOGGED         = new AvalonState( "LOGGED", 10 );
      public final static AvalonState CONTEXTUALIZED = new AvalonState( "CONTEXTUALIZED",
20 );
      public final static AvalonState COMPOSED       = new AvalonState( "COMPOSED", 30 );
      public final static AvalonState CONFIGURED     = new AvalonState( "CONFIGURED", 40 );
      public final static AvalonState NAMED          = new AvalonState( "NAMED", 50 );
      public final static AvalonState INITIALIZED    = new AvalonState( "INITIALIZED", 60
);
      public final static AvalonState STARTED        = new AvalonState( "STARTED", 70 );
      public final static AvalonState RUNNING        = new AvalonState( "RUNNING", 70 );
  
      //from here to stopped may want to go to a different class ??
      public final static AvalonState EXPORTED       = new AvalonState( "EXPORTED", 80 );
      public final static AvalonState UNEXPORTED     = new AvalonState( "UNEXPORTED", 90 );
      public final static AvalonState SUSPENDED      = new AvalonState( "SUSPENDED", 100 );
      public final static AvalonState RESUMED        = new AvalonState( "RESUMED", 110 );
  
      public final static AvalonState STOPPED        = new AvalonState( "STOPPED", 120 );
      public final static AvalonState DISPOSED       = new AvalonState( "DISPOSED", 130 );
      public final static AvalonState FINALIZED      = new AvalonState( "FINALIZED", 140 );
  
      protected AvalonState( final String name, final int value )
      {
          super( name, value );
      }
  }
  
  
  
  1.1                  jakarta-avalon/proposal/4.0/src/java/org/apache/avalon/camelot/CamelotUtil.java
  
  Index: CamelotUtil.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 file. 
   */
  package org.apache.avalon.camelot;
  
  import java.io.File;
  import java.io.IOException;
  import java.net.MalformedURLException;
  import java.util.Iterator;
  import org.apache.avalon.component.Component;
  import org.apache.aut.io.ExtensionFileFilter;
  import org.apache.avalon.container.*;
  
  /**
   * Utility methods for Camelot related facilities.
   *
   * @author <a href="mailto:donaldp@apache.org">Peter Donald</a>
   */
  public final class CamelotUtil
  {
      /**
       * Private constructor so impossible to instantiate.
       */
      private CamelotUtil()
      {
      }
  
      public static void deployFromDirectory( final Deployer deployer, 
                                              final File directory,
                                              final String extention )
          throws DeploymentException
      {
          deployFromDirectory( deployer, directory, new String[] { extention } );
      }
  
      public static void deployFromDirectory( final Deployer deployer, 
                                              final File directory,
                                              final String[] extentions )
          throws DeploymentException
                                            
      {
          final ExtensionFileFilter filter = new ExtensionFileFilter( extentions );
          final File[] files = directory.listFiles( filter );
  
          if( null != files )
          {
              deployFiles( deployer, files );
          }
      }
  
      public static void deployFiles( final Deployer deployer, final File[] files )
          throws DeploymentException
      {
          for( int i = 0; i < files.length; i++ )
          {
              final String filename = files[ i ].getName();
              
              int index = filename.lastIndexOf( '.' );
              if( -1 == index ) index = filename.length();
              
              final String name = filename.substring( 0, index );
              
              try
              { 
                  final File file = files[ i ].getCanonicalFile();
                  deployer.deploy( name, file.toURL() );
              }
              catch( final MalformedURLException mue )
              {
                  throw new DeploymentException( "Malformed URL for " + files[ i ], mue );
              }
              catch( final IOException ioe )
              {
                  throw new DeploymentException( "Unable to get canonical representation "
+ 
                                                 "for file " + files[ i ], ioe );
              }
          }
      }
  }
  
  
  
  1.1                  jakarta-avalon/proposal/4.0/src/java/org/apache/avalon/camelot/DefaultFactory.java
  
  Index: DefaultFactory.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 file. 
   */
  package org.apache.avalon.camelot;
  
  import java.net.URL;
  import java.util.HashMap;
  import org.apache.avalon.component.Component;
  import org.apache.avalon.AbstractLoggable;
  import org.apache.avalon.container.*;
  
  
  /**
   * This is the component that creates the components. 
   *
   * @author <a href="mailto:donaldp@apache.org">Peter Donald</a>
   */
  public class DefaultFactory
      extends AbstractLoggable
      implements Factory, Component
  {
      protected static class LoaderEntry
      {
          Loader      m_loader;
          long        m_lastModified;
      }
  
      protected final HashMap        m_loaders      = new HashMap();
  
      /**
       * Create a component whos position is indicated by locator.
       *
       * @param locator the locator indicating the component location
       * @return the component
       * @exception FactoryException if an error occurs
       */
      public Object create( final Locator locator ) 
          throws FactoryException
      {
          final Loader loader = getLoaderFor( locator.getLocation() );
  
          try { return loader.load( locator.getName() ); }
          catch( final Exception e )
          {
              throw new FactoryException( "Unable to create " + locator.getName() + 
                                          " from " + locator.getLocation(), e );
          }
      }
  
      public Object create( final Locator locator, final Class clazz )
          throws FactoryException
      {
          final Object object = create( locator );
          
          if( !clazz.isInstance( object ) )
          {
              throw new FactoryException( "Created object of type " + object.getClass().getName()
+
                                          " not compatable with type " + clazz.getName() );
          }
          
          return object;
      }
  
      protected Loader getLoaderFor( final URL url )
      {
          final String location = url.toString();
          LoaderEntry loader = (LoaderEntry)m_loaders.get( location );
          
          if( null == loader )
          {
              getLogger().info( "Creating ClassLoader for " + location );
              loader = new LoaderEntry();
  
              loader.m_loader = setupLoader( url );
              loader.m_lastModified = System.currentTimeMillis();
              
              m_loaders.put( location, loader );
          }
          else
          {
              //TODO: Check it up to date and reload if necessary
          }
          
          return loader.m_loader;
      }
  
      protected Loader setupLoader( final URL url )
      {
          final ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
          final Loader loader = createLoader( url, classLoader );
  
  /* FIX ME 
          setupLogger( loader );
  */
          return loader;
      }
      
      /**
       * Create a new loader.
       * Put in another method so that it can be overridden.
       *
       * @param location the location the Loader will load from
       * @return the loader
       */
      protected Loader createLoader( final URL url, final ClassLoader classLoader )
      {
          return new DefaultLoader( url, classLoader );
      }
  }
  
  
  
  1.1                  jakarta-avalon/proposal/4.0/src/java/org/apache/avalon/camelot/DefaultLoader.java
  
  Index: DefaultLoader.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 file. 
   */
  package org.apache.avalon.camelot;
  
  import java.net.URL;
  import java.net.URLClassLoader;
  import org.apache.aut.ObjectUtil;
  import org.apache.aut.ExceptionUtil;
  import org.apache.avalon.component.Component;
  import org.apache.avalon.container.*;
  
  /**
   * Class used to load resources from a source.
   * 
   * @author <a href="mailto:donaldp@apache.org">Peter Donald</a>
   */
  public class DefaultLoader
      implements Loader, Component
  {
      protected ClassLoader         m_classLoader;
  
      public DefaultLoader( final ClassLoader classLoader )
      {
          m_classLoader = classLoader;
      }
  
      public DefaultLoader( final URL location, final ClassLoader classLoader )
      {
          m_classLoader = new URLClassLoader( new URL[] { location }, classLoader );
      }
  
      public DefaultLoader( final URL location )
      {
          this( location, Thread.currentThread().getContextClassLoader() );
      }    
  
      public DefaultLoader()
      {
          final ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
          m_classLoader = new URLClassLoader( new URL[0], classLoader );
      }
  
      /**
       * Retrieve classloader associated with source.
       *
       * @return the ClassLoader
       */
      public ClassLoader getClassLoader()
      {
          return m_classLoader;
      }
  
      public Object load( final String classname, final Class clazz )
          throws FactoryException
      {
          final Object object = load( classname );
  
          if( !clazz.isInstance( object ) )
          {
              throw new FactoryException( "Created object of type " + object.getClass().getName()
+
                                          " not compatable with type " + clazz.getName() );
          }
  
          return object;
      }
  
      /**
       * Load an object from source.
       *
       * @param classname the name of object
       * @return the Object
       * @exception Exception if an error occurs
       */
      public Object load( final String classname )
          throws FactoryException
      {
          try
          {
              return ObjectUtil.createObject( m_classLoader, classname );
          }
          catch( final ClassNotFoundException cnfe )
          {
              throw new FactoryException( "Failed to locate class " + classname, cnfe );
          }
          catch( final InstantiationException ie )
          {
              throw new FactoryException( "Failed to instantiate class " + classname, ie );
          }
          catch( final IllegalAccessException iae )
          {
              throw new FactoryException( "Failed to instantiate class " + classname + 
                                          " as it does not have a publicly accesable " +
                                          "default constructor", iae );
          }
          catch( final Throwable t )
          {
              throw new FactoryException( "Failed to get class " + classname + 
                                          " due to " + ExceptionUtil.printStackTrace( t, 5,
true ),
                                          t );
          }
      }
  }
  
  
  
  1.1                  jakarta-avalon/proposal/4.0/src/java/org/apache/avalon/camelot/DefaultLocator.java
  
  Index: DefaultLocator.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 file. 
   */
  package org.apache.avalon.camelot;
  
  import java.net.URL;
  import org.apache.avalon.component.Component;
  import org.apache.avalon.container.*;
  
  /**
   * This contains information required to locate a component.
   * 
   * @author <a href="mailto:donaldp@apache.org">Peter Donald</a>
   */
  public class DefaultLocator
      implements Locator, Component
  {
      protected final String     m_name;
      protected final URL        m_location;
  
      public DefaultLocator( final String name, final URL location )
      {
          m_name = name;
          m_location = location;
      }
      
      /**
       * Retrieve "name" of component type.
       * The "name" usually indicates the classname.
       *
       * @return the name
       */
      public String getName()
      {
          return m_name;
      }
  
      /**
       * Retrieve location of component.
       * Usually references the archive (zip/jar/war/ear)
       * which contains the name (ie classname)
       *
       * @return the URL of location
       */
      public URL getLocation()
      {
          return m_location;
      }
  }
  
  
  
  1.1                  jakarta-avalon/proposal/4.0/src/java/org/apache/avalon/camelot/DefaultLocatorRegistry.java
  
  Index: DefaultLocatorRegistry.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 file. 
   */
  package org.apache.avalon.camelot;
  
  import org.apache.avalon.component.Component;
  import org.apache.avalon.container.*;
  
  /**
   * Represents a Registry of locators.
   *
   * @author <a href="mailto:donaldp@apache.org">Peter Donald</a>
   */
  public class DefaultLocatorRegistry
      extends DefaultRegistry
      implements LocatorRegistry, Component
  {
      public DefaultLocatorRegistry()
      {
          super( Locator.class );
      }
  
      /**
       * Retrieve a Locator by name.
       *
       * @param name the name
       * @return the Info
       * @exception RegistryException if an error occurs
       */
      public Locator getLocator( String name ) 
          throws RegistryException
      {
          return (Locator)getInfo( name );
      }
  }
  
  
  
  1.1                  jakarta-avalon/proposal/4.0/src/java/org/apache/avalon/camelot/DefaultRegistry.java
  
  Index: DefaultRegistry.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 file. 
   */
  package org.apache.avalon.camelot;
  
  import java.util.HashMap;
  import java.util.Iterator;
  import org.apache.avalon.component.Component;
  import org.apache.avalon.container.*;
  
  /**
   * Represents a Registry of names to types.
   *
   * @author <a href="mailto:donaldp@apache.org">Peter Donald</a>
   */
  public class DefaultRegistry
      implements Registry, Component
  {
      protected final HashMap        m_infos       = new HashMap();
      protected final Class          m_infoClass;
  
      public DefaultRegistry( final Class clazz )
      {
          m_infoClass = clazz;
      }
  
      public void register( final String name, final Info info )
          throws RegistryException
      {
          if( null != m_infos.get( name ) )
          {
              throw new RegistryException( "Name " + name + " already registered" );
          }
          else
          {
              checkInfo( name, info );
              m_infos.put( name, info );
          }
      }
  
      public void unregister( final String name )
          throws RegistryException
      {
          if( null == m_infos.remove( name ) )
          {
              throw new RegistryException( "Name " + name + " not registered" );
          }
      }
  
      public Info getInfo( final String name )
          throws RegistryException
      {
          final Info info = (Info)m_infos.get( name );
  
          if( null == info )
          {
              throw new RegistryException( "Name " + name + " not registered" );
          }
          else
          {
              return info;
          }
      }
  
      public Iterator getInfoNames()
      {
          return m_infos.keySet().iterator();
      }
  
      protected void checkInfo( final String name, final Info info )
          throws RegistryException
      {
          if( !m_infoClass.isAssignableFrom( info.getClass() ) )
          {
              throw new RegistryException( "Only Infos of type " + m_infoClass.getName() +
                                           " may be placed in registry." );
          }
      }
  }
  
  
  
  1.1                  jakarta-avalon/proposal/4.0/src/java/org/apache/avalon/camelot/DeployerUtil.java
  
  Index: DeployerUtil.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 file.
   */
  package org.apache.avalon.camelot;
  
  import org.apache.avalon.container.*;
  import java.io.File;
  import java.io.IOException;
  import java.io.InputStream;
  import java.net.MalformedURLException;
  import java.net.URL;
  import java.net.URL;
  import java.util.Properties;
  import java.util.jar.Manifest;
  import java.util.zip.ZipEntry;
  import java.util.zip.ZipException;
  import java.util.zip.ZipFile;
  import org.apache.avalon.component.ComponentManager;
  import org.apache.avalon.component.ComponentNotAccessibleException;
  import org.apache.avalon.component.ComponentNotFoundException;
  import org.apache.avalon.component.Composer;
  import org.apache.avalon.configuration.Configuration;
  import org.apache.avalon.configuration.ConfigurationException;
  import org.apache.avalon.configuration.DefaultConfigurationBuilder;
  import org.xml.sax.SAXException;
  
  /**
   * This class deploys resources from camelot based system.
   *
   * @author <a href="mailto:donaldp@apache.org">Peter Donald</a>
   */
  public final class DeployerUtil
  {
      protected static DefaultConfigurationBuilder  c_configurationBuilder;
  
      /**
       * Private constructor to block instantiation.
       */
      private DeployerUtil()
      {
      }
  
      protected static DefaultConfigurationBuilder getBuilder()
      {
          if( null == c_configurationBuilder )
          {
              c_configurationBuilder = new DefaultConfigurationBuilder();
          }
  
          return c_configurationBuilder;
      }
  
      /**
       * Get zipFile represented by URL.
       *
       * @param url the URL
       * @return the ZipFile
       * @exception DeploymentException if an error occurs
       */
  /*
      public final static ZipFile getZipFileFor( final URL url )
          throws DeploymentException
      {
          final File file = getFileFor( url );
          return getZipFileFor( file );
      }
  */  
      /**
       * Retrieve zip file for file.
       *
       * @param file the file
       * @return the zipFile
       * @exception DeploymentException if an error occurs
       */
      public final static ZipFile getZipFileFor( final File file )
          throws DeploymentException
      {
          try { return new ZipFile( file ); }
          catch( final IOException ioe )
          {
              throw new DeploymentException( "Error opening " + file + 
                                             " due to " + ioe.getMessage(),
                                             ioe );
          }        
      }
      
      /**
       * Utility method to load configuration from zip.
       *
       * @param zipFile the zip file
       * @param filename the property filename
       * @return the Configuration
       * @exception DeploymentException if an error occurs
       */
      public final static Configuration loadConfiguration( final ZipFile zipFile, 
                                                           final String filename )
          throws DeploymentException
      {
          return buildConfiguration( loadResourceStream( zipFile, filename ) );
      }
      
      /**
       * Build a configuration tree based on input stream.
       *
       * @param input the InputStream
       * @return the Configuration tree
       * @exception DeploymentException if an error occurs
       */
      public final static Configuration buildConfiguration( final InputStream input )
          throws DeploymentException
      {
          try { return getBuilder().build( input ); }
          catch( final SAXException se )
          {
              throw new DeploymentException( "Malformed configuration data", se );
          }
          catch( final ConfigurationException ce )
          {
              throw new DeploymentException( "Error building configuration", ce );
          }
          catch( final IOException ioe )
          {
              throw new DeploymentException( "Error reading configuration", ioe );
          }
      }
      
      /**
       * Utility method to load a manifest from a zip file.
       *
       * @param zipFile the zip file
       * @return the Manifest
       */
      public final static Manifest loadManifest( final ZipFile zipFile )
          throws DeploymentException
      {
          final InputStream input = loadResourceStream( zipFile, "META-INF/MANIFEST.MF" );
          
          try { return new Manifest( input ); }
          catch( final IOException ioe ) 
          {
              throw new DeploymentException( "Error reading manifest", ioe );
          }
          finally
          {
              try { input.close(); }
              catch( final IOException ioe ) {}
          }
      }
      
      /**
       * Utility method to load properties from zip.
       *
       * @param zipFile the zip file
       * @param filename the property filename
       * @return the Properties
       * @exception DeploymentException if an error occurs
       */
      public final static Properties loadProperties( final ZipFile zipFile, 
                                                     final String filename )
          throws DeploymentException
      {
          final Properties properties = new Properties();
          
          try { properties.load( loadResourceStream( zipFile, filename ) ); }
          catch( final IOException ioe )
          {
              throw new DeploymentException( "Error reading " + filename + 
                                             " from " + zipFile.getName(),
                                             ioe );
          }
          
          return properties;
      }
      
      /**
       * Load a resource from a zip file.
       *
       * @param zipFile the ZipFile
       * @param filename the filename
       * @return the InputStream
       * @exception DeploymentException if an error occurs
       */
      public final static InputStream loadResourceStream( final ZipFile zipFile, 
                                                          final String filename )
          throws DeploymentException
      {
          final ZipEntry entry = zipFile.getEntry( filename );
          
          if( null == entry )
          {
              throw new DeploymentException( "Unable to locate " + filename + 
                                             " in " + zipFile.getName() );
          }
          
          try { return zipFile.getInputStream( entry ); }
          catch( final IOException ioe )
          {
              throw new DeploymentException( "Error reading " + filename + 
                                             " from " + zipFile.getName(),
                                             ioe );
          }        
      }
  }
  
  
  

Mime
View raw message