You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@avalon.apache.org by ni...@apache.org on 2004/06/03 19:25:29 UTC
svn commit: rev 20783 - in avalon/trunk/tools/magic/engine/src: dist/bin dist/plugins/plugin java/org/apache/avalon/magic
Author: niclas
Date: Thu Jun 3 10:25:28 2004
New Revision: 20783
Added:
avalon/trunk/tools/magic/engine/src/java/org/apache/avalon/magic/ArtifactException.java
avalon/trunk/tools/magic/engine/src/java/org/apache/avalon/magic/Util.java
Modified:
avalon/trunk/tools/magic/engine/src/dist/bin/magic
avalon/trunk/tools/magic/engine/src/dist/plugins/plugin/magic.bsh
avalon/trunk/tools/magic/engine/src/java/org/apache/avalon/magic/Artifact.java
avalon/trunk/tools/magic/engine/src/java/org/apache/avalon/magic/Builder.java
avalon/trunk/tools/magic/engine/src/java/org/apache/avalon/magic/Main.java
avalon/trunk/tools/magic/engine/src/java/org/apache/avalon/magic/PluginContext.java
avalon/trunk/tools/magic/engine/src/java/org/apache/avalon/magic/PluginProperties.java
avalon/trunk/tools/magic/engine/src/java/org/apache/avalon/magic/PluginServiceManager.java
avalon/trunk/tools/magic/engine/src/java/org/apache/avalon/magic/ScriptFacade.java
Log:
Some major reshuffling needed to tie the artifact handling closer to the core functionality. Also a massive thinking error in the propery management has been fixed.
Modified: avalon/trunk/tools/magic/engine/src/dist/bin/magic
==============================================================================
--- avalon/trunk/tools/magic/engine/src/dist/bin/magic (original)
+++ avalon/trunk/tools/magic/engine/src/dist/bin/magic Thu Jun 3 10:25:28 2004
@@ -1,6 +1,8 @@
#!/bin/sh
#
+START=`date +%s%N`
+
BIN_DIR=`dirname $0`
SYSTEM_DIR=`dirname $BIN_DIR`
@@ -11,3 +13,10 @@
CP=$CP:$ANT_HOME/lib/ant.jar:$ANT_HOME/lib/ant-trax.jar
java $JVM_OPTS -Dmagic.system.dir=$SYSTEM_DIR -cp $CP org.apache.avalon.magic.Main $@
+
+END=`date +%s%N`
+
+NS=`expr $END - $START`
+MS=`expr $NS / 1000000`
+
+echo "Total True! Build time: $MS ms"
\ No newline at end of file
Modified: avalon/trunk/tools/magic/engine/src/dist/plugins/plugin/magic.bsh
==============================================================================
--- avalon/trunk/tools/magic/engine/src/dist/plugins/plugin/magic.bsh (original)
+++ avalon/trunk/tools/magic/engine/src/dist/plugins/plugin/magic.bsh Thu Jun 3 10:25:28 2004
@@ -16,8 +16,12 @@
*/
import java.io.File;
+import java.io.IOException;
import org.apache.avalon.magic.AbstractPlugin;
+import org.apache.avalon.magic.Artifact;
+import org.apache.avalon.magic.ArtifactException;
+import org.apache.avalon.magic.BuildException;
import org.apache.avalon.magic.Plugin;
import org.apache.avalon.magic.PluginContext;
import org.apache.avalon.magic.PluginFacade;
@@ -62,84 +66,101 @@
/* Copies the plugin to a Java file and tries to compile it */
public void validate()
- throws ServiceException
+ throws ServiceException, IOException, BuildException
{
- notifyPreMethod( "validate" );
- getLogger().info( "Validating plugin: " + m_Context.getProjectName() );
-
- File srcDir = new File( m_Context.getProjectDir(), "src/dist/" );
- File srcFile = new File( srcDir, "magic.bsh" );
- File destDir = m_Context.getTempDir();
-
- /* Remove any existing content in the Temp dir. */
- Delete delete = m_Project.createTask( "delete" );
- delete.setDir( destDir );
- delete.init();
- delete.execute();
- destDir.mkdirs();
+ try
+ {
+ notifyPreMethod( "validate" );
+ getLogger().info( "Validating plugin: " + m_Context.getProjectName() );
- PluginServiceManager sm = m_ServiceManager;
-
- PluginFacade pf = sm.getFacade( srcDir );
- String classname = pf.getPluginClassname();
-
- File classfile = new File( destDir, classname + ".java" );
-
- Copy copy = m_Project.createTask( "copy" );
- copy.setVerbose( true );
- copy.setTofile( classfile );
- copy.setFile( srcFile );
- copy.execute();
-
- File cache = new File( m_Context.getSystemDir(), "cache" );
- cache.mkdirs();
-
- String classpath = m_Context.getProperty( "java.class.path" );
-
- Javac javac = (Javac) m_Project.createTask( "javac" );
- /* Set the destination */
- javac.setDestdir( cache );
- javac.setDeprecation( true );
- javac.setDebug( true );
- javac.setVerbose( false );
-
- /* Add Class path defined in the magic.properties */
- Path cp = javac.createClasspath();
- Path.PathElement pe = cp.createPathElement();
- pe.setPath( cache.getAbsolutePath() + "/" );
- pe = cp.createPathElement();
- pe.setPath( classpath );
- /* Later; also add dependency Jars */
-
- /* Add the source path */
- Path src = javac.createSrc();
- Path.PathElement srcPE = src.createPathElement();
- srcPE.setLocation( destDir );
- javac.init();
- javac.execute();
-
- notifyPostMethod( "validate" );
+ File srcDir = new File( m_Context.getProjectDir(), "src/dist/" );
+ File srcFile = new File( srcDir, "magic.bsh" );
+ File destDir = m_Context.getTempDir();
+
+ /* Remove any existing content in the Temp dir. */
+ Delete delete = (Delete) m_Project.createTask( "delete" );
+ delete.setDir( destDir );
+ delete.init();
+ delete.execute();
+ destDir.mkdirs();
+
+ PluginServiceManager sm = m_ServiceManager;
+
+ PluginFacade pf = sm.getFacade( srcDir );
+ String classname = pf.getPluginClassname();
+
+ File classfile = new File( destDir, classname + ".java" );
+
+ Copy copy = (Copy) m_Project.createTask( "copy" );
+ copy.setVerbose( true );
+ copy.setTofile( classfile );
+ copy.setFile( srcFile );
+ copy.execute();
+
+ File cache = new File( m_Context.getSystemDir(), "cache" );
+ cache.mkdirs();
+
+ String classpath = m_Context.getProperty( "java.class.path" );
+
+ Javac javac = (Javac) m_Project.createTask( "javac" );
+ /* Set the destination */
+ javac.setDestdir( cache );
+ javac.setDeprecation( true );
+ javac.setDebug( true );
+ javac.setVerbose( false );
+
+ /* Add Class path defined in the magic.properties */
+ Path cp = javac.createClasspath();
+ Path.PathElement pe = cp.createPathElement();
+ pe.setPath( cache.getAbsolutePath() + "/" );
+ pe = cp.createPathElement();
+ pe.setPath( classpath );
+
+ Artifact artifact = Artifact.resolve( m_Context, m_Context.getProjectName() );
+ Artifact[] deps = artifact.getDependencies();
+ Path depsPath = new Path( m_Project );
+ for( int i=0 ; i < deps.length ; i++ )
+ {
+ File localFile = deps[i].getContentFile();
+ Path.PathElement fpe = depsPath.createPathElement();
+ fpe.setLocation( localFile );
+ }
+ cp.add( depsPath );
+ System.out.println( cp );
+ /* Add the source path */
+ Path src = javac.createSrc();
+ Path.PathElement srcPE = src.createPathElement();
+ srcPE.setLocation( destDir );
+ javac.init();
+ javac.execute();
+
+ notifyPostMethod( "validate" );
+ } catch( Exception e )
+ {
+ e.printStackTrace();
+ throw e;
+ }
}
public void install()
+ throws ServiceException, IOException, BuildException
{
validate();
- notifyPreMethod( "install" );
+ notifyPreMethod( "install" );
getLogger().info( "Installing plugin: " + m_Context.getProjectName() );
- String projectName = m_Context.getProjectName();
+ String projectName = m_Context.getProperty( "project.name" );
File systemDir = m_Context.getSystemDir();
File localpluginrepo = new File( systemDir, "plugins/" + projectName );
File srcDir = new File( m_Context.getProjectDir(), "src/dist" );
FileSet src = new FileSet();
src.setDir( srcDir );
- Copy copy = m_Project.createTask( "copy" );
+ Copy copy = (Copy) m_Project.createTask( "copy" );
copy.setVerbose( true );
copy.setTodir( localpluginrepo );
copy.addFileset( src );
copy.execute();
notifyPostMethod( "install" );
}
-
}
Modified: avalon/trunk/tools/magic/engine/src/java/org/apache/avalon/magic/Artifact.java
==============================================================================
--- avalon/trunk/tools/magic/engine/src/java/org/apache/avalon/magic/Artifact.java (original)
+++ avalon/trunk/tools/magic/engine/src/java/org/apache/avalon/magic/Artifact.java Thu Jun 3 10:25:28 2004
@@ -17,33 +17,39 @@
package org.apache.avalon.magic;
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileReader;
+import java.io.InputStream;
+import java.io.IOException;
+
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.net.URLClassLoader;
+import java.net.URLConnection;
+
+import java.util.ArrayList;
+import java.util.Properties;
+import java.util.Iterator;
+
public class Artifact
{
private static final String DEFAULT_REPOSITORY = "http://www.ibiblio.org/maven";
- private String m_Repository;
- private String m_ArtifactId;
- private String m_GroupId;
- private String m_Version;
- private String m_Type;
-
- public Artifact( String id, String version )
- {
- this( id, id, version );
- }
-
- public Artifact( String artifactId, String groupId, String version)
- {
- this( artifactId, groupId, version, "jar" );
- }
-
- public Artifact( String artifactId, String groupId, String version, String type )
- {
- this( artifactId, groupId, version, type, DEFAULT_REPOSITORY );
- }
-
- public Artifact( String artifactId, String groupId, String version, String type, String repository )
- {
+ private String m_Repository;
+ private String m_ArtifactId;
+ private String m_GroupId;
+ private String m_Version;
+ private String m_Type;
+ private PluginContext m_Context;
+
+ private Artifact( PluginContext context, String artifactId, String groupId, String version, String type, String repository )
+ {
+ if( repository == null || "".equals( repository ) )
+ repository = DEFAULT_REPOSITORY;
+
+ m_Context = context;
m_Repository = repository;
m_ArtifactId = artifactId;
m_GroupId = groupId;
@@ -51,6 +57,130 @@
m_Type = type;
}
+ public static Artifact resolve( PluginContext context, String artifactId )
+ throws IOException, ArtifactException
+ {
+ if( artifactId.startsWith( "artifact:" ) )
+ return resolveDirect( context, artifactId );
+ else
+ return resolveIndirect( context, artifactId );
+ }
+
+
+ private static Artifact resolveDirect( PluginContext context, String artifactId )
+ throws IOException, ArtifactException
+ {
+ String id = artifactId;
+ int posColon1 = id.indexOf( ':' );
+ int posColon2 = id.indexOf( ":", posColon1 + 1 );
+ if( posColon2 < 0 )
+ throw new ArtifactException( "Invalid format in '" + id + "'. No type." );
+ int posHash = id.indexOf( '#' );
+ int posSlash = id.lastIndexOf( '/' );
+ if( posSlash < 0 )
+ throw new ArtifactException( "Invalid format in '" + id + "'. No group." );
+
+ String type = id.substring( posColon1 + 1, posColon2 );
+ String group = id.substring( posColon2 + 1, posSlash );
+ String name;
+ String version;
+ if( posHash < 0 )
+ {
+ name = id.substring( posSlash+1 );
+ version = "1.0-dev.0";
+ }
+ else
+ {
+ name = id.substring( posSlash+1, posHash );
+ version = id.substring( posHash+1 );
+ }
+ String repository = context.getProperty( "artifact.repository" );
+
+ Artifact artifact = new Artifact( context, name, group, version, type, repository );
+ return artifact;
+ }
+
+ private static Artifact resolveIndirect( PluginContext context, String artifactId )
+ throws IOException
+ {
+ File definitionsDir = new File( context.getProjectSystemDir(), "definitions" );
+ File file = new File( definitionsDir, artifactId );
+ Properties p = new Properties();
+ if( file.exists() )
+ {
+ FileInputStream fis = new FileInputStream( file );
+ try
+ {
+ p.load( fis );
+ } finally
+ {
+ if( fis != null )
+ fis.close();
+ }
+ }
+
+ String repository = p.getProperty( "artifact.repository" );
+ if( repository == null )
+ repository = context.getProperty( "artifact.repository" );
+
+ String groupId = p.getProperty( "artifact.group" );
+ if( groupId == null )
+ groupId = artifactId;
+
+ String version = p.getProperty( "artifact.version" );
+ if( version == null )
+ version = "1.0.dev-0";
+
+ String type = p.getProperty( "artifact.type" );
+ if( type == null )
+ type = "jar";
+
+ Artifact artifact = new Artifact( context, artifactId, groupId, version, type, repository );
+ return artifact;
+ }
+
+ public Artifact[] getDependencies()
+ throws IOException, ArtifactException
+ {
+ File depsDir = new File( m_Context.getProjectSystemDir(), "dependencies" );
+ File file = new File( depsDir, getArtifactId() );
+ if( ! file.exists() )
+ return new Artifact[0];
+ FileReader reader = null;
+ BufferedReader br = null;
+ ArrayList deps = new ArrayList();
+ try
+ {
+ reader = new FileReader( file );
+ br = new BufferedReader( reader );
+ String line;
+ while( ( line = br.readLine() ) != null )
+ {
+ line = line.trim();
+ if( ! line.equals( "" ) )
+ deps.add( line );
+ }
+ } catch( IOException e )
+ {
+ e.printStackTrace();
+ throw e;
+ } finally
+ {
+ if( reader != null )
+ reader.close();
+ if( br != null )
+ br.close();
+ }
+ Artifact[] result = new Artifact[ deps.size() ];
+ Iterator list = deps.iterator();
+ for( int i=0 ; list.hasNext() ; i++ )
+ {
+ String dep = (String) list.next();
+ result[i] = resolve( m_Context, dep );
+ }
+ return result;
+ }
+
public String getRepository()
{
return m_Repository;
@@ -84,5 +214,44 @@
public String getFilename()
{
return m_ArtifactId + "-" + m_Version + "." + m_Type;
+ }
+
+ public URL toRemoteURL()
+ throws MalformedURLException
+ {
+ String href = getRepository() + "/" +
+ getGroupId() + "/" +
+ getType() + "s/" +
+ getArtifactId() + "-" +
+ getVersion() + ".jar" ;
+ return new URL( href );
+ }
+
+ public File toLocalFile()
+ throws IOException
+ {
+ String localRepo = m_Context.getProperty( "artifact.local.repository.dir" );
+ if( localRepo == null || "".equals( localRepo ) )
+ localRepo = m_Context.getProperty( "user.home" ) + "/.maven/repository";
+
+ String href = localRepo + "/" +
+ getGroupId() + "/" +
+ getType() + "s/" +
+ getArtifactId() + "-" +
+ getVersion() + ".jar" ;
+ File localFile = new File( href );
+ return localFile;
+ }
+
+ public File getContentFile()
+ throws IOException
+ {
+ File localfile = toLocalFile();
+ if( ! localfile.exists() )
+ {
+ localfile.getParentFile().mkdirs();
+ Util.download( this, localfile );
+ }
+ return localfile;
}
}
Added: avalon/trunk/tools/magic/engine/src/java/org/apache/avalon/magic/ArtifactException.java
==============================================================================
--- (empty file)
+++ avalon/trunk/tools/magic/engine/src/java/org/apache/avalon/magic/ArtifactException.java Thu Jun 3 10:25:28 2004
@@ -0,0 +1,36 @@
+/*
+Copyright 2004 The Apache Software Foundation
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+implied.
+
+See the License for the specific language governing permissions and
+limitations under the License.
+*/
+
+package org.apache.avalon.magic;
+
+
+public class ArtifactException extends BuildException
+{
+ public ArtifactException()
+ {
+ }
+
+ public ArtifactException( String message )
+ {
+ super( message );
+ }
+
+ public ArtifactException( String message, Throwable cause )
+ {
+ super( message, cause );
+ }
+}
Modified: avalon/trunk/tools/magic/engine/src/java/org/apache/avalon/magic/Builder.java
==============================================================================
--- avalon/trunk/tools/magic/engine/src/java/org/apache/avalon/magic/Builder.java (original)
+++ avalon/trunk/tools/magic/engine/src/java/org/apache/avalon/magic/Builder.java Thu Jun 3 10:25:28 2004
@@ -63,28 +63,33 @@
public void execute()
throws Exception
{
- PluginProperties props = new PluginProperties();
+ PluginProperties propset1 = new PluginProperties();
+ PluginProperties propset2 = new PluginProperties();
+ PluginProperties propset3 = new PluginProperties();
// This is included twice, so a reference to other parts
// can be obtained in the local project properties file.
- loadProjectLocalProperties( props );
+ loadProjectLocalProperties( propset1 );
- loadGlobalProperties( props );
- loadMagicSystemProperties( props );
- loadMagicPluginProperties( props );
- loadProjectSystemProperties( props );
- loadProjectLocalProperties( props );
- loadUserProjectProperties( props );
- loadUserSystemProperties( props );
- loadUserHomeProperties( props );
+ loadGlobalProperties( propset1 );
+ loadMagicSystemProperties( propset1 );
- m_AntProject = initializeAntProject( props );
+ String projSys = propset1.getProperty( "project.system" );
+ loadProjectSystemProperties( propset2, projSys );
+ loadProjectLocalProperties( propset2 );
+
+ loadUserHomeProperties( propset3 );
+ loadUserSystemProperties( propset3);
+ loadUserProjectSystemProperties( propset3, projSys );
+ loadUserProjectProperties( propset3 );
+
+ m_AntProject = initializeAntProject( propset1 );
FacadeFactory factory = new FacadeFactory();
if( factory instanceof LogEnabled )
((LogEnabled) factory).enableLogging( m_Logger );
- PluginServiceManager sm = new PluginServiceManager( factory, props, m_AntProject );
+ PluginServiceManager sm = new PluginServiceManager( factory, propset1, propset2, propset3, m_AntProject );
sm.enableLogging( m_Logger );
for( int i=0 ; i < m_CallMethods.length ; i++ )
@@ -95,8 +100,19 @@
String pluginname = ".";
if( pos <= 0 )
{
- // project method
- plugin = sm.lookupPlugin( "." );
+ if( pluginname.startsWith( "@" ) )
+ {
+ // indirect file
+ String filename = pluginname.substring(1);
+ File sequenceFile = new File( m_ProjectDir, filename );
+ Main.sequence( m_ProjectDir, sequenceFile );
+ continue;
+ }
+ else
+ {
+ // project method
+ plugin = sm.lookupPlugin( "." );
+ }
}
else
{
@@ -153,23 +169,8 @@
load( props, file );
}
- private void loadMagicPluginProperties( PluginProperties props )
- {
-/* I think this happens in the servicemanager.lookup(), which would
- mean on demand only = much faster.
- File[] plugins = m_SystemDir.listFiles();
- for( int i=0 ; i < plugins.length ; i++ )
- {
- File file = new File( plugins[i], "magic.properties" );
- if( file.exists() )
- load( props, file );
- }
-*/
- }
-
- private void loadProjectSystemProperties( PluginProperties props )
+ private void loadProjectSystemProperties( PluginProperties props, String projSys )
{
- String projSys = props.getProperty( "project.system.dir" );
if( projSys == null )
return;
File dir = new File( projSys );
@@ -192,6 +193,18 @@
load( props, file );
}
+ private void loadUserProjectSystemProperties( PluginProperties props, String projSys )
+ {
+ if( projSys == null )
+ return;
+ File dir = new File( projSys );
+ File file = new File( dir, "user-magic.properties" );
+ if( file.exists() )
+ {
+ load( props, file );
+ }
+ }
+
private void loadUserSystemProperties( PluginProperties props )
{
File file = new File( m_SystemDir, "user-magic.properties" );
@@ -201,7 +214,7 @@
private void loadUserHomeProperties( PluginProperties props )
{
- File dir = new File( System.getProperty( "user.dir" ) );
+ File dir = new File( System.getProperty( "user.home" ) );
File file = new File( dir, ".magic.properties" );
if( file.exists() )
load( props, file );
Modified: avalon/trunk/tools/magic/engine/src/java/org/apache/avalon/magic/Main.java
==============================================================================
--- avalon/trunk/tools/magic/engine/src/java/org/apache/avalon/magic/Main.java (original)
+++ avalon/trunk/tools/magic/engine/src/java/org/apache/avalon/magic/Main.java Thu Jun 3 10:25:28 2004
@@ -30,18 +30,15 @@
static private Builder m_Application;
static public void main( String[] args )
- throws Exception
{
- long t0 = System.currentTimeMillis();
try
{
File projectDir = getProjectDir();
process( args, projectDir );
- } finally
+ } catch( Throwable e )
{
- long t1 = System.currentTimeMillis();
- System.out.println( "Build Time: " + (t1 - t0) + " ms." );
- }
+ print( e );
+ }
}
static private File getProjectDir()
@@ -56,7 +53,8 @@
{
if( args.length == 0 )
{
- String[] jobs = sequence( dir );
+ File sequenceFile = new File( dir, "magic.sequence" );
+ String[] jobs = sequence( dir, sequenceFile );
for( int i = 0 ; i < jobs.length ; i++ )
{
doJob( jobs[i], dir );
@@ -69,10 +67,9 @@
}
}
- static String[] sequence( File projDir )
+ static String[] sequence( File projDir, File sequenceFile )
throws Exception
{
- File sequenceFile = new File( projDir, "magic.sequence" );
if( ! sequenceFile.exists() )
return new String[0];
FileReader reader = null;
@@ -86,7 +83,9 @@
String line;
while( (line = br.readLine() ) != null )
{
- result.add( line.trim() );
+ line = line.trim();
+ if( ! line.startsWith( "#" ) )
+ result.add( line );
}
String[] retVal = new String[ result.size() ];
result.toArray( retVal );
@@ -125,5 +124,13 @@
// requested dir.
File newProjectDir = new File( dir, subdir );
process( result, newProjectDir );
+ }
+
+ static private void print( Throwable e )
+ {
+ e.printStackTrace();
+ Throwable f = e.getCause();
+ if( f != null )
+ print( f );
}
}
Modified: avalon/trunk/tools/magic/engine/src/java/org/apache/avalon/magic/PluginContext.java
==============================================================================
--- avalon/trunk/tools/magic/engine/src/java/org/apache/avalon/magic/PluginContext.java (original)
+++ avalon/trunk/tools/magic/engine/src/java/org/apache/avalon/magic/PluginContext.java Thu Jun 3 10:25:28 2004
@@ -18,6 +18,8 @@
package org.apache.avalon.magic;
import java.io.File;
+import java.io.IOException;
+
import java.util.Iterator;
import java.util.Stack;
import java.util.StringTokenizer;
Modified: avalon/trunk/tools/magic/engine/src/java/org/apache/avalon/magic/PluginProperties.java
==============================================================================
--- avalon/trunk/tools/magic/engine/src/java/org/apache/avalon/magic/PluginProperties.java (original)
+++ avalon/trunk/tools/magic/engine/src/java/org/apache/avalon/magic/PluginProperties.java Thu Jun 3 10:25:28 2004
@@ -17,6 +17,8 @@
package org.apache.avalon.magic;
+import java.util.Iterator;
+import java.util.Map;
import java.util.Properties;
import java.util.Stack;
import java.util.StringTokenizer;
@@ -29,7 +31,15 @@
public PluginProperties( Properties content )
{
- super( content );
+ super();
+ Iterator list = content.entrySet().iterator();
+ while( list.hasNext() )
+ {
+ Map.Entry entry = (Map.Entry) list.next();
+ Object key = entry.getKey();
+ Object value = entry.getValue();
+ put( key, value );
+ }
}
public String getProperty( String name )
Modified: avalon/trunk/tools/magic/engine/src/java/org/apache/avalon/magic/PluginServiceManager.java
==============================================================================
--- avalon/trunk/tools/magic/engine/src/java/org/apache/avalon/magic/PluginServiceManager.java (original)
+++ avalon/trunk/tools/magic/engine/src/java/org/apache/avalon/magic/PluginServiceManager.java Thu Jun 3 10:25:28 2004
@@ -46,29 +46,52 @@
private File m_LocalPlugins;
private File m_TempDir;
- private PluginProperties m_GlobalProperties;
+ private PluginProperties m_AllProperties;
+
+ /* Properties that have lower priority than the Plugin Properties */
+ private PluginProperties m_CommonProperties;
+ /* Properties that are defined inside the Plugins, gets aggregated here. */
+ private PluginProperties m_PluginProperties;
+ /* Properties that have higher priority than the Plugin Properties */
+ private PluginProperties m_ProjectProperties;
+ /* Properties that have the highest priority */
+ private PluginProperties m_UserProperties;
+
private FacadeFactory m_FacadeFactory;
private Project m_AntProject;
- PluginServiceManager( FacadeFactory factory, PluginProperties globalProps, Project ant )
+ PluginServiceManager( FacadeFactory factory, PluginProperties commonProps, PluginProperties projProps, PluginProperties userProps, Project ant )
{
if( factory == null )
throw new IllegalArgumentException( "Null argument: factory" );
- if( globalProps == null )
- throw new IllegalArgumentException( "Null argument: globalProps" );
+ if( commonProps == null )
+ throw new IllegalArgumentException( "Null argument: commonProps" );
+ if( projProps == null )
+ throw new IllegalArgumentException( "Null argument: projProps" );
+ if( userProps == null )
+ throw new IllegalArgumentException( "Null argument: userProps" );
if( ant == null )
throw new IllegalArgumentException( "Null argument: ant" );
DUMMY = new Object();
m_FacadeFactory = factory;
- m_GlobalProperties = globalProps;
+ m_ProjectProperties = projProps;
+ m_CommonProperties = commonProps;
+ m_UserProperties = userProps;
+ m_PluginProperties = new PluginProperties();
+ m_AllProperties = new PluginProperties();
+ try
+ {
+ repopulateProperties( null );
+ } catch( Exception e )
+ {}
m_PluginsByKey = new HashMap();
m_PluginsByValue = new HashMap();
- m_SystemDir = new File( globalProps.getProperty( "magic.home.dir" ) );
- m_LocalPlugins = new File( globalProps.getProperty( "magic.plugins.dir" ) );;
- m_ProjectDir = new File( globalProps.getProperty( "magic.project.dir" ) );;
- m_TempDir = new File( globalProps.getProperty( "magic.temp.dir" ) );;
+ m_SystemDir = new File( commonProps.getProperty( "magic.home.dir" ) );
+ m_LocalPlugins = new File( commonProps.getProperty( "magic.plugins.dir" ) );;
+ m_ProjectDir = new File( commonProps.getProperty( "magic.project.dir" ) );;
+ m_TempDir = new File( commonProps.getProperty( "magic.temp.dir" ) );;
m_AntProject = ant;
}
@@ -149,7 +172,9 @@
throws CreationException
{
PluginContext ctx = new PluginContext( scriptDir );
-
+ if( ctx instanceof LogEnabled )
+ ((LogEnabled) ctx).enableLogging( getLogger() );
+
PluginFacade facade = m_FacadeFactory.create( ctx );
return facade;
}
@@ -182,17 +207,15 @@
else
{
pluginDir = new File( m_LocalPlugins, service );
- appendProperties( m_GlobalProperties, pluginDir );
+ appendProperties( m_PluginProperties, pluginDir );
}
if( pluginDir == null )
throw new ServiceException( "Plugin '" + service + "' is not present in " + m_LocalPlugins + "." );
- PluginProperties props = new PluginProperties( m_GlobalProperties );
+ repopulateProperties( m_ProjectDir );
- appendProperties( props, m_ProjectDir );
-
- String projectName = props.getProperty( "project.name" );
- String psLoc = props.getProperty( "project.system" ) ;
+ String projectName = m_AllProperties.getProperty( "project.name" );
+ String psLoc = m_AllProperties.getProperty( "project.system" ) ;
File projectSystemDir;
if( psLoc != null )
@@ -207,8 +230,10 @@
throw new IllegalArgumentException( "The required Project System Directory (Can be set with ${project.system}) doesn't exist : " + projectSystemDir.getAbsolutePath() );
PluginContext ctx = new PluginContext( projectName, m_ProjectDir,
- projectSystemDir.getAbsoluteFile(), props, service, pluginDir,
+ projectSystemDir.getAbsoluteFile(), m_AllProperties, service, pluginDir,
m_SystemDir, m_TempDir, m_AntProject );
+ if( ctx instanceof LogEnabled )
+ ((LogEnabled) ctx).enableLogging( getLogger() );
try
{
@@ -252,5 +277,17 @@
} catch( IOException e )
{} // Ignore.
}
+ }
+
+ private void repopulateProperties( File dir )
+ throws ServiceException
+ {
+ m_AllProperties.putAll( m_CommonProperties );
+ m_AllProperties.putAll( m_PluginProperties );
+ m_AllProperties.putAll( m_ProjectProperties );
+ if( dir != null )
+ appendProperties( m_AllProperties, dir );
+ m_AllProperties.putAll( m_UserProperties );
+ String user = m_AllProperties.getProperty( "artifact.remote.username" );
}
}
Modified: avalon/trunk/tools/magic/engine/src/java/org/apache/avalon/magic/ScriptFacade.java
==============================================================================
--- avalon/trunk/tools/magic/engine/src/java/org/apache/avalon/magic/ScriptFacade.java (original)
+++ avalon/trunk/tools/magic/engine/src/java/org/apache/avalon/magic/ScriptFacade.java Thu Jun 3 10:25:28 2004
@@ -21,6 +21,9 @@
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
+
+import java.net.URL;
+
import java.util.StringTokenizer;
import org.apache.avalon.framework.logger.AbstractLogEnabled;
@@ -29,7 +32,6 @@
import bsh.EvalError;
import bsh.Interpreter;
-
public class ScriptFacade extends AbstractLogEnabled
implements PluginFacade
{
@@ -67,7 +69,7 @@
/** Returns the Plugin instance of that this PluginDelegate refers to.
*/
public Plugin resolve()
- throws EvalError, IOException
+ throws EvalError, IOException, ArtifactException
{
if( m_Plugin != null )
return m_Plugin;
@@ -80,7 +82,20 @@
bsh.setErr( System.err );
BshClassManager classman = bsh.getClassManager();
+ String pluginname = m_Context.getPluginName();
+ Artifact thisArtifact = Artifact.resolve( m_Context, pluginname );
+ System.out.println( thisArtifact );
+ Artifact[] deps = thisArtifact.getDependencies();
+ System.out.println( "Deps: " + deps.length );
+ URL[] urls = Util.getURLs( deps );
+ for( int i = 0 ; i < urls.length ; i++ )
+ {
+ if( getLogger().isDebugEnabled() )
+ getLogger().debug( "Adding to BeanShell classpath:" + urls[i] );
+ classman.addClassPath( urls[i] );
+ }
+
if( ! classman.classExists( m_Classname ) )
{
bsh.eval( m_Script );
Added: avalon/trunk/tools/magic/engine/src/java/org/apache/avalon/magic/Util.java
==============================================================================
--- (empty file)
+++ avalon/trunk/tools/magic/engine/src/java/org/apache/avalon/magic/Util.java Thu Jun 3 10:25:28 2004
@@ -0,0 +1,197 @@
+/*
+Copyright 2004 The Apache Software Foundation
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+implied.
+
+See the License for the specific language governing permissions and
+limitations under the License.
+*/
+
+package org.apache.avalon.magic;
+
+import java.io.BufferedInputStream;
+import java.io.BufferedOutputStream;
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.PrintStream;
+import java.io.IOException;
+
+import java.net.URL;
+import java.net.URLConnection;
+
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
+
+public class Util
+{
+ static public URL[] getURLs( Artifact[] artifacts )
+ throws IOException
+ {
+ URL[] jars = new URL[ artifacts.length ];
+ for( int i=0 ; i < artifacts.length ; i++ )
+ {
+ File file = artifacts[i].toLocalFile();
+ jars[i] = file.toURL();
+ }
+ return jars;
+ }
+
+ static public void download( Artifact artifact, File dest )
+ throws IOException
+ {
+ URL url = artifact.toRemoteURL();
+ download( url, dest );
+ }
+
+ static public void download( URL url, File dest )
+ throws IOException
+ {
+ FileOutputStream out = null;
+ BufferedOutputStream bos = null;
+ InputStream in = null;
+ BufferedInputStream bis = null;
+ File dlFile = File.createTempFile( "~magic", ".tmp", dest.getParentFile() );
+ dlFile.deleteOnExit();
+ try
+ {
+ out = new FileOutputStream( dlFile );
+ bos = new BufferedOutputStream( out );
+ URLConnection conn = url.openConnection();
+ conn.connect();
+ in = conn.getInputStream();
+ bis = new BufferedInputStream( in );
+ int b;
+ int counter = 0;
+ int kbs = 0;
+ while( ( b = bis.read() ) != -1 )
+ {
+ counter++;
+ if( counter >= 1000 )
+ {
+ counter = 0;
+ kbs++;
+ System.out.print( "." );
+ }
+ bos.write( b );
+ }
+ bos.flush();
+ System.out.println( "\nDownloaded: " + (counter + kbs * 1000) + " bytes." );
+ dlFile.renameTo( dest );
+ } finally
+ {
+ if( bos != null )
+ bos.close();
+ if( out != null )
+ out.close();
+ if( bis != null )
+ bis.close();
+ if( in != null )
+ in.close();
+ dlFile.delete();
+ }
+ }
+
+ static public boolean verify( File content, File checksum )
+ throws IOException
+ {
+ byte[] chk1 = digest( content );
+ byte[] chk2 = readChecksumFile( checksum );
+ return MessageDigest.isEqual( chk1, chk2 );
+ }
+
+ static public void checksum( File inFile )
+ throws IOException
+ {
+ byte[] checksum = digest( inFile );
+
+ String outFilename = inFile.getAbsolutePath() + ".md5";
+ File outFile = new File( outFilename );
+
+ FileOutputStream fos = new FileOutputStream( outFile );
+ PrintStream out = new PrintStream( fos );
+
+ for( int i=0 ; i < checksum.length ; i++ )
+ {
+ int b = checksum[i];
+ if( b < 0 )
+ b = 256 + b;
+ String hex = Integer.toHexString( b );
+ if( hex.length() == 1 )
+ hex = "0" + hex;
+ out.print( hex );
+ }
+ out.println();
+ out.flush();
+ if( fos != null )
+ fos.close();
+ if( out != null )
+ out.close();
+ }
+
+ static private byte[] digest( File content )
+ throws IOException
+ {
+ MessageDigest digest = null;
+ try
+ {
+ digest = MessageDigest.getInstance( "MD5" );
+ } catch( NoSuchAlgorithmException e )
+ {} // can't happen.
+
+ FileInputStream fis = null;
+
+ fis = new FileInputStream( content );
+ byte[] data = new byte[10000];
+ while( true ) // read all the bytes
+ {
+ int available = fis.available();
+ int length = fis.read( data, 0, 10000 );
+ if( length == -1 )
+ break;
+ digest.update( data, 0, length );
+ }
+ if( fis != null )
+ fis.close();
+ return digest.digest();
+ }
+
+ static private byte[] readChecksumFile( File checksumFile )
+ throws IOException
+ {
+ try
+ {
+ FileInputStream fis = new FileInputStream( checksumFile );
+ BufferedReader in = new BufferedReader( new InputStreamReader(fis) );
+
+ String line = in.readLine().trim();
+ byte[] data = new byte[ line.length() / 2 ];
+ int pos = 0;
+ for( int i=0 ; i < line.length() ; i += 2 )
+ {
+ String hex = line.substring( i, i+2 );
+ int value = Integer.valueOf( hex, 16 ).intValue();
+ if( value > 127 )
+ value = value - 256;
+ data[pos++] = (byte) value;
+ }
+ System.out.println();
+ fis.close();
+ return data;
+ } catch( Exception e )
+ {
+ return null;
+ }
+ }
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: cvs-unsubscribe@avalon.apache.org
For additional commands, e-mail: cvs-help@avalon.apache.org