KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > util > collection > WeakClassCache


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.util.collection;
23
24 import java.lang.ref.WeakReference JavaDoc;
25 import java.util.Map JavaDoc;
26 import java.util.WeakHashMap JavaDoc;
27
28 /**
29  * A weak class cache that instantiates does not a hold a
30  * strong reference to either the classloader or class.<p>
31  *
32  * It creates the class specific data in two stages
33  * to avoid recursion.<p>
34  *
35  * instantiate - creates the data<br>
36  * generate - fills in the details
37  *
38  * @author <a HREF="mailto:adrian@jboss.org">Adrian Brock</a>
39  */

40 public abstract class WeakClassCache
41 {
42    /** The cache */
43    protected Map JavaDoc cache = new WeakHashMap JavaDoc();
44
45    /**
46     * Get the information for a class
47     *
48     * @param clazz the class
49     * @return the info
50     */

51    public Object JavaDoc get(Class JavaDoc clazz)
52    {
53       if (clazz == null)
54          throw new IllegalArgumentException JavaDoc("Null class");
55       
56       Map JavaDoc classLoaderCache = getClassLoaderCache(clazz.getClassLoader());
57
58       WeakReference JavaDoc weak = (WeakReference JavaDoc) classLoaderCache.get(clazz.getName());
59       if (weak != null)
60       {
61          Object JavaDoc result = weak.get();
62          if (result != null)
63             return result;
64       }
65
66       Object JavaDoc result = instantiate(clazz);
67
68       weak = new WeakReference JavaDoc(result);
69       classLoaderCache.put(clazz.getName(), weak);
70       
71       generate(clazz, result);
72       
73       return result;
74    }
75    
76    /**
77     * Get the information for a class
78     *
79     * @param name the name
80     * @param cl the classloader
81     * @return the info
82     * @throws ClassNotFoundException when the class cannot be found
83     */

84    public Object JavaDoc get(String JavaDoc name, ClassLoader JavaDoc cl) throws ClassNotFoundException JavaDoc
85    {
86       if (name == null)
87          throw new IllegalArgumentException JavaDoc("Null name");
88       if (cl == null)
89          throw new IllegalArgumentException JavaDoc("Null classloader");
90       Class JavaDoc clazz = cl.loadClass(name);
91       return get(clazz);
92    }
93    
94    /**
95     * Instantiate for a class
96     *
97     * @param clazz the class
98     * @return the result
99     */

100    protected abstract Object JavaDoc instantiate(Class JavaDoc clazz);
101    
102    /**
103     * Fill in the result
104     *
105     * @param clazz the class
106     * @param result the result
107     */

108    protected abstract void generate(Class JavaDoc clazz, Object JavaDoc result);
109    
110    /**
111     * Get the cache for the classloader
112     *
113     * @param cl the classloader
114     * @return the map
115     */

116    protected Map JavaDoc getClassLoaderCache(ClassLoader JavaDoc cl)
117    {
118       synchronized (cache)
119       {
120          Map JavaDoc result = (Map JavaDoc) cache.get(cl);
121          if (result == null)
122          {
123             result = CollectionsFactory.createConcurrentReaderMap();
124             cache.put(cl, result);
125          }
126          return result;
127       }
128    }
129 }
130
Popular Tags