Clover coverage report - Ant Coverage
Coverage timestamp: Tue Apr 8 2003 20:43:55 EST
file stats: LOC: 214   Methods: 5
NCLOC: 82   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
LoaderUtils.java 50% 69.4% 100% 67.9%
 1   
 /*
 2   
  * The Apache Software License, Version 1.1
 3   
  *
 4   
  * Copyright (c) 2002-2003 The Apache Software Foundation.  All rights
 5   
  * reserved.
 6   
  *
 7   
  * Redistribution and use in source and binary forms, with or without
 8   
  * modification, are permitted provided that the following conditions
 9   
  * are met:
 10   
  *
 11   
  * 1. Redistributions of source code must retain the above copyright
 12   
  *    notice, this list of conditions and the following disclaimer.
 13   
  *
 14   
  * 2. Redistributions in binary form must reproduce the above copyright
 15   
  *    notice, this list of conditions and the following disclaimer in
 16   
  *    the documentation and/or other materials provided with the
 17   
  *    distribution.
 18   
  *
 19   
  * 3. The end-user documentation included with the redistribution, if
 20   
  *    any, must include the following acknowlegement:
 21   
  *       "This product includes software developed by the
 22   
  *        Apache Software Foundation (http://www.apache.org/)."
 23   
  *    Alternately, this acknowlegement may appear in the software itself,
 24   
  *    if and wherever such third-party acknowlegements normally appear.
 25   
  *
 26   
  * 4. The names "Ant" and "Apache Software
 27   
  *    Foundation" must not be used to endorse or promote products derived
 28   
  *    from this software without prior written permission. For written
 29   
  *    permission, please contact apache@apache.org.
 30   
  *
 31   
  * 5. Products derived from this software may not be called "Apache"
 32   
  *    nor may "Apache" appear in their names without prior written
 33   
  *    permission of the Apache Group.
 34   
  *
 35   
  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 36   
  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 37   
  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 38   
  * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
 39   
  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 40   
  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 41   
  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 42   
  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 43   
  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 44   
  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 45   
  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 46   
  * SUCH DAMAGE.
 47   
  * ====================================================================
 48   
  *
 49   
  * This software consists of voluntary contributions made by many
 50   
  * individuals on behalf of the Apache Software Foundation.  For more
 51   
  * information on the Apache Software Foundation, please see
 52   
  * <http://www.apache.org/>.
 53   
  */
 54   
 package org.apache.tools.ant.util;
 55   
 
 56   
 import java.io.File;
 57   
 import java.lang.reflect.InvocationTargetException;
 58   
 import java.lang.reflect.Method;
 59   
 import java.net.URL;
 60   
 import org.apache.tools.ant.BuildException;
 61   
 
 62   
 /**
 63   
  * ClassLoader utility methods
 64   
  *
 65   
  * @author Conor MacNeill
 66   
  */
 67   
 public class LoaderUtils {
 68   
     /** The getContextClassLoader method */
 69   
     private static Method getContextClassLoader;
 70   
     /** The setContextClassLoader method */
 71   
     private static Method setContextClassLoader;
 72   
 
 73   
     // Set up the reflection-based Java2 methods if possible
 74   
     static {
 75  1
         try {
 76  1
             getContextClassLoader
 77   
                  = Thread.class.getMethod("getContextClassLoader",
 78   
                 new Class[0]);
 79  1
             Class[] setContextArgs = new Class[]{ClassLoader.class};
 80  1
             setContextClassLoader
 81   
                  = Thread.class.getMethod("setContextClassLoader",
 82   
                 setContextArgs);
 83   
         } catch (Exception e) {
 84   
             // ignore any problems accessing the methods - probably JDK 1.1
 85   
         }
 86   
     }
 87   
 
 88   
     /**
 89   
      * JDK1.1 compatible access to get the context class loader. Has no
 90   
      * effect on JDK 1.1
 91   
      *
 92   
      * @param loader the ClassLoader to be used as the context class loader
 93   
      *      on the current thread.
 94   
      */
 95  284
     public static void setContextClassLoader(ClassLoader loader) {
 96  284
         if (setContextClassLoader == null) {
 97  0
             return;
 98   
         }
 99   
 
 100  284
         try {
 101  284
             Thread currentThread = Thread.currentThread();
 102  284
             setContextClassLoader.invoke(currentThread,
 103   
                 new Object[]{loader});
 104   
         } catch (IllegalAccessException e) {
 105  0
             throw new BuildException
 106   
                 ("Unexpected IllegalAccessException", e);
 107   
         } catch (InvocationTargetException e) {
 108  0
             throw new BuildException
 109   
                 ("Unexpected InvocationTargetException", e);
 110   
         }
 111   
 
 112   
     }
 113   
 
 114   
 
 115   
     /**
 116   
      * JDK1.1 compatible access to set the context class loader.
 117   
      *
 118   
      * @return the ClassLoader instance being used as the context
 119   
      *      classloader on the current thread. Returns null on JDK 1.1
 120   
      */
 121  717
     public static ClassLoader getContextClassLoader() {
 122  717
         if (getContextClassLoader == null) {
 123  0
             return null;
 124   
         }
 125   
 
 126  717
         try {
 127  717
             Thread currentThread = Thread.currentThread();
 128  717
             return (ClassLoader) getContextClassLoader.invoke(currentThread,
 129   
                 new Object[0]);
 130   
         } catch (IllegalAccessException e) {
 131  0
             throw new BuildException
 132   
                 ("Unexpected IllegalAccessException", e);
 133   
         } catch (InvocationTargetException e) {
 134  0
             throw new BuildException
 135   
                 ("Unexpected InvocationTargetException", e);
 136   
         }
 137   
     }
 138   
 
 139   
     /**
 140   
      * Indicates if the context class loader methods are available
 141   
      *
 142   
      * @return true if the get and set methods dealing with the context
 143   
      *      classloader are available.
 144   
      */
 145  284
     public static boolean isContextLoaderAvailable() {
 146  284
         return getContextClassLoader != null &&
 147   
             setContextClassLoader != null;
 148   
     }
 149   
 
 150   
     /**
 151   
      * Find the directory or jar file the class has been loaded from.
 152   
      *
 153   
      * @return null if we cannot determine the location.
 154   
      *
 155   
      * @since Ant 1.6
 156   
      */
 157  1
     public static File getClassSource(Class c) {
 158  1
         String classFile = c.getName().replace('.', '/') + ".class";
 159  1
         return getResourceSource(c.getClassLoader(), classFile);
 160   
     }
 161   
 
 162   
     /**
 163   
      * Find the directory or a give resource has been loaded from.
 164   
      *
 165   
      * @return null if we cannot determine the location.
 166   
      *
 167   
      * @since Ant 1.6
 168   
      */
 169  5
     public static File getResourceSource(ClassLoader c, String resource) {
 170  5
         FileUtils fileUtils = FileUtils.newFileUtils();
 171  5
         if (c == null) {
 172  1
             c = LoaderUtils.class.getClassLoader();
 173   
         }
 174   
         
 175  5
         URL url = c.getResource(resource);
 176  5
         if (url != null) {
 177  5
             String u = url.toString();
 178  5
             if (u.startsWith("jar:file:")) {
 179  5
                 int pling = u.indexOf("!");
 180  5
                 String jarName = u.substring(4, pling);
 181  5
                 return new File(fileUtils.fromURI(jarName));
 182  0
             } else if (u.startsWith("file:")) {
 183  0
                 int tail = u.indexOf(resource);
 184  0
                 String dirName = u.substring(0, tail);
 185  0
                 return new File(fileUtils.fromURI(dirName));
 186   
             }
 187   
         }
 188  0
         return null;
 189   
     }
 190   
 
 191   
     // if we want to drop JDK 1.1, here is code that does something similar
 192   
     // - stolen from Diagnostics, stolen from Axis, stolen from somewhere else
 193   
     //
 194   
     //  try {
 195   
     //      java.net.URL url = clazz.getProtectionDomain().getCodeSource().getLocation();
 196   
     //      String location = url.toString();
 197   
     //      if (location.startsWith("jar")) {
 198   
     //          url = ((java.net.JarURLConnection) url.openConnection()).getJarFileURL();
 199   
     //          location = url.toString();
 200   
     //      }
 201   
     //  
 202   
     //      if (location.startsWith("file")) {
 203   
     //          java.io.File file = new java.io.File(url.getFile());
 204   
     //          return file.getAbsolutePath();
 205   
     //      } else {
 206   
     //          return url.toString();
 207   
     //      }
 208   
     //  } catch (Throwable t) {
 209   
     //  }
 210   
     //  return null;
 211   
 
 212   
 }
 213   
 
 214