KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > xb > binding > introspection > ClassInfos


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.xb.binding.introspection;
23
24 import java.util.Map JavaDoc;
25 import java.util.WeakHashMap JavaDoc;
26 import java.lang.ref.WeakReference JavaDoc;
27 import org.jboss.xb.binding.JBossXBRuntimeException;
28 import org.jboss.xb.util.NoopMap;
29 import EDU.oswego.cs.dl.util.concurrent.ConcurrentHashMap;
30
31 /**
32  * @author <a HREF="mailto:alex@jboss.org">Alexey Loubyansky</a>
33  * @version <tt>$Revision: $</tt>
34  */

35 public class ClassInfos
36 {
37    private static Map JavaDoc classloaderCache = new WeakHashMap JavaDoc();
38
39    /**
40     * Disables caching of ClassInfo's. Already cached ClassInfo's will be lost after
41     * the method returns.
42     */

43    public static void disableCache()
44    {
45       synchronized(classloaderCache)
46       {
47          classloaderCache = NoopMap.INSTANCE;
48       }
49    }
50
51    /**
52     * Enables caching of ClassInfo's unless caching is already enabled.
53     */

54    public static void enableCache()
55    {
56       synchronized(classloaderCache)
57       {
58          if(!isCacheEnabled())
59          {
60             classloaderCache = new WeakHashMap JavaDoc();
61          }
62       }
63    }
64
65    /**
66     * @return true if caching is enabled, false otherwise.
67     */

68    public static boolean isCacheEnabled()
69    {
70       synchronized(classloaderCache)
71       {
72          return classloaderCache != NoopMap.INSTANCE;
73       }
74    }
75
76    /**
77     * Flushes all the cached ClassInfo's.
78     */

79    public static void flushCache()
80    {
81       synchronized(classloaderCache)
82       {
83          classloaderCache.clear();
84       }
85    }
86
87    /**
88     * Evicts ClassInfo for a specific class.
89     * @param cls fully qualified class name of the class
90     */

91    public static void flushCache(String JavaDoc cls)
92    {
93       ClassLoader JavaDoc cl = Thread.currentThread().getContextClassLoader();
94       Map JavaDoc classLoaderCache = getClassLoaderCache(cl);
95       classLoaderCache.remove(cls);
96    }
97
98    /**
99     * Evicts ClassInfo for a specific class.
100     * @param cls the class to remove the ClassInfo for
101     */

102    public static void flushCache(Class JavaDoc cls)
103    {
104       Map JavaDoc classLoaderCache = getClassLoaderCache(cls.getClassLoader());
105       classLoaderCache.remove(cls.getName());
106    }
107
108    public static ClassInfo getClassInfo(Class JavaDoc cls)
109    {
110       Map JavaDoc classLoaderCache = getClassLoaderCache(cls.getClassLoader());
111
112       WeakReference JavaDoc weak = (WeakReference JavaDoc)classLoaderCache.get(cls.getName());
113       if(weak != null)
114       {
115          Object JavaDoc result = weak.get();
116          if(result != null)
117          {
118             return (ClassInfo)result;
119          }
120       }
121
122       ClassInfo clsInfo = new ClassInfo(cls);
123       weak = new WeakReference JavaDoc(clsInfo);
124       classLoaderCache.put(cls.getName(), weak);
125       return clsInfo;
126    }
127
128    public static ClassInfo getClassInfo(String JavaDoc name, boolean required)
129    {
130       ClassLoader JavaDoc cl = Thread.currentThread().getContextClassLoader();
131       Map JavaDoc classLoaderCache = getClassLoaderCache(cl);
132
133       WeakReference JavaDoc weak = (WeakReference JavaDoc)classLoaderCache.get(name);
134       if(weak != null)
135       {
136          Object JavaDoc result = weak.get();
137          if(result != null)
138          {
139             return (ClassInfo)result;
140          }
141       }
142
143       try
144       {
145          ClassInfo clsInfo = new ClassInfo(cl.loadClass(name));
146          weak = new WeakReference JavaDoc(clsInfo);
147          classLoaderCache.put(name, weak);
148          return clsInfo;
149       }
150       catch(ClassNotFoundException JavaDoc e)
151       {
152          if(required)
153          {
154             throw new JBossXBRuntimeException("Failed to load class " + name);
155          }
156       }
157
158       return null;
159    }
160
161    private static Map JavaDoc getClassLoaderCache(ClassLoader JavaDoc cl)
162    {
163       synchronized(classloaderCache)
164       {
165          Map JavaDoc result = (Map JavaDoc) classloaderCache.get(cl);
166          if (result == null)
167          {
168             result = new ConcurrentHashMap();
169             classloaderCache.put(cl, result);
170          }
171          return result;
172       }
173    }
174 }
175
Popular Tags