View Javadoc

    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      public ResourceHelper() {
    50      }
    51
    52      /***
    53       * Returns the default classloader (may be null).
    54       *
    55       * @return The default classloader
    56       */
    57      public static ClassLoader getDefaultClassLoader() {
    58          return defaultClassLoader;
    59      }
    60
    61      /***
    62       * Sets the default classloader
    63       *
    64       * @param defaultClassLoader - the new default ClassLoader
    65       */
    66      public static void setDefaultClassLoader(ClassLoader defaultClassLoader) {
    67          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      public static URL getResourceURL(String resource) throws IOException {
    78          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      public static URL getResourceURL(ClassLoader loader, String resource)
    90              throws IOException {
    91          URL url = null;
    92          if (loader != null) {
    93              url = loader.getResource(resource);
    94          }
    95          if (url == null) {
    96              url = ClassLoader.getSystemResource(resource);
    97          }
    98
    99          if (url == null) {
    100             throw new IOException("Could not find resource " + resource);
    101         }
    102         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     public static InputStream getResourceAsStream(String resource)
    113             throws IOException {
    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     public static InputStream getResourceAsStream(ClassLoader loader,
    126             String resource) throws IOException {
    127         InputStream in = null;
    128         if (loader != null) {
    129             in = loader.getResourceAsStream(resource);
    130         }
    131         if (in == null) {
    132             in = ClassLoader.getSystemResourceAsStream(resource);
    133         }
    134         if (in == null) {
    135             throw new IOException("Could not find resource " + resource);
    136         }
    137         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     public static Properties getResourceAsProperties(String resource)
    148             throws IOException {
    149         Properties props = new Properties();
    150         InputStream in = null;
    151         String propfile = resource;
    152         in = getResourceAsStream(propfile);
    153         props.load(in);
    154         in.close();
    155         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     public static Properties getResourceAsProperties(ClassLoader loader,
    167             String resource) throws IOException {
    168         Properties props = new Properties();
    169         InputStream in = null;
    170         String propfile = resource;
    171         in = getResourceAsStream(loader, propfile);
    172         props.load(in);
    173         in.close();
    174         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     public static Reader getResourceAsReader(String resource)
    185             throws IOException {
    186         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     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 The resource to find
    206      * @return The resource
    207      * @throws java.io.IOException If the resource cannot be found or read
    208      */
    209     public static File getResourceAsFile(String resource) throws IOException {
    210         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     public static File getResourceAsFile(ClassLoader loader, String resource)
    222             throws IOException {
    223         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     public static InputStream getUrlAsStream(String urlString)
    234             throws IOException {
    235         URL url = new URL(urlString);
    236         URLConnection conn = url.openConnection();
    237         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     public static Reader getUrlAsReader(String urlString) throws IOException {
    248         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     public static Properties getUrlAsProperties(String urlString)
    259             throws IOException {
    260         Properties props = new Properties();
    261         InputStream in = null;
    262         String propfile = urlString;
    263         in = getUrlAsStream(propfile);
    264         props.load(in);
    265         in.close();
    266         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     public static Class classForName(String className)
    277             throws ClassNotFoundException {
    278         Class clazz = null;
    279         clazz = getClassLoader().loadClass(className);
    280         if (clazz == null) {
    281             clazz = Class.forName(className);
    282         }
    283         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     public static Object instantiate(String className)
    297             throws ClassNotFoundException, InstantiationException,
    298             IllegalAccessException {
    299         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     public static Object instantiate(Class clazz)
    312             throws InstantiationException, IllegalAccessException {
    313         return clazz.newInstance();
    314     }
    315
    316     private static ClassLoader getClassLoader() {
    317         if (defaultClassLoader != null) {
    318             return defaultClassLoader;
    319         } else {
    320             return Thread.currentThread().getContextClassLoader();
    321         }
    322     }
    323
    324 }