geirm       01/05/07 15:47:45

  Added:       jjar/src/java/org/apache/commons/jjar ClasspathUtil.java
                        DependencyEngine.java JJAR.java JJARPackage.java
                        JJARTask.java Repository.java RepositoryProps.java
                        RepositoryXML.java Transport.java Version.java
  Log:
  initial src checkin
  
  Revision  Changes    Path
  1.1                  
jakarta-commons-sandbox/jjar/src/java/org/apache/commons/jjar/ClasspathUtil.java
  
  Index: ClasspathUtil.java
  ===================================================================
  /*
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2001 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, 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 acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Commons", 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 [EMAIL PROTECTED]
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * 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 (INCLUDING, 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.commons.jjar;
  
  import java.lang.Package;
  
  import java.util.ArrayList;
  import java.util.Iterator;
  import java.util.Collection;
  import java.util.Map;
  import java.util.List;
  import java.util.StringTokenizer;
  import java.util.Enumeration;
  
  import java.util.jar.JarFile;
  import java.util.jar.Manifest;
  import java.util.jar.Attributes;
  import java.util.jar.JarInputStream;
  import java.util.jar.JarOutputStream;
  import java.util.jar.JarEntry;
  
  import java.io.File;
  import java.io.FileOutputStream;
  import java.io.BufferedInputStream;
  import java.io.BufferedOutputStream;
  import java.io.InputStream;
  import java.io.ByteArrayOutputStream;
  
  import java.net.JarURLConnection;
  import java.net.URLConnection;
  import java.net.URL;
  
  
  /**
   *  classpath support for JJAR.  This is currently a jumble - will
   *  clean up.
   *
   *  @author <a href="mailto:[EMAIL PROTECTED]";>Geir Magnusson Jr.</a>
   *  @version $Id: ClasspathUtil.java,v 1.1 2001/05/07 22:47:44 geirm Exp $ 
   */
  public class ClasspathUtil
  {
      /**
       *  current default repository
       */
  
      public static String getDefaultRepository()
      {
          return "http://207.138.188.222/jjar/";;
      }
  
      /**
       *  lists the jars currently in the classpath
       */
      public static void listJars( String path )
      {
         List jars = getJarList( path );
  
         Iterator i = jars.iterator();
         
         while( i.hasNext() )
         {
             String jar =  (String) i.next();
             System.out.println(" inspecting :  " + jar );
         }
  
         return;
      }
      
      /**
       *  checks to see if the classpath <code>path</code> contains
       *  the package / version
       */
      public static boolean containsVersionedJar( String path, String pkg, String 
version)
      {
          List jars = getJarList( path );
  
         /*
          *  now, with this set of jars, see if we can find those that match
          *  the package name
          */
  
          Iterator i = jars.iterator();
   
          while( i.hasNext() )
          {
              String jar =  (String) i.next();
  
              try
              {
                  URL url = new URL("jar:file:" + jar + "!/");
                  JarURLConnection conn  = (JarURLConnection) url.openConnection(); 
  
                  if (packageMatch( conn, pkg, version ) )
                  {
                      // System.out.println( jar + " : pkg = " + pkg + " ver = " + 
version );
  
                      return true;
                  }
              }
              catch( Exception e )
              {
                  //System.out.println("Exception (continuing) : " + e );
              }
          }
          
          return false;
      }
  
      /**
       *  returns a JJARPackage( info ) for a given jar
       */
      public static JJARPackage getPackage( String jarname )
          throws Exception
      {
          URL url = new URL("jar:file:" + jarname + "!/");
          JarURLConnection conn  = (JarURLConnection) url.openConnection(); 
          
          return getPackage( conn );
      }
  
      public static JJARPackage getPackage( JarURLConnection jarConn )
          throws Exception
      {
          if (jarConn == null)
              return null;
  
          Manifest mfst = jarConn.getManifest();
          
          if (mfst == null)
          {
              return null;
          }
          
          /*
           * get the attributes we care about
           */
  
          Attributes att = mfst.getMainAttributes();
  
          String jarpkg = att.getValue( Attributes.Name.IMPLEMENTATION_TITLE );
          String jarver = att.getValue( Attributes.Name.IMPLEMENTATION_VERSION );
  
          if (jarpkg == null || jarver == null)
          {
              /*
               * unusable
               */
              
              return null;
          }
          
          return new JJARPackage( jarpkg, jarver );
      }
         
      /**
       *  determines if two packages are equal
       */
      public static boolean packageMatch( JarURLConnection jarConn, String pkg, String 
version )
          throws Exception
      {
          JJARPackage jjarp = getPackage( jarConn );
  
          if ( jjarp == null)
              return false;
  
          /*
           *  compare
           */
          
          return jjarp.equals( pkg, version );
      } 
  
  
      /**
       *  returns a list of jars in the specified claspath.
       *  will check the system classpath by default
       */
      public static List getJarList( String path )
      {
          if (path == null)
          {
              path = "java.class.path";
          }
  
          String classpath = System.getProperty( path );
          String pathSep = System.getProperty("path.separator");
  
          /*
           *  take the classpath, and look for jars
           */
  
          StringTokenizer st = new StringTokenizer( classpath, pathSep );
  
          ArrayList jars = new ArrayList();
  
          while( st.hasMoreTokens() )
          {
              String foo = st.nextToken();
              
              if ( foo.indexOf("jar") != -1 )
                  jars.add(foo);
          }
  
          return jars;
      }    
  
      public static void main( String args[] )
      {
          /*
           *  see if the default classpath has the jar we need
           */
          if ( ClasspathUtil.containsVersionedJar( null, "commons-beanutils", "0.1") )
              System.out.println("Found commons-beanutils v0.1");
          else
          {
              /*
               * didn't find it...
               */
  
              try
              {
                  /*
                   *  so go to the repository
                   */
  
                  URL url = new 
URL("jar:file:/home/gmj/jakarta/jakarta-commons/beanutils/dist/commons-beanutils.jar!/");
                  JarURLConnection conn  = (JarURLConnection) url.openConnection(); 
  
                  /*
                   * ensure that it's right
                   */
  
                  if (!packageMatch( conn, "commons-beanutils", "0.1" ))
                  {
                      System.out.println("repository doesn't have it");
                      return;
                  }
  
                  System.out.println("jar in repository correct. Fetching");
  
                  String jar = "commons-beanutils-0.1.jar";
  
                  url = new 
URL("file:/home/gmj/jakarta/jakarta-commons/beanutils/dist/commons-beanutils.jar");
                  URLConnection uconn  = (URLConnection) url.openConnection(); 
  
                  Transport.fetchJar( uconn, jar );
  
                  /*
                   *  add to classpath and verify
                   */
                  
                  String classpath = System.getProperty( "java.class.path" );
                  String pathSep = System.getProperty("path.separator");
  
                  classpath = classpath + pathSep + jar;
  
                  System.setProperty("java.class.path", classpath);
  
                  if ( ClasspathUtil.containsVersionedJar( null, "commons-beanutils", 
"0.1") )
                      System.out.println("Found commons-beanutils v0.1");
                  
              }
              catch( Exception e )
              {
                  System.out.println("exception " + e );
              }
          }
          
      }
  
      /**
       *  doesn't work.  don't use
       */
      public static boolean markJar( String jarname, String packagename, String 
version )
          throws Exception
      {
          JarFile jf = new JarFile( jarname );
  
          Manifest mfst = jf.getManifest();
          
          if (mfst == null)
          {
              return false;
          }
          
          /*
           * set the attributes we care about
           */
  
          Attributes att = mfst.getMainAttributes();
  
          Object a = att.put( Attributes.Name.IMPLEMENTATION_TITLE, packagename );
          Object b = att.put( Attributes.Name.IMPLEMENTATION_VERSION, version );
  
          System.out.println(" Object a " + a );
          System.out.println(" Object b " + b );
  
          JarOutputStream jos = new JarOutputStream( new FileOutputStream( new File( 
jarname + "-jjar")));
          byte[] buffer = new byte[1024]; 
          int bytesRead; 
  
          for( Enumeration e = jf.entries(); e.hasMoreElements(); )
          {
              JarEntry entry = (JarEntry) e.nextElement(); 
  
              if( entry.getName().equals("META-INF/MANIFEST.MF"))
              {
                  ByteArrayOutputStream baos = new ByteArrayOutputStream();
  
                  mfst.write( baos );
  
                  JarEntry ee = new JarEntry( entry );
  
                  ee.setSize(baos.size() );
  
                  jos.putNextEntry(ee );
                  mfst.write( jos );
                  continue;
              }
  
              System.out.println(" Name : " + entry.getName() );
  
              InputStream entryStream = jf.getInputStream( entry); 
  
              jos.putNextEntry(entry );
  
              while ((bytesRead = entryStream.read(buffer)) != -1) 
              { 
                  jos.write(buffer, 0, bytesRead);
              }
          }
  
          jos.close();
          jf.close();
  
          return true;
      }   
  }
  
  
  
  
  
  
  
  
  
  
  
  1.1                  
