Coverage Report - org.apache.commons.jjar.JJARTask
 
Classes in this File Line Coverage Branch Coverage Complexity
JJARTask
0%
0/73
0%
0/20
2.556
 
 1  
 /*
 2  
  * Copyright 2001,2004 The Apache Software Foundation.
 3  
  * 
 4  
  * Licensed under the Apache License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  * 
 8  
  *      http://www.apache.org/licenses/LICENSE-2.0
 9  
  * 
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 
 17  
 package org.apache.commons.jjar;
 18  
 
 19  
 import java.net.URL;
 20  
 import java.net.URLConnection;
 21  
 import java.util.List;
 22  
 import java.util.Iterator;
 23  
 
 24  
 import org.apache.tools.ant.BuildException;
 25  
 import org.apache.tools.ant.Task;
 26  
 import org.apache.tools.ant.types.Path;
 27  
 
 28  
 /**
 29  
  * An ant task for doing JJAR stuff
 30  
  *
 31  
  * @version $Id: JJARTask.java 155454 2005-02-26 13:23:34Z dirkv $
 32  
  */
 33  0
 public class JJARTask extends Task
 34  
 {
 35  0
     private static String defaultRepository = ClasspathUtil.getDefaultRepository();
 36  0
     private String pkg = null;
 37  0
     private String ver = null;
 38  0
     private String localrep = ".";
 39  0
     private boolean verifyignore = true;
 40  0
     private boolean onlydependencies = false;
 41  0
     private String classpath = null;
 42  0
     private String pathrefid = null;
 43  
 
 44  
     /**
 45  
      *  [REQUIRED] sets the package and version.  ver can be 
 46  
      *  null to get the default
 47  
      */
 48  
     public void setPackage( String pkg)
 49  
     {
 50  0
         this.pkg = pkg;
 51  0
     }
 52  
 
 53  
     public void setVersion( String version )
 54  
     {
 55  0
         this.ver = version;
 56  0
     }
 57  
 
 58  
     public void setLocalrepository( String localrep )
 59  
     {
 60  0
         this.localrep = localrep;
 61  0
     }
 62  
 
 63  
     public void setVerifyignore( boolean b )
 64  
     {
 65  0
         this.verifyignore = b;
 66  0
     }
 67  
 
 68  
     public void setOnlyDependencies( boolean b )
 69  
     {
 70  0
         this.onlydependencies = b;
 71  0
     }
 72  
 
 73  
     public void setRepository( String rep )
 74  
     {
 75  0
         this.defaultRepository = rep;
 76  0
     }
 77  
 
 78  
     public void setClasspath( String classpath )
 79  
     {
 80  0
         this.classpath = classpath;
 81  0
     }
 82  
 
 83  
     public void setPathrefid( String pathrefid )
 84  
     {
 85  0
         this.pathrefid = pathrefid;
 86  0
     }
 87  
 
 88  
     /**
 89  
      * Execute the input script with Velocity
 90  
      *
 91  
      * @throws BuildException  
 92  
      * BuildExceptions are thrown when required attributes are missing.
 93  
      * Exceptions thrown by Velocity are rethrown as BuildExceptions.
 94  
      */
 95  
     public void execute () 
 96  
         throws BuildException
 97  
     {
 98  
         /*
 99  
          *  make sure that we at least have a package name
 100  
          */
 101  0
         if( pkg == null)
 102  0
             throw new BuildException("Need to specify a package to fetch");
 103  
     
 104  
         /*
 105  
          *  get the repository
 106  
          */
 107  0
         Repository repository = null;
 108  
 
 109  
         try
 110  
         {
 111  0
             repository = new RepositoryXML();
 112  0
             repository.load(  new URL( defaultRepository + "repository.xml") );
 113  
             
 114  
         }
 115  0
         catch( Exception e )
 116  
         {
 117  0
             throw new BuildException("Problem getting repository info", e );
 118  0
         }
 119  
 
 120  0
         log("Fetching package '" + pkg + "' from repository '" + defaultRepository 
 121  
             + "' into local repository '" + localrep + "'");
 122  
 
 123  
         /*
 124  
          *  create an antpath
 125  
          */
 126  
       
 127  0
         Path antpath = new Path( project );
 128  
 
 129  
         try
 130  
         {
 131  
             /*
 132  
              *  do it : first, see if there is a dependency list
 133  
              */
 134  
 
 135  0
             if( ver == null)
 136  0
                 ver = repository.getPackageDefaultVersion( pkg );
 137  
 
 138  0
             List deplist = repository.getDependencyList( pkg, ver );
 139  
             
 140  0
             Iterator ii = deplist.iterator();
 141  
 
 142  0
             while(ii.hasNext())
 143  
             {
 144  
                 /*
 145  
                  *  for now, use the fetchtarget names
 146  
                  */
 147  
 
 148  0
                 String depnameuni = (String) ii.next();
 149  
 
 150  0
                 JJARPackage jjarp = new JJARPackage( depnameuni );
 151  
 
 152  0
                 String depname = jjarp.getName();
 153  0
                 String depver = jjarp.getVersionString();
 154  0
                 String deptarget = repository.getFetchTarget( depname, depver );
 155  
                          
 156  
                 /*
 157  
                  *  see if the dependent package is already in the classpath
 158  
                  */
 159  
                 
 160  0
                 if ( verifyignore && ClasspathUtil.containsVersionedJar( classpath, depname, depver))
 161  
                 {
 162  0
                     log("  Dependency '" + depname + "' already in classpath - skipping");
 163  0
                     continue;
 164  
                 }
 165  
                 
 166  
                 /*
 167  
                  *  now get it
 168  
                  */
 169  
                 
 170  0
                 URL url = new URL( defaultRepository + deptarget );
 171  0
                 URLConnection uconn  = (URLConnection) url.openConnection(); 
 172  
                 
 173  0
                 log("  Fetching dependency : " + deptarget );
 174  
                 
 175  0
                 Transport.fetchJar( uconn, localrep + "/" + deptarget );                
 176  
 
 177  
                 /*
 178  
                  *  now if they want us to add to a classpath, we will have
 179  
                  *  a pathrefid, so for now, pretend it's fresh
 180  
                  */
 181  
 
 182  0
                 log("Adding depedency element " + localrep + "/" + deptarget + " to classpath");
 183  
 
 184  0
                 antpath.createPathElement().setPath( localrep + "/" + deptarget );
 185  0
             }
 186  
             
 187  
             /*
 188  
              *  now get the actual package.  If all we want were the dependencies
 189  
              *  for something (example : we are building something...) then
 190  
              *  bail, because we are done.
 191  
              */
 192  
 
 193  0
             if( onlydependencies )
 194  
             {
 195  0
                 log("  Dependency-only mode : not fetching target " + pkg );
 196  
                 return;
 197  
             }
 198  
 
 199  
             /*
 200  
              *  if we don't have a version, we need to get it from the repository
 201  
              */
 202  
 
 203  0
             if (ver == null)
 204  
             {
 205  0
                 ver = repository.getPackageDefaultVersion( pkg );
 206  
             }
 207  
 
 208  0
             if( verifyignore && ClasspathUtil.containsVersionedJar( classpath, pkg, ver))
 209  
             {                
 210  0
                 log("  Skipping fetch of '" + pkg + "' - already exists in classpath");
 211  
                 // ignore and continue
 212  
             }
 213  
             else
 214  
             {
 215  0
                 String fetchTarget = repository.getFetchTarget( pkg, ver );
 216  
 
 217  0
                 URL url = new URL( defaultRepository + fetchTarget );
 218  0
                 URLConnection uconn  = (URLConnection) url.openConnection(); 
 219  
 
 220  0
                 log("  Fetching package : " + pkg );
 221  
                 
 222  0
                 Transport.fetchJar( uconn, localrep + "/" + fetchTarget );
 223  
 
 224  0
                 log("Adding package element " + localrep + "/" + fetchTarget + " to classpath");
 225  
 
 226  0
                 antpath.createPathElement().setPath( localrep + "/" + fetchTarget );
 227  
  
 228  
             }
 229  
         }
 230  0
         catch( Exception e )
 231  
         {
 232  0
             throw new BuildException("Exception : ",e);
 233  
         }
 234  
         finally
 235  
         {
 236  0
             if( pathrefid != null)
 237  
             {
 238  0
                 project.addReference( pathrefid, antpath);
 239  
             }
 240  
         }
 241  0
     }
 242  
 
 243  
    
 244  
 }