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/04/24 14:10:44 UTC
cvs commit: avalon-sandbox/merlin/assembly/src/java/org/apache/avalon/assembly/repository/impl FileRepository.java package.html
mcconnell 2003/04/24 05:10:44
Added: merlin/assembly/src/java/org/apache/avalon/assembly/repository/impl
FileRepository.java package.html
Log:
Implementation of a repository based on an underlying file system.
Revision Changes Path
1.1 avalon-sandbox/merlin/assembly/src/java/org/apache/avalon/assembly/repository/impl/FileRepository.java
Index: FileRepository.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.io.File;
import java.net.URL;
import org.apache.avalon.assembly.repository.Repository;
import org.apache.avalon.assembly.repository.RepositoryException;
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;
/**
* A component that provides access to versioned resources based on
* an underlying file system.
*
* @author <a href="mailto:mcconnell@osm.net">Stephen McConnell</a>
* @version $Revision: 1.1 $ $Date: 2003/04/24 12:10:44 $
*/
public class FileRepository implements Repository, Contextualizable
{
//------------------------------------------------------------------
// static
//------------------------------------------------------------------
public static final String BASE_KEY = "urn:avalon:repository.base";
public static Repository newInstance( File base ) throws ContextException
{
FileRepository repository = new FileRepository();
DefaultContext context = new DefaultContext();
context.put( BASE_KEY, base );
repository.contextualize( context );
return repository;
}
//------------------------------------------------------------------
// state
//------------------------------------------------------------------
private File m_base;
//------------------------------------------------------------------
// lifecycle
//------------------------------------------------------------------
/**
* Contextualization of the component during which the
* repository base directory is supplied.
*
* @param context the supplied runtime context
* @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
{
Object object = context.get( BASE_KEY );
if( object instanceof File )
{
m_base = (File) object;
}
}
//------------------------------------------------------------------
// implementation
//------------------------------------------------------------------
/**
* 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.
*
* @param artifact the artifact name
* @param version the version identifier
* @param type the resource type
* @return the resource
*/
public URL getArtifact(
final String artifact, final String version, final String type )
throws RepositoryException
{
int n = verify( artifact );
final String group = getGroupName( artifact, n );
final String name = getResourceName( artifact, n );
return getArtifact( group, name, version, type );
}
/**
* Get a resource relative to the supplied application name,
* resource name, version and resource type.
*
* @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
*/
public URL getArtifact(
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
+ ". File: '" + application + "'.";
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 );
}
File resource = new File( types, name + "-" + version + "." + 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
{
if( type.equals( "jar" ) )
{
final URL url = resource.toURL();
return new URL( "jar:" + url.toString() + "!/" );
}
else
{
return resource.toURL();
}
}
catch( Throwable e )
{
final String error =
"Unable to resolve URL for resource: " + resource;
throw new RepositoryException( error );
}
}
private int verify( String artifact ) throws RepositoryException
{
int n = artifact.indexOf( SEPERATOR );
if( n < 1 )
{
final String error =
"Invalid artifact name: " + artifact;
throw new RepositoryException( error );
}
return n;
}
private String getGroupName( String artifact, int index )
{
return artifact.substring( 0, index );
}
private String getResourceName( String artifact, int index )
{
return artifact.substring( index + 1 );
}
}
1.1 avalon-sandbox/merlin/assembly/src/java/org/apache/avalon/assembly/repository/impl/package.html
Index: package.html
===================================================================
<body>
<p>
The <code>repository.impl</code> package contains implementations
of the {@link org.apache.avalon.assembly.repository.Repository} interface which
defines the contract for a abstract resource repository where resources
references can be established based on group, name, type and version
attributes.
</p>
</body>
---------------------------------------------------------------------
To unsubscribe, e-mail: cvs-unsubscribe@avalon.apache.org
For additional commands, e-mail: cvs-help@avalon.apache.org