1 package org.apache.commons.jcs3.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.concurrent.ConcurrentHashMap;
25 import java.util.concurrent.ConcurrentMap;
26
27 import org.apache.commons.jcs3.auxiliary.AbstractAuxiliaryCacheFactory;
28 import org.apache.commons.jcs3.auxiliary.AuxiliaryCache;
29 import org.apache.commons.jcs3.auxiliary.AuxiliaryCacheAttributes;
30 import org.apache.commons.jcs3.auxiliary.remote.behavior.IRemoteCacheAttributes;
31 import org.apache.commons.jcs3.auxiliary.remote.server.behavior.RemoteType;
32 import org.apache.commons.jcs3.engine.behavior.ICompositeCacheManager;
33 import org.apache.commons.jcs3.engine.behavior.IElementSerializer;
34 import org.apache.commons.jcs3.engine.logging.behavior.ICacheEventLogger;
35
36
37
38
39
40
41
42 public class RemoteCacheFactory
43 extends AbstractAuxiliaryCacheFactory
44 {
45
46 private RemoteCacheMonitor monitor;
47
48
49 private ConcurrentMap<RemoteLocation, RemoteCacheManager> managers;
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64 @Override
65 public <K, V> AuxiliaryCache<K, V> createCache(
66 final AuxiliaryCacheAttributes iaca, final ICompositeCacheManager cacheMgr,
67 final ICacheEventLogger cacheEventLogger, final IElementSerializer elementSerializer )
68 {
69 final RemoteCacheAttributes rca = (RemoteCacheAttributes) iaca;
70
71 final ArrayList<RemoteCacheNoWait<K,V>> noWaits = new ArrayList<>();
72
73 switch (rca.getRemoteType())
74 {
75 case LOCAL:
76
77 final ArrayList<RemoteLocation> failovers = new ArrayList<>();
78
79
80
81
82 if ( rca.getRemoteLocation() != null )
83 {
84 failovers.add( rca.getRemoteLocation() );
85 final RemoteCacheManager rcm = getManager( rca, cacheMgr, cacheEventLogger, elementSerializer );
86 noWaits.add(rcm.getCache(rca));
87 }
88
89
90 final String failoverList = rca.getFailoverServers();
91 if (failoverList != null && !failoverList.isEmpty())
92 {
93 final String[] failoverServers = failoverList.split("\\s*,\\s*");
94 for (String server : failoverServers)
95 {
96 final RemoteLocation location = RemoteLocation.parseServerAndPort(server);
97
98 if (location != null)
99 {
100 failovers.add( location );
101 final RemoteCacheAttributes frca = (RemoteCacheAttributes) rca.clone();
102 frca.setRemoteLocation(location);
103 final RemoteCacheManager rcm = getManager( frca, cacheMgr, cacheEventLogger, elementSerializer );
104
105
106
107 if (noWaits.isEmpty())
108 {
109 frca.setFailoverIndex(0);
110 noWaits.add(rcm.getCache(frca));
111 }
112 }
113 }
114
115 }
116
117
118 rca.setFailovers( failovers );
119 break;
120
121 case CLUSTER:
122
123 final String[] clusterServers = rca.getClusterServers().split("\\s*,\\s*");
124 for (String server: clusterServers)
125 {
126 if (server.isEmpty())
127 {
128 continue;
129 }
130
131 final RemoteLocation location = RemoteLocation.parseServerAndPort(server);
132
133 if (location != null)
134 {
135 final RemoteCacheAttributes crca = (RemoteCacheAttributes) rca.clone();
136 crca.setRemoteLocation(location);
137 final RemoteCacheManager rcm = getManager( crca, cacheMgr, cacheEventLogger, elementSerializer );
138 crca.setRemoteType( RemoteType.CLUSTER );
139 noWaits.add(rcm.getCache(crca));
140 }
141 }
142 break;
143 }
144
145 return new RemoteCacheNoWaitFacade<>(noWaits, rca, cacheEventLogger, elementSerializer, this);
146 }
147
148
149
150
151
152
153
154
155
156
157
158 public RemoteCacheManager getManager( final IRemoteCacheAttributes cattr )
159 {
160 final RemoteCacheAttributes rca = (RemoteCacheAttributes) cattr.clone();
161 if (rca.getRemoteLocation() == null)
162 {
163 rca.setRemoteLocation("", Registry.REGISTRY_PORT);
164 }
165
166 return managers.get(rca.getRemoteLocation());
167 }
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184 public RemoteCacheManager getManager( final IRemoteCacheAttributes cattr,
185 final ICompositeCacheManager cacheMgr,
186 final ICacheEventLogger cacheEventLogger,
187 final IElementSerializer elementSerializer )
188 {
189 final RemoteCacheAttributes rca = (RemoteCacheAttributes) cattr.clone();
190 if (rca.getRemoteLocation() == null)
191 {
192 rca.setRemoteLocation("", Registry.REGISTRY_PORT);
193 }
194
195 return managers.computeIfAbsent(rca.getRemoteLocation(), key -> {
196
197 RemoteCacheManager manager = new RemoteCacheManager(rca, cacheMgr, monitor, cacheEventLogger, elementSerializer);
198 monitor.addManager(manager);
199
200 return manager;
201 });
202 }
203
204
205
206
207 @Override
208 public void initialize()
209 {
210 super.initialize();
211
212 managers = new ConcurrentHashMap<>();
213
214 monitor = new RemoteCacheMonitor();
215 monitor.setDaemon(true);
216 }
217
218
219
220
221 @Override
222 public void dispose()
223 {
224 managers.values().forEach(RemoteCacheManager::release);
225 managers.clear();
226
227 if (monitor != null)
228 {
229 monitor.notifyShutdown();
230 try
231 {
232 monitor.join(5000);
233 }
234 catch (final InterruptedException e)
235 {
236
237 }
238 monitor = null;
239 }
240
241 super.dispose();
242 }
243 }