You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@avalon.apache.org by mc...@apache.org on 2003/06/28 00:52:13 UTC
cvs commit: avalon-sandbox/merlin/assembly/src/test/org/apache/avalon/assembly/repository LocalRepositoryTestCase.java
mcconnell 2003/06/27 15:52:12
Modified: merlin/assembly/src/java/org/apache/avalon/assembly/appliance/impl
DefaultAppliance.java
merlin/assembly/src/java/org/apache/avalon/assembly/engine/impl
EngineClassLoader.java
merlin/assembly/src/java/org/apache/avalon/assembly/lifecycle/impl
DefaultDeploymentService.java
merlin/assembly/src/java/org/apache/avalon/assembly/lifestyle/impl
SingletonLifestyleHandler.java
merlin/assembly/src/java/org/apache/avalon/assembly/repository/impl
FileRepository.java
merlin/assembly/src/test/org/apache/avalon/assembly
TestBase.java
merlin/assembly/src/test/org/apache/avalon/assembly/engine
ApplianceTestCase.java EngineTestCase.java
FactoryTestCase.java
merlin/assembly/src/test/org/apache/avalon/assembly/repository
LocalRepositoryTestCase.java
Added: merlin/assembly/src/java/org/apache/avalon/assembly/repository/impl
DefaultAuthenticator.java HttpController.java
Log:
Updates incorporating a structured repository implemetation.
Revision Changes Path
1.18 +1 -3 avalon-sandbox/merlin/assembly/src/java/org/apache/avalon/assembly/appliance/impl/DefaultAppliance.java
Index: DefaultAppliance.java
===================================================================
RCS file: /home/cvs/avalon-sandbox/merlin/assembly/src/java/org/apache/avalon/assembly/appliance/impl/DefaultAppliance.java,v
retrieving revision 1.17
retrieving revision 1.18
diff -u -r1.17 -r1.18
--- DefaultAppliance.java 24 Jun 2003 20:50:18 -0000 1.17
+++ DefaultAppliance.java 27 Jun 2003 22:52:11 -0000 1.18
@@ -663,7 +663,6 @@
* assembly directive, or implicity disabled as a result of an assembly failure.
*
* @return TRUE if the profile is enabled.
- * @see #setEnabled( boolean )
*/
public boolean isEnabled()
{
@@ -673,7 +672,6 @@
/**
* Set the enabled status of the profile to the supplied value.
* @param value the enabled status - TRUE or FALSE
- * @see #isEnabled()
*/
private void setEnabled( boolean value )
{
1.11 +3 -19 avalon-sandbox/merlin/assembly/src/java/org/apache/avalon/assembly/engine/impl/EngineClassLoader.java
Index: EngineClassLoader.java
===================================================================
RCS file: /home/cvs/avalon-sandbox/merlin/assembly/src/java/org/apache/avalon/assembly/engine/impl/EngineClassLoader.java,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -r1.10 -r1.11
--- EngineClassLoader.java 23 Jun 2003 02:37:49 -0000 1.10
+++ EngineClassLoader.java 27 Jun 2003 22:52:11 -0000 1.11
@@ -291,27 +291,11 @@
*/
public void contextualize( Locator context ) throws ContextException
{
+ m_repository = (Repository) context.get( Repository.KEY );
+
if( context.hasEntry( "urn:assembly:system" ) )
{
m_root = (File) context.get( "urn:assembly:system" );
- }
-
- //
- // a repository is option - if not supplied and no root system directory
- // if declared, then resource references in a classpath will raise an error.
- //
-
- if( context.hasEntry( Repository.KEY ) )
- {
- m_repository = (Repository) context.get( Repository.KEY );
- }
- else
- {
- if( m_root != null )
- {
- File repository = new File( m_root, "repository" );
- m_repository = FileRepository.newInstance( repository );
- }
}
if( context.hasEntry( "urn:assembly:system-map" ) )
1.16 +3 -3 avalon-sandbox/merlin/assembly/src/java/org/apache/avalon/assembly/lifecycle/impl/DefaultDeploymentService.java
Index: DefaultDeploymentService.java
===================================================================
RCS file: /home/cvs/avalon-sandbox/merlin/assembly/src/java/org/apache/avalon/assembly/lifecycle/impl/DefaultDeploymentService.java,v
retrieving revision 1.15
retrieving revision 1.16
diff -u -r1.15 -r1.16
--- DefaultDeploymentService.java 22 Jun 2003 00:28:33 -0000 1.15
+++ DefaultDeploymentService.java 27 Jun 2003 22:52:12 -0000 1.16
@@ -310,7 +310,7 @@
else
{
final String error =
- "Component deployment failure in appliance: "
+ "Component deployment failure in: "
+ appliance;
throw new DeploymentException( error, ce );
}
@@ -322,7 +322,7 @@
catch( Throwable e )
{
final String error =
- "Component deployment failure in appliance: "
+ "Component deployment failure in: "
+ appliance;
throw new DeploymentException( error, e );
}
1.6 +9 -1 avalon-sandbox/merlin/assembly/src/java/org/apache/avalon/assembly/lifestyle/impl/SingletonLifestyleHandler.java
Index: SingletonLifestyleHandler.java
===================================================================
RCS file: /home/cvs/avalon-sandbox/merlin/assembly/src/java/org/apache/avalon/assembly/lifestyle/impl/SingletonLifestyleHandler.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- SingletonLifestyleHandler.java 24 Jun 2003 20:50:19 -0000 1.5
+++ SingletonLifestyleHandler.java 27 Jun 2003 22:52:12 -0000 1.6
@@ -99,6 +99,14 @@
Object object = newInstance();
super.processAccessStage( object );
+
+ if( getLogger().isDebugEnabled() )
+ {
+ final String debug =
+ "access complete: " + getAppliance();
+ getLogger().debug( debug );
+ }
+
return object;
}
1.5 +202 -236 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.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- FileRepository.java 26 May 2003 17:51:52 -0000 1.4
+++ FileRepository.java 27 Jun 2003 22:52:12 -0000 1.5
@@ -60,25 +60,31 @@
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
+import java.net.Authenticator;
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.logger.AbstractLogEnabled;
import org.apache.avalon.framework.context.Context;
import org.apache.avalon.framework.context.ContextException;
import org.apache.avalon.framework.context.Contextualizable;
import org.apache.avalon.framework.context.DefaultContext;
+import org.apache.avalon.framework.configuration.Configurable;
+import org.apache.avalon.framework.configuration.Configuration;
+import org.apache.avalon.framework.configuration.ConfigurationException;
+import org.apache.excalibur.configuration.ConfigurationUtil;
/**
* A component that provides access to versioned resources based on
* an underlying file system.
*
- * @author <a href="mailto:mcconnell@osm.net">Stephen McConnell</a>
- * @author <a href="mailto:dwayne@java-fan.com">David Bernard</a>
+ * @author <a href="mailto:dev@avalon.apache.org">Avalon Development Team</a>
* @version $Revision$ $Date$
*/
-public class FileRepository implements Repository, Contextualizable
+public class FileRepository extends AbstractLogEnabled
+ implements Contextualizable, Configurable, Repository
{
//------------------------------------------------------------------
// static
@@ -86,47 +92,6 @@
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;
- }
-
//------------------------------------------------------------------
// state
//------------------------------------------------------------------
@@ -141,6 +106,7 @@
*/
private URL[] m_hosts;
+
//------------------------------------------------------------------
// lifecycle
//------------------------------------------------------------------
@@ -153,10 +119,128 @@
* @exception ContextException of the supplied context does not include
* a File instance under the context entry 'urn:avalon:repository.base'.
*/
- public void contextualize( Context context ) throws ContextException
+ public void contextualize( final Context context ) throws ContextException
{
m_base = (File) context.get( BASE_KEY );
- m_hosts = (URL[]) context.get( HOSTS_KEY );
+ }
+
+ /**
+ * <p>Configuration of the repository by the container.</p>
+ * <p>Example configuration:</p>
+ * <pre>
+ * <repository>
+ * <cache></cache> <!-- optional -->
+ * <proxy> <!-- optional -->
+ * <host></host>
+ * <port></port>
+ * <credentials>
+ * <username></username>
+ * <password></password>
+ * </credentials>
+ * </proxy>
+ * <hosts> <!-- optional -->
+ * <host></host>
+ * <host></host>
+ * <hosts>
+ * </repository>
+ * </pre>
+ *
+ * @param config the repostory configuration
+ * @exception ConfigurationException if a confiuration related error occurs
+ */
+ public void configure( final Configuration config ) throws ConfigurationException
+ {
+ if( config == null )
+ {
+ throw new NullPointerException( "config" );
+ }
+
+ //
+ // set the repository cache
+ //
+
+ String cache = config.getChild( "cache" ).getValue( null );
+ if( cache != null )
+ {
+ m_base = new File( cache );
+ getLogger().debug( "setting cache to: " + m_base );
+ }
+ m_base.mkdirs();
+
+ //
+ // set the remote repository urls
+ //
+
+ final ArrayList list = new ArrayList();
+ final Configuration[] hosts = config.getChild( "hosts" ).getChildren( "host" );
+ for( int i=0; i<hosts.length; i++ )
+ {
+ Configuration host = hosts[i];
+ try
+ {
+ URL url = new URL( host.getValue() );
+ final String protocol = url.getProtocol();
+ if( url.getProtocol().equals( "http" ) )
+ {
+ list.add( url );
+ getLogger().debug( "register host: " + url );
+ }
+ else
+ {
+ final String error =
+ "Unsupported protocol: " + protocol
+ + ConfigurationUtil.list( host );
+ }
+ }
+ catch( Throwable e )
+ {
+ final String error =
+ "Invalid host declaration: "
+ + ConfigurationUtil.list( host );
+ throw new ConfigurationException( error, e );
+ }
+ }
+
+ m_hosts = (URL[]) list.toArray( new URL[0] );
+
+ //
+ // handle the proxy declaration
+ //
+
+ Configuration proxy = config.getChild( "proxy", false );
+ if( proxy != null )
+ {
+
+ final String host = proxy.getChild( "host" ).getValue( null );
+ final String port = proxy.getChild( "port" ).getValue( null );
+
+ getLogger().debug( "configuring proxy on:" + host + ":" + port );
+
+ if( host == null )
+ {
+ final String error =
+ "Proxy configuration does not contain the required 'host' element."
+ + ConfigurationUtil.list( proxy );
+ throw new ConfigurationException( error );
+ }
+
+ if( port == null )
+ {
+ final String error =
+ "Proxy configuration does not contain the required 'port' element."
+ + ConfigurationUtil.list( proxy );
+ throw new ConfigurationException( error );
+ }
+
+ System.getProperties().put( "proxySet", "true" );
+ System.getProperties().put( "proxyHost", host );
+ System.getProperties().put( "proxyPort", port );
+
+ DefaultAuthenticator authenticator =
+ new DefaultAuthenticator( proxy.getChild( "credentials" ) );
+ Authenticator.setDefault( authenticator );
+
+ }
}
//------------------------------------------------------------------
@@ -196,6 +280,75 @@
final String group, final String name, final String version, final String type )
throws RepositoryException
{
+ String path = group + "/" + type + "s/" + name;
+ if( ( version != null ) && ( version.length() != 0 ) )
+ {
+ path = path + "-" + version;
+ }
+
+ path = path + "." + type;
+
+ RepositoryException repositoryException = null;
+ try
+ {
+ getLocalArtifact( group, name, version, type );
+ }
+ catch( RepositoryException e )
+ {
+ repositoryException = e;
+ getLogger().debug( "resolving: " + path );
+ for( int i=0; i<m_hosts.length; i++ )
+ {
+ final URL source = m_hosts[i];
+ final String host = source.toString();
+ final File local = new File( m_base, path );
+ final String url = host + path;
+
+ try
+ {
+ if( HttpController.getFile( url, local, true ) )
+ {
+ final String message = "source: " + host;
+ getLogger().debug( message );
+ return getLocalArtifact( group, name, version, type );
+ }
+ }
+ catch( Throwable anything )
+ {
+ // ignore and try the next host
+ }
+ }
+ }
+ catch( Throwable e )
+ {
+ final String error =
+ "Unexpected error while attempting to resolve artifact: " + path;
+ throw new RepositoryException( error, e );
+ }
+
+ //
+ // fallback to throwing a local repository error
+ //
+
+ throw repositoryException;
+
+ }
+
+ /**
+ * Get a resource relative to the supplied application name,
+ * resource name, version and resource type from the local cache.
+ *
+ * @param group the application or group name
+ * @param name the resource name
+ * @param version the version identifier
+ * @param type the resource type
+ * @return the resource
+ */
+ private URL getLocalArtifact(
+ final String group, final String name,
+ final String version, final String type )
+ throws RepositoryException
+ {
File application = new File( m_base, group );
if( !application.exists() )
{
@@ -216,7 +369,7 @@
//
// we only append the version identifier if the version
- // value is not null and at least more than 0 characters
+ // value is not null and more than 0 characters
//
String resourceName = name;
@@ -257,7 +410,7 @@
}
}
- private int verify( String artifact ) throws RepositoryException
+ private int verify( final String artifact ) throws RepositoryException
{
int n = artifact.indexOf( SEPERATOR );
if( n < 1 )
@@ -277,193 +430,6 @@
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 );
- }
-
- /**
- * Replicate an group application package using the available hosts in
- * order implied by the hosts sequence.
- *
- * @param group the group identifier
- * @param file the target file
- * @exception RepositoryException if the replication was not successful
- */
- public void download( String group, File file ) throws RepositoryException
- {
- final List errors = new ArrayList();
- if( getRemoteResource( group, file, errors ) )
- {
- return;
- }
-
- //
- // build a consolidated exception - this code will be updated
- // to use a special exception that contains the exception set
- //
-
- 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() );
- }
-
- /**
- * Expand the supplied file under the declared group identifier.
- * @param group the group identifier
- * @param file the file to expand
- */
- public void expand( String group, File file )
- {
- // todo
- }
-
- /**
- * Replicate a resource from one of the remote servers to the repository
- * base directory. If the replication is unsuccessful, the list will
- * the set of exceptions raised during host traversal.
- *
- * @param group the group identifier
- * @param file the file that the resource should be downloaded to
- * @param list an empty list into which any exceptions should be included
- * @return TRUE if the resource was sucefully replicated else FALSE
- */
- 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 );
- }
}
}
1.1 avalon-sandbox/merlin/assembly/src/java/org/apache/avalon/assembly/repository/impl/DefaultAuthenticator.java
Index: DefaultAuthenticator.java
===================================================================
/*
============================================================================
The Apache Software License, Version 1.1
============================================================================
Copyright (C) 1999-2002 The Apache Software Foundation. All rights reserved.
Redistribution and use in source and binary forms, with or without modifica-
tion, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. The end-user documentation included with the redistribution, if any, must
include the following acknowledgment: "This product includes software
developed by the Apache Software Foundation (http://www.apache.org/)."
Alternately, this acknowledgment may appear in the software itself, if
and wherever such third-party acknowledgments normally appear.
4. The names "Jakarta", "Apache Avalon", "Avalon Framework" and
"Apache Software Foundation" must not be used to endorse or promote
products derived from this software without prior written
permission. For written permission, please contact apache@apache.org.
5. Products derived from this software may not be called "Apache", nor may
"Apache" appear in their name, without prior written permission of the
Apache Software Foundation.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
This software consists of voluntary contributions made by many individuals
on behalf of the Apache Software Foundation. For more information on the
Apache Software Foundation, please see <http://www.apache.org/>.
*/
package org.apache.avalon.assembly.repository.impl;
import java.net.Authenticator;
import java.net.PasswordAuthentication;
import org.apache.avalon.framework.configuration.Configuration;
/**
* Default authenticator that provides support for username password
* based authentication in conjunction with the repository proxy settings.
*
* @author <a href="mailto:dev@avalon.apache.org">Avalon Development Team</a>
* @version $Revision: 1.1 $ $Date: 2003/06/27 22:52:12 $
*/
public final class DefaultAuthenticator extends Authenticator
{
/**
* Proxy username.
*/
private String m_username;
/**
* Proxy password.
*/
private char[] m_password;
/**
* <p>Configuration of the authenticator by the container.</p>
*
* <p>Example configuration:</p>
* <code>
* <credentials>
* <username>test</username>
* <password>test</password>
* <credentials>
* </code>
* @param config the authenticator configuration
* @exception NullPointerException if the supplied configuration is null
*/
public DefaultAuthenticator( Configuration config ) throws NullPointerException
{
if( config == null )
{
throw new NullPointerException( "config" );
}
m_username = config.getChild( "username" ).getValue( null );
String password = config.getChild( "password" ).getValue( null );
if( password == null )
{
m_password = new char[0];
}
else
{
m_password = password.toCharArray();
}
}
/**
* Returns the password authenticator.
* @return the password authenticator
*/
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication( m_username, m_password );
}
}
1.1 avalon-sandbox/merlin/assembly/src/java/org/apache/avalon/assembly/repository/impl/HttpController.java
Index: HttpController.java
===================================================================
package org.apache.avalon.assembly.repository.impl;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.Authenticator;
import java.net.HttpURLConnection;
import java.net.PasswordAuthentication;
import java.net.URL;
import java.net.URLConnection;
/**
* Class supporting the retrival of remotely located resources.
* Implementation is based on original code from the Maven project
* refactored to seperate static proxy establishment from transport
* concerns.
*
* @author costin@dnt.ro
* @author gg@grtmail.com (Added Java 1.1 style HTTP basic auth)
* @author <a href="mailto:jason@zenplex.com">Jason van Zyl</a>
* @author <a href="mailto:dev@avalon.apache.org">Avalon Development Team</a>
* @version $Revision: 1.1 $ $Date: 2003/06/27 22:52:12 $
*/
class HttpController
{
/**
* Retrieve a remote file. Returns true if the file was updated, false
* if the existing cached resource was not modified.
*
* @param url the of the file to retrieve
* @param destinationFile where to store it
* @param useTimestamp whether to check the modified timestamp on the
* <code>destinationFile</code> against the remote <code>source</code>
* @param useChecksum Flag to indicate the use of the checksum for the retrieved
* artifact if it is available.
*/
public static boolean getFile(
String url, File destinationFile, boolean useTimestamp, boolean useChecksum )
throws Exception
{
// Get the requested file.
boolean result = getFile( url, destinationFile, useTimestamp );
// Get the checksum if requested.
if ( useChecksum )
{
File checksumFile = new File( destinationFile + ".md5" );
try
{
getFile( url + ".md5", checksumFile, useTimestamp );
}
catch ( Exception e )
{
// do nothing we will check later in the process
// for the checksums.
}
}
return result;
}
/**
* 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).
*
* @param url the of the file to retrieve
* @param destinationFile where to store it
* @param 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 getFile( String url,
File destinationFile,
boolean useTimestamp )
throws Exception
{
String[] s = parseUrl( url );
String username = s[0];
String password = s[1];
String parsedUrl = s[2];
URL source = new URL( parsedUrl );
//set the timestamp to the file date.
long timestamp = 0;
boolean hasTimestamp = false;
if ( useTimestamp && destinationFile.exists() )
{
timestamp = destinationFile.lastModified();
hasTimestamp = true;
}
//set up the URL connection
URLConnection connection = source.openConnection();
//modify the headers
//NB: things like user authentication could go in here too.
if ( useTimestamp && hasTimestamp )
{
connection.setIfModifiedSince( timestamp );
}
//connect to the remote site (may take some time)
connection.connect();
//next test for a 304 result (HTTP only)
if ( connection instanceof HttpURLConnection )
{
HttpURLConnection httpConnection = (HttpURLConnection) connection;
if ( httpConnection.getResponseCode() == HttpURLConnection.HTTP_NOT_MODIFIED )
{
return false;
}
// test for 401 result (HTTP only)
if ( 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 is = null;
for ( int i = 0; i < 3; i++ )
{
try
{
is = connection.getInputStream();
break;
}
catch ( IOException ex )
{
// do nothing
}
}
if ( is == null )
{
final String error =
"Connection returned a null input stream: " + url;
throw new IOException( error );
}
File parent = destinationFile.getParentFile();
parent.mkdirs();
FileOutputStream fos = new FileOutputStream( destinationFile );
byte[] buffer = new byte[100 * 1024];
int length;
while ( ( length = is.read( buffer ) ) >= 0 )
{
fos.write( buffer, 0, length );
}
fos.close();
is.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 ( useTimestamp )
{
long remoteTimestamp = connection.getLastModified();
if ( remoteTimestamp != 0 )
{
touchFile( destinationFile, remoteTimestamp );
}
}
return true;
}
/**
* Parse an url which might contain a username and password. If the
* given url doesn't contain a username and password then return the
* origin url unchanged.
*
* @param url The url to parse.
* @return The username, password and url.
*/
static String[] parseUrl( String url )
{
String[] parsedUrl = new String[3];
parsedUrl[0] = null;
parsedUrl[1] = null;
parsedUrl[2] = url;
// 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 i = url.indexOf( "@" );
if ( i > 0 )
{
String s = url.substring( 7, i );
int j = s.indexOf( ":" );
parsedUrl[0] = s.substring( 0, j );
parsedUrl[1] = s.substring( j + 1 );
parsedUrl[2] = "http://" + url.substring( i + 1 );
}
return parsedUrl;
}
/**
* set the timestamp of a named file to a specified time.
*
* @param file the file to touch
* @param timemillis in milliseconds since the start of the era
* @return true if it succeeded. False means that this is a java1.1 system
* and that file times can not be set
* @exception Exception Thrown in unrecoverable error. Likely this
* comes from file access failures.
*/
private static boolean touchFile( File file, long timemillis )
throws Exception
{
long modifiedTime;
if ( timemillis < 0 )
{
modifiedTime = System.currentTimeMillis();
}
else
{
modifiedTime = timemillis;
}
file.setLastModified( modifiedTime );
return true;
}
}
1.7 +91 -0 avalon-sandbox/merlin/assembly/src/test/org/apache/avalon/assembly/TestBase.java
Index: TestBase.java
===================================================================
RCS file: /home/cvs/avalon-sandbox/merlin/assembly/src/test/org/apache/avalon/assembly/TestBase.java,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -r1.6 -r1.7
--- TestBase.java 26 Apr 2003 12:37:47 -0000 1.6
+++ TestBase.java 27 Jun 2003 22:52:12 -0000 1.7
@@ -52,13 +52,24 @@
import java.io.File;
+import org.apache.avalon.assembly.engine.impl.EngineClassLoader;
+import org.apache.avalon.assembly.locator.impl.DefaultLocator;
import org.apache.avalon.assembly.locator.impl.DefaultLocator;
import org.apache.avalon.assembly.logging.impl.DefaultLoggingManager;
import org.apache.avalon.assembly.logging.LoggingDescriptor;
import org.apache.avalon.assembly.logging.LoggingManager;
import org.apache.avalon.assembly.logging.TargetDescriptor;
+import org.apache.avalon.assembly.repository.Repository;
+import org.apache.avalon.assembly.repository.impl.FileRepository;
+import org.apache.avalon.assembly.util.ExceptionHelper;
import org.apache.avalon.framework.logger.Logger;
+import org.apache.avalon.framework.context.DefaultContext;
+import org.apache.avalon.framework.parameters.Parameters;
import org.apache.avalon.meta.model.Category;
+import org.apache.excalibur.event.command.CommandManager;
+import org.apache.excalibur.event.command.TPCThreadManager;
+import org.apache.excalibur.mpool.DefaultPoolManager;
+import org.apache.excalibur.mpool.PoolManager;
import junit.framework.TestCase;
@@ -107,4 +118,84 @@
{
return m_logManager.getLoggerForCategory( "test" );
}
+
+ protected EngineClassLoader setUpEngine( boolean bootstrap ) throws Exception
+ {
+ try
+ {
+ EngineClassLoader engine = new EngineClassLoader();
+ engine.enableLogging( getLogger() );
+ DefaultLocator context = new DefaultLocator();
+ context.put( LoggingManager.KEY, m_logManager );
+ context.put( "urn:assembly:engine.bootstrap", "" + bootstrap );
+ context.put( Repository.KEY, getRepository() );
+ context.put( PoolManager.ROLE, getPoolManager() );
+ context.makeReadOnly();
+ engine.contextualize( context );
+ engine.initialize();
+ return engine;
+ }
+ catch( Exception e )
+ {
+ ExceptionHelper.printException( "Engine setup failure.", e, this, true );
+ assertTrue( false );
+ throw e;
+ }
+ }
+
+ private Repository getRepository() throws Exception
+ {
+ File base = new File( System.getProperty("basedir") );
+ File target = new File( base, "target" );
+ File root = new File( target, "test-repository" );
+ root.mkdirs();
+ FileRepository repository = new FileRepository();
+ repository.enableLogging( getLogger().getChildLogger( "repository" ) );
+ DefaultContext context = new DefaultContext();
+ context.put( FileRepository.BASE_KEY, root );
+ context.makeReadOnly();
+ repository.contextualize( context );
+ return repository;
+ }
+
+ private PoolManager getPoolManager() throws Exception
+ {
+ try
+ {
+ //
+ // Set up the ThreadManager that the CommandManager uses
+ //
+
+ TPCThreadManager threadManager = new TPCThreadManager();
+ threadManager.enableLogging( getLogger().getChildLogger( "threads" ) );
+ Parameters params = new Parameters();
+ params.setParameter( "threads-per-processor", "2" );
+ params.setParameter( "sleep-time", "1000" );
+ params.setParameter( "block-timeout", "250" );
+ threadManager.parameterize( params );
+ threadManager.initialize();
+
+ //
+ // Set up the CommandManager that the PoolManager uses.
+ //
+
+ CommandManager commandManager = new CommandManager();
+ threadManager.register( commandManager );
+
+ //
+ // Set up the PoolManager that the pooled lifecycle helper needs
+ //
+
+ DefaultPoolManager poolManager =
+ new DefaultPoolManager( commandManager.getCommandSink() );
+ return poolManager;
+ }
+ catch( Throwable e )
+ {
+ final String error =
+ "Internal error during establishment of the default pool manager. Cause: ";
+ throw new Exception( error + e.toString() );
+ }
+ }
+
}
1.10 +6 -62 avalon-sandbox/merlin/assembly/src/test/org/apache/avalon/assembly/engine/ApplianceTestCase.java
Index: ApplianceTestCase.java
===================================================================
RCS file: /home/cvs/avalon-sandbox/merlin/assembly/src/test/org/apache/avalon/assembly/engine/ApplianceTestCase.java,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -r1.9 -r1.10
--- ApplianceTestCase.java 30 Apr 2003 02:03:41 -0000 1.9
+++ ApplianceTestCase.java 27 Jun 2003 22:52:12 -0000 1.10
@@ -50,6 +50,8 @@
package org.apache.avalon.assembly.engine;
+import java.io.File;
+
import org.apache.avalon.assembly.TestBase;
import org.apache.avalon.assembly.appliance.Appliance;
import org.apache.avalon.assembly.appliance.DependencyGraph;
@@ -57,8 +59,12 @@
import org.apache.avalon.assembly.engine.impl.DefaultTypeRepository;
import org.apache.avalon.assembly.locator.impl.DefaultLocator;
import org.apache.avalon.assembly.logging.LoggingManager;
+import org.apache.avalon.assembly.repository.Repository;
+import org.apache.avalon.assembly.repository.impl.FileRepository;
import org.apache.avalon.assembly.util.ExceptionHelper;
import org.apache.avalon.framework.parameters.Parameters;
+import org.apache.avalon.framework.context.DefaultContext;
+import org.apache.avalon.framework.logger.ConsoleLogger;
import org.apache.avalon.meta.info.StageDescriptor;
import org.apache.excalibur.event.command.CommandManager;
import org.apache.excalibur.event.command.TPCThreadManager;
@@ -122,68 +128,6 @@
String error = ExceptionHelper.packException( reason, e );
getLogger().error( error );
assertTrue( false );
- }
- }
-
- protected EngineClassLoader setUpEngine( boolean bootstrap ) throws Exception
- {
- try
- {
- EngineClassLoader engine = new EngineClassLoader();
- engine.enableLogging( getLogger() );
- DefaultLocator context = new DefaultLocator();
- context.put( "urn:assembly:engine.bootstrap", "" + bootstrap );
- context.put( LoggingManager.KEY, m_logManager );
- context.put( PoolManager.ROLE, getPoolManager() );
- context.makeReadOnly();
- engine.contextualize( context );
- engine.initialize();
- return engine;
- } catch( Exception e )
- {
- final String reason = "Engine setup failure.";
- String error = ExceptionHelper.packException( reason, e );
- getLogger().error( error );
- throw new Exception( reason );
- }
- }
-
- private PoolManager getPoolManager() throws Exception
- {
- try
- {
- //
- // Set up the ThreadManager that the CommandManager uses
- //
-
- TPCThreadManager threadManager = new TPCThreadManager();
- threadManager.enableLogging( getLogger().getChildLogger( "threads" ) );
- Parameters params = new Parameters();
- params.setParameter( "threads-per-processor", "2" );
- params.setParameter( "sleep-time", "1000" );
- params.setParameter( "block-timeout", "250" );
- threadManager.parameterize( params );
- threadManager.initialize();
-
- //
- // Set up the CommandManager that the PoolManager uses.
- //
-
- CommandManager commandManager = new CommandManager();
- threadManager.register( commandManager );
-
- //
- // Set up the PoolManager that the pooled lifecycle helper needs
- //
-
- DefaultPoolManager poolManager =
- new DefaultPoolManager( commandManager.getCommandSink() );
- return poolManager;
- } catch( Throwable e )
- {
- final String error =
- "Internal error during establishment of the default pool manager. Cause: ";
- throw new Exception( error + e.toString() );
}
}
}
1.11 +0 -62 avalon-sandbox/merlin/assembly/src/test/org/apache/avalon/assembly/engine/EngineTestCase.java
Index: EngineTestCase.java
===================================================================
RCS file: /home/cvs/avalon-sandbox/merlin/assembly/src/test/org/apache/avalon/assembly/engine/EngineTestCase.java,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -r1.10 -r1.11
--- EngineTestCase.java 18 Jun 2003 11:16:15 -0000 1.10
+++ EngineTestCase.java 27 Jun 2003 22:52:12 -0000 1.11
@@ -265,66 +265,4 @@
}
}
- protected EngineClassLoader setUpEngine( boolean bootstrap ) throws Exception
- {
- try
- {
- EngineClassLoader engine = new EngineClassLoader();
- engine.enableLogging( getLogger() );
- DefaultLocator context = new DefaultLocator();
- context.put( LoggingManager.KEY, m_logManager );
- context.put( "urn:assembly:engine.bootstrap", "" + bootstrap );
- context.put( PoolManager.ROLE, getPoolManager() );
- context.makeReadOnly();
- engine.contextualize( context );
- engine.initialize();
- return engine;
- }
- catch( Exception e )
- {
- ExceptionHelper.printException( "Engine setup failure.", e, this, true );
- assertTrue( false );
- throw e;
- }
- }
-
- private PoolManager getPoolManager() throws Exception
- {
- try
- {
- //
- // Set up the ThreadManager that the CommandManager uses
- //
-
- TPCThreadManager threadManager = new TPCThreadManager();
- threadManager.enableLogging( getLogger().getChildLogger( "threads" ) );
- Parameters params = new Parameters();
- params.setParameter( "threads-per-processor", "2" );
- params.setParameter( "sleep-time", "1000" );
- params.setParameter( "block-timeout", "250" );
- threadManager.parameterize( params );
- threadManager.initialize();
-
- //
- // Set up the CommandManager that the PoolManager uses.
- //
-
- CommandManager commandManager = new CommandManager();
- threadManager.register( commandManager );
-
- //
- // Set up the PoolManager that the pooled lifecycle helper needs
- //
-
- DefaultPoolManager poolManager =
- new DefaultPoolManager( commandManager.getCommandSink() );
- return poolManager;
- }
- catch( Throwable e )
- {
- final String error =
- "Internal error during establishment of the default pool manager. Cause: ";
- throw new Exception( error + e.toString() );
- }
- }
}
1.11 +0 -63 avalon-sandbox/merlin/assembly/src/test/org/apache/avalon/assembly/engine/FactoryTestCase.java
Index: FactoryTestCase.java
===================================================================
RCS file: /home/cvs/avalon-sandbox/merlin/assembly/src/test/org/apache/avalon/assembly/engine/FactoryTestCase.java,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -r1.10 -r1.11
--- FactoryTestCase.java 18 Jun 2003 11:16:15 -0000 1.10
+++ FactoryTestCase.java 27 Jun 2003 22:52:12 -0000 1.11
@@ -136,67 +136,4 @@
assertTrue( false );
}
}
-
- protected EngineClassLoader setUpEngine( boolean bootstrap ) throws Exception
- {
- try
- {
- EngineClassLoader engine = new EngineClassLoader();
- engine.enableLogging( getLogger() );
- DefaultLocator context = new DefaultLocator();
- context.put( LoggingManager.KEY, m_logManager );
- context.put( "urn:assembly:engine.bootstrap", "" + bootstrap );
- context.put( PoolManager.ROLE, getPoolManager() );
- context.makeReadOnly();
- engine.contextualize( context );
- engine.initialize();
- return engine;
- }
- catch( Exception e )
- {
- ExceptionHelper.printException( "Engine setup failure.", e, this, true );
- assertTrue( false );
- throw e;
- }
- }
-
- private PoolManager getPoolManager() throws Exception
- {
- try
- {
- //
- // Set up the ThreadManager that the CommandManager uses
- //
-
- TPCThreadManager threadManager = new TPCThreadManager();
- threadManager.enableLogging( getLogger().getChildLogger( "threads" ) );
- Parameters params = new Parameters();
- params.setParameter( "threads-per-processor", "2" );
- params.setParameter( "sleep-time", "1000" );
- params.setParameter( "block-timeout", "250" );
- threadManager.parameterize( params );
- threadManager.initialize();
-
- //
- // Set up the CommandManager that the PoolManager uses.
- //
-
- CommandManager commandManager = new CommandManager();
- threadManager.register( commandManager );
-
- //
- // Set up the PoolManager that the pooled lifecycle helper needs
- //
-
- DefaultPoolManager poolManager =
- new DefaultPoolManager( commandManager.getCommandSink() );
- return poolManager;
- }
- catch( Throwable e )
- {
- final String error =
- "Internal error during establishment of the default pool manager. Cause: ";
- throw new Exception( error + e.toString() );
- }
- }
}
1.3 +8 -8 avalon-sandbox/merlin/assembly/src/test/org/apache/avalon/assembly/repository/LocalRepositoryTestCase.java
Index: LocalRepositoryTestCase.java
===================================================================
RCS file: /home/cvs/avalon-sandbox/merlin/assembly/src/test/org/apache/avalon/assembly/repository/LocalRepositoryTestCase.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- LocalRepositoryTestCase.java 8 May 2003 22:28:29 -0000 1.2
+++ LocalRepositoryTestCase.java 27 Jun 2003 22:52:12 -0000 1.3
@@ -54,6 +54,8 @@
import org.apache.avalon.assembly.repository.Repository;
import org.apache.avalon.assembly.repository.impl.FileRepository;
+import org.apache.avalon.framework.logger.ConsoleLogger;
+import org.apache.avalon.framework.context.DefaultContext;
import junit.framework.TestCase;
@@ -82,18 +84,16 @@
File target = new File( base, "target" );
File repository = new File( target, "test-repository" );
repository.mkdirs();
- m_repository = (FileRepository) FileRepository.newInstance( repository );
+ m_repository = new FileRepository();
+ DefaultContext context = new DefaultContext();
+ context.put( FileRepository.BASE_KEY, repository );
+ context.makeReadOnly();
+ m_repository.enableLogging( new ConsoleLogger() );
+ m_repository.contextualize( context );
}
public void testInstall() throws Exception
{
- //
- // this testcase is incomplete - objective is to be able to
- // a packaged appication and populate the Merlin repository
- // - a little more needs to be done to handle the unpacking side
- // of things - until this text is disabled.
- //
- //m_repository.install( "james" );
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: cvs-unsubscribe@avalon.apache.org
For additional commands, e-mail: cvs-help@avalon.apache.org