KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mortbay > util > Loader


1 // ========================================================================
2
// $Id: Loader.java,v 1.4 2004/11/05 07:09:33 gregwilkins Exp $
3
// Copyright 1999-2004 Mort Bay Consulting Pty. Ltd.
4
// ------------------------------------------------------------------------
5
// Licensed under the Apache License, Version 2.0 (the "License");
6
// you may not use this file except in compliance with the License.
7
// You may obtain a copy of the License at
8
// http://www.apache.org/licenses/LICENSE-2.0
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
// ========================================================================
15

16 package org.mortbay.util;
17
18 /* ------------------------------------------------------------ */
19 /** ClassLoader Helper.
20  * This helper class allows classes to be loaded either from the
21  * Thread's ContextClassLoader, the classloader of the derived class
22  * or the system ClassLoader.
23  *
24  * <B>Usage:</B><PRE>
25  * public class MyClass {
26  * void myMethod() {
27  * ...
28  * Class c=Loader.loadClass(this.getClass(),classname);
29  * ...
30  * }
31  * </PRE>
32  * @version $Id: Loader.java,v 1.4 2004/11/05 07:09:33 gregwilkins Exp $
33  * @author Greg Wilkins (gregw)
34  */

35 public class Loader
36 {
37     /* ------------------------------------------------------------ */
38     public static Class JavaDoc loadClass(Class JavaDoc loadClass,String JavaDoc name)
39         throws ClassNotFoundException JavaDoc
40     {
41         ClassNotFoundException JavaDoc cnfe=null;
42         ClassLoader JavaDoc loader=Thread.currentThread().getContextClassLoader();
43         if (loader!=null)
44         {
45             try
46             {
47                return loader.loadClass(name);
48             }
49             catch (ClassNotFoundException JavaDoc e)
50             {
51                cnfe=e;
52             }
53         }
54
55         loader=loadClass.getClassLoader();
56         if (loader!=null)
57         {
58             try
59             {
60                return loader.loadClass(name);
61             }
62             catch (ClassNotFoundException JavaDoc e)
63             {
64                if(cnfe==null)cnfe=e;
65             }
66         }
67
68
69        try
70        {
71             return Class.forName(name);
72        }
73        catch (ClassNotFoundException JavaDoc e)
74        {
75             if(cnfe==null)cnfe=e;
76         throw cnfe;
77        }
78
79     }
80 }
81
82
Popular Tags