01: /**
02: * EasyBeans
03: * Copyright (C) 2006 Bull S.A.S.
04: * Contact: easybeans@ow2.org
05: *
06: * This library is free software; you can redistribute it and/or
07: * modify it under the terms of the GNU Lesser General Public
08: * License as published by the Free Software Foundation; either
09: * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19: * USA
20: *
21: * --------------------------------------------------------------------------
22: * $Id: ClassUtils.java 1970 2007-10-16 11:49:25Z benoitf $
23: * --------------------------------------------------------------------------
24: */package org.ow2.easybeans.util.loader;
25:
26: /**
27: * The <code>ClassUtils</code> class is the central point used to load classes.
28: * @author Guillaume Sauthier
29: */
30: public final class ClassUtils {
31:
32: /**
33: * Default constructor.
34: */
35: private ClassUtils() {
36: }
37:
38: /**
39: * Look up the class in the Tread Context ClassLoader and in the "current" ClassLoader.
40: * @param className The class name to load
41: * @return the corresponding Class instance
42: * @throws ClassNotFoundException if the Class was not found.
43: */
44: public static Class forName(final String className)
45: throws ClassNotFoundException {
46: // Load classes from different classloaders :
47: // 1. Thread Context ClassLoader
48: // 2. ClassUtils ClassLoader
49:
50: ClassLoader tccl = Thread.currentThread()
51: .getContextClassLoader();
52: Class cls = null;
53:
54: try {
55: // Try with TCCL
56: cls = Class.forName(className, true, tccl);
57: } catch (ClassNotFoundException cnfe) {
58:
59: // Try now with the classloader used to load ClassUtils
60: ClassLoader current = ClassUtils.class.getClassLoader();
61: try {
62: cls = Class.forName(className, true, current);
63: } catch (ClassNotFoundException cnfe2) {
64: // If this is still unknown, throw an Exception
65: throw cnfe2;
66: }
67: }
68:
69: return cls;
70: }
71:
72: }
|