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/repository/main/src/java/org/apache/avalon/repository Bootstrapper.java InitialRepositoryFactory.java TargetReference.java
Date Thu, 20 Nov 2003 04:19:07 GMT
mcconnell    2003/11/19 20:19:07

  Modified:    repository/main .cvsignore project.xml
               repository/main/src/java/org/apache/avalon/repository
                        Bootstrapper.java InitialRepositoryFactory.java
                        TargetReference.java
  Log:
  Refactoring to abstract out common utilities to util package.
  
  Revision  Changes    Path
  1.2       +1 -2      avalon-sandbox/repository/main/.cvsignore
  
  Index: .cvsignore
  ===================================================================
  RCS file: /home/cvs/avalon-sandbox/repository/main/.cvsignore,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- .cvsignore	17 Nov 2003 08:18:46 -0000	1.1
  +++ .cvsignore	20 Nov 2003 04:19:07 -0000	1.2
  @@ -2,5 +2,4 @@
   velocity.log
   build.properties
   target
  -.classpath
  -.project
  \ No newline at end of file
  +.*
  \ No newline at end of file
  
  
  
  1.2       +5 -0      avalon-sandbox/repository/main/project.xml
  
  Index: project.xml
  ===================================================================
  RCS file: /home/cvs/avalon-sandbox/repository/main/project.xml,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- project.xml	17 Nov 2003 08:18:46 -0000	1.1
  +++ project.xml	20 Nov 2003 04:19:07 -0000	1.2
  @@ -27,6 +27,11 @@
       </dependency>
       <dependency>
         <groupId>avalon-repository</groupId>
  +      <artifactId>avalon-repository-util</artifactId>
  +      <version>1.2-dev</version>
  +    </dependency>
  +    <dependency>
  +      <groupId>avalon-repository</groupId>
         <artifactId>avalon-repository-impl</artifactId>
         <version>1.2-dev</version>
       </dependency>
  
  
  
  1.6       +17 -6     avalon-sandbox/repository/main/src/java/org/apache/avalon/repository/Bootstrapper.java
  
  Index: Bootstrapper.java
  ===================================================================
  RCS file: /home/cvs/avalon-sandbox/repository/main/src/java/org/apache/avalon/repository/Bootstrapper.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- Bootstrapper.java	19 Nov 2003 18:17:48 -0000	1.5
  +++ Bootstrapper.java	20 Nov 2003 04:19:07 -0000	1.6
  @@ -57,6 +57,8 @@
   import java.lang.reflect.Method ;
   import java.lang.reflect.InvocationTargetException ;
   
  +import org.apache.avalon.repository.util.MavenArtifactFactory ;
  +
   
   /**
    * Application and component bootstrapper used to instantiate, and or invoke
  @@ -95,10 +97,11 @@
       {
           m_repository = a_repository ;
           m_descriptor = artifact;
  +        long time = System.currentTimeMillis();
           m_loader = a_repository.getClassLoader( artifact ) ;
  +        long after = System.currentTimeMillis();
       }
   
  -    
       /**
        * Gets the ClassLoader used by this Bootstrapper.
        * 
  @@ -109,7 +112,6 @@
           return m_loader ;
       }
       
  -
       /**
        * Invokes the main application entry point for a class.
        * 
  @@ -481,16 +483,25 @@
           Repository l_repository ;
           Artifact l_jar = MavenArtifactFactory.createJarArtifact( l_jarSpec ) ;
           Bootstrapper l_bootstrapper ;
  -        
  +
           try
           {
               InitialRepositoryFactory l_factory = 
                   new InitialRepositoryFactory( l_repositories ) ;
  -            RepositoryContext l_context = l_factory.getDefaultContext() ;
  +            RepositoryContext l_context = l_factory.getDefaultContext();
  +
               l_repository = l_factory.create( l_context ) ;
  +
  +            System.out.println(
  +              "local cache: [" 
  +              + l_repository.getLocation() 
  +              + "]");
  +
               l_bootstrapper = new Bootstrapper( l_repository, l_jar ) ;
   
  -            l_bootstrapper.main( l_fqcn, a_args ) ;
  +            System.out.println("established: " + l_jarSpec );
  +
  +            //l_bootstrapper.main( l_fqcn, a_args ) ;
           }
           catch ( RepositoryException e )
           {
  
  
  
  1.7       +31 -294   avalon-sandbox/repository/main/src/java/org/apache/avalon/repository/InitialRepositoryFactory.java
  
  Index: InitialRepositoryFactory.java
  ===================================================================
  RCS file: /home/cvs/avalon-sandbox/repository/main/src/java/org/apache/avalon/repository/InitialRepositoryFactory.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- InitialRepositoryFactory.java	19 Nov 2003 17:28:37 -0000	1.6
  +++ InitialRepositoryFactory.java	20 Nov 2003 04:19:07 -0000	1.7
  @@ -70,6 +70,9 @@
   import javax.naming.NamingEnumeration ;
   import javax.naming.directory.Attributes ;
   
  +import org.apache.avalon.repository.util.LOADER;
  +import org.apache.avalon.repository.util.MavenArtifactFactory ;
  +import org.apache.avalon.repository.util.RepositoryUtils ;
   
   /**
    * Sets up the environment to create repositories by downloading the required 
  @@ -209,9 +212,10 @@
            if( implementation == null ) throw new NullPointerException( "implementation"
);
            if( repositories == null ) throw new NullPointerException( "repositories" );
   
  -        /* 
  -         * Create the temporary directory to pull down files into
  -         */
  +        //
  +        // Create the temporary directory to pull down files into
  +        //
  +
           String l_userHome = System.getProperty( "user.home" ) ;
           File l_tmpDir = new File( l_userHome, ".bootstrap" ) ;
           if ( ! l_tmpDir.exists() )
  @@ -219,51 +223,43 @@
               l_tmpDir.mkdirs() ;
           }
           
  -        /*
  -         * Build the url to access the properties of the implementation artifact
  -         * which is default mechanism dependent.
  -         */
  +        //
  +        // Build the url to access the properties of the implementation artifact
  +        // which is default mechanism dependent.
  +        //
   
           m_repositories = repositories;
           Properties l_props = RepositoryUtils.getProperties( m_repositories, implementation
) ;
           Attributes l_attrs = RepositoryUtils.getAsAttributes( l_props ) ;
           
  -        /*
  -         * Download every dependency referenced by the implementation into the
  -         * temporary cache to build the repository classloader with.
  -         */
  +        //
  +        // Download every dependency referenced by the implementation into the
  +        // temporary cache to build the repository classloader with.
  +        //
  +
           ArrayList l_urlList = new ArrayList() ;
           Artifact l_dep = null;
           String l_spec = null;
           File l_targetFile = null;
           try
           {
  -            // TODO:
  -            // If the key that is supplied here is does not have a value we get null 
  -            // back instead of an empty list
  -
  -            NamingEnumeration l_list = l_attrs.get( AVALON_ARTIFACT_DEPENDENCY ).getAll()
;
  +            NamingEnumeration l_list = 
  +              l_attrs.get( AVALON_ARTIFACT_DEPENDENCY ).getAll() ;
               while ( l_list.hasMore() )
               {
                   l_spec = ( String ) l_list.next() ;
                   l_dep = MavenArtifactFactory.createJarArtifact( l_spec ) ;
  -                
  -                StringBuffer l_target = 
  -                  new StringBuffer( l_tmpDir.toString() ) ;
  -                
  -                if ( '/' != l_target.charAt( l_target.length() - 1 ) )
  -                {
  -                    l_target.append( '/' ) ;
  -                }
  -                
  -                l_target.append( l_dep.getURL( "" ) ) ;
  -                l_targetFile = new File( l_target.toString() ) ;
  -
  -                cacheArtifact( 
  -                  l_dep, m_repositories,
  -                  l_targetFile, true ) ;
  -                l_urlList.add( l_targetFile.toURL() ) ;
  +                l_urlList.add( LOADER.getResource( 
  +                  l_dep, m_repositories, l_tmpDir, true ) ) ;
               }
  +
  +            //
  +            // add the actual implementation jar file
  +            //
  +
  +            l_urlList.add( 
  +              LOADER.getResource( 
  +                implementation, m_repositories, l_tmpDir, true ) );
           }
           catch ( NamingException e )
           {
  @@ -272,14 +268,6 @@
                 + implementation + "]."; 
               throw new RepositoryException( error, e ) ;
           }
  -        catch ( MalformedURLException e )
  -        {
  -            final String error = 
  -              "Unable to create a url to a local file [" + l_targetFile
  -              + "] for the dependency [" + l_spec 
  -              + "] from the implementation reference: [" + implementation + "]."; 
  -            throw new RepositoryException( "Bad url: " + l_dep, e ) ;
  -        }
           catch ( Exception e )
           {
               final String error = 
  @@ -289,26 +277,6 @@
           }
   
           //
  -        // add the actual implementation jar file
  -        //
  -
  -        try
  -        {
  -            StringBuffer target = new StringBuffer( l_tmpDir.toString() ) ;
  -            if ( '/' != target.charAt( target.length() - 1 ) ) target.append( '/' ) ;
  -            target.append( implementation.getURL( "" ) ) ;
  -            File file = new File( target.toString() ) ;
  -            cacheArtifact( implementation, m_repositories, file, true ) ;
  -            l_urlList.add( file.toURL() ) ;
  -        }
  -        catch( Throwable e )
  -        {
  -            final String error = 
  -              "Unable to download target artifact[" + implementation + "]."; 
  -            throw new RepositoryException( error, e ) ;
  -        }
  -
  -        //
           // create the classloader to load the implementation stack
           //
           
  @@ -344,7 +312,7 @@
       }
   
       // ------------------------------------------------------------------------
  -    // RepositoryFactory Implementations
  +    // RepositoryFactory
       // ------------------------------------------------------------------------
       
       /**
  @@ -361,7 +329,6 @@
       {
           return m_delegate.create( context ) ;
       }
  -
       
       /**
        * Factory method that creates a default repository configuration bean 
  @@ -375,239 +342,9 @@
       }
       
       // ------------------------------------------------------------------------
  -    // Utility Methods
  +    // utilities
       // ------------------------------------------------------------------------
       
  -    /**
  -     * Attempts to download and cache a remote artifact trying a set of remote
  -     * repositories.  The operation is not fail fast and so it keeps trying if
  -     * the first repository does not have the artifact in question.
  -     * 
  -     * @param a_artifact the artifact to retrieve and cache
  -     * @param a_repos the remote repositories to try to download from 
  -     * @param a_destFile where to store it locally in the cache
  -     * @param a_useTimestamp whether to check the modified timestamp on the
  -     *      <code>destinationFile</code> against the remote <code>source</code>
  -     * @return TRUE if the file was updated else FALSE
  -     */
  -    public static boolean cacheArtifact( Artifact a_artifact, 
  -        String [] a_repos, File a_destFile, boolean a_useTimestamp ) 
  -        throws RepositoryException
  -    {
  -        boolean l_updated = false ;
  -        Exception l_cause = null ;
  -        
  -        /*
  -         * Don't fail fast - keep trying until we get something.
  -         */
  -        for ( int ii = 0; ii < a_repos.length; ii++ )
  -        {
  -            try 
  -            {
  -                String l_url = a_artifact.getURL( a_repos[ii] ) ;
  -                l_updated = getTempFile( l_url, a_destFile, a_useTimestamp ) ;
  -                return l_updated ;
  -            }
  -            catch ( Exception e )
  -            {
  -                l_cause = e ;
  -            }
  -        }
  -        
  -        if ( null == l_cause )
  -        {
  -            throw new RepositoryException( "Failed to download artifact to "
  -                + "local cache file " + a_destFile.getAbsolutePath() ) ;
  -        }
  -
  -        // Should not really execute but it shuts up the compiler
  -        throw new RepositoryException( "Failed to download artifact to "
  -            + "local cache file " + a_destFile.getAbsolutePath(), l_cause ) ;
  -    }
  -
  -    
  -    /**
  -     * Retrieve a remote file.  Returns true if the file was successfully
  -     * retrieved or if it is up to date (when the useTimestamp flag is set).
  -     * 
  -     * Moved here from HttpController.
  -     *
  -     * @param a_url the of the file to retrieve
  -     * @param a_destFile where to store it
  -     * @param a_useTimestamp whether to check the modified timestamp on the
  -     *      <code>destinationFile</code> against the remote <code>source</code>
  -     * @return TRUE if the file was updated else FALSE
  -     */
  -    public static boolean getTempFile( 
  -      String a_url, File a_destFile, boolean a_useTimestamp ) 
  -      throws Exception
  -    {
  -        //
  -        // if timestamp is enabled and the destination file exists and 
  -        // the source is a file - then do a quick check using native File
  -        // last modification dates to see if anything needs to be done
  -        // 
  -
  -        if( a_useTimestamp && a_destFile.exists() && a_url.startsWith(
"file:" ) )
  -        {
  -            try
  -            {
  -                URL sourceFileUrl = new URL( a_url );
  -                String sourcePath = sourceFileUrl.getPath();
  -                File sourceFile = new File( sourcePath );
  -                if( a_destFile.lastModified() >= sourceFile.lastModified()  )
  -                {
  -                    return false;
  -                }
  -            }
  -            catch( Throwable e )
  -            {
  -                e.printStackTrace();
  -            }
  -        }
  -
  -        //
  -        // otherwise continue with classic processing
  -        //
  -
  -        URL l_source = null ; 
  -        String l_username = null ;
  -        String l_password = null ;
  -
  -        // We want to be able to deal with Basic Auth where the username
  -        // and password are part of the URL. An example of the URL string
  -        // we would like to be able to parse is like the following:
  -        //
  -        // http://username:password@repository.mycompany.com
  -
  -        int l_atIdx = a_url.indexOf( "@" ) ;
  -        if ( l_atIdx > 0 )
  -        {
  -            String s = a_url.substring( 7, l_atIdx ) ;
  -            int l_colonIdx = s.indexOf( ":" ) ;
  -            l_username = s.substring( 0, l_colonIdx ) ;
  -            l_password = s.substring( l_colonIdx + 1 ) ;
  -            l_source = new URL( "http://" + a_url.substring( l_atIdx + 1 ) ) ;
  -        }
  -        else
  -        {
  -            l_source = new URL( a_url ) ;
  -        }
  -
  -        //set the timestamp to the file date.
  -        long l_timestamp = 0 ;
  -        boolean l_hasTimestamp = false ;
  -        if ( a_useTimestamp && a_destFile.exists() )
  -        {
  -            l_timestamp = a_destFile.lastModified() ;
  -            l_hasTimestamp = true ;
  -        }
  -
  -        //set up the URL connection
  -        URLConnection l_connection = l_source.openConnection() ;
  -
  -        //modify the headers
  -        //NB: things like user authentication could go in here too.
  -        if ( a_useTimestamp && l_hasTimestamp )
  -        {
  -            l_connection.setIfModifiedSince( l_timestamp ) ;
  -        }
  -
  -        //connect to the remote site (may take some time)
  -        l_connection.connect() ;
  -        //next test for a 304 result (HTTP only)
  -        if ( l_connection instanceof HttpURLConnection )
  -        {
  -            HttpURLConnection l_httpConnection = 
  -                ( HttpURLConnection ) l_connection ;
  -            
  -            if ( l_httpConnection.getResponseCode() == 
  -                    HttpURLConnection.HTTP_NOT_MODIFIED )
  -            {
  -                return false ;
  -            }
  -            
  -            // test for 401 result (HTTP only)
  -            if ( l_httpConnection.getResponseCode() == 
  -                    HttpURLConnection.HTTP_UNAUTHORIZED )
  -            {
  -                throw new Exception( "Not authorized." ) ;
  -            }
  -        }
  -
  -        // REVISIT: at this point even non HTTP connections may support the
  -        // if-modified-since behaviour - we just check the date of the
  -        // content and skip the write if it is not newer.
  -        // Some protocols (FTP) dont include dates, of course.
  -
  -        InputStream l_in = null ;
  -        for ( int ii = 0; ii < 3; ii++ )
  -        {
  -            try
  -            {
  -                l_in = l_connection.getInputStream() ;
  -                break ;
  -            }
  -            catch ( IOException ex )
  -            {
  -                // do nothing
  -            }
  -        }
  -        if ( l_in == null )
  -        {
  -            final String error = 
  -              "Connection returned a null input stream: " + a_url ;
  -            throw new IOException( error ) ;
  -        }
  -
  -        File parent = a_destFile.getParentFile() ;
  -        parent.mkdirs() ;
  -
  -        FileOutputStream l_out = new FileOutputStream( a_destFile ) ;
  -
  -        byte[] buffer = new byte[100 * 1024] ;
  -        int length ;
  -
  -        System.out.print( "Source: " + l_source + "\n") ;
  -        while ( ( length = l_in.read( buffer ) ) >= 0 )
  -        {
  -            l_out.write( buffer, 0, length ) ;
  -            System.out.print( "." ) ;
  -        }
  -
  -        System.out.println( "" ) ;
  -        l_out.close() ;
  -        l_in.close() ;
  -
  -        // if (and only if) the use file time option is set, then the
  -        // saved file now has its timestamp set to that of the downloaded
  -        // file
  -
  -        if ( a_useTimestamp )
  -        {
  -            long l_remoteTimestamp = l_connection.getLastModified() ;
  -
  -            if ( l_remoteTimestamp != 0 )
  -            {
  -                long l_modifiedTime ;
  -
  -                if ( l_remoteTimestamp  < 0 )
  -                {
  -                    l_modifiedTime = System.currentTimeMillis() ;
  -                }
  -                else
  -                {
  -                    l_modifiedTime = l_remoteTimestamp  ;
  -                }
  -
  -                a_destFile.setLastModified( l_modifiedTime ) ;
  -                return true ;
  -            }
  -        }
  -        
  -        return true ;
  -    }
  -
       private void printClassLoader( ClassLoader loader )
       {
           if( loader == null ) return;
  
  
  
  1.2       +2 -0      avalon-sandbox/repository/main/src/java/org/apache/avalon/repository/TargetReference.java
  
  Index: TargetReference.java
  ===================================================================
  RCS file: /home/cvs/avalon-sandbox/repository/main/src/java/org/apache/avalon/repository/TargetReference.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- TargetReference.java	19 Nov 2003 17:28:37 -0000	1.1
  +++ TargetReference.java	20 Nov 2003 04:19:07 -0000	1.2
  @@ -53,6 +53,8 @@
   
   import java.util.Properties ;
   
  +import org.apache.avalon.repository.util.MavenArtifactFactory ;
  +import org.apache.avalon.repository.util.RepositoryUtils ;
   
   /**
    * A build descriptor can be generated from a jar that has extra jar build 
  
  
  

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


Mime
View raw message