You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@avalon.apache.org by do...@apache.org on 2001/11/17 01:57:01 UTC
cvs commit: jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/extension DefaultPackageRepository.java
donaldp 01/11/16 16:57:01
Modified: src/scratchpad/org/apache/avalon/excalibur/extension
DefaultPackageRepository.java
Log:
Reimplement the Repository so that it is read-only. It scans the directories on path once and forever more assumes that all the packages detected are the only packages present and thius will never change.
Revision Changes Path
1.4 +71 -124 jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/extension/DefaultPackageRepository.java
Index: DefaultPackageRepository.java
===================================================================
RCS file: /home/cvs/jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/extension/DefaultPackageRepository.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- DefaultPackageRepository.java 2001/11/13 00:22:38 1.3
+++ DefaultPackageRepository.java 2001/11/17 00:57:01 1.4
@@ -15,64 +15,56 @@
import java.util.jar.Manifest;
import org.apache.avalon.excalibur.io.FileUtil;
import org.apache.avalon.excalibur.util.DeweyDecimal;
+import org.apache.avalon.framework.logger.AbstractLogEnabled;
/**
* <p>Interface used to contain "Optional Packages" (formerly known as
* "Standard Extensions"). It is assumed that each "Optional Package" is
- * represented by a single file on the file system. This Repository manages
- * a single sub-tree on file system.</p>
+ * represented by a single file on the file system. This Repository searches
+ * a path to find the Optional Packages.</p>
*
- * <p>Each package directory contains a data file named
- * <code>packages.cache</code>. This file caches relevent meta-information
- * about "Optional Packages" and maps that to a specific file.</p>
- *
- * <p>The filename for each "Optional Package" is derived from a simple
- * algorithm. The first <code>Extension</code> listed in "Optional Packages" manifest
- * is used to name package. The <code>Implementation-Vendor-ID</code> and
- * <code>Extension-Name</code> are processed so that each string only contains
- * valid characters. The set of valid characters include all ascii
- * alphanumeric characters and '.'. Invalid characters are replaced with the
- * '_' character.</p>
- *
- * <p>The filename is then constructed using these components and the
- * <code>Implementation-Version</code> using the following pattern.</p>
- *
- * <pre>
- * <code><Implementation-Vendor-ID>/<Extension-Name>-<Implementation-Version>.jar</code>
- * </pre>
- *
* @author <a href="mailto:donaldp@apache.org">Peter Donald</a>
- * @version $Revision: 1.3 $ $Date: 2001/11/13 00:22:38 $
+ * @version $Revision: 1.4 $ $Date: 2001/11/17 00:57:01 $
* @see OptionalPackage
* @see PackageRepository
*/
public class DefaultPackageRepository
+ extends AbstractLogEnabled
implements PackageRepository
{
///Map between files and <code>OptionalPackage</code> objects.
private final HashMap m_packages = new HashMap();
- ///Base directory of repository
- private final File m_baseDirectory;
+ ///The set of directories in which to look for Optional Packages
+ private final File[] m_path;
+ private boolean m_needToScan;
+
/**
- * Construct a package repository with specified base directory.
+ * Construct a package repository with path.
*
- * @param baseDirectory the base directory for repository
+ * @param path The set of directories in which to look for Optional Packages
*/
- public DefaultPackageRepository( final File baseDirectory )
+ public DefaultPackageRepository( final File[] path )
{
- m_baseDirectory = baseDirectory;
-
- if( null == m_baseDirectory )
+ if( null == path )
{
- throw new NullPointerException( "baseDirectory property is null" );
+ throw new NullPointerException( "path property is null" );
}
- if( !m_baseDirectory.isDirectory() )
+ for( int i = 0; i < path.length; i++ )
{
- throw new IllegalArgumentException( "baseDirectory must be a directory" );
+ final File directory = path[ i ];
+
+ if( !directory.exists() || !directory.isDirectory() )
+ {
+ throw new IllegalArgumentException( "path element " + directory +
+ " must exist and must be a directory" );
+ }
}
+
+ m_path = path;
+ m_needToScan = true;
}
/**
@@ -113,9 +105,13 @@
*/
public synchronized OptionalPackage[] getOptionalPackages( final Extension extension )
{
- final ArrayList results = new ArrayList();
+ if( m_needToScan )
+ {
+ //Check cache consistency and reload if necessary..
+ scanPath();
+ }
- //TODO: Check cache consistency and reload if necessary..
+ final ArrayList results = new ArrayList();
final ArrayList candidates = (ArrayList)m_packages.get( extension.getExtensionName() );
if( null != candidates )
{
@@ -141,47 +137,50 @@
return (OptionalPackage[])results.toArray( new OptionalPackage[ 0 ] );
}
- /**
- * Install the specified <code>Extension</code> (which represents
- * an optional package required by another package). The mechanism
- * and location of installation is not specified.
- *
- * <p>It is an error for an extension with the same
- * <code>Implementation-Version</code>, <code>Implementation-Vendor-ID</code>
- * and <code>Extension-Name</code> to already exist on local system.</p>
- *
- * <p>It is expected that after successful completion of this
- * method that an OptionalPackage will be accessible via getOptionalPackage().</p>
- *
- * @param file the file containing "Optional Package".
- * @exception IOException if an error occurs
- */
- public void install( final File inputFile )
- throws IOException, SecurityException
+ private synchronized void scanPath()
{
- final OptionalPackage preInstallPackage = getOptionalPackage( inputFile );
- final Extension[] extensions = preInstallPackage.getAvailableExtensions();
-
- if( 0 == extensions.length )
+ for( int i = 0; i < m_path.length; i++ )
{
- throw new IOException( "File contains no extensions and thus " +
- "not a valid Optional Package" );
+ scanDirectory( m_path[ i ] );
}
+ }
- final String filename = getFilename( extensions[ 0 ] );
+ private synchronized void scanDirectory( final File directory )
+ {
+ final File[] files = directory.listFiles();
+ for( int i = 0; i < files.length; i++ )
+ {
+ final File file = files[ i ];
+ final String name = file.getName();
- //Create file beneath writeable repository here
- final File file = (new File( m_baseDirectory, filename )).getCanonicalFile();
- FileUtil.copyFile( inputFile, file );
+ if( !name.endsWith( ".jar" ) )
+ {
+ debug( "Skipping " + file + " as it does not end with '.jar'" );
+ continue;
+ }
- final OptionalPackage optionalPackage =
- new OptionalPackage( file,
- preInstallPackage.getAvailableExtensions(),
- preInstallPackage.getRequiredExtensions() );
+ if( !file.isFile() )
+ {
+ debug( "Skipping " + file + " as it is not a file." );
+ continue;
+ }
- cacheOptionalPackage( optionalPackage );
+ if( !file.canRead() )
+ {
+ debug( "Skipping " + file + " as it is not readable." );
+ continue;
+ }
- //Update cache on filesystem here...
+ try
+ {
+ final OptionalPackage optionalPackage = getOptionalPackage( file );
+ addOptionalPackage( optionalPackage );
+ }
+ catch( final IOException ioe )
+ {
+ debug( "Skipping " + file + " as it could not be loaded due to " + ioe );
+ }
+ }
}
/**
@@ -191,7 +190,7 @@
*
* @param optionalPackage the OptionalPackage to be added to repository
*/
- protected final void cacheOptionalPackage( final OptionalPackage optionalPackage )
+ protected final void addOptionalPackage( final OptionalPackage optionalPackage )
{
final Extension extension = optionalPackage.getAvailableExtensions()[ 0 ];
ArrayList candidates = (ArrayList)m_packages.get( extension.getExtensionName() );
@@ -235,61 +234,9 @@
}
}
- /**
- * Generate the filename for specified <code>Extension</code>.
- * This extension must specify <code>Implementation-Vendor-Id</code>
- * and <code>Implementation-Version</code>.
- *
- * @param extension the extension
- * @return the generated filename
- */
- private String getFilename( final Extension extension )
+ protected void debug( final String message )
{
- if( null == extension.getImplementationVendorId() )
- {
- throw new NullPointerException( "ImplementationVendorId is null" );
- }
-
- if( null == extension.getImplementationVersion() )
- {
- throw new NullPointerException( "ImplementationVersion is null" );
- }
-
- final String name = getCleanName( extension.getExtensionName() );
- final String implVendorId = getCleanName( extension.getImplementationVendorId() );
- final DeweyDecimal implVersion = extension.getImplementationVersion();
-
- return new StringBuffer(implVendorId).append(File.separator)
- .append(name).append("-").append(implVersion).append(".jar").toString();
- }
-
- /**
- * Clean name so that invalid characters are replaced with '_'.
- * The set of valid characters consists of ascii alphanumeric
- * characters and '.'.
- *
- * @param name the input name
- * @return the cleaned name
- */
- private String getCleanName( final String name )
- {
- final int size = name.length();
- final StringBuffer sb = new StringBuffer( size );
-
- for( int i = 0; i < size; i++ )
- {
- final char ch = name.charAt( i );
-
- if( Character.isLetterOrDigit( ch ) || '.' == ch )
- {
- sb.append( ch );
- }
- else
- {
- sb.append( '_' );
- }
- }
-
- return sb.toString();
+ System.out.println( message );
+ //getLogger().debug( message );
}
}
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>