KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > shiftone > cache > decorator > cluster > ClusterCacheFactory


1 package org.shiftone.cache.decorator.cluster;
2
3
4
5 import org.shiftone.cache.Cache;
6 import org.shiftone.cache.CacheException;
7 import org.shiftone.cache.CacheFactory;
8 import org.shiftone.cache.util.Log;
9 import org.shiftone.cache.util.WeakMap;
10
11 import java.util.Random JavaDoc;
12
13
14 /**
15  * @version $Revision: 1.12 $
16  * @author <a HREF="mailto:jeff@shiftone.org">Jeff Drost</a>
17  */

18 public class ClusterCacheFactory implements CacheFactory
19 {
20
21     private static final Log LOG = new Log(ClusterCacheFactory.class);
22     private static final Random JavaDoc random = new Random JavaDoc();
23     public static final String JavaDoc DEFAULT_BUS_NAME = "shiftone-cache";
24     private WeakMap registeredCacheMap = new WeakMap();
25     private final long instanceId = random.nextLong();
26     private ClusterBus bus;
27     private CacheFactory delegate = null; // configured
28
private String JavaDoc channelProperties = ""; // configured
29
private String JavaDoc busName = DEFAULT_BUS_NAME; // configured
30

31     private void init() throws CacheException
32     {
33
34         // if the bus has not been initialized, do so now
35
if (bus == null)
36         {
37             try
38             {
39                 bus = new ClusterBus(this);
40             }
41             catch (Exception JavaDoc e)
42             {
43                 throw new CacheException("error initializing JGroups NotificationBus", e);
44             }
45         }
46     }
47
48
49     public Cache newInstance(String JavaDoc cacheName, long timeoutMilliSeconds, int maxSize)
50     {
51
52         Cache delegateCache;
53         ClusterCache clusterCache;
54
55         try
56         {
57             init();
58
59             clusterCache = getRegisteredCache(cacheName);
60
61             // if another cache of the same name has been registered
62
// then return it. The down side of this is that the second
63
// version time the cache is returned, the timeout and max size
64
// has no effect on the returned cache.
65
// Also, this class does not extends AbstractDecoratorCacheFactory
66
// because it needs to decide if the delegate cache should be
67
// created.
68
if (clusterCache == null)
69             {
70                 delegateCache = delegate.newInstance(cacheName, timeoutMilliSeconds, maxSize);
71                 clusterCache = new ClusterCache(cacheName, delegateCache, this);
72
73                 registerCache(cacheName, clusterCache);
74             }
75             else
76             {
77                 LOG.info("returning existing cache : " + cacheName);
78             }
79         }
80         catch (Exception JavaDoc e)
81         {
82             throw new RuntimeException JavaDoc("unable to create cluster decorator");
83         }
84
85         return clusterCache;
86     }
87
88
89     private synchronized void registerCache(String JavaDoc cacheName, ClusterCache clusterCache)
90     {
91         registeredCacheMap.put(cacheName, clusterCache);
92     }
93
94
95     private synchronized ClusterCache getRegisteredCache(String JavaDoc cacheName)
96     {
97         return (ClusterCache) registeredCacheMap.get(cacheName);
98     }
99
100
101     public String JavaDoc getBusName()
102     {
103         return busName;
104     }
105
106
107     public void setBusName(String JavaDoc busName)
108     {
109         this.busName = busName;
110     }
111
112
113     public String JavaDoc getChannelProperties()
114     {
115         return channelProperties;
116     }
117
118
119     public void setChannelProperties(String JavaDoc channelProperties)
120     {
121         this.channelProperties = channelProperties;
122     }
123
124
125     public void setDelegate(CacheFactory delegate)
126     {
127         this.delegate = delegate;
128     }
129
130
131     public String JavaDoc toString()
132     {
133         return "ClusterCacheFactory[" + busName + "]->" + delegate;
134     }
135
136
137     //-----------------------------------------------------------
138
public void sendRemoveNotification(String JavaDoc cacheName, Object JavaDoc key)
139     {
140         bus.sendNotification(new RemoveNotification(instanceId, cacheName, key));
141     }
142
143
144     public void sendClearNotification(String JavaDoc cacheName)
145     {
146         bus.sendNotification(new ClearNotification(instanceId, cacheName));
147     }
148
149
150     public void handleNotification(Notification notification)
151     {
152
153         ClusterCache clusterCache;
154
155         // don't process self notification
156
if (notification.getSenderInstanceId() != instanceId)
157         {
158             clusterCache = getRegisteredCache(notification.getCacheName());
159
160             // if this factory doesn't have this cache, it isn't
161
// really possible to execute a notification on it
162
if (clusterCache != null)
163             {
164                 LOG.debug("execute : " + notification);
165                 notification.execute(clusterCache.getCache());
166             }
167         }
168     }
169 }
170
Popular Tags