1 package org.apache.commons.jcs.auxiliary.remote;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.rmi.registry.Registry;
23 import java.util.ArrayList;
24 import java.util.StringTokenizer;
25 import java.util.concurrent.ConcurrentHashMap;
26 import java.util.concurrent.ConcurrentMap;
27 import java.util.concurrent.locks.Lock;
28 import java.util.concurrent.locks.ReentrantLock;
29
30 import org.apache.commons.jcs.auxiliary.AbstractAuxiliaryCacheFactory;
31 import org.apache.commons.jcs.auxiliary.AuxiliaryCache;
32 import org.apache.commons.jcs.auxiliary.AuxiliaryCacheAttributes;
33 import org.apache.commons.jcs.auxiliary.remote.behavior.IRemoteCacheAttributes;
34 import org.apache.commons.jcs.auxiliary.remote.server.behavior.RemoteType;
35 import org.apache.commons.jcs.engine.behavior.ICompositeCacheManager;
36 import org.apache.commons.jcs.engine.behavior.IElementSerializer;
37 import org.apache.commons.jcs.engine.logging.behavior.ICacheEventLogger;
38
39
40
41
42
43
44
45 public class RemoteCacheFactory
46 extends AbstractAuxiliaryCacheFactory
47 {
48
49 private RemoteCacheMonitor monitor;
50
51
52 private ConcurrentMap<RemoteLocation, RemoteCacheManager> managers;
53
54
55 private Lock managerLock;
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70 @Override
71 public <K, V> AuxiliaryCache<K, V> createCache(
72 AuxiliaryCacheAttributes iaca, ICompositeCacheManager cacheMgr,
73 ICacheEventLogger cacheEventLogger, IElementSerializer elementSerializer )
74 {
75 RemoteCacheAttributes rca = (RemoteCacheAttributes) iaca;
76
77 ArrayList<RemoteCacheNoWait<K,V>> noWaits = new ArrayList<RemoteCacheNoWait<K,V>>();
78
79 switch (rca.getRemoteType())
80 {
81 case LOCAL:
82
83 ArrayList<RemoteLocation> failovers = new ArrayList<RemoteLocation>();
84
85
86
87
88 boolean primaryDefined = false;
89 if ( rca.getRemoteLocation() != null )
90 {
91 primaryDefined = true;
92
93 failovers.add( rca.getRemoteLocation() );
94 RemoteCacheManager rcm = getManager( rca, cacheMgr, cacheEventLogger, elementSerializer );
95 RemoteCacheNoWait<K,V> ic = rcm.getCache( rca );
96 noWaits.add( ic );
97 }
98
99
100 String failoverList = rca.getFailoverServers();
101 if ( failoverList != null )
102 {
103 StringTokenizer fit = new StringTokenizer( failoverList, "," );
104 int fCnt = 0;
105 while ( fit.hasMoreTokens() )
106 {
107 fCnt++;
108
109 String server = fit.nextToken();
110 RemoteLocation location = RemoteLocation.parseServerAndPort(server);
111
112 if (location != null)
113 {
114 failovers.add( location );
115 rca.setRemoteLocation(location);
116 RemoteCacheManager rcm = getManager( rca, cacheMgr, cacheEventLogger, elementSerializer );
117
118
119
120 if ( ( !primaryDefined && fCnt == 1 ) || noWaits.size() <= 0 )
121 {
122 RemoteCacheNoWait<K,V> ic = rcm.getCache( rca );
123 noWaits.add( ic );
124 }
125 }
126 }
127
128 }
129
130
131 rca.setFailovers( failovers );
132 break;
133
134 case CLUSTER:
135
136 StringTokenizer it = new StringTokenizer( rca.getClusterServers(), "," );
137 while ( it.hasMoreElements() )
138 {
139 String server = (String) it.nextElement();
140 RemoteLocation location = RemoteLocation.parseServerAndPort(server);
141
142 if (location != null)
143 {
144 rca.setRemoteLocation(location);
145 RemoteCacheManager rcm = getManager( rca, cacheMgr, cacheEventLogger, elementSerializer );
146 rca.setRemoteType( RemoteType.CLUSTER );
147 RemoteCacheNoWait<K,V> ic = rcm.getCache( rca );
148 noWaits.add( ic );
149 }
150 }
151 break;
152 }
153
154 RemoteCacheNoWaitFacade<K, V> rcnwf =
155 new RemoteCacheNoWaitFacade<K, V>(noWaits, rca, cacheEventLogger, elementSerializer, this );
156
157 return rcnwf;
158 }
159
160
161
162
163
164
165
166
167
168
169
170
171 public RemoteCacheManager getManager( IRemoteCacheAttributes cattr )
172 {
173 if ( cattr.getRemoteLocation() == null )
174 {
175 cattr.setRemoteLocation("", Registry.REGISTRY_PORT);
176 }
177
178 RemoteLocation loc = cattr.getRemoteLocation();
179 RemoteCacheManager ins = managers.get( loc );
180
181 return ins;
182 }
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198 public RemoteCacheManager getManager( IRemoteCacheAttributes cattr, ICompositeCacheManager cacheMgr,
199 ICacheEventLogger cacheEventLogger,
200 IElementSerializer elementSerializer )
201 {
202 RemoteCacheManager ins = getManager( cattr );
203
204 if ( ins == null )
205 {
206 managerLock.lock();
207
208 try
209 {
210 ins = managers.get( cattr.getRemoteLocation() );
211
212 if (ins == null)
213 {
214 ins = new RemoteCacheManager( cattr, cacheMgr, monitor, cacheEventLogger, elementSerializer);
215 managers.put( cattr.getRemoteLocation(), ins );
216 monitor.addManager(ins);
217 }
218 }
219 finally
220 {
221 managerLock.unlock();
222 }
223 }
224
225 return ins;
226 }
227
228
229
230
231 @Override
232 public void initialize()
233 {
234 super.initialize();
235
236 managers = new ConcurrentHashMap<RemoteLocation, RemoteCacheManager>();
237 managerLock = new ReentrantLock();
238
239 monitor = new RemoteCacheMonitor();
240 monitor.setDaemon(true);
241 }
242
243
244
245
246 @Override
247 public void dispose()
248 {
249 for (RemoteCacheManager manager : managers.values())
250 {
251 manager.release();
252 }
253
254 managers.clear();
255
256 if (monitor != null)
257 {
258 monitor.notifyShutdown();
259 try
260 {
261 monitor.join(5000);
262 }
263 catch (InterruptedException e)
264 {
265
266 }
267 monitor = null;
268 }
269
270 super.dispose();
271 }
272 }