akarasulu 2003/11/11 22:59:16
Modified: repository/impl/src/java/org/apache/avalon/repository/impl
DefaultFileRepository.java
DefaultRepositoryFactory.java
Removed: repository/impl/src/java/org/apache/avalon/repository/impl
DefaultAuthenticator.java
Log:
Readded some code I removed before and
moved some default utility classes into API.
Revision Changes Path
1.8 +213 -65
avalon-sandbox/repository/impl/src/java/org/apache/avalon/repository/impl/DefaultFileRepository.java
Index: DefaultFileRepository.java
===================================================================
RCS file:
/home/cvs/avalon-sandbox/repository/impl/src/java/org/apache/avalon/repository/impl/DefaultFileRepository.java,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -r1.7 -r1.8
--- DefaultFileRepository.java 12 Nov 2003 05:59:22 -0000 1.7
+++ DefaultFileRepository.java 12 Nov 2003 06:59:16 -0000 1.8
@@ -52,14 +52,22 @@
import java.io.File ;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
import java.util.ArrayList ;
+import java.util.Enumeration;
+import java.util.jar.JarFile;
+import java.util.zip.ZipEntry;
+import java.net.JarURLConnection;
import java.net.URL ;
import java.net.Authenticator ;
import java.net.URLClassLoader ;
import javax.naming.directory.Attributes ;
+import org.apache.avalon.repository.BlockManifest;
import org.apache.avalon.repository.Repository ;
import org.apache.avalon.repository.ProxyContext ;
import org.apache.avalon.repository.JarDescriptor ;
@@ -403,72 +411,212 @@
}
- /**
- * 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() )
- {
- final String error =
- "Unknown group: '" + group
- + "' relative to the repository base: '" + m_base + "'.";
- throw new RepositoryException( error );
- }
-
- File types = new File( application, type + "s" );
- if( !types.exists() )
- {
- final String error =
- "No resources of the type: '" + type
- + "' in the group: '" + group
- + "'. File: '" + types + "'.";
- throw new RepositoryException( error );
- }
+ /**
+ * Install a block archive into the repository.
+ * @param url the block archive url
+ * @param buffer a string buffer against which messages may be logged
+ * @return the block manifest
+ */
+ public BlockManifest install( URL url, StringBuffer buffer ) throws
RepositoryException
+ {
+ String path = url.getFile();
- //
- // we only append the version identifier if the version
- // value is not null and more than 0 characters
- //
+ try
+ {
+ File temp = File.createTempFile( "avalon-", "-bar" );
+ temp.delete();
+ HttpController.getFile( url.toString(), temp, true );
+ temp.deleteOnExit();
+ return expand( temp.toURL(), buffer );
+ }
+ catch( RepositoryException e )
+ {
+ throw e;
+ }
+ catch( Throwable e )
+ {
+ final String error =
+ "Cannot install target: " + url;
+ throw new RepositoryException( error, e );
+ }
+ }
- String resourceName = name;
- if( ( version != null ) && ( version.length() != 0 ) )
- {
- resourceName = resourceName + "-" + version;
- }
- File resource = new File( types, resourceName + "." + type );
+ /**
+ * Expand a block archive into the repository.
+ * @param url the block archive url
+ * @param buffer a string buffer against which messages may be logged
+ * @return the block manifest
+ */
+ private BlockManifest expand( URL url, StringBuffer buffer ) throws
RepositoryException
+ {
+ try
+ {
+ URL jurl = new URL( "jar:" + url.toString() + "!/" );
+ JarURLConnection connection = (JarURLConnection) jurl.openConnection();
+ BlockManifest manifest =
+ new DefaultBlockManifest( connection.getManifest() );
+ final String group = manifest.getBlockGroup();
+
+ buffer.append( "\nBlock Group: " + group );
+ final File root = new File( m_base, group );
+ buffer.append( "\nLocal target: " + root );
+ JarFile jar = connection.getJarFile();
+ Enumeration entries = jar.entries();
+ while( entries.hasMoreElements() )
+ {
+ ZipEntry entry = (ZipEntry) entries.nextElement();
+ if( !entry.getName().startsWith( "META-INF" ) )
+ {
+ installEntry( buffer, root, jar, entry );
+ }
+ }
+ buffer.append( "\nInstall successful." );
+ return manifest;
+ }
+ catch( Throwable e )
+ {
+ final String error =
+ "Could not install block: " + url;
+ throw new RepositoryException( error, e );
+ }
+ }
- if( !resource.exists() )
- {
- final String error =
- "No resources of the type: '" + type
- + "' in the group: '" + group
- + "' with the name: '" + name
- + "' and version: '" + version
- + ". File: '" + resource + "'.";
- throw new RepositoryException( error );
- }
- try
- {
- return resource.toURL();
- }
- catch( Throwable e )
- {
- final String error =
- "Unable to resolve URL for resource: " + resource;
- throw new RepositoryException( error );
- }
- }
+ /**
+ * Internal utility to install a entry from a jar file into the local
repository.
+ * @param buffer the buffer to log messages to
+ * @param root the root directory corresponding to the bar group
+ * @param jar the block archive
+ * @param entry the entry from the archive to install
+ */
+ private void installEntry(
+ StringBuffer buffer, File root, JarFile jar, ZipEntry entry ) throws
Exception
+ {
+ if( entry.isDirectory() ) return;
+
+ final String name = entry.getName();
+ File file = new File( root, name );
+
+ long timestamp = entry.getTime();
+ if( file.exists() )
+ {
+ if( file.lastModified() == timestamp )
+ {
+ buffer.append( "\nEntry: " + name + " (already exists)" );
+ return;
+ }
+ else if( file.lastModified() > timestamp )
+ {
+ buffer.append( "\nEntry: " + name + " (local version is more
recent)" );
+ return;
+ }
+ else
+ {
+ buffer.append( "\nEntry: " + name + " (updating local version)" );
+ }
+ }
+ else
+ {
+ buffer.append( "\nEntry: " + name );
+ }
+
+ InputStream is = jar.getInputStream( entry );
+ if ( is == null )
+ {
+ final String error =
+ "Entry returned a null input stream: " + name;
+ buffer.append( "\n " + error );
+ throw new IOException( error );
+ }
+
+ file.getParentFile().mkdirs();
+ FileOutputStream fos = new FileOutputStream( file );
+ byte[] buf = new byte[100 * 1024];
+ int length;
+ while ( ( length = is.read( buf ) ) >= 0 )
+ {
+ fos.write( buf, 0, length );
+ }
+ fos.close();
+ is.close();
+
+ if ( timestamp < 0 )
+ {
+ file.setLastModified( System.currentTimeMillis() );
+ }
+ else
+ {
+ file.setLastModified( timestamp );
+ }
+ }
+
+
+ /**
+ * 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() )
+ {
+ final String error =
+ "Unknown group: '" + group
+ + "' relative to the repository base: '" + m_base + "'.";
+ throw new RepositoryException( error );
+ }
+
+ File types = new File( application, type + "s" );
+ if( !types.exists() )
+ {
+ final String error =
+ "No resources of the type: '" + type
+ + "' in the group: '" + group
+ + "'. File: '" + types + "'.";
+ throw new RepositoryException( error );
+ }
+
+ //
+ // we only append the version identifier if the version
+ // value is not null and more than 0 characters
+ //
+
+ String resourceName = name;
+ if( ( version != null ) && ( version.length() != 0 ) )
+ {
+ resourceName = resourceName + "-" + version;
+ }
+ File resource = new File( types, resourceName + "." + type );
+
+ if( !resource.exists() )
+ {
+ final String error =
+ "No resources of the type: '" + type
+ + "' in the group: '" + group
+ + "' with the name: '" + name
+ + "' and version: '" + version
+ + ". File: '" + resource + "'.";
+ throw new RepositoryException( error );
+ }
+
+ try
+ {
+ return resource.toURL();
+ }
+ catch( Throwable e )
+ {
+ final String error =
+ "Unable to resolve URL for resource: " + resource;
+ throw new RepositoryException( error );
+ }
+ }
}
1.6 +2 -1
avalon-sandbox/repository/impl/src/java/org/apache/avalon/repository/impl/DefaultRepositoryFactory.java
Index: DefaultRepositoryFactory.java
===================================================================
RCS file:
/home/cvs/avalon-sandbox/repository/impl/src/java/org/apache/avalon/repository/impl/DefaultRepositoryFactory.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- DefaultRepositoryFactory.java 12 Nov 2003 02:52:05 -0000 1.5
+++ DefaultRepositoryFactory.java 12 Nov 2003 06:59:16 -0000 1.6
@@ -68,6 +68,7 @@
import org.apache.avalon.repository.RepositoryContext ;
import org.apache.avalon.repository.RepositoryFactory ;
import org.apache.avalon.repository.RepositoryException ;
+import org.apache.avalon.repository.DefaultAuthenticator ;
/**
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]