jakarta-commons-sandbox/jjar/src/java/org/apache/commons/jjar/DependencyEngine.java
  
  Index: DependencyEngine.java
  ===================================================================
  /*
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2001 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, 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 acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Commons", 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 [EMAIL PROTECTED]
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * 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 (INCLUDING, 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.commons.jjar;
  
  import java.util.HashMap;
  import java.util.List;
  import java.util.Iterator;
  import java.util.ArrayList;
  
  /**
   *  <p>
   *  Simple class to figure out ordered dependency lists.  Basic
   *  idea is that you load it with datum consisting of a set 
   *  consisting of  a package name and list of packages that 
   *  it's dependent upon.
   *  </p>
   *
   *  <p>
   *  Then, you should be able to ask for the dependencies for any
   *  package placed in there.
   *  </p>
   *
   *  <p> will detect loops at 'runtime', not loadtime.  Just punts
   *  when that happens
   *  </p>
   *
   *  @author <a href="mailto:[EMAIL PROTECTED]";>Geir Magnusson Jr.</a>
   *  @version $Id: DependencyEngine.java,v 1.1 2001/05/07 22:47:45 geirm Exp $ 
   */
  public class DependencyEngine
  {    
      private HashMap projects = new HashMap();
      private ArrayList buildList = null;
  
      /**
       *  CTOR 
       */
      public DependencyEngine()
      {
      }
  
      /**
       *  returns a list of dependencies for a given package
       */
      public List getDependencies( String pkg )
      {
          buildList = new ArrayList();
  
          try
          {
              doIt( pkg );
          }
          catch( Exception e )
          {
              System.out.println("DE.getDependencies() : " + pkg + " : "  + e);
          }
  
          if( buildList.size() > 0)
              buildList.remove( buildList.size() - 1 );
          return buildList;
      }
  
      /**
       *  from previous use - generates a dependency list
       *  spanning the entire tree.  Returns a list
       *  of names.
       */
      public List generateNamelist()
          throws Exception
      {
          /*
           *  get the project list
           */
  
          buildList = new ArrayList();
  
          Iterator i = projects.keySet().iterator();
  
          while(i.hasNext())
          {
              String s = (String) i.next();
  
              /*
               *  make them by name
               */
  
              doIt( s );
          }
  
          return buildList;
      }
  
      /**
       *  from previous use - generates a dependency list
       *  spanning the entire tree.  Returns a list
       *  of cookies.
       */
      public List generateCookielist()
          throws Exception
      {
          /*
           *  get the project list
           */
  
          List list = generateNamelist();
          ArrayList cookies = new ArrayList();
  
          Iterator i = list.iterator();
  
          while(i.hasNext())
          {
              String s = (String) i.next();
              Node n = (Node) projects.get( s );
  
              cookies.add( n.getCookie() );
          }
          
          return cookies;
      }
  
      /**
       *  The recursive worker...
       */
      void doIt( String current )
          throws Exception
      {
          Node project = (Node) projects.get(current);
  
          if (project == null)
          {
              /*
               *  we may have a dependency that isn't a project.  
               *  so what... (This shouldn't happen)
               */
  
              buildList.add( current );
              return;
          }
  
          /*
           *  check status of this one
           */
  
          int status = project.getStatus();
  
          if ( status == Node.WORKING )
          {
             throw new Exception("Detected loop while trying to build " + current);
          }
          else if ( status == Node.ZILCH )
          {
              /*
               *   not working - so mark as working and start on the dependencies
               */
              project.setStatus( Node.WORKING );
  
              /*
               *  do we have any dependencies?
               */
              Iterator deps =  project.getDeps();
  
              /*
               *  if so, work on each
               */
  
              while( deps.hasNext() )
              {
                  String dep = (String) deps.next();
                  Node depnode = (Node) projects.get( dep );
  
                  if (depnode == null)
                  {
                      /*
                       *  we don't have this as a project, so 
                       *  let the client try to build it...
                       */
                      
                      // System.out.println("Adding non-project dep  build list : " + 
current );
  
                      buildList.add( dep );
                      continue;
                  }
  
                  /*
                   * now, look at the status of this dependency
                   */
  
                  int depstatus = depnode.getStatus();
  
                  if ( depstatus == Node.WORKING )
                  {
                      /*
                       *  gaak. loop!
                       */
                      throw new Exception("LOOP : checking dep " + dep + " for current 
= " + current );
                  }
                  else if (  depstatus == Node.ZILCH )
                  {
                      //                    System.out.println(" trying to build " + 
current + " : need to build dep " + dep );
                      
                      /*
                       *  recurse
                       */
  
                      doIt( dep );
                  }
                  else if(  depstatus == Node.DONE ) 
                  {
                      // can skip
                  }
              }
              
              /*
               *  if all clear, can build and mark as done.  We don't care
               *  if the client couldn't do it for now.  That may change.
               *  the client can tell
               */
              
              //System.out.println("Adding to build list : " + current );
  
              buildList.add( current );
              project.setStatus( Node.DONE );
              
              return;
          }
  
          /*
           *   node is done
           */
  
          return; 
      }
  
      /**
       *  Adds a project and it's associated dependencies.  The dependencies
       *  currently do not have to be projects themselves.
       *
       *  @param project  Name of project to add
       *  @param dependencies  java.util.List of project dependencies
       *  @throws Exception in the even that it already has the project in the list
       */
      public void addProject( String project, List dependencies, Object cookie )
          throws Exception
      {
          /*
           *  first, see if we have it
           */
          Node n = (Node) projects.get( project );
  
          if (n != null)
          {
              System.out.println(" addProject() : rejecting duplicate : " + project );
              throw new Exception("already have it...");
          }
  
          // System.out.println(" addProject() : adding project : " + project );
  
          /*
           *  make a new one and add the dependencies
           */
          n = new Node( project, cookie );
  
          Iterator i = dependencies.iterator();
  
          while( i.hasNext() )
          {
              String dep = (String) i.next();
  
              if ( dep.equals( project ) )
              {
                  // System.out.println(" addProject() : rejecting self- dependency : 
" + project );
              }
              else
              {
                  // System.out.println(" addProject() :  adding dependency : " + dep 
+ " for project : "  + project );
                  n.addDep(  dep  );
              }
          }
  
          /*
           * add to the pile
           */
  
          projects.put( project, n );
  
          return;
      }
  
      /**
       *  little demonstration test
       */
      public static void main(String arg[] )
      {
          /*
           *  tests the engine with
           *   A  B  C
           *   ^  ^  ^\ 
           *    \/ \/  |
           *    D <-E  |
           *    ^      /
           *     \    /
           *      F <-
           */
  
          try
          {
              DependencyEngine d = new DependencyEngine();
             
              ArrayList deps = null;
          
              /*
               *  A : no dependencies 
               */
              deps = new ArrayList();
              d.addProject( "A", deps, "A" );
              
              /*
               *  B : no dependencies
               */
              deps = new ArrayList();
              d.addProject("B", deps, "B" );
              
              /*
               *   C -> F
               */
              deps = new ArrayList();
              deps.add("F");
              d.addProject("C", deps, "C");
              
              /*
               *  D -> A, B
               */
              deps = new ArrayList();
              deps.add("A");
              deps.add("B");
              d.addProject("D", deps, "D");
              
              /*
               *  E -> B, C, D
               */
              deps = new ArrayList();
              deps.add("B");
              deps.add("C");
              deps.add("D");
              d.addProject("E", deps, "E" );
  
              /*
               *   F -> D
               */
              deps = new ArrayList();
              deps.add("D");
              d.addProject("F", deps, "F" );
  
              /*
               *  generate the list
               */
              List l = d.generateCookielist();
  
              /*
               *  show us
               */
  
              Iterator i = l.iterator();
  
              while(i.hasNext())
              {
                  String s = (String) i.next();
  
                  //System.out.println("Building : " + s );
              }
  
          }
          catch( Exception e )
          {
              System.out.println("main : "  + e );
          }
      }
  }
  
  class Node
  {
      public static  int ZILCH = 0;
      public static  int WORKING = 1;
      public static  int DONE = 2;
  
      private int status = ZILCH;
      private ArrayList deps = new ArrayList();
      private String name = "";
      private Object cookie = null;
  
      public Node( String name, Object cookie)
      {
          this.name = name;
          this.cookie = cookie;
      }
      
      public Object getCookie()
      {
          return cookie;
      }
  
      public void addDep( String dep )
      {
          deps.add( dep );
      }
      
      public Iterator getDeps()
      {
          return deps.iterator();
      }
      
      public void setStatus( int i )
      {
          status = i;
      }
      
      public int getStatus()
      {
          return status;
      }
  }
  
  
  
  
  
  1.1                  
