View Javadoc

1   /*
2    *  jDTAUS Core RI Client Container
3    *  Copyright (C) 2005 Christian Schulte
4    *  <cs@schulte.it>
5    *
6    *  This library is free software; you can redistribute it and/or
7    *  modify it under the terms of the GNU Lesser General Public
8    *  License as published by the Free Software Foundation; either
9    *  version 2.1 of the License, or any later version.
10   *
11   *  This library is distributed in the hope that it will be useful,
12   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   *  Lesser General Public License for more details.
15   *
16   *  You should have received a copy of the GNU Lesser General Public
17   *  License along with this library; if not, write to the Free Software
18   *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19   *
20   */
21  package org.jdtaus.core.container.ri.client;
22  
23  import java.io.IOException;
24  import java.util.Enumeration;
25  
26  /**
27   * Utility for loading classes.
28   *
29   * @author <a href="mailto:cs@schulte.it">Christian Schulte</a>
30   * @version $JDTAUS: ClassLoaderFactory.java 8641 2012-09-27 06:45:17Z schulte $
31   */
32  abstract class ClassLoaderFactory
33  {
34  
35      /**
36       * Name of the system property controlling the use of the context
37       * classloader.
38       */
39      private static final String SYS_ENABLE_CONTEXT_CLASSLOADER =
40          "org.jdtaus.core.container.ClassLoaderFactory.enableContextClassloader";
41  
42      /**
43       * Loads a class.
44       * <p>This method tries to load the class named {@code className} using the
45       * current thread's context classloader, if that classloader is enabled and
46       * not {@code null}, or the classloader of the given class, if no context
47       * classloader is available or the context classloader is disabled. Use of
48       * the context classloader must be explicitly enabled by setting the system
49       * property {@code org.jdtaus.core.container.ClassLoaderFactory.enableContextClassloader}
50       * to {@code true}.</p>
51       *
52       * @param clazz The clazz whose classloader to use for loading classes, if
53       * no thread context classloader is available or if the thread context
54       * classloader is disabled.
55       * @param className The name of the class to load.
56       *
57       * @return The class with name {@code className}.
58       *
59       * @throws ClassNotFoundException if no class matching {@code className} was
60       * found.
61       * @throws NullPointerException if {@code clazz} or {@code className} is
62       * {@code null}.
63       */
64      static Class loadClass( final Class clazz,
65                              final String className )
66          throws ClassNotFoundException
67      {
68          if ( clazz == null )
69          {
70              throw new NullPointerException( "clazz" );
71          }
72          if ( className == null )
73          {
74              throw new NullPointerException( "className" );
75          }
76  
77          return Class.forName( className, true, getClassLoader( clazz ) );
78      }
79  
80      /**
81       * Loads resources.
82       * <p>This method tries to load resources matching {@code resourceName}
83       * using the current thread's context classloader, if that classloader is
84       * enabled and not {@code null}, or using the classloader of the given
85       * class, if the context classloader has been disabled. Use of the context
86       * classloader must be explicitly enabled by setting the system
87       * property {@code org.jdtaus.core.container.ClassLoaderFactory.enableContextClassloader}
88       * to {@code true}.</p>
89       *
90       * @param clazz The clazz whose classloader to use for loading classes, if
91       * no thread context classloader is available or if the thread context
92       * classloader is disabled.
93       * @param resourceName The name of the resources to load.
94       *
95       * @return All resources matching with name {@code resourceName}.
96       *
97       * @throws IOException if loading resources fails.
98       * @throws NullPointerException if {@code clazz} or {@code resourceName} is
99       * {@code null}.
100      */
101     static Enumeration loadResources( final Class clazz,
102                                       final String resourceName )
103         throws IOException
104     {
105         if ( clazz == null )
106         {
107             throw new NullPointerException( "clazz" );
108         }
109         if ( resourceName == null )
110         {
111             throw new NullPointerException( "resourceName" );
112         }
113 
114         return getClassLoader( clazz ).getResources( resourceName );
115     }
116 
117     private static ClassLoader getClassLoader( final Class clazz )
118     {
119         final ClassLoader classLoader;
120         if ( Boolean.getBoolean( SYS_ENABLE_CONTEXT_CLASSLOADER ) &&
121              Thread.currentThread().getContextClassLoader() != null )
122         {
123             classLoader = Thread.currentThread().getContextClassLoader();
124         }
125         else
126         {
127             classLoader = clazz.getClassLoader() != null
128                           ? clazz.getClassLoader()
129                           : ClassLoader.getSystemClassLoader();
130 
131         }
132 
133         return classLoader;
134     }
135 
136 }