Clover coverage report - JFox Service-Oriented Application Framework - 1.0-RC2
Coverage timestamp: 星期四 十二月 15 2005 11:58:01 CST
file stats: LOC: 324   Methods: 20
NCLOC: 133   Classes: 1
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover
 
 Source file Conditionals Statements Methods TOTAL
ResourceHelper.java 50% 49.2% 45% 48.4%
coverage coverage
 1   
 /**
 2   
  * @(#)ResourceHelper.java
 3   
  * 
 4   
  * JFoxSOAF, Service-Oriented Application Framework
 5   
  * 
 6   
  * Copyright(c) JFoxSOAF Team
 7   
  * 
 8   
  * Licensed under the GNU LGPL, Version 2.1 (the "License"); 
 9   
  * you may not use this file except in compliance with the License. 
 10   
  * You may obtain a copy of the License at  
 11   
  * 
 12   
  * http://www.gnu.org/copyleft/lesser.html
 13   
  * 
 14   
  * Unless required by applicable law or agreed to in writing, software
 15   
  * distributed under the License is distributed on an "AS IS" BASIS, 
 16   
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 17   
  * See the License for the specific language governing permissions and 
 18   
  * limitations under the License. 
 19   
  * 
 20   
  * For more information, please visit:
 21   
  * http://www.jfox.cn/confluence/display/JFoxSOAF/Home
 22   
  * http://www.huihoo.org/jfox/jfoxsoaf
 23   
  */
 24   
 
 25   
 package org.huihoo.jfox.soaf.util.resource;
 26   
 
 27   
 import java.io.File;
 28   
 import java.io.IOException;
 29   
 import java.io.InputStream;
 30   
 import java.io.InputStreamReader;
 31   
 import java.io.Reader;
 32   
 import java.net.URL;
 33   
 import java.net.URLConnection;
 34   
 import java.util.Properties;
 35   
 
 36   
 /**
 37   
  * A class to simplify access to resources through the classloader.
 38   
  * 
 39   
  * @author <a href="mailto:founder_chen@yahoo.com.cn">Peter Cheng </a>
 40   
  * @author <a href="mailto:clinton.begin@ibatis.com">Clinton Begin </a>
 41   
  * @version $Revision: 1.6 $ $Date: 2005/05/22 06:50:55 $
 42   
  * @version Revision: 1.0
 43   
  */
 44   
 
 45   
 public class ResourceHelper {
 46   
 
 47   
     private static ClassLoader defaultClassLoader;
 48   
 
 49  0
     public ResourceHelper() {
 50   
     }
 51   
 
 52   
     /**
 53   
      * Returns the default classloader (may be null).
 54   
      * 
 55   
      * @return The default classloader
 56   
      */
 57  0
     public static ClassLoader getDefaultClassLoader() {
 58  0
         return defaultClassLoader;
 59   
     }
 60   
 
 61   
     /**
 62   
      * Sets the default classloader
 63   
      * 
 64   
      * @param defaultClassLoader - the new default ClassLoader
 65   
      */
 66  0
     public static void setDefaultClassLoader(ClassLoader defaultClassLoader) {
 67  0
         ResourceHelper.defaultClassLoader = defaultClassLoader;
 68   
     }
 69   
 
 70   
     /**
 71   
      * Returns the URL of the resource on the classpath
 72   
      * 
 73   
      * @param resource The resource to find
 74   
      * @return The resource
 75   
      * @throws java.io.IOException If the resource cannot be found or read
 76   
      */
 77  30
     public static URL getResourceURL(String resource) throws IOException {
 78  30
         return getResourceURL(getClassLoader(), resource);
 79   
     }
 80   
 
 81   
     /**
 82   
      * Returns the URL of the resource on the classpath
 83   
      * 
 84   
      * @param loader The classloader used to load the resource
 85   
      * @param resource The resource to find
 86   
      * @return The resource
 87   
      * @throws java.io.IOException If the resource cannot be found or read
 88   
      */
 89  30
     public static URL getResourceURL(ClassLoader loader, String resource)
 90   
             throws IOException {
 91  30
         URL url = null;
 92  30
         if (loader != null) {
 93  30
             url = loader.getResource(resource);
 94   
         }
 95  30
         if (url == null) {
 96  0
             url = ClassLoader.getSystemResource(resource);
 97   
         }
 98   
 
 99  30
         if (url == null) {
 100  0
             throw new IOException("Could not find resource " + resource);
 101   
         }
 102  30
         return url;
 103   
     }
 104   
 
 105   
     /**
 106   
      * Returns a resource on the classpath as a Stream object
 107   
      * 
 108   
      * @param resource The resource to find
 109   
      * @return The resource
 110   
      * @throws java.io.IOException If the resource cannot be found or read
 111   
      */
 112  114
     public static InputStream getResourceAsStream(String resource)
 113   
             throws IOException {
 114  114
         return getResourceAsStream(getClassLoader(), resource);
 115   
     }
 116   
 
 117   
     /**
 118   
      * Returns a resource on the classpath as a Stream object
 119   
      * 
 120   
      * @param loader The classloader used to load the resource
 121   
      * @param resource The resource to find
 122   
      * @return The resource
 123   
      * @throws java.io.IOException If the resource cannot be found or read
 124   
      */
 125  114
     public static InputStream getResourceAsStream(ClassLoader loader,
 126   
             String resource) throws IOException {
 127  114
         InputStream in = null;
 128  114
         if (loader != null) {
 129  114
             in = loader.getResourceAsStream(resource);
 130   
         }
 131  114
         if (in == null) {
 132  0
             in = ClassLoader.getSystemResourceAsStream(resource);
 133   
         }
 134  114
         if (in == null) {
 135  0
             throw new IOException("Could not find resource " + resource);
 136   
         }
 137  114
         return in;
 138   
     }
 139   
 
 140   
     /**
 141   
      * Returns a resource on the classpath as a Properties object
 142   
      * 
 143   
      * @param resource The resource to find
 144   
      * @return The resource
 145   
      * @throws java.io.IOException If the resource cannot be found or read
 146   
      */
 147  30
     public static Properties getResourceAsProperties(String resource)
 148   
             throws IOException {
 149  30
         Properties props = new Properties();
 150  30
         InputStream in = null;
 151  30
         String propfile = resource;
 152  30
         in = getResourceAsStream(propfile);
 153  30
         props.load(in);
 154  30
         in.close();
 155  30
         return props;
 156   
     }
 157   
 
 158   
     /**
 159   
      * Returns a resource on the classpath as a Properties object
 160   
      * 
 161   
      * @param loader The classloader used to load the resource
 162   
      * @param resource The resource to find
 163   
      * @return The resource
 164   
      * @throws java.io.IOException If the resource cannot be found or read
 165   
      */
 166  0
     public static Properties getResourceAsProperties(ClassLoader loader,
 167   
             String resource) throws IOException {
 168  0
         Properties props = new Properties();
 169  0
         InputStream in = null;
 170  0
         String propfile = resource;
 171  0
         in = getResourceAsStream(loader, propfile);
 172  0
         props.load(in);
 173  0
         in.close();
 174  0
         return props;
 175   
     }
 176   
 
 177   
     /**
 178   
      * Returns a resource on the classpath as a Reader object
 179   
      * 
 180   
      * @param resource The resource to find
 181   
      * @return The resource
 182   
      * @throws java.io.IOException If the resource cannot be found or read
 183   
      */
 184  0
     public static Reader getResourceAsReader(String resource)
 185   
             throws IOException {
 186  0
         return new InputStreamReader(getResourceAsStream(resource));
 187   
     }
 188   
 
 189   
     /**
 190   
      * Returns a resource on the classpath as a Reader object
 191   
      * 
 192   
      * @param loader The classloader used to load the resource
 193   
      * @param resource The resource to find
 194   
      * @return The resource
 195   
      * @throws java.io.IOException If the resource cannot be found or read
 196   
      */
 197  0
     public static Reader getResourceAsReader(ClassLoader loader, String resource)
 198   
             throws IOException {
 199  0
         return new InputStreamReader(getResourceAsStream(loader, resource));
 200   
     }
 201   
 
 202   
     /**
 203   
      * Returns a resource on the classpath as a File object
 204   
      * 
 205   
      * @param resource The resource to find
 206   
      * @return The resource
 207   
      * @throws java.io.IOException If the resource cannot be found or read
 208   
      */
 209  0
     public static File getResourceAsFile(String resource) throws IOException {
 210  0
         return new File(getResourceURL(resource).getFile());
 211   
     }
 212   
 
 213   
     /**
 214   
      * Returns a resource on the classpath as a File object
 215   
      * 
 216   
      * @param loader - the classloader used to load the resource
 217   
      * @param resource - the resource to find
 218   
      * @return The resource
 219   
      * @throws java.io.IOException If the resource cannot be found or read
 220   
      */
 221  0
     public static File getResourceAsFile(ClassLoader loader, String resource)
 222   
             throws IOException {
 223  0
         return new File(getResourceURL(loader, resource).getFile());
 224   
     }
 225   
 
 226   
     /**
 227   
      * Gets a URL as an input stream
 228   
      * 
 229   
      * @param urlString - the URL to get
 230   
      * @return An input stream with the data from the URL
 231   
      * @throws java.io.IOException If the resource cannot be found or read
 232   
      */
 233  0
     public static InputStream getUrlAsStream(String urlString)
 234   
             throws IOException {
 235  0
         URL url = new URL(urlString);
 236  0
         URLConnection conn = url.openConnection();
 237  0
         return conn.getInputStream();
 238   
     }
 239   
 
 240   
     /**
 241   
      * Gets a URL as a Reader
 242   
      * 
 243   
      * @param urlString - the URL to get
 244   
      * @return A Reader with the data from the URL
 245   
      * @throws java.io.IOException If the resource cannot be found or read
 246   
      */
 247  0
     public static Reader getUrlAsReader(String urlString) throws IOException {
 248  0
         return new InputStreamReader(getUrlAsStream(urlString));
 249   
     }
 250   
 
 251   
     /**
 252   
      * Gets a URL as a Properties object
 253   
      * 
 254   
      * @param urlString - the URL to get
 255   
      * @return A Properties object with the data from the URL
 256   
      * @throws java.io.IOException If the resource cannot be found or read
 257   
      */
 258  0
     public static Properties getUrlAsProperties(String urlString)
 259   
             throws IOException {
 260  0
         Properties props = new Properties();
 261  0
         InputStream in = null;
 262  0
         String propfile = urlString;
 263  0
         in = getUrlAsStream(propfile);
 264  0
         props.load(in);
 265  0
         in.close();
 266  0
         return props;
 267   
     }
 268   
 
 269   
     /**
 270   
      * Loads a class
 271   
      * 
 272   
      * @param className - the class to load
 273   
      * @return The loaded class
 274   
      * @throws ClassNotFoundException If the class cannot be found (duh!)
 275   
      */
 276  153
     public static Class classForName(String className)
 277   
             throws ClassNotFoundException {
 278  153
         Class clazz = null;
 279  153
         clazz = getClassLoader().loadClass(className);
 280  153
         if (clazz == null) {
 281  0
             clazz = Class.forName(className);
 282   
         }
 283  153
         return clazz;
 284   
     }
 285   
 
 286   
     /**
 287   
      * Creates an instance of a class
 288   
      * 
 289   
      * @param className - the class to create
 290   
      * @return An instance of the class
 291   
      * @throws ClassNotFoundException If the class cannot be found (duh!)
 292   
      * @throws InstantiationException If the class cannot be instantiaed
 293   
      * @throws IllegalAccessException If the class is not public, or other
 294   
      *             access problems arise
 295   
      */
 296  21
     public static Object instantiate(String className)
 297   
             throws ClassNotFoundException, InstantiationException,
 298   
             IllegalAccessException {
 299  21
         return instantiate(classForName(className));
 300   
     }
 301   
 
 302   
     /**
 303   
      * Creates an instance of a class
 304   
      * 
 305   
      * @param clazz - the class to create
 306   
      * @return An instance of the class
 307   
      * @throws InstantiationException If the class cannot be instantiaed
 308   
      * @throws IllegalAccessException If the class is not public, or other
 309   
      *             access problems arise
 310   
      */
 311  21
     public static Object instantiate(Class clazz)
 312   
             throws InstantiationException, IllegalAccessException {
 313  21
         return clazz.newInstance();
 314   
     }
 315   
 
 316  297
     private static ClassLoader getClassLoader() {
 317  297
         if (defaultClassLoader != null) {
 318  0
             return defaultClassLoader;
 319   
         } else {
 320  297
             return Thread.currentThread().getContextClassLoader();
 321   
         }
 322   
     }
 323   
 
 324   
 }