Author: niclas Date: Fri May 21 04:22:41 2004 New Revision: 20192 Added: avalon/trunk/tools/magic/artifact/build.properties avalon/trunk/tools/magic/artifact/src/dist/build.bsh avalon/trunk/tools/magic/artifact/src/dist/build.properties avalon/trunk/tools/magic/engine/src/java/org/apache/merlin/magic/Artifact.java avalon/trunk/tools/magic/engine/src/test/ avalon/trunk/tools/magic/engine/src/test/org/ avalon/trunk/tools/magic/engine/src/test/org/apache/ avalon/trunk/tools/magic/engine/src/test/org/apache/merlin/ avalon/trunk/tools/magic/engine/src/test/org/apache/merlin/magic/ avalon/trunk/tools/magic/engine/src/test/org/apache/merlin/magic/PluginContextTestCase.java avalon/trunk/tools/magic/engine/src/test/org/apache/merlin/magic/test.properties Modified: avalon/trunk/tools/magic/engine/src/java/org/apache/merlin/magic/Builder.java avalon/trunk/tools/magic/engine/src/java/org/apache/merlin/magic/PluginContext.java avalon/trunk/tools/magic/engine/src/java/org/apache/merlin/magic/PluginServiceManager.java avalon/trunk/tools/magic/engine/src/java/org/apache/merlin/magic/ScriptFacade.java Log: More work on the Magic stuff...
Added: avalon/trunk/tools/magic/artifact/build.properties ============================================================================== --- (empty file) +++ avalon/trunk/tools/magic/artifact/build.properties Fri May 21 04:22:41 2004 @@ -0,0 +1,2 @@ + +project.name = artifact Added: avalon/trunk/tools/magic/artifact/src/dist/build.bsh ============================================================================== --- (empty file) +++ avalon/trunk/tools/magic/artifact/src/dist/build.bsh Fri May 21 04:22:41 2004 @@ -0,0 +1,106 @@ + +import java.io.BufferedInputStream; +import java.io.BufferedOutputStream; +import java.io.File; +import java.io.FileOutputStream; +import java.io.InputStream; + +import java.net.URL; + +import java.util.ArrayList; +import java.util.Iterator; + +import org.apache.merlin.magic.Artifact; +import org.apache.merlin.magic.Plugin; +import org.apache.merlin.magic.PluginContext; + +import org.apache.avalon.framework.context.Context; +import org.apache.avalon.framework.context.Contextualizable; + +import org.apache.avalon.framework.logger.AbstractLogEnabled; + +import org.apache.tools.ant.Project; +import org.apache.tools.ant.types.Path; + +public class ArtifactPlugin extends AbstractLogEnabled + implements Plugin, Contextualizable +{ + private PluginContext m_PluginContext; + + private Project m_Project; + + private ArrayList m_CompileListeners; + + private boolean m_Compiled = false; + + public void contextualize( Context ctx ) + { + m_PluginContext = (PluginContext) ctx; + m_Project = m_PluginContext.getAntProject(); + } + + public ClassLoader getClassloader( Artifact[] dependencies ) + { + } + + public Path getClassPath( Artifact[] dependencies ) + { + } + + public void upload( Artifact artifact ) + { + } + + private void download( Artifact artifact, File dest ) + throws IOException, MalformedURLException + { + FileOutputStream out = null; + BufferedOutputStream bos = null; + try + { + out = new FileOutputStream( dest ); + bos = new BufferedOutputStream( out ); + URL url = toRemoteFile( artifact ); + Connection conn = url.openConnection(); + conn.connect(); + InputStream in = conn.getInputStream(); + BufferedInputStream bis = new BufferedInputStream( in ); + int b; + while( ( b = bis.read() ) != -1 ) + { + bos.write( b ); + } + } finally + { + if( out != null ) + out.close(); + if( bos != null ) + bos.close(); + if( in != null ) + in.close(); + if( bis != null ) + bis.close(); + } + } + + private URL toRemoteURL( Artifact artifact ) + throws MalformedURLException + { + String href = artifact.getRepository() + "/" + + artifact.getGroupId() + "/" + + artifact.getType() + "s/" + + artifact.getArtifactId() + "-" + + artifact.getVersion() + ".jar" ; + return new URL( href ); + } + + private File toLocalFile( Artifact artifact ) + { + String href = m_PluginContext.getProperty( "artifact.local.repository.dir" ) + "/" + + artifact.getGroupId() + "/" + + artifact.getType() + "s/" + + artifact.getArtifactId() + "-" + + artifact.getVersion() + ".jar" ; + return new URL( href ); + } +} Added: avalon/trunk/tools/magic/artifact/src/dist/build.properties ============================================================================== --- (empty file) +++ avalon/trunk/tools/magic/artifact/src/dist/build.properties Fri May 21 04:22:41 2004 @@ -0,0 +1,2 @@ + +artifact.local.repository.dir = ${user.dir}/.maven/repository \ No newline at end of file Added: avalon/trunk/tools/magic/engine/src/java/org/apache/merlin/magic/Artifact.java ============================================================================== --- (empty file) +++ avalon/trunk/tools/magic/engine/src/java/org/apache/merlin/magic/Artifact.java Fri May 21 04:22:41 2004 @@ -0,0 +1,63 @@ + +package org.apache.merlin.magic; + + +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 ) + { + m_Repository = repository; + m_ArtifactId = artifactId; + m_GroupId = groupId; + m_Version = version; + m_Type = type; + } + + public String getRepository() + { + return m_Repository; + } + + public String getGroupId() + { + return m_GroupId; + } + + public String getType() + { + return m_Type; + } + + public String getArtifactId() + { + return m_ArtifactId; + } + + public String getVersion() + { + return m_Version; + } +} Modified: avalon/trunk/tools/magic/engine/src/java/org/apache/merlin/magic/Builder.java ============================================================================== --- avalon/trunk/tools/magic/engine/src/java/org/apache/merlin/magic/Builder.java (original) +++ avalon/trunk/tools/magic/engine/src/java/org/apache/merlin/magic/Builder.java Fri May 21 04:22:41 2004 @@ -1,21 +1,13 @@ package org.apache.merlin.magic; -import org.apache.avalon.framework.logger.LogEnabled; -import org.apache.avalon.framework.logger.Logger; -import org.apache.avalon.framework.logger.ConsoleLogger; - -import org.apache.avalon.framework.service.ServiceException; - -import bsh.Interpreter; - import java.io.File; -import java.io.FileReader; - import java.lang.reflect.Method; - -import java.net.URL; - import java.util.Properties; + +import org.apache.avalon.framework.logger.ConsoleLogger; +import org.apache.avalon.framework.logger.LogEnabled; +import org.apache.avalon.framework.logger.Logger; +import org.apache.avalon.framework.service.ServiceException; public class Builder { Modified: avalon/trunk/tools/magic/engine/src/java/org/apache/merlin/magic/PluginContext.java ============================================================================== --- avalon/trunk/tools/magic/engine/src/java/org/apache/merlin/magic/PluginContext.java (original) +++ avalon/trunk/tools/magic/engine/src/java/org/apache/merlin/magic/PluginContext.java Fri May 21 04:22:41 2004 @@ -1,15 +1,15 @@ package org.apache.merlin.magic; -import bsh.Interpreter; - import java.io.File; import java.util.Properties; - -import org.apache.tools.ant.Project; +import java.util.Stack; +import java.util.StringTokenizer; import org.apache.avalon.framework.context.Context; - import org.apache.avalon.framework.logger.AbstractLogEnabled; +import org.apache.tools.ant.Project; + +import bsh.Interpreter; public class PluginContext extends AbstractLogEnabled implements Context @@ -29,12 +29,12 @@ PluginContext( String projectname, File projectDir, Properties projectProps, String pluginname, File pluginDir, File systemDir ) { - m_ProjectName = projectname; + m_ProjectName = projectname.trim(); m_ProjectDir = projectDir; m_ProjectProperties = projectProps; m_PluginDir = pluginDir; - m_PluginName = pluginname; + m_PluginName = pluginname.trim(); m_SystemDir = systemDir; initializeAntProject(); @@ -112,7 +112,12 @@ public String getProperty( String name ) { - return m_ProjectProperties.getProperty( name ); + name = name.trim(); + String value = m_ProjectProperties.getProperty( name ); + if( value == null ) + return null; + value = value.trim(); + return resolve( value ); } public Project getAntProject() @@ -128,5 +133,74 @@ public void enableBeanShellTracing( boolean on ) { Interpreter.TRACE = on; + } + + public String resolve( String value ) + { + // optimization for common case. + int pos1 = value.indexOf( "${" ); + if( pos1 < 0 ) + return value; + + Stack stack = new Stack(); + StringTokenizer st = new StringTokenizer( value, "${}", true ); + + while( st.hasMoreTokens() ) + { + String token = st.nextToken(); + if( token.equals( "}" ) ) + { + String name = (String) stack.pop(); + String open = (String) stack.pop(); + if( open.equals( "${" ) ) + { + String propValue = getProperty( name ); + if( propValue == null ) + push( stack, "${" + name + "}" ); + else + push( stack, propValue ); + } + else + { + push( stack, "${" + name + "}" ); + } + } + else + { + if( token.equals( "$" ) ) + stack.push( "$" ); + else + { + push( stack, token ); + } + } + } + String result = ""; + while( stack.size() > 0 ) + { + result = (String) stack.pop() + result; + } + return result; + } + + private void push( Stack stack , String value ) + { + if( stack.size() > 0 ) + { + String data = (String) stack.pop(); + if( data.equals( "${" ) ) + { + stack.push( data ); + stack.push( value ); + } + else + { + stack.push( data + value ); + } + } + else + { + stack.push( value ); + } } } Modified: avalon/trunk/tools/magic/engine/src/java/org/apache/merlin/magic/PluginServiceManager.java ============================================================================== --- avalon/trunk/tools/magic/engine/src/java/org/apache/merlin/magic/PluginServiceManager.java (original) +++ avalon/trunk/tools/magic/engine/src/java/org/apache/merlin/magic/PluginServiceManager.java Fri May 21 04:22:41 2004 @@ -3,21 +3,17 @@ import java.io.File; import java.io.FileInputStream; import java.io.IOException; - -import java.util.Map; import java.util.HashMap; +import java.util.Map; import java.util.Properties; import org.apache.avalon.framework.activity.Initializable; - +import org.apache.avalon.framework.context.Contextualizable; import org.apache.avalon.framework.logger.AbstractLogEnabled; import org.apache.avalon.framework.logger.LogEnabled; - -import org.apache.avalon.framework.context.Contextualizable; - -import org.apache.avalon.framework.service.Serviceable; import org.apache.avalon.framework.service.ServiceException; import org.apache.avalon.framework.service.ServiceManager; +import org.apache.avalon.framework.service.Serviceable; public class PluginServiceManager extends AbstractLogEnabled implements ServiceManager Modified: avalon/trunk/tools/magic/engine/src/java/org/apache/merlin/magic/ScriptFacade.java ============================================================================== --- avalon/trunk/tools/magic/engine/src/java/org/apache/merlin/magic/ScriptFacade.java (original) +++ avalon/trunk/tools/magic/engine/src/java/org/apache/merlin/magic/ScriptFacade.java Fri May 21 04:22:41 2004 @@ -1,19 +1,17 @@ package org.apache.merlin.magic; -import bsh.EvalError; -import bsh.Interpreter; -import bsh.BshClassManager; - import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; - -import java.util.Properties; import java.util.StringTokenizer; import org.apache.avalon.framework.logger.AbstractLogEnabled; +import bsh.BshClassManager; +import bsh.EvalError; +import bsh.Interpreter; + public class ScriptFacade extends AbstractLogEnabled implements PluginFacade @@ -66,9 +64,9 @@ { bsh.eval( m_Script ); } - String expr1 = "import org.apache.merlin.magic.Plugin; Plugin plugin = new " + classname + "();"; - System.out.println( expr1 ); - bsh.eval( expr1 ); + String expr1 = "import org.apache.merlin.magic.Plugin; Plugin plugin = new " + classname + "();"; + System.out.println( expr1 ); + bsh.eval( expr1 ); m_Plugin = (Plugin) bsh.get( "plugin" ); return m_Plugin; } Added: avalon/trunk/tools/magic/engine/src/test/org/apache/merlin/magic/PluginContextTestCase.java ============================================================================== --- (empty file) +++ avalon/trunk/tools/magic/engine/src/test/org/apache/merlin/magic/PluginContextTestCase.java Fri May 21 04:22:41 2004 @@ -0,0 +1,107 @@ +package org.apache.merlin.magic; + +import java.io.File; +import java.io.InputStream; +import java.util.Properties; + +import junit.framework.TestCase; + + +/** + * @author Niclas Hedhman, [EMAIL PROTECTED] + */ +public class PluginContextTestCase extends TestCase +{ + + private PluginContext m_Context; + private File m_PluginDir; + private File m_SystemDir; + private File m_ProjectDir; + + /* + * @see TestCase#setUp() + */ + protected void setUp() throws Exception + { + super.setUp(); + m_ProjectDir = new File( "target/projectdir"); + m_ProjectDir.mkdir(); + + Properties projectProps = new Properties(); + InputStream in = getClass().getResourceAsStream( "test.properties"); + projectProps.load( in ); + + m_PluginDir = new File( "target/plugins"); + m_PluginDir.mkdir(); + + m_SystemDir = new File( "target/system"); + m_SystemDir.mkdir(); + + m_Context = new PluginContext( " testcase project ", m_ProjectDir, projectProps, + "testcase plugin", m_PluginDir, m_SystemDir ); + m_Context.setPluginClassname( "TestCasePlugin"); + } + + public void testGetProjectName() + { + assertEquals( "Project Name failed.", "testcase project", m_Context.getProjectName()); + } + + public void testGetProjectDir() + { + assertEquals( "Project Dir failed.", m_ProjectDir, m_Context.getProjectDir()); + } + + public void testGetProjectProperties() + { + } + + public void testGetPluginName() + { + assertEquals( "Plugin Name failed.", "testcase plugin", m_Context.getPluginName()); + } + + public void testGetPluginDir() + { + assertEquals( "Plugin Dir failed.", m_PluginDir, m_Context.getPluginDir()); + } + + public void testGetSystemDir() + { + assertEquals( "System Dir failed.", m_SystemDir, m_Context.getSystemDir()); + } + + public void testGetPluginClassname() + { + assertEquals( "Plugin ClassName failed.", "TestCasePlugin", m_Context.getPluginClassname()); + } + + public void testGetProperty() + { + String p1 = "niclas${abc.def}hedhman"; + String value = m_Context.resolve( p1 ); + assertEquals( "Unresolvable failed.", p1, value ); + + p1 = "niclas ${a.property } hedhman"; + value = m_Context.resolve( p1 ); + assertEquals( "Single Level resolution failed.", "niclas has the surname of hedhman", value ); + + p1 = "${a2.this}"; + value = m_Context.resolve( p1 ); + assertEquals( "Property resolution failed.", "this is", value ); + + p1 = "Hey, ${a2.${a1}} ${a2.${a4}} ${a3}"; + value = m_Context.resolve( p1 ); + assertEquals( "Nested resolution failed.", "Hey, this is this is not this is funky", value ); + + p1 = "${${${${${${b1}}}}}}"; + value = m_Context.resolve( p1 ); + assertEquals( "Nested resolution failed.", "YEAH!!!!", value ); + } + + public void testGetAntProject() + { + assertTrue( "Ant Project failed.", m_Context.getAntProject() != null ); + assertEquals( "Ant Project failed.", "testcase project", m_Context.getAntProject().getName() ); + } +} Added: avalon/trunk/tools/magic/engine/src/test/org/apache/merlin/magic/test.properties ============================================================================== --- (empty file) +++ avalon/trunk/tools/magic/engine/src/test/org/apache/merlin/magic/test.properties Fri May 21 04:22:41 2004 @@ -0,0 +1,16 @@ + +a.property = has the surname of + +a1 = this +a2.this = ${a1} is +a3 = ${a2.${a1}} funky +a2.not = ${a1} is not +a4 = not + +b1 = b2 +b2 = b3 +b3 = b4 +b4 = b5 +b5 = b6 +b6 = YEAH!!!! + --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]