jakarta-commons-sandbox/jjar/src/java/org/apache/commons/jjar/JJAR.java
  
  Index: JJAR.java
  ===================================================================
  /*
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2001 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, 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 acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Commons", 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 [EMAIL PROTECTED]
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * 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 (INCLUDING, 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.commons.jjar;
  
  import java.lang.reflect.Method;
  import java.util.List;
  import java.util.Iterator;
  import java.net.URLConnection;
  import java.net.URL;
  
  /**
   *  JJAR command line program
   *
   *  @author <a href="mailto:[EMAIL PROTECTED]";>Geir Magnusson Jr.</a>
   *  @version $Id: JJAR.java,v 1.1 2001/05/07 22:47:45 geirm Exp $
   */
  public class JJAR
  {
      public static String title = "JJAR : Jakarta Jar Archive Respository v0.1";
      public static String defaultRepository = ClasspathUtil.getDefaultRepository();
  
      public static void main( String args[])
      {
          /*
           *  first thing is the verb
           */
  
          if (args.length < 1 )
          {
              System.out.println(title);
              return;
          }
  
          String verb = args[0];
          Class [] ca = {args.getClass() };
          Object [] arg = { args };
  
          try 
          {
              Method m = Class.forName("org.apache.commons.jjar.JJAR").getMethod(verb, 
ca );
  
              m.invoke( null, arg );
          }
          catch( NoSuchMethodException nsme )
          {
              System.out.println("JJAR : invalid action " + verb );
              JJAR.help(  args );
          }
          catch( Exception e )
          {
              System.out.println("JJAR : exception : " + e );
          }
      }
  
      public static void help( String args[] )
      {
          header();
          
          System.out.println("list : list the JJAR packages in the repository");
          System.out.println("fetch : fetch a JJAR package from the repository");
          System.out.println("See the README.txt for more info.");
      }
  
      public static void fetch( String args[] )
      {
          boolean classpath = false;
          String jarname = null;
          String packagename = null;
          String version = null;
          boolean verifyignore = false;
          boolean getdeps = true;
  
          String dir = ".";
  
          if ( args.length < 3 )
          {
              System.out.println("Usage :  fetch [-v] -j jarname -p package -v version 
");
              return;
          }
  
          for( int i = 1; i < args.length; i++)
          {
              String arg = args[i];
  
              if (arg.startsWith("-"))
              {
                  if (arg.equals("-j"))
                  {
                      jarname = args[i+1];
                      i++;
                  }
                  else if (arg.equals("-p"))
                  {
                      packagename = args[i+1];
                      i++;
                  }
                  else if (arg.equals("-v"))
                  {
                      version = args[i+1];
                      i++;
                  }
                  else if (arg.equals("-c"))
                  {
                      classpath = true;
                  }
                  else if (arg.equals("-vi"))
                  {
                      verifyignore = true;
                  }
                  else if( arg.equals("-d"))
                  {
                      dir = args[i+1];
                      i++;
                  }
                  else if( arg.equals("-nd"))
                  {
                      getdeps = false;
                  }
                  
              }
          }
  
          if ( packagename == null)
          {
              System.out.println("Usage :  fetch -j jarname -p package -v version ");
              return;
          }
  
        
          /*
           * now go get
           */
          
          try
          {
              /*
               *  get the repository info
               */
  
              Repository rep = new RepositoryXML();
              rep.load( new URL( defaultRepository + "repository.xml") );
              /*
               *  get the fetch target
               */
  
              if( version == null)
                  version = rep.getPackageDefaultVersion( packagename );
  
              System.out.println("Fetching " + packagename + " v" + version + " into 
file " + (jarname == null? "<default name>" : jarname ) );
   
              String fetchTarget = rep.getFetchTarget( packagename, version );
  
              if (fetchTarget == null)
              {
                  System.out.println("JJAR.fetch : target not found for " + 
packagename + " v" + version );
                  return;
              }
  
              /*
               *  are there any dependencies for this fetch target?
               */
  
              String deptarget = "";
  
              if (getdeps)
              {
                  List deplist = rep.getDependencyList( packagename, version );
                  
                  Iterator ii = deplist.iterator();
                  
                  while(ii.hasNext())
                  {
                      /*
                       *  for now, use the fetchtarget names
                       */
                      
                      String depnameuni = (String) ii.next();
                      
                      JJARPackage jjarp = new JJARPackage( depnameuni );
                      
                      String depname = jjarp.getName();
                      String depver = jjarp.getVersionString();
                      
                      deptarget = rep.getFetchTarget( depname, depver );
                      
                      if( deptarget == null)
                      {
                          System.out.println("  Repository error : returned null for 
dependency " + depname + " v" + depver 
                                             + " Skipping.");
                          continue;
                      }
                      
                      /*
                       *  check to see if exists if verifyignore
                       */
  
    
                      if (verifyignore )
                      {
                          JJARPackage jpak = null;
                          
                          try
                          {
                              jpak = ClasspathUtil.getPackage( dir + "/" + deptarget );
                          }
                          catch(Exception e)
                          {
                              // ignore 
                          }
  
                          if (jpak != null)
                          {
                              if( jpak.equals( depname, depver ))
                              {
                                  System.out.println(" Dependency '" + depname + "' 
exists.  Skipping.");
                                  continue;
                              }
                          }
                      }
                       
                      /*
                       *  now get it
                       */
  
                      URL url = new URL( defaultRepository + deptarget );
                      URLConnection uconn  = (URLConnection) url.openConnection(); 
                      
                      System.out.println(" Fetching dependency : " + deptarget + " to 
" + dir + "/" + deptarget  );
  
                      Transport.fetchJar( uconn, dir + "/" + deptarget );              
  
                  }
              }
  
              /*
               *  if verifyignore then check the jar to see if it satisfies the request
               */
              
              if (jarname == null)
                  jarname = fetchTarget;
  
              if( verifyignore)
              {
                  try
                  {
                      JJARPackage packageinfo = ClasspathUtil.getPackage( dir + "/" + 
jarname );
  
                      if( packageinfo.equals( packagename, version ))
                      {
                          System.out.println("Target exists.  skipping fetch");
                          return;
                      }
                  }
                  catch(Exception e)
                  {
                      // ignore and continue
                  }
              }
  
              URL url = new URL( defaultRepository + fetchTarget );
              URLConnection uconn  = (URLConnection) url.openConnection(); 
  
              String destjar = dir + "/" + jarname;
  
              System.out.println(" Fetching target : " + fetchTarget + " to " + 
destjar );
  
              Transport.fetchJar( uconn, destjar);
          }
          catch( Exception e )
          {
              System.out.println("JJAR.fetch : exception " + e );
              e.printStackTrace();
          } 
       }
  
      public static void verify( String args[] )
      {
          header();
  
          /*
           *  do we do it to classpath, or was a jar specified
           */
  
          String flag = null;
          String jarname = null;
  
          if (args.length > 1 )
          {
              flag = args[1];
  
              if (flag.equals("-j"))
              {
                  if (args.length >= 3)
                  {
                      jarname = args[2];
      
                      try 
                      {
                          JJARPackage jjarp = ClasspathUtil.getPackage( jarname );
                          
                          if (jjarp != null)
                              System.out.println("    " + jjarp.getName() + " v" + 
jjarp.getVersionString() );
                          else
                              System.out.println("    " + jarname + "not a jjar 
compatable jar");
                      }
                      catch( Exception e )
                      {
                      }
                  }
              }
              else
              {
                  /*
                   * do the classpath
                   */
                  List jars = ClasspathUtil.getJarList( null );
                  
                  Iterator i = jars.iterator();
                  
                  while( i.hasNext() )
                  {
                      String jar =  (String) i.next();
                      
                      try 
                      {
                          JJARPackage jjarp = ClasspathUtil.getPackage( jar );
                          
                          if (jjarp != null)
                              System.out.println("    " + jjarp.getName() + " v" + 
jjarp.getVersionString() );
                          else
                              System.out.println("    " + jar + "not a jjar compatable 
jar");
                      }
                      catch( Exception e )
                      {
                      }
                  }
              }
          }
          else
          {
              System.out.println(" usage : verify -c | -j jarname");
          }
          return;
      }
  
      public static void list( String args[] ) 
      {
          header();
  
          try
          {
              URL url = new URL(defaultRepository + "repository.xml");
              
              Repository rep = new RepositoryXML();
              rep.load(url);
  
              if (args.length >= 3 )
              {
                  String flag = args[1];
  
                  if (flag.equals("-p"))
                  {
                      String pkg = args[2];
                  
                      dumpInfo( rep, pkg );
                  }
              }
              else
              {
                  System.out.println("Repository contains " + rep.getPackageCount() + 
" packages : " );
  
                  Iterator i = rep.getPackageListIter();
             
                  while( i.hasNext() )
                  {
                      String p = (String) i.next();
                      dumpInfo( rep, p );
                      System.out.println("");
                  }
              }
          }
          catch( Exception e )
          {
              System.out.println("JJAR.list : exception " + e );
              e.printStackTrace();
          } 
      }
  
      private static void dumpInfo(Repository rep, String p )
      {
          System.out.println(" " + p );
          System.out.println("    desc     : " + rep.getPackageDescription( p ) );
          System.out.println("    default  : " + rep.getPackageDefaultVersion( p ));
          System.out.println("    versions : ");
          
          List l = rep.getPackageVersionList( p );
          
          Iterator ii = l.iterator();
          
          while( ii.hasNext() )
          {
              String version = (String) ii.next() ;
  
              System.out.print(  "      " + version + " deps : "  );
              
              List depl = rep.getDependencyList( p, version );
              
              Iterator iii = depl.iterator();
              
              while( iii.hasNext() )
              {
                  System.out.print( iii.next() + ", " );
              }   
              
              System.out.println("");
          }
               
         System.out.println("--");
        
      }
  
      public static void mark( String args[] )
      {
          //   mark -j jarname -p package -v version
  
          String packagename = null;
          String jarname = null;
          String version = null;
  
          if ( args.length < 7 )
          {
              System.out.println("Usage : mark  -j jarname -p package -v version ");
              return;
          }
  
          for( int i = 1; i < args.length; i++)
          {
              String arg = args[i];
  
              if (arg.startsWith("-"))
              {
                  if (arg.equals("-j"))
                  {
                      jarname = args[i+1];
                      i++;
                  }
                  else if (arg.equals("-p"))
                  {
                      packagename = args[i+1];
                      i++;
                  }
                  else if (arg.equals("-v"))
                  {
                      version = args[i+1];
                      i++;
                  }
                  
              }
          }
  
          if ( packagename == null || jarname == null || version == null)
          {
              System.out.println("Usage :  mark -j jarname -p package -v version ");
              return;
          }
          
          try
          {
              if (ClasspathUtil.markJar( jarname, packagename, version ))
                  System.out.println("successful");
  
          }
          catch( Exception e)
          {
              System.out.println("Exception e : " + e );
              }
          
      }
  
      private static void header()
      {
          System.out.println(title);
          System.out.println("===================================");
      }
  }
  
  
  
  1.1                  
jakarta-commons-sandbox/jjar/src/java/org/apache/commons/jjar/JJARPackage.java
  
  Index: JJARPackage.java
  ===================================================================
  /*
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2001 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, 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 acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Commons", 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 [EMAIL PROTECTED]
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * 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 (INCLUDING, 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.commons.jjar;
  
  import java.util.HashMap;
  
  /**
   *  little class to handle the
   *  package/version duple.
   *  
   *  the intent was to extend to carry more 
   *  information, but the repository seems to
   *  be doing ok for that.
   *
   *  It might be nice to continue here though
   *  and make the repository surface area smaller
   * 
   *  @author <a href="mailto:[EMAIL PROTECTED]";>Geir Magnusson Jr.</a>
   *  @version $Id: JJARPackage.java,v 1.1 2001/05/07 22:47:45 geirm Exp $
   */
  public class JJARPackage 
  {
      private String name;
      private Version version;
      private HashMap properties;
      
      private static String DESC = "desc";
      private static String JAR = "jar";
      private static String HREF = "href";
     
      /**
       *  usual CTOR
       */
      public JJARPackage( String name, String version )
      {
          this.name = name;
          this.version = new Version(version);
      }
  
      /**
       *  ctor for <package>:<version> unispec
       */
      public JJARPackage( String onepiece )
      {
          int i = onepiece.indexOf(":");
          
          if (i != -1 )
          {
              this.name = onepiece.substring(0, i );
              this.version = new Version( onepiece.substring( i + 1 ) );
          }
      }
  
      public String getName() 
      {
          return name;
      }
      
      public Version getVersion()
      {
          return version;
      }
      
      public String getVersionString()
      {
          return version.toString();
      }
  
      public boolean equals( JJARPackage jpack )
      {
          return equals( jpack.getName(), jpack.getVersionString() );
      }
    
      public boolean equals( String pkg, String ver )
      {
          if ( pkg != null && this.name.equals( pkg ) )
          {
              // do something better here ?
  
              if ( ver != null && version.equals( ver ) )
              {
                  return true;
              }
          }
                  
          return false;
      }
  }  
  
  
  
  
  
  
  1.1                  
jakarta-commons-sandbox/jjar/src/java/org/apache/commons/jjar/JJARTask.java
  
  Index: JJARTask.java
  ===================================================================
  /*
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2001 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, 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 acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Commons", 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 [EMAIL PROTECTED]
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * 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 (INCLUDING, 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.commons.jjar;
  
  import java.net.URL;
  import java.net.URLConnection;
  import java.util.List;
  import java.util.Iterator;
  
  import org.apache.tools.ant.BuildException;
  import org.apache.tools.ant.Task;
  import org.apache.tools.ant.types.Path;
  
  /**
   * An ant task for doing JJAR stuff
   *
   * @version $Id: JJARTask.java,v 1.1 2001/05/07 22:47:45 geirm Exp $
   */
  public class JJARTask extends Task
  {
      private static String defaultRepository = ClasspathUtil.getDefaultRepository();
      private String pkg = null;
      private String ver = null;
      private String localrep = ".";
      private boolean verifyignore = true;
      private String classpath = null;
  
      /**
       *  [REQUIRED] sets the package and version.  ver can be 
       *  null to get the default
       */
      public void setPackage( String pkg)
      {
          this.pkg = pkg;
      }
  
      public void setVersion( String version )
      {
          this.ver = version;
      }
  
      public void setLocalrepository( String localrep )
      {
          this.localrep = localrep;
      }
  
      public void setVerifyignore( boolean b )
      {
          this.verifyignore = b;
      }
  
      public void setRepository( String rep )
      {
          this.defaultRepository = rep;
      }
  
      public void setClasspath( String classpath )
      {
          this.classpath = classpath;
      }
  
      /**
       * Execute the input script with Velocity
       *
       * @throws BuildException  
       * BuildExceptions are thrown when required attributes are missing.
       * Exceptions thrown by Velocity are rethrown as BuildExceptions.
       */
      public void execute () 
          throws BuildException
      {
          /*
           *  make sure that we at least have a package name
           */
          if( pkg == null)
              throw new BuildException("Need to specify a package to fetch");
      
          /*
           *  get the repository
           */
          Repository repository = null;
  
          try
          {
              repository = new RepositoryXML();
              repository.load(  new URL( defaultRepository + "repository.xml") );
              
          }
          catch( Exception e )
          {
              throw new BuildException("Problem getting repository info", e );
          }
  
          log("Fetching package '" + pkg + "' from repository '" + defaultRepository 
              + "' into local repository '" + localrep + "'");
  
          try
          {
              /*
               *  do it : first, see if there is a dependency list
               */
  
              if( ver == null)
                  ver = repository.getPackageDefaultVersion( pkg );
  
              List deplist = repository.getDependencyList( pkg, ver );
              
              Iterator ii = deplist.iterator();
  
              while(ii.hasNext())
              {
                  /*
                   *  for now, use the fetchtarget names
                   */
  
                  String depnameuni = (String) ii.next();
  
                  JJARPackage jjarp = new JJARPackage( depnameuni );
  
                  String depname = jjarp.getName();
                  String depver = jjarp.getVersionString();
                  String deptarget = repository.getFetchTarget( depname, depver );
                           
                  /*
                   *  see if the dependent package is already in the classpath
                   */
                  
                  if ( verifyignore && ClasspathUtil.containsVersionedJar( classpath, 
depname, depver))
                  {
                      log("  Dependency '" + depname + "' already in classpath - 
skipping");
                      continue;
                  }
                  
                  /*
                   *  now get it
                   */
                  
                  URL url = new URL( defaultRepository + deptarget );
                  URLConnection uconn  = (URLConnection) url.openConnection(); 
                  
                  log("  Fetching dependency : " + deptarget );
                  
                  Transport.fetchJar( uconn, localrep + "/" + deptarget );             
   
              }
              
              /*
               *  now get the actual package
               */
  
              /*
               *  if we don't have a version, we need to get it from the repository
               */
  
              if (ver == null)
              {
                  ver = repository.getPackageDefaultVersion( pkg );
              }
  
              if( verifyignore && ClasspathUtil.containsVersionedJar( classpath, pkg, 
ver))
              {                
                  log("  Skipping fetch of '" + pkg + "' - already exists in 
classpath");
                  // ignore and continue
              }
              else
              {
                  String fetchTarget = repository.getFetchTarget( pkg, ver );
  
                  URL url = new URL( defaultRepository + fetchTarget );
                  URLConnection uconn  = (URLConnection) url.openConnection(); 
  
                  log("  Fetching package : " + pkg );
                  
                  Transport.fetchJar( uconn, localrep + "/" + fetchTarget );
              }
          }
          catch( Exception e )
          {
              throw new BuildException("Exception : ",e);
          }
      }
  
     
  }
  
  
  
  1.1                  
jakarta-commons-sandbox/jjar/src/java/org/apache/commons/jjar/Repository.java
  
  Index: Repository.java
  ===================================================================
  /*
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2001 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, 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 acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Commons", 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 [EMAIL PROTECTED]
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * 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 (INCLUDING, 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.commons.jjar;
  
  import java.util.Properties;
  import java.util.StringTokenizer;
  import java.util.ArrayList;
  import java.util.Iterator;
  import java.util.List;
  
  import java.io.InputStream;
  
  import java.net.URL;
  import java.net.URLConnection;
  
  /**
   *  first cut at a repository interface
   *
   *  @author <a href="mailto:[EMAIL PROTECTED]";>Geir Magnusson Jr.</a>
   *  @version $Id: Repository.java,v 1.1 2001/05/07 22:47:45 geirm Exp $
   */
  public interface Repository
  {
      public void load( URL url );
     
      public List getDependencyList( String pkg, String version );
     
      public Iterator getPackageListIter();
  
      public int getPackageCount();
   
      public String getPackageDefaultVersion( String pkg );
    
      public String getPackageDescription( String pkg );
     
      public String getFetchTarget( String pkg, String version );
  
      public boolean isPackage( String pkg );
  
      public List getPackageVersionList( String pkg );
  }
  
  
  
  
  
  
  1.1                  
jakarta-commons-sandbox/jjar/src/java/org/apache/commons/jjar/RepositoryProps.java
  
  Index: RepositoryProps.java
  ===================================================================
  /*
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2001 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, 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 acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Commons", 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 [EMAIL PROTECTED]
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * 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 (INCLUDING, 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.commons.jjar;
  
  import java.util.Properties;
  import java.util.StringTokenizer;
  import java.util.ArrayList;
  import java.util.Iterator;
  import java.util.List;
  
  import java.io.InputStream;
  
  import java.net.URL;
  import java.net.URLConnection;
  
  public class RepositoryProps extends Properties implements Repository
  {
      private List packageList = null;
      private DependencyEngine de = new DependencyEngine();
  
      public void load( URL url )
      {
          try
          {
              URLConnection conn = url.openConnection();
              InputStream is = conn.getInputStream();
              
              load(is);
              
              is.close();
          }
          catch(Exception e )
              {
              }
  
          /*
           *  get the list of packages
           */
  
          String packages = getProperty("packages");
          
          packages.trim();
  
          packageList = makeList( packages );
  
          /*
           * setup the dependency engine
           */
  
          Iterator i = packageList.iterator();
  
          while( i.hasNext() )
          {
              /*
               * for each package
               */
  
              String p = (String) i.next();
  
              //            System.out.println(" processing " + p );
  
              /*
               *  get the dependency list
               */
  
              String depstr = getProperty( p + ".dependencies");
  
              depstr.trim();
  
              List dep = makeList( depstr );
              
              /*
               * now, remove an <none> marks
               */
  
              Iterator ii = dep.iterator();
  
              List deplist = new ArrayList();
  
              while( ii.hasNext() )
              {
                  String sdep = (String) ii.next();
  
                  if ( !sdep.equals("<none>"))
                      deplist.add( sdep );
              }
  
              try
              {
                  de.addProject( p, deplist, p );
              }
              catch( Exception e )
                  {
                      System.out.println("Repository.Repository() : " + p + " : " + e 
);
                  }
          }
      }
  
      public List getDependencyList( String pkg, String version )
      {
          if( isPackage( pkg ))
              return de.getDependencies( pkg );
  
          return new ArrayList();
      }
  
      public Iterator getPackageListIter()
      {
          return packageList.iterator();
      }
  
      public int getPackageCount()
      {
          return packageList.size();
      }
  
      public String getPackageDefaultVersion( String pkg )
      {
          if (isPackage( pkg ) )
          {
              return getProperty( pkg + ".default" );
          }
  
          return null;
      }
  
      public String getPackageDescription( String pkg )
      {
          if (isPackage( pkg ) )
          {
              return getProperty( pkg + ".desc" );
          }
  
          return null;
      }
  
      public String getFetchTarget( String pkg, String version )
      {
          if (!isPackage( pkg ))
              return null;
  
          String ft = getProperty( pkg + "." + version + ".jar");
  
          return ft;
      }
  
      public boolean isPackage( String pkg )
      {
          Iterator i = packageList.iterator();
  
          while( i.hasNext() )
          {
              String p = (String) i.next();
  
              if (p.equals( pkg ) )
                  return true;
          }
      
          return false;
      }
  
      public List getPackageVersionList( String pkg )
      {
          if(!isPackage( pkg ))
             return null;
       
          return makeList( getProperty( pkg + ".version" ) );
      }
  
      private List makeList( String thing )
      {
          StringTokenizer st = new StringTokenizer( thing, ", ");
  
          List list = new ArrayList();
  
          while(st.hasMoreTokens() )
          {
              list.add( st.nextToken().trim() );
          }
  
          return list;
      }
  }
  
  
  
  
  
  
  1.1                  
jakarta-commons-sandbox/jjar/src/java/org/apache/commons/jjar/RepositoryXML.java
  
  Index: RepositoryXML.java
  ===================================================================
  /*
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2001 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, 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 acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Commons", 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 [EMAIL PROTECTED]
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * 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 (INCLUDING, 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.commons.jjar;
  
  import java.util.Properties;
  import java.util.StringTokenizer;
  import java.util.ArrayList;
  import java.util.Iterator;
  import java.util.List;
  import java.util.Stack;
  import java.util.Map;
  import java.util.HashMap;
  
  import java.io.StringWriter;
  import java.io.Writer;
  import java.io.InputStream;
  import java.io.InputStreamReader;
  import java.io.BufferedInputStream;
  import java.io.IOException;
  
  import java.net.URL;
  import java.net.URLConnection;
  
  import org.xml.sax.*;
  import uk.co.wilson.xml.MinML2;
  
  /**
   *  Repository implementation that supports XML repository
   *  definitions.  Uses a great small little lightweight SAX
   *  parser - but it's not clear what it doesn't support, so
   *  be careful
   *
   *  @author <a href="mailto:[EMAIL PROTECTED]";>Geir Magnusson Jr.</a>
   *  @version $Id: RepositoryXML.java,v 1.1 2001/05/07 22:47:45 geirm Exp $
   */
  public class RepositoryXML extends MinML2 implements Repository
  {
        /** list of packages */
      private HashMap packageMap = new HashMap();
        int packageCount = 0;
  
        /** handles package dependency generation */
        private DependencyEngine de = new DependencyEngine();
  
        /** root of our document tree */
      private Node root = null;
  
        /** stack used for SAX parsing */
      private Stack  stack = new Stack();
   
        /** user for SAX parsing to avoid character events */
      private StringWriter saxWriter = null;
  
      /** string constants for use in data structure */
        private static String PACKAGENAME = "packagename";
        private static String DEFAULTVERSION = "defaultversion";
        private static String NODE = "node";
        private static String DESCRIPTION = "description";
        private static String HREF = "href";
        private static String VERSION = "version";  
      private static String VERSION_INFO = "version_info";
      private static String VERSION_JAR = "version_jar";
      private static String DEP_PACKAGE = "dep_package";
      private static String DEP_VERSION = "dep_version";
      private static String VERSION_DEPS = "version_deps";
      private static String VERSION_ARRAY = "version_array";
   
        /**
         *  loads the object with the XML data from the
         *  passed in URL.  Invokes the SAX parser.
         *
         *  @param url location of the repository.xml
         */
      public void load( URL url )
      {
          try 
          {
              URLConnection conn = url.openConnection();
              InputStream is = conn.getInputStream();
  
              parse(new InputStreamReader(new BufferedInputStream( is)));
              process();
          }
          catch ( IOException e) 
          {
              System.out.println("IOException: " + e);
              e.printStackTrace();
          }
          catch ( SAXException e) 
          {
              System.out.println("SAXException: " + e);
              e.printStackTrace();
          }
          catch ( Throwable e) 
          {
              System.out.println("Other Exception: " + e);
              e.printStackTrace();
          }
      }
   
        /**
         *  returns a list of dependencies for
         *  a given package.  The list contains
         *  Maps, with 'package' and 'version'
         *  as the keys for the necessary info.
         *
         *  @param pkg package to get dep list for
         *  @return List of Maps
         */
        public List getDependencyList( String pkg, String version )
      {
          if( isPackage( pkg ))
              return de.getDependencies( pkg + ":" + version );
  
          return new ArrayList();
      }
  
      /**
       *  returns an iterator over the
       *   list of package names
       */ 
      public Iterator getPackageListIter()
      {
          return  packageMap.keySet().iterator();
      }
  
      /**
       *  returns the number of packages known to
       *  the repository
       */
      public int getPackageCount()
      {
          return packageMap.size();
      }
  
      /**
       *  returns the default version of the package to the caller
       */
      public String getPackageDefaultVersion( String pkg )
      {
          if (isPackage( pkg ) )
          {
              HashMap hpack = (HashMap) packageMap.get( pkg );
              return (String) hpack.get( DEFAULTVERSION);
          }
  
          return null;
      }
  
      /**
       *  returns the info/desc string to the caller
       */
      public String getPackageDescription( String pkg )
      {
          if (isPackage( pkg ) )
          {
              HashMap hpack = (HashMap) packageMap.get( pkg );
              return (String) hpack.get( DESCRIPTION);
          }
  
          return "";
      }
  
      /**
       *  returns the 'fetch' target (jarname) for the 
       *  given package : version
       */
      public String getFetchTarget( String pkg, String version )
      {
          if (!isPackage( pkg ))
              return null;
  
          Map hp = (HashMap) packageMap.get( pkg );
          Iterator i =((ArrayList) hp.get( VERSION_ARRAY )).iterator();        
          
          while( i.hasNext() )
          {
              HashMap vi = (HashMap) i.next();
              String ver = (String) vi.get( VERSION );
  
              if (ver != null)
              {
                  if (version.equals( ver ) )
                  {
                      String out = (String) vi.get(VERSION_JAR);
  
                      return out;
                  }
              }
          }
  
          return null;
      }
  
      /**
       *  determines if something is a known package
       */
      public boolean isPackage( String pkg )
      {
                Object o = packageMap.get( pkg );
  
                if( o != null)
                        return true;
  
                return false;
      }
  
      /**
       *  returns a given property 
       *  not implemented
       */
      public String getProp( String s )
      {
          return s;
      }
  
      /**
       *  returns a list of the know versions available
       *  for this package
       */
      public List getPackageVersionList( String pkg )
      {
          if(!isPackage( pkg ))
             return null;
       
          Map hp = (HashMap) packageMap.get( pkg );
  
          ArrayList verlist = new ArrayList();
  
          Iterator i =((ArrayList) hp.get( VERSION_ARRAY )).iterator();
          
          while( i.hasNext() )
          {
              verlist.add( ( (HashMap)i.next() ).get( VERSION ) );
          }
  
          return verlist;
      }
  
      private List makeList( String thing )
      {
          StringTokenizer st = new StringTokenizer( thing, ", ");
  
          List list = new ArrayList();
  
          while(st.hasMoreTokens() )
          {
              list.add( st.nextToken().trim() );
          }
  
          return list;
      }
  
        /**
         *  takes the intermediary form of input data, and loads the
         *  internal data structures for use later.  This needs to be
         *  called right after parsing, and before use.
         */
      private void process()
      {
          /*
           *  need to build a package list.  Get the package groups.
           */
  
                List packagegroup = getDocNodeList( root, "packagegroup");
          
          //System.out.println("packagegroup has " + packagegroup.size() + "elements");
          
          Iterator i = packagegroup.iterator();
  
          while( i.hasNext() )
          {
              Node pkggrp = (Node) i.next();
  
              //System.out.println("Processing packagegroup : " + 
pkggrp.getAttribute("name") );
  
                        List packages = getDocNodeList( pkggrp, "package");
  
                        /*
                         * for each package...
                         */
  
                        Iterator ii = packages.iterator();
  
                        while( ii.hasNext() )
                        {
                                processPackageNode( (Node) ii.next() );
                        }
          }
      }
        
        private void processPackageNode( Node pkg )
        {
                HashMap pkginfo = new HashMap();
  
                /*
                 *  start with the basics
                 */
  
                pkginfo.put( PACKAGENAME, pkg.getAttribute("name") );
                pkginfo.put( DEFAULTVERSION, pkg.getAttribute("default"));
                pkginfo.put( NODE, pkg );
  
                /*
                 * now get the information node
                 */
  
                Node info = getDocNode( pkg, "info/desc");
  
          if( info != null)
              pkginfo.put( DESCRIPTION, info.getValue());
  
          info = getDocNode( pkg, "info/href");
  
          if( info != null)
              pkginfo.put( HREF, info.getValue());
  
                /*
                 *  process version info
                 */
  
          List  versionlist = getDocNodeList( pkg, "versionset/version");
          ArrayList versionArray = new ArrayList();
  
          Iterator v = versionlist.iterator();
  
          while( v.hasNext() )
          {
              Node n = (Node) v.next();
  
              HashMap vi = new HashMap();
  
              vi.put( VERSION, n.getAttribute("version") );
              
              Node nn = getDocNode( n, "note");
              vi.put( VERSION_INFO, nn.getValue() );
  
              nn = getDocNode( n, "jar");
              vi.put( VERSION_JAR, nn.getValue() );
  
              /*  the dependencies */
  
              ArrayList deplist = new ArrayList();
  
              List deps = getDocNodeList( n, "dependencies/dep");
   
              if (deps != null)
              {
                  Iterator ii = deps.iterator();
                  
                  while( ii.hasNext() )
                  {
                      HashMap h = new HashMap();
                      Node ndep = (Node) ii.next();
                      
                      h.put( DEP_PACKAGE, ndep.getAttribute("package") );
                      h.put( DEP_VERSION, ndep.getAttribute("version") );
                      
                      deplist.add( h );
                  }
                  
                  vi.put( VERSION_DEPS, deplist );
              }
     
              versionArray.add( vi );
   
              /*
               *  add the package:version to the dependency engine
               */
  
              String token = pkginfo.get( PACKAGENAME ) + ":" +  (String) vi.get( 
VERSION );
  
              // System.out.println("Adding " + token + " to dependency engine.");
  
              ArrayList depar = new ArrayList();
  
              Iterator depiter = deplist.iterator();
              
              while( depiter.hasNext() )
              {
                  HashMap h = (HashMap) depiter.next();
  
                  String deptoken = (String) h.get(DEP_PACKAGE) + ":" +  (String) 
h.get(DEP_VERSION);
                  depar.add( deptoken );
              }
  
              try
              {
                  de.addProject( token, depar, token );
              }
              catch( Exception e )
                  {}
  
          }
  
          pkginfo.put( VERSION_ARRAY, versionArray );
  
                /*
                 *  add the info to the packageMap
                 */
  
                packageMap.put( pkginfo.get( PACKAGENAME ), pkginfo );
  
          //            dumpPackageInfo( pkginfo );
        }
  
      /**
       *  simple dumper for debugging
       */
      private void dumpPackageInfo( Map h )
        {
                System.out.println("Package : " + h.get( PACKAGENAME ) );
                System.out.println("  default version : " + h.get(DEFAULTVERSION ));
                System.out.println("     description  : " + h.get(DESCRIPTION ));
                System.out.println("             href : " + h.get(HREF ));
  
          System.out.println("      version info -> " );
  
          ArrayList vl = (ArrayList) h.get( VERSION_ARRAY );
          Iterator i = vl.iterator();
          
          while( i.hasNext() )
          {
              HashMap vi = (HashMap) i.next();
  
              System.out.println("      ver : " + vi.get( VERSION ) );
              System.out.println("     info : " + vi.get( VERSION_INFO ) );
              System.out.println("      jar : " + vi.get( VERSION_JAR ) );
          }
        }
  
  
      /**
       *  Returns a given node from the document tree.
       *  if a multivalued node, will return the first
       *
       *  @param root  node to start seaching on
       *  @param path node path, a la "foo/bar/woogie"
       *  @return node or null
       */
        private Node getDocNode( Node root, String path )
        {
                List ar = getDocNodeList(root, path );
  
                if (ar == null || ar.size() == 0)
                        return null;
  
                return (Node) ar.get(0);
        }
  
      /**
       *  returns the node list for a given path relative to the
       *  passed in node
       *
       *  @param root node to start from
       *  @param path note path
       *  @return List of nodes that match
       */
        private List getDocNodeList( Node root, String path)
        {
                StringTokenizer st = new StringTokenizer( path, "/");
                
                Node n = root;
                ArrayList ar = null;
  
                while( st.hasMoreTokens() )
                {
                        String token = st.nextToken();
                        
                        ar = n.getChildren(token);
                        
                        if (ar == null || ar.size() == 0)
                                return null;
  
                        n = (Node) ar.get(0);
                }
  
                return ar;
        }
  
      /**
       *  start element event handler for SAX.  
       */
      public Writer startElement( String namespaceURI, String localName, String qName, 
Attributes atts,  Writer writer)
          throws SAXException
      {
          /*
           *  get the top element on the list
           */
                
                Node parent = null;
                Node element = new Node( qName );
                
          if( root == null)
                {
              root = element;
          }
          else
          {
              /*
               * get parent node
               */
  
              try
              {
                  parent = (Node) stack.peek();
              }
              catch(Exception e)
                  {}
          }
  
          /*
           *  everything is a list
           */
  
          if (parent != null)
          {    
                        parent.addChildNode( element);
          }
  
          /*
           *  now add our attributes
           */
  
          for (int i = 0; i < atts.getLength(); i++)
          {
              element.setAttribute( atts.getQName(i), atts.getValue(i) );
          }
  
          /*
           *  now put this element on the stack for later
           */
  
          stack.push(element);
          
          saxWriter = new StringWriter();
          return saxWriter;
      }
     
  
      /**
       *  end element event handler for SAX parsing
       */
      public void endElement( String namespaceURI, String localName, String qName)
          throws SAXException
      {
          try
          {
              Node element = (Node) stack.pop();
              
              String s = saxWriter.toString();
  
              if ( s.length() > 0)
                  element.setValue( s );
          }
          catch( Exception e )
          {
              System.out.println("endElement : " + e );
          }
      }
      
      public void fatalError (SAXParseException e) 
        throws SAXException 
      {
          System.out.println("RepositoryXML.fatalError(): " + e);
          throw e;
      }
      
      /**
       *  used for debugging
       */
      public static void main ( String[] args) 
      {
        RepositoryXML rep = new RepositoryXML();
  
        try
        {
            rep.load( new URL("file://" + args[0] ));
        }
        catch( Exception e )
            { System.out.println(e );}
  
        
        List al = rep.getDependencyList( "ant", "1.3");
  
        Iterator i = al.iterator();
  
        System.out.println("dep for ant 1.3 ");
  
        while( i.hasNext() )
        {
            System.out.println("  " + (String) i.next() );
        }
                
    }
      /**
       *  meethod for debugging parsing
       */
      private static  void show( String indent,  Map map )
      {
          System.out.println("");
  
          Iterator i = map.keySet().iterator();
  
          while(i.hasNext() )
          {
              String s = (String) i.next();
              Object o = map.get( s );
              System.out.print( indent + "<" + s + ">");
  
              if ( o instanceof Map )
              {
                  show( indent + "  ", (Map) o);
              }
              else if ( o instanceof List )
              {
                  Iterator j = ((List) o).iterator();
                  
                  while( j.hasNext() )
                  {
                      Object oo = j.next();
                      
                      if (oo instanceof Map)
                          show( indent + "  ", (Map) oo);
                      else
                          System.out.println("woo!" + oo.getClass());
                  }
              }
              else
              {
                  System.out.println( map.get( s ));
              }
                      
        }
      }
  
        /**
         *  small class to hold our node information for
         *  XML processing
         */
        class Node 
        {
                String name = null;
                String value = "";
  
                HashMap attributes = new HashMap();
                HashMap children = new HashMap();
  
                Node( String name )
                {
                        this.name = name;
                }
  
                void setValue( String val )
                {
                        value = val;
                }
                
                String getValue()
                {
                        return value;
                }
  
                void setAttribute( String key, String val )
                {
                        attributes.put( key, val );
                }
                
                String getAttribute( String key )
                {
                        return (String) attributes.get( key );
                }
  
                void addChildNode( Node node )
                {
                        ArrayList ar = (ArrayList) children.get( node.name );
  
                        if (ar == null)
                                ar = new ArrayList();
  
                        ar.add( node );
  
                        children.put( node.name, ar );
                }
  
                ArrayList getChildren( String name )
                {
                        return (ArrayList) children.get( name );
                }
                                
        }
  
  }
  
  
  
  
  
  
  
  
  1.1                  
jakarta-commons-sandbox/jjar/src/java/org/apache/commons/jjar/Transport.java
  
  Index: Transport.java
  ===================================================================
  /*
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2001 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, 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 acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Commons", 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 [EMAIL PROTECTED]
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * 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 (INCLUDING, 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.commons.jjar;
  
  import java.lang.Package;
  
  import java.util.ArrayList;
  import java.util.Iterator;
  import java.util.Collection;
  import java.util.Map;
  import java.util.List;
  import java.util.StringTokenizer;
  import java.util.jar.JarFile;
  import java.util.jar.Manifest;
  import java.util.jar.Attributes;
  import java.util.jar.JarInputStream;
  
  import java.io.File;
  import java.io.FileOutputStream;
  import java.io.BufferedInputStream;
  import java.io.BufferedOutputStream;
  import java.io.InputStream;
  import java.io.OutputStream;
  
  import java.net.JarURLConnection;
  import java.net.URLConnection;
  import java.net.URL;
  
  
  /**
   *  bit moving  support for JJAR
   * 
   *  @author <a href="mailto:[EMAIL PROTECTED]";>Geir Magnusson Jr.</a>
   *  @version $Id: Transport.java,v 1.1 2001/05/07 22:47:45 geirm Exp $
   */
  public class Transport
  {
      public static boolean fetchJar( URLConnection source, String dest )
      {
          BufferedInputStream bis = null;
          BufferedOutputStream bos = null;
  
          try
          {
              bis = new BufferedInputStream( source.getInputStream());
              bos = new BufferedOutputStream( new FileOutputStream( dest ) );
         
              return fetch( bis, bos );
          }
          catch( Exception e )
          {
              System.out.println("Transport.fetchJar() :  " + e );
          }
          finally
          {
              try{
                  if (bos != null)
                      bos.close();
              }catch(Exception e){}
          }
  
          return false;
      }
  
      private static boolean fetch( InputStream is, OutputStream os )
          throws Exception
      {
          byte[] buf = new byte[ 1024];
          int count = 0;
          
          do 
          {
              os.write( buf, 0, count);
              count = is.read(buf, 0, buf.length);
          } while (count != -1);
  
          return true;
      }
  }
  
  
  
  1.1                  
jakarta-commons-sandbox/jjar/src/java/org/apache/commons/jjar/Version.java
  
  Index: Version.java
  ===================================================================
  /*
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2001 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, 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 acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Commons", 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 [EMAIL PROTECTED]
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * 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 (INCLUDING, 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.commons.jjar;
  
  /**
   *  simple class to handle JJAR version information
   *
   *  should be of format 
   *     <major>.<minor>-<modifier>
   *
   *  @author <a href="mailto:[EMAIL PROTECTED]";>Geir Magnusson Jr.</a>
   *  @version $Id: Version.java,v 1.1 2001/05/07 22:47:45 geirm Exp $
   */
  public class Version
  {
        private static String VER_SEP = ".";
        private static String MOD_SEP = "-";
        
        private boolean proper = false;
        private String version;
        private int major = -1;
        private int minor = -1;
        private String modifier = "";
  
        public Version( String version )
        {
                this.version = version;
                parse();
        }
  
        public boolean isProper()
        {
          return proper;
        }
  
      public String toString()
      {
          return version;
      }
  
      public int getMajor()
      {
          return this.major;
      }
  
      public int getMinor()
      {
          return this.minor;
      }
  
      public String getModifier()
      {
          return this.modifier;
      }
  
      public boolean equals( String version )
      {
          Version v = new Version( version );
  
          return equals( v );
      }
  
      public boolean equals( Version v )
      {
          return (v.getMajor() == major  && v.getMinor() == minor && 
v.getModifier().equals( modifier));
      }
            
        private void parse()
        {
                /*
                 *  first, look for the separators
                 */
  
                int wherever = version.indexOf( VER_SEP );
                int wheremod = version.indexOf( MOD_SEP );
  
                /*
                 *  we must have the VER_SEP or it's not 
                 *  properly formatted
                 */
  
                if (wherever != -1 )
                {
                        /*
                         *  major : get the 'left' of VER_SEP
                         */
                        
                        String maj = version.substring( 0,wherever);
                        
                        try
                        {
                                major = Integer.parseInt( maj );
                                proper = true;
                        }
                        catch( Exception e )
                        {}
  
              /*
               *  minor :
               */
  
                        String min = version.substring( wherever + 1, 
                                                                                       
 (wheremod == -1 ? version.length() :  wheremod ) );
  
              try
                        {
                                minor = Integer.parseInt( min );
                                proper = true;
                        }
                        catch( Exception e )
                        {}
                }
  
          if ( wheremod != -1 )
          {
              /*
               *  now the modifier
               */
  
              modifier = version.substring( wheremod + 1 );
          }
                
        }
  
      public static void main( String args[])
      {
          Version jv = new Version( "1.1");
          System.out.println( jv.getMajor() + " : " + jv.getMinor() + " : " + 
jv.getModifier());
  
          jv = new Version( "2.1-rc1");
          System.out.println( jv.getMajor() + " : " + jv.getMinor() + " : " + 
jv.getModifier());
      }
  
  }
  
  
  
  
  
  

Reply via email to