KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > util > loader > ClassUtils


1 /**
2  * EasyBeans
3  * Copyright (C) 2006 Bull S.A.S.
4  * Contact: easybeans@objectweb.org
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: ClassUtils.java 921 2006-07-25 09:55:35Z sauthieg $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.easybeans.util.loader;
27
28 /**
29  * The <code>ClassUtils</code> class is the central point used to load classes.
30  * @author Guillaume Sauthier
31  */

32 public final class ClassUtils {
33
34     /**
35      * Default constructor.
36      */

37     private ClassUtils() {}
38
39     /**
40      * Look up the class in the Tread Context ClassLoader and in the "current" ClassLoader.
41      * @param className The class name to load
42      * @return the corresponding Class instance
43      * @throws ClassNotFoundException if the Class was not found.
44      */

45     public static Class JavaDoc forName(final String JavaDoc className) throws ClassNotFoundException JavaDoc {
46         // Load classes from different classloaders :
47
// 1. Thread Context ClassLoader
48
// 2. ClassUtils ClassLoader
49

50         ClassLoader JavaDoc tccl = Thread.currentThread().getContextClassLoader();
51         Class JavaDoc cls = null;
52
53         try {
54             // Try with TCCL
55
cls = Class.forName(className, true, tccl);
56         } catch (ClassNotFoundException JavaDoc cnfe) {
57
58             // Try now with the classloader used to load ClassUtils
59
ClassLoader JavaDoc current = ClassUtils.class.getClassLoader();
60             try {
61                 cls = Class.forName(className, true, current);
62             } catch (ClassNotFoundException JavaDoc cnfe2) {
63                 // If this is still unknown, throw an Exception
64
throw cnfe2;
65             }
66         }
67
68         return cls;
69     }
70
71 }
72
Popular Tags