KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > mobilitools > smi > lib > ClassCache


1 /*
2 * MobiliTools: an implementation of the Object Management Group's
3 * Mobile Agent Facility specification.
4 * Copyright (C) 2003 France Telecom R&D
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 of the License, or (at your option) 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 USA
19 *
20 * MobiliTools $Name: $
21 *
22 * Contact: mobilitools-smi@lists.debian-sf.objectweb.org
23 *
24 * Authors: Bruno Dillenseger
25 */

26
27
28 package org.objectweb.mobilitools.smi.lib;
29
30
31 import java.util.Hashtable JavaDoc;
32
33
34 /**
35  * MobiliTools $Name: $, $Id: ClassCache.java,v 1.1.1.1 2003/03/28 14:48:06 dillense Exp $
36  * <P>
37  * Cache for classes, bytecode and any resource associated to a given codebase.
38  * A cache singleton is managed for each codebase value.
39  * A counter is maintained for each so that the cache is cleared when its
40  * codebase is not used by any agent any more.
41 */

42 public class ClassCache extends Hashtable JavaDoc
43 {
44     static protected Hashtable JavaDoc caches = new Hashtable JavaDoc();
45     protected volatile long refCount=0;
46
47
48     /**
49         Ensure that a cache exists for the given codebase - creating a new cache instance
50         if necessary - and returns its reference. In other words, there is a cache
51         singleton for each given codebase value.
52         @param codebase the codebase associated to the cache
53         @return the cache associated to the given codebase
54     */

55     static public synchronized ClassCache getCache(String JavaDoc codebase)
56     {
57         ClassCache cache = (ClassCache)caches.get(codebase);
58         if (cache == null)
59         {
60             cache = new ClassCache();
61             caches.put(codebase, cache);
62         }
63         return cache;
64     }
65
66
67     /**
68         Increments the reference counter of the cache associated to the given codebase.
69         Does nothing if no cache exists for the given codebase.
70         @param codebase the codebase of the target cache.
71     */

72     static public synchronized void inc(String JavaDoc codebase)
73     {
74         ClassCache cache = getCache(codebase);
75         synchronized (cache)
76         {
77             if (cache != null)
78             {
79                 ++cache.refCount;
80             }
81         }
82     }
83
84
85     /**
86         Decrements the reference counter of the cache associated to the given codebase.
87         If the reference counter's new value is zero, then the cache is cleared.
88         Does nothing if no cache exists for the given codebase.
89         @param codebase the codebase of the target cache.
90     */

91     static public synchronized void dec(String JavaDoc codebase)
92     {
93         ClassCache cache = getCache(codebase);
94         synchronized (cache)
95         {
96             --cache.refCount;
97             if (cache.refCount == 0)
98             {
99                 cache.clear();
100                 caches.remove(codebase);
101             }
102             else if (cache.refCount < 0)
103             {
104                 throw new Error JavaDoc("cache.refCount negatif pour codebase " + codebase);
105             }
106         }
107     }
108
109
110     protected ClassCache()
111     {
112         super();
113     }
114 }
115
Popular Tags