Author: niclas Date: Thu Jun 3 10:27:17 2004 New Revision: 20784 Removed: avalon/trunk/tools/magic/checksum/ Modified: avalon/trunk/tools/magic/artifact/src/dist/magic.bsh avalon/trunk/tools/magic/artifact/src/dist/magic.properties avalon/trunk/tools/magic/jar/src/dist/magic.bsh avalon/trunk/tools/magic/java/src/dist/magic.bsh avalon/trunk/tools/magic/magic.sequence avalon/trunk/tools/magic/prepare/src/dist/magic.bsh Log: Artifact and Jar uploads through scp should now be much closer to working properly.
Modified: avalon/trunk/tools/magic/artifact/src/dist/magic.bsh ============================================================================== --- avalon/trunk/tools/magic/artifact/src/dist/magic.bsh (original) +++ avalon/trunk/tools/magic/artifact/src/dist/magic.bsh Thu Jun 3 10:27:17 2004 @@ -15,142 +15,39 @@ limitations under the License. */ -import java.io.BufferedInputStream; -import java.io.BufferedOutputStream; -import java.io.BufferedReader; +import com.jcraft.jsch.ChannelExec; +import com.jcraft.jsch.Session; + import java.io.File; import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.FileReader; -import java.io.InputStream; import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; -import java.net.MalformedURLException; -import java.net.URL; -import java.net.URLClassLoader; -import java.net.URLConnection; - -import java.util.ArrayList; -import java.util.Iterator; -import java.util.Properties; +import java.util.LinkedList; +import java.util.List; import org.apache.avalon.magic.AbstractPlugin; import org.apache.avalon.magic.Artifact; -import org.apache.avalon.magic.Plugin; + +import org.apache.avalon.framework.service.Serviceable; +import org.apache.avalon.framework.service.ServiceException; +import org.apache.avalon.framework.service.ServiceManager; import org.apache.tools.ant.Project; import org.apache.tools.ant.types.Path; +import org.apache.tools.ant.taskdefs.optional.ssh.Scp; +import org.apache.tools.ant.taskdefs.optional.ssh.SSHUserInfo; public class ArtifactPlugin extends AbstractPlugin + implements Serviceable { - public Artifact resolve( String artifactId ) - throws IOException - { - File definitionsDir = new File( m_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 = m_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( artifactId, groupId, version, type, repository ); - if( getLogger().isDebugEnabled() ) - getLogger().debug( "Artifact: " + artifact ); - return artifact; - } - - - public Artifact[] getDependencies( Artifact artifact ) - throws IOException - { - File definitionsDir = new File( m_Context.getProjectSystemDir(), "dependencies" ); - File file = new File( definitionsDir, artifact.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( dep ); - } - return result; - } - + private Object m_PreparePlugin; - public ClassLoader getClassloader( Artifact[] artifacts ) - throws IOException - { - URL[] jars = getLocalURLs( artifacts ); - URLClassLoader classloader = new URLClassLoader( jars ); - return classloader; - } - - public URL[] getLocalURLs( Artifact[] artifacts ) - throws IOException + public void service( ServiceManager man ) + throws ServiceException { - URL[] jars = new URL[ artifacts.length ]; - for( int i=0 ; i < artifacts.length ; i++ ) - { - File localFile = toLocalFile( artifacts[i] ); - if( ! localFile.exists() ) - { - localFile.getParentFile().mkdirs(); - download( artifacts[i], localFile ); - } - jars[i] = localFile.toURL(); - } - return jars; + m_PreparePlugin = man.lookup( "prepare" ); } public Path getClassPath( Artifact[] artifacts ) @@ -159,19 +56,7 @@ Path result = new Path( m_Project ); for( int i=0 ; i < artifacts.length ; i++ ) { - File localFile = toLocalFile( artifacts[i] ); - if( ! localFile.exists() ) - { - if( getLogger().isDebugEnabled() ) - getLogger().debug( artifacts[i] + " does NOT exists locally." ); - localFile.getParentFile().mkdirs(); - download( artifacts[i], localFile ); - } - else - { - if( getLogger().isDebugEnabled() ) - getLogger().debug( artifacts[i] + " exists locally." ); - } + File localFile = artifacts[i].toLocalFile(); Path.PathElement pe = result.createPathElement(); pe.setLocation( localFile ); } @@ -180,75 +65,87 @@ return result; } - public void upload( Artifact artifact, File content ) - { - } - - private void download( Artifact artifact, File dest ) - throws IOException, MalformedURLException + public void install( Artifact artifact, File content ) + 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 ); - URL url = toRemoteURL( artifact ); - URLConnection conn = url.openConnection(); - conn.connect(); - in = conn.getInputStream(); - bis = new BufferedInputStream( in ); - int b; - int counter = 0; - while( ( b = bis.read() ) != -1 ) - { - counter++; - if( counter > 1000 ) - { - counter = 0; - System.out.print( "." ); - } - bos.write( b ); - } - System.out.println( "\nDownloaded: " + counter + " bytes." ); - dlFile.renameTo( dest ); - } finally - { - if( out != null ) - out.close(); - if( bos != null ) - bos.close(); - if( in != null ) - in.close(); - if( bis != null ) - bis.close(); - dlFile.delete(); + File dest = artifact.toLocalFile(); + PreparePlugin prepare = (PreparePlugin) m_PreparePlugin; + prepare.copyFile( content, dest ); + } catch( Exception e ) + { + e.printStackTrace(); + throw new IOException( "Unable to install " + artifact ); } } - 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 ) + public void upload( Artifact artifact ) + throws IOException { - String href = m_Context.getProperty( "artifact.local.repository.dir" ) + "/" + - artifact.getGroupId() + "/" + - artifact.getType() + "s/" + - artifact.getArtifactId() + "-" + - artifact.getVersion() + ".jar" ; - return new File( href ); + int port = -1; + try + { + port = Integer.parseInt( m_Context.getProperty( "artifact.remote.port" ) ); + } catch( NumberFormatException e ) + {} // ignore + + try + { + boolean trust = "true".equals( m_Context.getProperty( "artifact.remote.trust" ) ); + boolean failOnError = "true".equals( m_Context.getProperty( "artifact.remote.fail.on.error" ) ); + String host = m_Context.getProperty( "artifact.remote.host" ); + String destDir = m_Context.getProperty( "artifact.remote.dir" ); + String keyfile = m_Context.getProperty( "artifact.remote.keys.file" ); + String knownhosts = m_Context.getProperty( "artifact.remote.knownhosts.file" ); + String passphrase = m_Context.getProperty( "artifact.remote.passphrase" ); + String password = m_Context.getProperty( "artifact.remote.password" ); + String username = m_Context.getProperty( "artifact.remote.username" ); + + System.out.println( " Host:" + host ); + System.out.println( " User:" + username ); + System.out.println( " Dir:" + destDir ); + System.out.println( " KeyFile:" + keyfile ); + System.out.println( " Known:" + knownhosts ); + System.out.println( " Phrase:" + passphrase ); + System.out.println( "Password:" + password ); + + String fullDest = username + "@" + host + ":" + destDir + "/"; + + m_Project.addTaskDefinition( "scp", Scp.class ); + Scp scp = (Scp) m_Project.createTask( "scp" ); + scp.init(); + scp.setFile( artifact.toLocalFile().getAbsolutePath() ); + scp.setFailonerror( failOnError ); + scp.setHost( host ); + + if( ! "".equals( keyfile ) ) + scp.setKeyfile( keyfile ); + + if( ! "".equals( knownhosts ) ) + scp.setKnownhosts( knownhosts ); + + if( ! "".equals( password ) ) + scp.setPassword( password ); + else + scp.setPassphrase( passphrase ); + + + if( port != -1 ) + scp.setPort( port ); + + scp.setTrust( trust ); + + if( ! "".equals( username ) ) + scp.setUsername( username ); + scp.setTodir( fullDest ); + + scp.execute(); + } catch( Exception e ) + { + e.printStackTrace(); + throw new IOException( "Can't upload " + artifact ); + } } } Modified: avalon/trunk/tools/magic/artifact/src/dist/magic.properties ============================================================================== --- avalon/trunk/tools/magic/artifact/src/dist/magic.properties (original) +++ avalon/trunk/tools/magic/artifact/src/dist/magic.properties Thu Jun 3 10:27:17 2004 @@ -2,3 +2,26 @@ artifact.local.repository.dir = ${user.home}/.maven/repository artifact.repository = http://www.ibiblio.org/maven + +artifact.remote.fail.on.error = true + +artifact.remote.host = minotaur.apache.org + +# artifact.remote.dir = /www/wwww.apache.org/dist/java-repository + +artifact.remote.dir = /home/niclas/dist + +artifact.remote.port = + +artifact.remote.username = + +artifact.remote.password = + +artifact.remote.keys.file = + +artifact.remote.passphrase = + +artifact.remote.trust = true + +artifact.remote.knownhosts.file = + Modified: avalon/trunk/tools/magic/jar/src/dist/magic.bsh ============================================================================== --- avalon/trunk/tools/magic/jar/src/dist/magic.bsh (original) +++ avalon/trunk/tools/magic/jar/src/dist/magic.bsh Thu Jun 3 10:27:17 2004 @@ -18,13 +18,15 @@ import java.io.File; import java.io.IOException; -import java.util.ArrayList; -import java.util.Iterator; +import java.lang.reflect.InvocationTargetException; import org.apache.avalon.magic.AbstractPlugin; import org.apache.avalon.magic.Artifact; -import org.apache.avalon.magic.Plugin; +import org.apache.avalon.magic.ArtifactException; import org.apache.avalon.magic.PluginContext; +import org.apache.avalon.magic.Util; + +import org.apache.avalon.framework.activity.Initializable; import org.apache.avalon.framework.service.Serviceable; import org.apache.avalon.framework.service.ServiceManager; @@ -37,13 +39,16 @@ import org.apache.tools.ant.taskdefs.Jar; public class JarPlugin extends AbstractPlugin - implements Serviceable + implements Serviceable, Initializable { - private Object m_JavaPlugin; - private Object m_PreparePlugin; - private Object m_ArtifactPlugin; - private boolean m_Jarred = false; - + private Object m_JavaPlugin; + private Object m_PreparePlugin; + private Object m_ArtifactPlugin; + private boolean m_Jarred = false; + + private Artifact m_CurrentArtifact; + private File m_JarFile; + public void service( ServiceManager man ) throws ServiceException { @@ -52,21 +57,59 @@ m_ArtifactPlugin = man.lookup( "artifact" ); } + public void initialize() + throws IOException, ArtifactException + { + String projectname = m_Context.getProjectName(); + ArtifactPlugin ap = (ArtifactPlugin) m_ArtifactPlugin; + m_CurrentArtifact = Artifact.resolve( m_Context, projectname ); + } + public void jar() - throws IOException + throws IOException, ArtifactException { - if( m_Jarred ) - return; - - JavacPlugin java = (JavacPlugin) m_JavaPlugin; - java.compile(); - - notifyPreMethod( "jar" ); - File manifest = prepareManifest(); - notifyStep( "jar", "manifest-created" ); - createJar( manifest ); - notifyPostMethod( "jar" ); - m_Jarred = true; + try + { + if( m_Jarred ) + return; + + JavacPlugin java = (JavacPlugin) m_JavaPlugin; + java.compile(); + + notifyPreMethod( "jar" ); + File manifest = prepareManifest(); + notifyStep( "jar", "manifest-created" ); + createJar( manifest ); + notifyPostMethod( "jar" ); + m_Jarred = true; + } catch( Exception e ) + { + e.printStackTrace(); + throw new ArtifactException( "Unable to JAR.", e ); + } + } + + public void install() + throws IOException, ArtifactException + { + jar(); + notifyPreMethod( "install" ); + ArtifactPlugin artplugin = (ArtifactPlugin) m_ArtifactPlugin; + artplugin.install( m_CurrentArtifact, m_JarFile ); + notifyStep( "install", "plugin-installed" ); + Util.checksum( m_CurrentArtifact.toLocalFile() ); + notifyStep( "install", "checksum-created" ); + notifyPostMethod( "install" ); + } + + public void upload() + throws IOException, ArtifactException + { + install(); + notifyPreMethod( "upload" ); + ArtifactPlugin artplugin = (ArtifactPlugin) m_ArtifactPlugin; + artplugin.upload( m_CurrentArtifact ); + notifyPostMethod( "upload" ); } private File prepareManifest() @@ -97,20 +140,16 @@ private void createJar( File manifest ) throws IOException { - String projectname = m_Context.getProjectName(); - ArtifactPlugin ap = (ArtifactPlugin) m_ArtifactPlugin; - Artifact thisArtifact = ap.resolve( projectname ); - String dest = m_Context.getProperty( "jar.build.dest.dir" ); File destDir = new File( dest ); - File jarFile = new File( dest, thisArtifact.getFilename() ); + m_JarFile = new File( dest, m_CurrentArtifact.getFilename() ); destDir.mkdirs(); File fromDir = new File( m_Context.getProperty( "jar.build.src.dir" ) ); Jar jar = (Jar) m_Project.createTask( "jar" ); /* Set the destination */ - jar.setDestFile( jarFile ); + jar.setDestFile( m_JarFile ); jar.setBasedir( fromDir ); jar.init(); Modified: avalon/trunk/tools/magic/java/src/dist/magic.bsh ============================================================================== --- avalon/trunk/tools/magic/java/src/dist/magic.bsh (original) +++ avalon/trunk/tools/magic/java/src/dist/magic.bsh Thu Jun 3 10:27:17 2004 @@ -18,12 +18,9 @@ import java.io.File; import java.io.IOException; -import java.util.ArrayList; -import java.util.Iterator; - import org.apache.avalon.magic.AbstractPlugin; import org.apache.avalon.magic.Artifact; -import org.apache.avalon.magic.Plugin; +import org.apache.avalon.magic.ArtifactException; import org.apache.avalon.magic.PluginContext; import org.apache.avalon.framework.logger.Logger; @@ -53,7 +50,7 @@ } public void compile() - throws IOException + throws IOException, ArtifactException { if( m_Compiled ) return; @@ -67,7 +64,7 @@ } private void executeCompile() - throws IOException + throws IOException, ArtifactException { Logger logger = getLogger(); if( logger.isDebugEnabled() ) @@ -85,7 +82,7 @@ } public void compile( String classpath, File destDir, File srcDir ) - throws IOException + throws IOException, ArtifactException { Javac javac = (Javac) m_Project.createTask( "javac" ); /* Set the destination */ @@ -100,8 +97,8 @@ ArtifactPlugin ap = (ArtifactPlugin) m_ArtifactPlugin; String projectname = m_Context.getProjectName(); - Artifact thisArtifact = ap.resolve( projectname ); - Artifact[] deps = ap.getDependencies( thisArtifact ); + Artifact thisArtifact = Artifact.resolve( m_Context, projectname ); + Artifact[] deps = thisArtifact.getDependencies(); Path depPath = ap.getClassPath( deps ); cp.add( depPath ); Modified: avalon/trunk/tools/magic/magic.sequence ============================================================================== --- avalon/trunk/tools/magic/magic.sequence (original) +++ avalon/trunk/tools/magic/magic.sequence Thu Jun 3 10:27:17 2004 @@ -5,3 +5,4 @@ artifact plugin.install java plugin.install jar plugin.install +# test plugin.install Modified: avalon/trunk/tools/magic/prepare/src/dist/magic.bsh ============================================================================== --- avalon/trunk/tools/magic/prepare/src/dist/magic.bsh (original) +++ avalon/trunk/tools/magic/prepare/src/dist/magic.bsh Thu Jun 3 10:27:17 2004 @@ -90,6 +90,15 @@ copy.execute(); } + public void copyFile( File fromFile, File toFile ) + { + Copy copy = (Copy) m_Project.createTask( "copy" ); + copy.setTofile( toFile ); + copy.setFile( fromFile ); + copy.init(); + copy.execute(); + } + private void configureFilterSet( FilterSet fs ) { fs.setBeginToken( "@" ); --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]