avalon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mcconn...@apache.org
Subject cvs commit: avalon-sandbox/merlin/assembly/src/java/org/apache/avalon/assembly/repository/impl FileRepository.java
Date Wed, 30 Apr 2003 10:24:13 GMT
mcconnell    2003/04/30 03:24:12

  Modified:    merlin/assembly/src/java/org/apache/avalon/assembly/repository/impl
                        FileRepository.java
  Log:
  Updates to handle installation of an application.
  
  Revision  Changes    Path
  1.2       +211 -8    avalon-sandbox/merlin/assembly/src/java/org/apache/avalon/assembly/repository/impl/FileRepository.java
  
  Index: FileRepository.java
  ===================================================================
  RCS file: /home/cvs/avalon-sandbox/merlin/assembly/src/java/org/apache/avalon/assembly/repository/impl/FileRepository.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- FileRepository.java	24 Apr 2003 12:10:44 -0000	1.1
  +++ FileRepository.java	30 Apr 2003 10:24:12 -0000	1.2
  @@ -50,11 +50,21 @@
   
   package org.apache.avalon.assembly.repository.impl;
   
  +import java.io.BufferedInputStream;
  +import java.io.BufferedOutputStream;
   import java.io.File;
  +import java.io.FileOutputStream;
  +import java.io.InputStream;
  +import java.io.OutputStream;
   import java.net.URL;
  +import java.net.URLConnection;
  +import java.util.ArrayList;
  +import java.util.List;
   
   import org.apache.avalon.assembly.repository.Repository;
   import org.apache.avalon.assembly.repository.RepositoryException;
  +import org.apache.avalon.assembly.repository.TransportException;
  +import org.apache.avalon.assembly.util.ExceptionHelper;
   import org.apache.avalon.framework.context.Context;
   import org.apache.avalon.framework.context.ContextException;
   import org.apache.avalon.framework.context.Contextualizable;
  @@ -75,10 +85,42 @@
   
       public static final String BASE_KEY = "urn:avalon:repository.base";
   
  +    public static final String HOSTS_KEY = "urn:avalon:repository.hosts";
  +
  +    public static final URL DEFAULT_HOST = getDefaultHost();
  +
  +    private static URL getDefaultHost()
  +    {
  +        try
  +        {
  +            return new URL( "http://www.osm.net/repository/" );
  +        }
  +        catch( Throwable e )
  +        {
  +            throw new RuntimeException( "default-host" );
  +        }
  +    }
  +
  +   /**
  +    * Utility method to construct a new repository using the supplied
  +    * directory as a local file cache.
  +    */
       public static Repository newInstance( File base ) throws ContextException
       {
  +        final URL[] hosts = new URL[]{ DEFAULT_HOST };
  +        return newInstance( hosts, base );
  +    }
  +
  +   /**
  +    * Utility method to construct a new repository using the supplied
  +    * directory as a local file cache and the supplied URL array as a
  +    * sequence of remote repository sources.
  +    */
  +    public static Repository newInstance( URL[] hosts, File base ) throws ContextException
  +    {
           FileRepository repository = new FileRepository();
           DefaultContext context = new DefaultContext();
  +        context.put( HOSTS_KEY, hosts );
           context.put( BASE_KEY, base );
           repository.contextualize( context );
           return repository;
  @@ -88,8 +130,16 @@
       // state 
       //------------------------------------------------------------------
   
  +   /**
  +    * The directory referencing the local repository cache.
  +    */
       private File m_base;
   
  +   /**
  +    * Sequence of remote hosts.
  +    */
  +    private URL[] m_hosts;
  +
       //------------------------------------------------------------------
       // lifecycle 
       //------------------------------------------------------------------
  @@ -104,11 +154,8 @@
       */
       public void contextualize( Context context ) throws ContextException
       {
  -        Object object = context.get( BASE_KEY );
  -        if( object instanceof File )
  -        {
  -            m_base = (File) object;
  -        }
  +        m_base = (File) context.get( BASE_KEY );
  +        m_hosts = (URL[]) context.get( HOSTS_KEY );       
       }
   
       //------------------------------------------------------------------
  @@ -152,8 +199,7 @@
           if( !application.exists() )
           {
               final String error = 
  -              "Unknown group: " + group 
  -              + ". File: '" + application + "'.";
  +              "Unknown group: " + group;
               throw new RepositoryException( error );
           }
   
  @@ -178,6 +224,7 @@
                 + ". File: '" + resource + "'.";
               throw new RepositoryException( error );
           }
  +
           try
           {
               if( type.equals( "jar" ) )
  @@ -218,6 +265,162 @@
       private String getResourceName( String artifact, int index )
       {
           return artifact.substring( index + 1 );
  +    }
  +
  +    public void install( String group ) throws RepositoryException
  +    {
  +        m_base.mkdirs();
  +        final File file = new File( m_base, group + "-package.zip" );
  +        download( group, file );
  +        expand( group, file );
  +    }
  +
  +    public void download( String group, File file ) throws RepositoryException
  +    {
  +        final List errors = new ArrayList();
  +        if( getRemoteResource( group, file, errors ) )
  +        {
  +            //return getArtifact( group, name, version, type );
  +            System.out.println( "SUCCESS" );
  +            return;
  +        }
  +
  +        RepositoryException[] exceptions = 
  +         (RepositoryException[]) errors.toArray( new RepositoryException[0] );
  +
  +        final String header = 
  +         "Unable to resolve repository package for group: " + group;
  +        StringBuffer buffer = new StringBuffer( header );
  +        for( int i=0; i<exceptions.length; i++ )
  +        {
  +            buffer.append( "\n" );
  +            final String message = 
  +            ExceptionHelper.packException( "Connection: " + (i+1), exceptions[i] );
  +            buffer.append( message );
  +        }
  +        throw new RepositoryException( buffer.toString() );
  +    }
  +
  +    public void expand( String group, File file )
  +    {
  +        // todo
  +    }
  +
  +    private boolean getRemoteResource( String group, File file, List errors )
  +    {
  +        for( int i=0; i<m_hosts.length; i++ )
  +        {
  +            URL url = m_hosts[i];
  +            try
  +            {
  +                final URL path = new URL( url, url.getPath() + group + "/" + group + "-package.zip"
);
  +                transfer( path, file );
  +                return true;
  +            }
  +            catch( RepositoryException e )
  +            {
  +                errors.add( e );
  +            }
  +            catch( Throwable e )
  +            {
  +                final String error = 
  +                  "Unexpected error while handling url: " + url;
  +                errors.add( new RepositoryException( error, e ) );
  +            }
  +        }
  +        return false;
  +    }
  +
  +   /**
  +    * Transfer the content of the supplied URL to a local file.
  +    * @param url the source URL
  +    * @param file the destination file
  +    * @exception TransportException if a transport related error occurs
  +    */
  +    public static void transfer( final URL url, final File file ) throws TransportException
  +    {
  +        final URLConnection source = getConnection( url );
  +
  +        Throwable cause = null;
  +        BufferedOutputStream output = null;
  +        try
  +        {
  +            final BufferedInputStream input = new BufferedInputStream( source.getInputStream());
  +            output = new BufferedOutputStream( new FileOutputStream( file ) );
  +            transfer( input, output );
  +        }
  +        catch( Throwable e )
  +        {
  +            cause = e;
  +        }
  +        finally
  +        {
  +            //
  +            // cleanup
  +            //
  +
  +            if( output != null )
  +            {
  +                try
  +                {
  +                    output.close();
  +                }
  +                catch( Throwable e )
  +                {
  +                    // ignore
  +                }
  +            }
  +        }
  +
  +        //
  +        // check for errors
  +        //
  +
  +        if( cause != null )
  +        {
  +            final String error = 
  +              "Transfer failure while replicating: " + url
  +              + " to: " + file;
  +            throw new TransportException( error, cause );
  +        }
  +    }
  +
  +   /**
  +    * Internal utility to get the connection.
  +    * @param url the source url
  +    * @exception TransportException if a connection resolution error occurs
  +    */
  +    private static URLConnection getConnection( final URL url ) throws TransportException
  +    {
  +        try
  +        {
  +            return url.openConnection();
  +        }
  +        catch( Throwable e )
  +        {
  +            final String error = 
  +              "Unable to open a connection to the url: " + url;
  +            throw new TransportException( error, e );
  +        }
  +    }
  +
  +   /**
  +    * Transfer the input stream to the output stream.
  +    *
  +    * @param input the input stream to read from
  +    * @param output the output stream to write to
  +    * @exception Exception if an error occurs
  +    */
  +    private static void transfer( InputStream input, OutputStream output )
  +        throws Exception
  +    {
  +        byte[] buffer = new byte[1024];
  +        int count = 0;
  +        while( count != -1 )
  +        {
  +            output.write( buffer, 0, count );
  +            count = input.read( buffer, 0, buffer.length );
  +        }
       }
   
   }
  
  
  

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


Mime
View raw message