mcconnell 2003/12/09 01:19:10
Modified: repository/main/src/java/org/apache/avalon/repository/main
DefaultBuilder.java DefaultInitialContext.java
repository/spi/src/java/org/apache/avalon/repository/provider
Factory.java InitialContext.java
repository/test/src/test/org/apache/avalon/repository/main
DefaultInitialContextTest.java
Log:
Improve handling of working base directory by moving abstraction to the repository
inital context.
Revision Changes Path
1.7 +19 -155
avalon/repository/main/src/java/org/apache/avalon/repository/main/DefaultBuilder.java
Index: DefaultBuilder.java
===================================================================
RCS file:
/home/cvs/avalon/repository/main/src/java/org/apache/avalon/repository/main/DefaultBuilder.java,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -r1.6 -r1.7
--- DefaultBuilder.java 8 Dec 2003 12:48:48 -0000 1.6
+++ DefaultBuilder.java 9 Dec 2003 09:19:10 -0000 1.7
@@ -86,7 +86,7 @@
public class DefaultBuilder extends AbstractBuilder implements Builder
{
//-----------------------------------------------------------
- // state
+ // immutable state
//-----------------------------------------------------------
/**
@@ -115,7 +115,11 @@
* delegated.
*/
private final Factory m_delegate;
-
+
+ //-----------------------------------------------------------
+ // constructors
+ //-----------------------------------------------------------
+
/**
* Creates a DefaultBuilder for a specific target application.
*
@@ -131,7 +135,7 @@
Thread.currentThread().getContextClassLoader(),
artifact );
}
-
+
/**
* Creates a DefaultBuilder for a specific target application.
*
@@ -190,18 +194,16 @@
buffer.append( "\n artifact: " + artifact );
buffer.append( "\n build: " + descriptor.getBuild() );
buffer.append( "\n factory: " + descriptor.getFactory() );
- buffer.append( "\n source: " +
clazz.getProtectionDomain().getCodeSource().getLocation() );
+ buffer.append( "\n source: "
+ + clazz.getProtectionDomain().getCodeSource().getLocation() );
buffer.append( "\n repository: " + m_repository );
throw new RepositoryException( buffer.toString(), e );
}
}
- private ClassLoader getClassLoader( ClassLoader classloader )
- {
- if( null != classloader ) return classloader;
- return DefaultBuilder.class.getClassLoader();
- }
-
+ //-----------------------------------------------------------
+ // Builder
+ //-----------------------------------------------------------
/**
* Return the factory established by the loader.
@@ -222,151 +224,13 @@
return m_classloader;
}
- /**
- * Main wrapper.
- *
- * TODO add more properties to allow full repo specification via
- * system properties including a repository implementation replacement.
- *
- * @param args the command line arguments
- */
- public static void main( String [] args )
- {
- String spec = getArtifactSpec( args );
- if( null == spec )
- {
- final String error =
- "Cannot resolve an artifict target.";
- System.out.println( error );
- System.exit( -1 );
- }
-
- File cache = getCache( args );
- String[] hosts = getHosts( args );
- Artifact artifact = Artifact.createArtifact( spec );
-
- try
- {
- InitialContext context = new DefaultInitialContext( cache, hosts );
- System.out.println( "Building: " + artifact );
- Builder builder = new DefaultBuilder( context, artifact );
- Object object = builder.getFactory().create();
- System.out.println( "OBJECT: " + object );
- }
- catch ( Throwable e )
- {
- String message = ExceptionHelper.packException( e, true );
- System.out.println( message );
- System.exit( -1 );
- }
- }
-
- private static String getArtifactSpec( String[] args )
- {
- String artifact = getArgument( "-artifact", args );
- if( null != artifact )
- {
- return artifact;
- }
- else
- {
- return null;
- }
- }
-
- private static File getCache( String[] args )
- {
- String cache = getArgument( "-cache", args );
- if( null != cache )
- {
- return new File( cache );
- }
- else
- {
- return null;
- }
- }
-
- private static String[] getHosts( String[] args )
- {
- String hosts = getArgument( "-hosts", args );
- if( null != hosts )
- {
- return expandHosts( hosts );
- }
- else
- {
- return null;
- }
- }
-
- private static String getArgument( String key, String[] args )
- {
- for( int i=0; i<args.length; i++ )
- {
- if( args[i].equals( key ) )
- {
- if( args.length >= i+1 )
- {
- return args[ i+1 ];
- }
- else
- {
- final String error =
- "Missing CLI value for parameter: " + key;
- throw new IllegalArgumentException( error );
- }
- }
- }
- return null;
- }
-
- private static String[] expandHosts( String arg )
- {
- ArrayList list = new ArrayList();
- StringTokenizer tokenizer = new StringTokenizer( arg, "," );
- while( tokenizer.hasMoreTokens() )
- {
- list.add( tokenizer.nextToken() );
- }
- return (String[]) list.toArray( new String[0] );
- }
-
- private static String getMavenRepositoryURI()
- {
- String home = getMavenHome();
- return "file:///" + getMavenHomeRepository();
- }
-
- private static String getMavenHomeRepository()
- {
- return getMavenHome() + File.separator + "repository";
- }
+ //-----------------------------------------------------------
+ // internal
+ //-----------------------------------------------------------
- private static String getMavenHome()
+ private ClassLoader getClassLoader( ClassLoader classloader )
{
- try
- {
- String local =
- System.getProperty(
- "maven.home.local",
- Env.getEnvVariable( "MAVEN_HOME_LOCAL" ) );
- if( null != local ) return local;
-
- String maven =
- System.getProperty(
- "maven.home",
- Env.getEnvVariable( "MAVEN_HOME" ) );
- if( null != maven ) return maven;
-
- return System.getProperty( "user.home" ) + File.separator + ".maven";
-
- }
- catch( Throwable e )
- {
- final String error =
- "Internal error while attempting to access environment: " +
e.toString();
- throw new Error( error );
- }
+ if( null != classloader ) return classloader;
+ return DefaultBuilder.class.getClassLoader();
}
}
1.9 +36 -13
avalon/repository/main/src/java/org/apache/avalon/repository/main/DefaultInitialContext.java
Index: DefaultInitialContext.java
===================================================================
RCS file:
/home/cvs/avalon/repository/main/src/java/org/apache/avalon/repository/main/DefaultInitialContext.java,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -r1.8 -r1.9
--- DefaultInitialContext.java 8 Dec 2003 12:48:48 -0000 1.8
+++ DefaultInitialContext.java 9 Dec 2003 09:19:10 -0000 1.9
@@ -123,9 +123,6 @@
private static final File USER_HOME =
new File( System.getProperty( "user.home" ) );
- private static final File USER_DIR =
- getBaseDirectory();
-
private static final String[] DEFAULT_INITIAL_HOSTS =
new String[]{
"http://dpml.net/",
@@ -150,6 +147,11 @@
*/
private final String[] m_hosts;
+ /**
+ * The base working directory.
+ */
+ private final File m_base;
+
// ------------------------------------------------------------------------
// constructors
// ------------------------------------------------------------------------
@@ -186,7 +188,7 @@
public DefaultInitialContext( String[] hosts )
throws RepositoryException
{
- this( null, hosts );
+ this( (File) null, hosts );
}
/**
@@ -199,7 +201,7 @@
public DefaultInitialContext( File cache, String[] hosts )
throws RepositoryException
{
- this( null, cache, hosts );
+ this( (Artifact) null, cache, hosts );
}
/**
@@ -215,8 +217,7 @@
throws RepositoryException
{
this(
- DefaultInitialContext.class.getClassLoader(),
- artifact, cache, hosts );
+ null, null, artifact, cache, hosts );
}
/**
@@ -228,17 +229,17 @@
* @throws RepositoryException if an error occurs during establishment
*/
public DefaultInitialContext(
- ClassLoader parent, Artifact artifact, File cache, String[] hosts )
+ File base, ClassLoader loader, Artifact artifact, File cache, String[] hosts
)
throws RepositoryException
{
+ m_base = setupBaseDirectory( base );
Properties avalonHome = getLocalProperties( USER_HOME, AVALON );
- Properties avalonWork = getLocalProperties( USER_DIR, AVALON );
-
+ Properties avalonWork = getLocalProperties( m_base, AVALON );
m_cache = setupCache( cache, avalonHome, avalonWork );
m_hosts = setupHosts( hosts, avalonHome, avalonWork );
- Artifact implementation =
- setupImplementation( artifact );
+ Artifact implementation = setupImplementation( artifact );
+ ClassLoader parent = setupClassLoader( loader );
//
// Create the temporary directory to pull down files into
@@ -316,6 +317,16 @@
// ------------------------------------------------------------------------
/**
+ * Return the base working directory.
+ *
+ * @return the base directory
+ */
+ public File getInitialWorkingDirectory()
+ {
+ return m_base;
+ }
+
+ /**
* Return cache root directory.
*
* @return the cache directory
@@ -360,6 +371,12 @@
}
}
+ private ClassLoader setupClassLoader( ClassLoader classloader )
+ {
+ if( null != classloader ) return classloader;
+ return DefaultInitialContext.class.getClassLoader();
+ }
+
private File setupCache( File file, Properties home, Properties work )
{
if( null != file ) return file;
@@ -430,6 +447,12 @@
"Internal error while attempting to build default implementation
artifact.";
throw new RepositoryRuntimeException( error, e );
}
+ }
+
+ private File setupBaseDirectory( File base )
+ {
+ if( null != base ) return base;
+ return getBaseDirectory();
}
private String[] setupDefaultHosts( Properties home, Properties work )
1.2 +2 -1
avalon/repository/spi/src/java/org/apache/avalon/repository/provider/Factory.java
Index: Factory.java
===================================================================
RCS file:
/home/cvs/avalon/repository/spi/src/java/org/apache/avalon/repository/provider/Factory.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- Factory.java 6 Dec 2003 23:52:16 -0000 1.1
+++ Factory.java 9 Dec 2003 09:19:10 -0000 1.2
@@ -50,6 +50,7 @@
package org.apache.avalon.repository.provider;
+import java.io.File;
import java.util.Map;
/**
1.4 +8 -1
avalon/repository/spi/src/java/org/apache/avalon/repository/provider/InitialContext.java
Index: InitialContext.java
===================================================================
RCS file:
/home/cvs/avalon/repository/spi/src/java/org/apache/avalon/repository/provider/InitialContext.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- InitialContext.java 7 Dec 2003 03:15:16 -0000 1.3
+++ InitialContext.java 9 Dec 2003 09:19:10 -0000 1.4
@@ -73,6 +73,13 @@
String HOSTS_KEY = "avalon.repository.hosts";
/**
+ * Return the base working directory.
+ *
+ * @return the base directory
+ */
+ File getInitialWorkingDirectory();
+
+ /**
* Return cache root directory.
*
* @return the cache directory
1.4 +1 -11
avalon/repository/test/src/test/org/apache/avalon/repository/main/DefaultInitialContextTest.java
Index: DefaultInitialContextTest.java
===================================================================
RCS file:
/home/cvs/avalon/repository/test/src/test/org/apache/avalon/repository/main/DefaultInitialContextTest.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- DefaultInitialContextTest.java 7 Dec 2003 03:15:16 -0000 1.3
+++ DefaultInitialContextTest.java 9 Dec 2003 09:19:10 -0000 1.4
@@ -167,16 +167,6 @@
}
}
- private File getBaseDirectory()
- {
- String base = System.getProperty( "basedir" );
- if( null != base )
- {
- return new File( base );
- }
- return new File( System.getProperty( "user.dir" ) );
- }
-
private static String[] getDefaultHosts()
{
return new String[]{
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]