View Javadoc

1   /*
2    * Copyright 2002 - 2008 JEuclid, http://jeuclid.sf.net
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    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  /* $Id: ClassLoaderSupport.java,v 88b901bf20fb 2008/06/07 14:12:27 maxberger $ */
18  
19  package net.sourceforge.jeuclid.elements.support;
20  
21  /**
22   * Support utilities for classloading.
23   * 
24   * @version $Revision: 88b901bf20fb $
25   */
26  public final class ClassLoaderSupport {
27  
28      private static final class SingletonHolder {
29          private static final ClassLoaderSupport INSTANCE = new ClassLoaderSupport();
30  
31          private SingletonHolder() {
32          }
33      }
34  
35      /**
36       * Default Constructor.
37       */
38      protected ClassLoaderSupport() {
39          // Empty on purporse.
40      }
41  
42      /**
43       * accessor for singleton instance.
44       * 
45       * @return an instance of this class.
46       */
47      public static ClassLoaderSupport getInstance() {
48          return ClassLoaderSupport.SingletonHolder.INSTANCE;
49      }
50  
51      /**
52       * Try to load the given lass from the current context.
53       * 
54       * @param className
55       *            name of the class to load
56       * @return a Class object for the given class if possible.
57       * @throws ClassNotFoundException
58       *             if the class could not be found.
59       * @see ClassLoader#loadClass(String)
60       */
61      public Class<?> loadClass(final String className)
62              throws ClassNotFoundException {
63          Class<?> retVal;
64          try {
65              retVal = Thread.currentThread().getContextClassLoader().loadClass(
66                      className);
67          } catch (final ClassNotFoundException e) {
68              retVal = ClassLoaderSupport.class.getClassLoader().loadClass(
69                      className);
70          }
71          return retVal;
72      }
73  
74  }