View Javadoc

    1   /***
    2    * JFoxSOAF, Service-Oriented Application Framework
    3    *
    4    * Copyright (C) www.huihoo.org
    5    *
    6    * Distributable under GNU LGPL
    7    *
    8    * For more information, please visit: http://www.huihoo.org/jfox/jfoxsoaf
    9    */
    10
    11  package org.huihoo.jfox.soaf.util.resource;
    12
    13  import java.io.File;
    14  import java.io.IOException;
    15  import java.io.InputStream;
    16  import java.io.InputStreamReader;
    17  import java.io.Reader;
    18  import java.net.URL;
    19  import java.net.URLConnection;
    20  import java.util.Properties;
    21
    22  /***
    23   * A class to simplify access to resources through the classloader.
    24   *
    25   * @author <a href="mailto:founder_chen@yahoo.com.cn">Peter Cheng </a>
    26   * @author <a href="mailto:clinton.begin@ibatis.com">Clinton Begin </a>
    27   * @version $Revision: 1.1 $ $Date: 2004/10/28 05:12:31 $
    28   * @version Revision: 1.0
    29   */
    30
    31  public class ResourceHelper {
    32
    33      private static ClassLoader defaultClassLoader;
    34
    35      private ResourceHelper() {
    36      }
    37
    38      /***
    39       * Returns the default classloader (may be null).
    40       *
    41       * @return The default classloader
    42       */
    43      public static ClassLoader getDefaultClassLoader() {
    44          return defaultClassLoader;
    45      }
    46
    47      /***
    48       * Sets the default classloader
    49       *
    50       * @param defaultClassLoader -
    51       *            the new default ClassLoader
    52       */
    53      public static void setDefaultClassLoader(ClassLoader defaultClassLoader) {
    54          ResourceHelper.defaultClassLoader = defaultClassLoader;
    55      }
    56
    57      /***
    58       * Returns the URL of the resource on the classpath
    59       *
    60       * @param resource
    61       *            The resource to find
    62       * @return The resource
    63       * @throws IOException
    64       *             If the resource cannot be found or read
    65       */
    66      public static URL getResourceURL(String resource) throws IOException {
    67          return getResourceURL(getClassLoader(), resource);
    68      }
    69
    70      /***
    71       * Returns the URL of the resource on the classpath
    72       *
    73       * @param loader
    74       *            The classloader used to load the resource
    75       * @param resource
    76       *            The resource to find
    77       * @return The resource
    78       * @throws IOException
    79       *             If the resource cannot be found or read
    80       */
    81      public static URL getResourceURL(ClassLoader loader, String resource)
    82              throws IOException {
    83          URL url = null;
    84          if (loader != null)
    85              url = loader.getResource(resource);
    86          if (url == null)
    87              url = ClassLoader.getSystemResource(resource);
    88          if (url == null)
    89              throw new IOException("Could not find resource " + resource);
    90          return url;
    91      }
    92
    93      /***
    94       * Returns a resource on the classpath as a Stream object
    95       *
    96       * @param resource
    97       *            The resource to find
    98       * @return The resource
    99       * @throws IOException
    100      *             If the resource cannot be found or read
    101      */
    102     public static InputStream getResourceAsStream(String resource)
    103             throws IOException {
    104         return getResourceAsStream(getClassLoader(), resource);
    105     }
    106
    107     /***
    108      * Returns a resource on the classpath as a Stream object
    109      *
    110      * @param loader
    111      *            The classloader used to load the resource
    112      * @param resource
    113      *            The resource to find
    114      * @return The resource
    115      * @throws IOException
    116      *             If the resource cannot be found or read
    117      */
    118     public static InputStream getResourceAsStream(ClassLoader loader,
    119             String resource) throws IOException {
    120         InputStream in = null;
    121         if (loader != null)
    122             in = loader.getResourceAsStream(resource);
    123         if (in == null)
    124             in = ClassLoader.getSystemResourceAsStream(resource);
    125         if (in == null)
    126             throw new IOException("Could not find resource " + resource);
    127         return in;
    128     }
    129
    130     /***
    131      * Returns a resource on the classpath as a Properties object
    132      *
    133      * @param resource
    134      *            The resource to find
    135      * @return The resource
    136      * @throws IOException
    137      *             If the resource cannot be found or read
    138      */
    139     public static Properties getResourceAsProperties(String resource)
    140             throws IOException {
    141         Properties props = new Properties();
    142         InputStream in = null;
    143         String propfile = resource;
    144         in = getResourceAsStream(propfile);
    145         props.load(in);
    146         in.close();
    147         return props;
    148     }
    149
    150     /***
    151      * Returns a resource on the classpath as a Properties object
    152      *
    153      * @param loader
    154      *            The classloader used to load the resource
    155      * @param resource
    156      *            The resource to find
    157      * @return The resource
    158      * @throws IOException
    159      *             If the resource cannot be found or read
    160      */
    161     public static Properties getResourceAsProperties(ClassLoader loader,
    162             String resource) throws IOException {
    163         Properties props = new Properties();
    164         InputStream in = null;
    165         String propfile = resource;
    166         in = getResourceAsStream(loader, propfile);
    167         props.load(in);
    168         in.close();
    169         return props;
    170     }
    171
    172     /***
    173      * Returns a resource on the classpath as a Reader object
    174      *
    175      * @param resource
    176      *            The resource to find
    177      * @return The resource
    178      * @throws IOException
    179      *             If the resource cannot be found or read
    180      */
    181     public static Reader getResourceAsReader(String resource)
    182             throws IOException {
    183         return new InputStreamReader(getResourceAsStream(resource));
    184     }
    185
    186     /***
    187      * Returns a resource on the classpath as a Reader object
    188      *
    189      * @param loader
    190      *            The classloader used to load the resource
    191      * @param resource
    192      *            The resource to find
    193      * @return The resource
    194      * @throws IOException
    195      *             If the resource cannot be found or read
    196      */
    197     public static Reader getResourceAsReader(ClassLoader loader, String resource)
    198             throws IOException {
    199         return new InputStreamReader(getResourceAsStream(loader, resource));
    200     }
    201
    202     /***
    203      * Returns a resource on the classpath as a File object
    204      *
    205      * @param resource
    206      *            The resource to find
    207      * @return The resource
    208      * @throws IOException
    209      *             If the resource cannot be found or read
    210      */
    211     public static File getResourceAsFile(String resource) throws IOException {
    212         return new File(getResourceURL(resource).getFile());
    213     }
    214
    215     /***
    216      * Returns a resource on the classpath as a File object
    217      *
    218      * @param loader -
    219      *            the classloader used to load the resource
    220      * @param resource -
    221      *            the resource to find
    222      * @return The resource
    223      * @throws IOException
    224      *             If the resource cannot be found or read
    225      */
    226     public static File getResourceAsFile(ClassLoader loader, String resource)
    227             throws IOException {
    228         return new File(getResourceURL(loader, resource).getFile());
    229     }
    230
    231     /***
    232      * Gets a URL as an input stream
    233      *
    234      * @param urlString -
    235      *            the URL to get
    236      * @return An input stream with the data from the URL
    237      * @throws IOException
    238      *             If the resource cannot be found or read
    239      */
    240     public static InputStream getUrlAsStream(String urlString)
    241             throws IOException {
    242         URL url = new URL(urlString);
    243         URLConnection conn = url.openConnection();
    244         return conn.getInputStream();
    245     }
    246
    247     /***
    248      * Gets a URL as a Reader
    249      *
    250      * @param urlString -
    251      *            the URL to get
    252      * @return A Reader with the data from the URL
    253      * @throws IOException
    254      *             If the resource cannot be found or read
    255      */
    256     public static Reader getUrlAsReader(String urlString) throws IOException {
    257         return new InputStreamReader(getUrlAsStream(urlString));
    258     }
    259
    260     /***
    261      * Gets a URL as a Properties object
    262      *
    263      * @param urlString -
    264      *            the URL to get
    265      * @return A Properties object with the data from the URL
    266      * @throws IOException
    267      *             If the resource cannot be found or read
    268      */
    269     public static Properties getUrlAsProperties(String urlString)
    270             throws IOException {
    271         Properties props = new Properties();
    272         InputStream in = null;
    273         String propfile = urlString;
    274         in = getUrlAsStream(propfile);
    275         props.load(in);
    276         in.close();
    277         return props;
    278     }
    279
    280     /***
    281      * Loads a class
    282      *
    283      * @param className -
    284      *            the class to load
    285      * @return The loaded class
    286      * @throws ClassNotFoundException
    287      *             If the class cannot be found (duh!)
    288      */
    289     public static Class classForName(String className)
    290             throws ClassNotFoundException {
    291         Class clazz = null;
    292         try {
    293             clazz = getClassLoader().loadClass(className);
    294         } catch (Exception e) {
    295             // Ignore. Failsafe below.
    296         }
    297         if (clazz == null) {
    298             clazz = Class.forName(className);
    299         }
    300         return clazz;
    301     }
    302
    303     /***
    304      * Creates an instance of a class
    305      *
    306      * @param className -
    307      *            the class to create
    308      * @return An instance of the class
    309      * @throws ClassNotFoundException
    310      *             If the class cannot be found (duh!)
    311      * @throws InstantiationException
    312      *             If the class cannot be instantiaed
    313      * @throws IllegalAccessException
    314      *             If the class is not public, or other access problems arise
    315      */
    316     public static Object instantiate(String className)
    317             throws ClassNotFoundException, InstantiationException,
    318             IllegalAccessException {
    319         return instantiate(classForName(className));
    320     }
    321
    322     /***
    323      * Creates an instance of a class
    324      *
    325      * @param clazz -
    326      *            the class to create
    327      * @return An instance of the class
    328      * @throws InstantiationException
    329      *             If the class cannot be instantiaed
    330      * @throws IllegalAccessException
    331      *             If the class is not public, or other access problems arise
    332      */
    333     public static Object instantiate(Class clazz)
    334             throws InstantiationException, IllegalAccessException {
    335         return clazz.newInstance();
    336     }
    337
    338     private static ClassLoader getClassLoader() {
    339         if (defaultClassLoader != null) {
    340             return defaultClassLoader;
    341         } else {
    342             return Thread.currentThread().getContextClassLoader();
    343         }
    344     }
    345
    346 }