View Javadoc

    1   // ========================================================================
    2   // Copyright 2004-2005 Mort Bay Consulting Pty. Ltd.
    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   // http://www.apache.org/licenses/LICENSE-2.0
    8   // Unless required by applicable law or agreed to in writing, software
    9   // distributed under the License is distributed on an "AS IS" BASIS,
    10  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  // ========================================================================
    14
    15  package org.mortbay.util;
    16
    17  import java.net.URL;
    18  import java.util.Locale;
    19  import java.util.MissingResourceException;
    20  import java.util.ResourceBundle;
    21
    22  /* ------------------------------------------------------------ */
    23  /** ClassLoader Helper.
    24   * This helper class allows classes to be loaded either from the
    25   * Thread's ContextClassLoader, the classloader of the derived class
    26   * or the system ClassLoader.
    27   *
    28   * <B>Usage:</B><PRE>
    29   * public class MyClass {
    30   *     void myMethod() {
    31   *          ...
    32   *          Class c=Loader.loadClass(this.getClass(),classname);
    33   *          ...
    34   *     }
    35   * </PRE>
    36   * @author Greg Wilkins (gregw)
    37   */
    38  public class Loader
    39  {
    40      /* ------------------------------------------------------------ */
    41      public static URL getResource(Class loadClass,String name, boolean checkParents)
    42          throws ClassNotFoundException
    43      {
    44          URL url =null;
    45          ClassLoader loader=Thread.currentThread().getContextClassLoader();
    46          while (url==null && loader!=null )
    47          {
    48              url=loader.getResource(name);
    49              loader=(url==null&&checkParents)?loader.getParent():null;
    50          }
    51
    52          loader=loadClass==null?null:loadClass.getClassLoader();
    53          while (url==null && loader!=null )
    54          {
    55              url=loader.getResource(name);
    56              loader=(url==null&&checkParents)?loader.getParent():null;
    57          }
    58
    59          if (url==null)
    60          {
    61              url=ClassLoader.getSystemResource(name);
    62          }
    63
    64          return url;
    65      }
    66
    67      /* ------------------------------------------------------------ */
    68      public static Class loadClass(Class loadClass,String name)
    69          throws ClassNotFoundException
    70      {
    71          return loadClass(loadClass,name,false);
    72      }
    73
    74      /* ------------------------------------------------------------ */
    75      /** Load a class.
    76       *
    77       * @param loadClass
    78       * @param name
    79       * @param checkParents If true, try loading directly from parent classloaders.
    80       * @return Class
    81       * @throws ClassNotFoundException
    82       */
    83      public static Class loadClass(Class loadClass,String name,boolean checkParents)
    84          throws ClassNotFoundException
    85      {
    86          ClassNotFoundException ex=null;
    87          Class c =null;
    88          ClassLoader loader=Thread.currentThread().getContextClassLoader();
    89          while (c==null && loader!=null )
    90          {
    91              try { c=loader.loadClass(name); }
    92              catch (ClassNotFoundException e) {if(ex==null)ex=e;}
    93              loader=(c==null&&checkParents)?loader.getParent():null;
    94          }
    95
    96          loader=loadClass==null?null:loadClass.getClassLoader();
    97          while (c==null && loader!=null )
    98          {
    99              try { c=loader.loadClass(name); }
    100             catch (ClassNotFoundException e) {if(ex==null)ex=e;}
    101             loader=(c==null&&checkParents)?loader.getParent():null;
    102         }
    103
    104         if (c==null)
    105         {
    106             try { c=Class.forName(name); }
    107             catch (ClassNotFoundException e) {if(ex==null)ex=e;}
    108         }
    109
    110         if (c!=null)
    111             return c;
    112         throw ex;
    113     }
    114
    115     public static ResourceBundle getResourceBundle(Class loadClass,String name,boolean checkParents, Locale locale)
    116         throws MissingResourceException
    117     {
    118         MissingResourceException ex=null;
    119         ResourceBundle bundle =null;
    120         ClassLoader loader=Thread.currentThread().getContextClassLoader();
    121         while (bundle==null && loader!=null )
    122         {
    123             try { bundle=ResourceBundle.getBundle(name, locale, loader); }
    124             catch (MissingResourceException e) {if(ex==null)ex=e;}
    125             loader=(bundle==null&&checkParents)?loader.getParent():null;
    126         }
    127
    128         loader=loadClass==null?null:loadClass.getClassLoader();
    129         while (bundle==null && loader!=null )
    130         {
    131             try { bundle=ResourceBundle.getBundle(name, locale, loader); }
    132             catch (MissingResourceException e) {if(ex==null)ex=e;}
    133             loader=(bundle==null&&checkParents)?loader.getParent():null;
    134         }
    135
    136         if (bundle==null)
    137         {
    138             try { bundle=ResourceBundle.getBundle(name, locale); }
    139             catch (MissingResourceException e) {if(ex==null)ex=e;}
    140         }
    141
    142         if (bundle!=null)
    143             return bundle;
    144         throw ex;
    145     }
    146
    147
    148 }
    149