mcconnell 2003/09/14 06:07:15
Modified: merlin/repository/impl/src/java/org/apache/avalon/repository/impl
DefaultFileRepository.java
merlin/repository/spi/src/java/org/apache/avalon/repository
Repository.java
Log:
Improve interface and impl documentation and add block installation so that we can
install into the users local repository (independently of the system repository).
Revision Changes Path
1.3 +133 -3
avalon-sandbox/merlin/repository/impl/src/java/org/apache/avalon/repository/impl/DefaultFileRepository.java
Index: DefaultFileRepository.java
===================================================================
RCS file:
/home/cvs/avalon-sandbox/merlin/repository/impl/src/java/org/apache/avalon/repository/impl/DefaultFileRepository.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- DefaultFileRepository.java 17 Aug 2003 00:51:57 -0000 1.2
+++ DefaultFileRepository.java 14 Sep 2003 13:07:15 -0000 1.3
@@ -56,15 +56,22 @@
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
+import java.io.IOException;
import java.net.URL;
+import java.net.Authenticator;
+import java.net.JarURLConnection;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
-import java.net.Authenticator;
+import java.util.jar.JarFile;
+import java.util.zip.ZipEntry;
+import java.util.Iterator;
+import java.util.Enumeration;
import org.apache.avalon.repository.Repository;
import org.apache.avalon.repository.RepositoryException;
import org.apache.avalon.repository.ProxyContext;
+import org.apache.avalon.repository.BlockManifest;
/**
* A component that provides access to versioned resources based on
@@ -129,7 +136,8 @@
catch( Throwable e )
{
final String error =
- "Could not coerce supplied URL to a directory reference:
" + path;
+ "Could not coerce supplied URL to a directory reference:
"
+ + path;
throw new IllegalArgumentException( error );
}
}
@@ -157,6 +165,60 @@
//------------------------------------------------------------------
/**
+ * Return the repository location.
+ *
+ * @return the location
+ */
+ public String getLocation()
+ {
+ return m_base.toString();
+ }
+
+ /**
+ * Install a block archive into the repository.
+ * @param url the block archive url
+ * @param buffer a string buffer against which install messages may be logged
+ * @return the block manifest
+ */
+ public BlockManifest install( URL url, StringBuffer buffer ) throws
RepositoryException
+ {
+ buffer.append( "install target: " + url );
+
+ 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( "\n block group: " + group );
+ final File root = new File( m_base, group );
+
+ buffer.append( "\nInstall 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 sucessful." );
+ return manifest;
+ }
+ catch( Throwable e )
+ {
+ final String error =
+ "Could not install block: " + url;
+ throw new RepositoryException( error, e );
+ }
+ }
+
+ /**
* Get a resource relative to the supplied artifact name where the artifact name
* is equivalent to the group and resource name seperated by a colon.
*
@@ -344,5 +406,73 @@
private String getResourceName( String artifact, int index )
{
return artifact.substring( index + 1 );
+ }
+
+ /**
+ * 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 );
+ }
}
}
1.2 +15 -1
avalon-sandbox/merlin/repository/spi/src/java/org/apache/avalon/repository/Repository.java
Index: Repository.java
===================================================================
RCS file:
/home/cvs/avalon-sandbox/merlin/repository/spi/src/java/org/apache/avalon/repository/Repository.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- Repository.java 11 Aug 2003 22:37:16 -0000 1.1
+++ Repository.java 14 Sep 2003 13:07:15 -0000 1.2
@@ -71,6 +71,13 @@
static final String SEPERATOR = ":";
/**
+ * Install a block archive into the repository.
+ * @param url the block archive url
+ * @return the block manifest
+ */
+ BlockManifest install( URL url, StringBuffer buffer ) throws
RepositoryException;
+
+ /**
* Get a resource relative to the supplied artifact name where the artifact name
* is equivalent to the group and resource name seperated by the colon character.
*
@@ -96,5 +103,12 @@
URL getArtifact(
final String group, final String name, final String version, final String
type )
throws RepositoryException;
+
+ /**
+ * Return the repository location.
+ *
+ * @return the location
+ */
+ String getLocation();
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]