1 /*
2 * jDTAUS Core API
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;
22
23 /**
24 * Utility for loading classes.
25 *
26 * @author <a href="mailto:cs@schulte.it">Christian Schulte</a>
27 * @version $JDTAUS: ClassLoaderFactory.java 8641 2012-09-27 06:45:17Z schulte $
28 */
29 abstract class ClassLoaderFactory
30 {
31
32 /**
33 * Name of the system property controlling the use of the context
34 * classloader.
35 */
36 private static final String SYS_ENABLE_CONTEXT_CLASSLOADER =
37 "org.jdtaus.core.container.ClassLoaderFactory.enableContextClassloader";
38
39 /**
40 * Loads a class.
41 * <p>This method tries to load the class named {@code className} using the
42 * current thread's context classloader, if that classloader is enabled and
43 * not {@code null}, or the classloader of the given class, if no context
44 * classloader is available or the context classloader is disabled. Use of
45 * the context classloader must be explicitly enabled by setting the system
46 * property {@code org.jdtaus.core.container.ClassLoaderFactory.enableContextClassloader}
47 * to {@code true}.</p>
48 *
49 * @param clazz The clazz whose classloader to use for loading classes, if
50 * no thread context classloader is available or if the thread context
51 * classloader is disabled.
52 * @param className The name of the class to load.
53 *
54 * @return The class with name {@code className}.
55 *
56 * @throws ClassNotFoundException if no class matching {@code className} was
57 * found.
58 * @throws NullPointerException if {@code clazz} or {@code className} is
59 * {@code null}.
60 */
61 static Class loadClass( final Class clazz,
62 final String className )
63 throws ClassNotFoundException
64 {
65 if ( clazz == null )
66 {
67 throw new NullPointerException( "clazz" );
68 }
69 if ( className == null )
70 {
71 throw new NullPointerException( "className" );
72 }
73
74 final ClassLoader classLoader;
75 if ( Boolean.getBoolean( SYS_ENABLE_CONTEXT_CLASSLOADER ) &&
76 Thread.currentThread().getContextClassLoader() != null )
77 {
78 classLoader = Thread.currentThread().getContextClassLoader();
79 }
80 else
81 {
82 classLoader = clazz.getClassLoader() != null
83 ? clazz.getClassLoader()
84 : ClassLoader.getSystemClassLoader();
85
86 }
87
88 return Class.forName( className, true, classLoader );
89 }
90
91 }