KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis > utils > cache > ClassCache


1 /*
2  * Copyright 2001-2002,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.axis.utils.cache;
18
19 import org.apache.axis.utils.ClassUtils;
20
21 import java.util.Hashtable JavaDoc;
22
23 /**
24  * A cache class for JavaClass objects, which enables us to quickly reference
25  * methods.
26  *
27  * @author Doug Davis (dug@us.ibm.com)
28  * @author Glen Daniels (gdaniels@apache.org)
29  */

30 public class ClassCache {
31     Hashtable JavaDoc classCache = new Hashtable JavaDoc();
32
33     public ClassCache() {
34     }
35
36     /**
37      * Register a class in the cache. Creates a new JavaClass object
38      * around the given class, and inserts it into the Hashtable, replacing
39      * any previous entry.
40      *
41      * @param name the name of the class.
42      * @param cls a Java Class.
43      */

44     public synchronized void registerClass(String JavaDoc name, Class JavaDoc cls) {
45         if (name == null) return; // ??? Should we let this NPE?
46
JavaClass oldClass = (JavaClass) classCache.get(name);
47         if (oldClass != null && oldClass.getJavaClass() == cls) return;
48         classCache.put(name, new JavaClass(cls));
49     }
50
51     /**
52      * Remove an entry from the cache.
53      *
54      * @param name the name of the class to remove.
55      */

56     public synchronized void deregisterClass(String JavaDoc name) {
57         classCache.remove(name);
58     }
59
60     /**
61      * Query a given class' cache status.
62      *
63      * @param name a class name
64      * @return true if the class is in the cache, false otherwise
65      */

66     public boolean isClassRegistered(String JavaDoc name) {
67         return (classCache != null && classCache.get(name) != null);
68     }
69
70     /**
71      * Find the cached JavaClass entry for this class, creating one
72      * if necessary.
73      * @param className name of the class desired
74      * @param cl ClassLoader to use if we need to load the class
75      * @return JavaClass entry
76      */

77     public JavaClass lookup(String JavaDoc className, ClassLoader JavaDoc cl)
78             throws ClassNotFoundException JavaDoc {
79         if (className == null) {
80             return null;
81         }
82         JavaClass jc = (JavaClass) classCache.get(className);
83         if ((jc == null) && (cl != null)) {
84             // Try to load the class with the specified classloader
85
Class JavaDoc cls = ClassUtils.forName(className, true, cl);
86             jc = new JavaClass(cls);
87         }
88         return jc;
89     }
90 }
91
92 ;
93
Popular Tags