%line | %branch | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
org.apache.jcs.auxiliary.lateral.LateralCache |
|
|
1 | package org.apache.jcs.auxiliary.lateral; |
|
2 | ||
3 | /* |
|
4 | * Licensed to the Apache Software Foundation (ASF) under one |
|
5 | * or more contributor license agreements. See the NOTICE file |
|
6 | * distributed with this work for additional information |
|
7 | * regarding copyright ownership. The ASF licenses this file |
|
8 | * to you under the Apache License, Version 2.0 (the |
|
9 | * "License"); you may not use this file except in compliance |
|
10 | * with the License. You may obtain a copy of the License at |
|
11 | * |
|
12 | * http://www.apache.org/licenses/LICENSE-2.0 |
|
13 | * |
|
14 | * Unless required by applicable law or agreed to in writing, |
|
15 | * software distributed under the License is distributed on an |
|
16 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
|
17 | * KIND, either express or implied. See the License for the |
|
18 | * specific language governing permissions and limitations |
|
19 | * under the License. |
|
20 | */ |
|
21 | ||
22 | import java.io.IOException; |
|
23 | import java.io.Serializable; |
|
24 | import java.util.Set; |
|
25 | ||
26 | import org.apache.commons.logging.Log; |
|
27 | import org.apache.commons.logging.LogFactory; |
|
28 | import org.apache.jcs.auxiliary.AuxiliaryCacheAttributes; |
|
29 | import org.apache.jcs.auxiliary.lateral.behavior.ILateralCacheAttributes; |
|
30 | import org.apache.jcs.auxiliary.lateral.behavior.ILateralCacheService; |
|
31 | import org.apache.jcs.engine.CacheConstants; |
|
32 | import org.apache.jcs.engine.behavior.ICache; |
|
33 | import org.apache.jcs.engine.behavior.ICacheElement; |
|
34 | import org.apache.jcs.engine.behavior.ICacheType; |
|
35 | import org.apache.jcs.engine.behavior.IZombie; |
|
36 | ||
37 | /** |
|
38 | * Lateral distributor. Returns null on get by default. Net search not |
|
39 | * implemented. |
|
40 | */ |
|
41 | public class LateralCache |
|
42 | implements ICache |
|
43 | { |
|
44 | private static final long serialVersionUID = 6274549256562382782L; |
|
45 | ||
46 | 26 | private final static Log log = LogFactory.getLog( LateralCache.class ); |
47 | ||
48 | // generalize this, use another interface |
|
49 | private ILateralCacheAttributes cattr; |
|
50 | ||
51 | final String cacheName; |
|
52 | ||
53 | /** either http, socket.udp, or socket.tcp can set in config */ |
|
54 | private ILateralCacheService lateral; |
|
55 | ||
56 | private LateralCacheMonitor monitor; |
|
57 | ||
58 | /** |
|
59 | * Constructor for the LateralCache object |
|
60 | * <p> |
|
61 | * @param cattr |
|
62 | * @param lateral |
|
63 | * @param monitor |
|
64 | */ |
|
65 | public LateralCache( ILateralCacheAttributes cattr, ILateralCacheService lateral, LateralCacheMonitor monitor ) |
|
66 | 26 | { |
67 | 26 | this.cacheName = cattr.getCacheName(); |
68 | 26 | this.cattr = cattr; |
69 | 26 | this.lateral = lateral; |
70 | 26 | this.monitor = monitor; |
71 | 26 | } |
72 | ||
73 | /** |
|
74 | * Constructor for the LateralCache object |
|
75 | * <p> |
|
76 | * @param cattr |
|
77 | */ |
|
78 | protected LateralCache( ILateralCacheAttributes cattr ) |
|
79 | 0 | { |
80 | 0 | this.cacheName = cattr.getCacheName(); |
81 | 0 | this.cattr = cattr; |
82 | 0 | } |
83 | ||
84 | /** |
|
85 | * Update lateral. |
|
86 | * <p> |
|
87 | * @param ce |
|
88 | * @throws IOException |
|
89 | */ |
|
90 | public void update( ICacheElement ce ) |
|
91 | throws IOException |
|
92 | { |
|
93 | try |
|
94 | { |
|
95 | 32 | if ( log.isDebugEnabled() ) |
96 | { |
|
97 | 0 | log.debug( "update: lateral = [" + lateral + "], " + "LateralCacheInfo.listenerId = " |
98 | + LateralCacheInfo.listenerId ); |
|
99 | } |
|
100 | 32 | lateral.update( ce, LateralCacheInfo.listenerId ); |
101 | } |
|
102 | 0 | catch ( NullPointerException npe ) |
103 | { |
|
104 | 0 | log.error( "Failure updating lateral. lateral = " + lateral, npe ); |
105 | 0 | handleException( npe, "Failed to put [" + ce.getKey() + "] to " + ce.getCacheName() ); |
106 | 0 | return; |
107 | } |
|
108 | 0 | catch ( Exception ex ) |
109 | { |
|
110 | 0 | handleException( ex, "Failed to put [" + ce.getKey() + "] to " + ce.getCacheName() ); |
111 | 32 | } |
112 | 32 | } |
113 | ||
114 | /** |
|
115 | * The performace costs are too great. It is not recommended that you enable |
|
116 | * lateral gets. |
|
117 | * <p> |
|
118 | * @param key |
|
119 | * @return |
|
120 | * @throws IOException |
|
121 | */ |
|
122 | public ICacheElement get( Serializable key ) |
|
123 | throws IOException |
|
124 | { |
|
125 | 0 | ICacheElement obj = null; |
126 | ||
127 | 0 | if ( this.cattr.getPutOnlyMode() ) |
128 | { |
|
129 | 0 | return null; |
130 | } |
|
131 | try |
|
132 | { |
|
133 | 0 | obj = lateral.get( cacheName, key ); |
134 | } |
|
135 | 0 | catch ( Exception e ) |
136 | { |
|
137 | 0 | log.error( e ); |
138 | 0 | handleException( e, "Failed to get " + key + " from " + this.cattr.getCacheName() ); |
139 | 0 | } |
140 | 0 | return obj; |
141 | } |
|
142 | ||
143 | /** |
|
144 | * |
|
145 | * @param groupName |
|
146 | * @return A set of group keys. |
|
147 | */ |
|
148 | public Set getGroupKeys( String groupName ) |
|
149 | { |
|
150 | 0 | return lateral.getGroupKeys( cacheName, groupName ); |
151 | } |
|
152 | ||
153 | /** |
|
154 | * Synchronously remove from the remote cache; if failed, replace the remote |
|
155 | * handle with a zombie. |
|
156 | * <p> |
|
157 | * @param key |
|
158 | * @return |
|
159 | * @throws IOException |
|
160 | */ |
|
161 | public boolean remove( Serializable key ) |
|
162 | throws IOException |
|
163 | { |
|
164 | 0 | log.debug( "removing key:" + key ); |
165 | ||
166 | try |
|
167 | { |
|
168 | 0 | lateral.remove( cacheName, key, LateralCacheInfo.listenerId ); |
169 | } |
|
170 | 0 | catch ( Exception ex ) |
171 | { |
|
172 | 0 | handleException( ex, "Failed to remove " + key + " from " + this.cattr.getCacheName() ); |
173 | 0 | } |
174 | 0 | return false; |
175 | } |
|
176 | ||
177 | /** |
|
178 | * Synchronously removeAll from the remote cache; if failed, replace the |
|
179 | * remote handle with a zombie. |
|
180 | * <p> |
|
181 | * @throws IOException |
|
182 | */ |
|
183 | public void removeAll() |
|
184 | throws IOException |
|
185 | { |
|
186 | try |
|
187 | { |
|
188 | 0 | lateral.removeAll( cacheName, LateralCacheInfo.listenerId ); |
189 | } |
|
190 | 0 | catch ( Exception ex ) |
191 | { |
|
192 | 0 | handleException( ex, "Failed to remove all from " + this.cattr.getCacheName() ); |
193 | 0 | } |
194 | 0 | } |
195 | ||
196 | /** |
|
197 | * Synchronously dispose the cache. Not sure we want this. |
|
198 | * |
|
199 | * @throws IOException |
|
200 | */ |
|
201 | public void dispose() |
|
202 | throws IOException |
|
203 | { |
|
204 | 0 | log.debug( "Disposing of lateral cache" ); |
205 | ||
206 | ///* HELP: This section did nothing but generate compilation warnings. |
|
207 | // TODO: may limit this funcionality. It is dangerous. |
|
208 | // asmuts -- Added functionality to help with warnings. I'm not getting |
|
209 | // any. |
|
210 | try |
|
211 | { |
|
212 | 0 | lateral.dispose( this.cattr.getCacheName() ); |
213 | // Should remove connection |
|
214 | } |
|
215 | 0 | catch ( Exception ex ) |
216 | { |
|
217 | 0 | log.error( "Couldn't dispose", ex ); |
218 | 0 | handleException( ex, "Failed to dispose " + this.cattr.getCacheName() ); |
219 | 0 | } |
220 | //*/ |
|
221 | 0 | } |
222 | ||
223 | /** |
|
224 | * Returns the cache status. |
|
225 | * <p> |
|
226 | * @return The status value |
|
227 | */ |
|
228 | public int getStatus() |
|
229 | { |
|
230 | 2652 | return this.lateral instanceof IZombie ? CacheConstants.STATUS_ERROR : CacheConstants.STATUS_ALIVE; |
231 | } |
|
232 | ||
233 | /** |
|
234 | * Returns the current cache size. |
|
235 | * <p> |
|
236 | * @return The size value |
|
237 | */ |
|
238 | public int getSize() |
|
239 | { |
|
240 | 0 | return 0; |
241 | } |
|
242 | ||
243 | /** |
|
244 | * Gets the cacheType attribute of the LateralCache object |
|
245 | * <p> |
|
246 | * @return The cacheType value |
|
247 | */ |
|
248 | public int getCacheType() |
|
249 | { |
|
250 | 0 | return ICacheType.LATERAL_CACHE; |
251 | } |
|
252 | ||
253 | /** |
|
254 | * Gets the cacheName attribute of the LateralCache object |
|
255 | * <p> |
|
256 | * @return The cacheName value |
|
257 | */ |
|
258 | public String getCacheName() |
|
259 | { |
|
260 | 26 | return cacheName; |
261 | } |
|
262 | ||
263 | /** |
|
264 | * Not yet sure what to do here. |
|
265 | * <p> |
|
266 | * @param ex |
|
267 | * @param msg |
|
268 | * @throws IOException |
|
269 | */ |
|
270 | private void handleException( Exception ex, String msg ) |
|
271 | throws IOException |
|
272 | { |
|
273 | 0 | log.error( "Disabling lateral cache due to error " + msg, ex ); |
274 | ||
275 | 0 | lateral = new ZombieLateralCacheService(); |
276 | // may want to flush if region specifies |
|
277 | // Notify the cache monitor about the error, and kick off the recovery |
|
278 | // process. |
|
279 | 0 | monitor.notifyError(); |
280 | ||
281 | // could stop the net serach if it is built and try to reconnect? |
|
282 | 0 | if ( ex instanceof IOException ) |
283 | { |
|
284 | 0 | throw (IOException) ex; |
285 | } |
|
286 | 0 | throw new IOException( ex.getMessage() ); |
287 | } |
|
288 | ||
289 | /** |
|
290 | * Replaces the current remote cache service handle with the given handle. |
|
291 | * <p> |
|
292 | * @param lateral |
|
293 | */ |
|
294 | public void fixCache( ILateralCacheService lateral ) |
|
295 | { |
|
296 | 0 | if ( lateral != null ) |
297 | { |
|
298 | 0 | this.lateral = lateral; |
299 | 0 | } |
300 | else |
|
301 | { |
|
302 | 0 | log.warn( "Fix cache called with null lateral." ); |
303 | } |
|
304 | 0 | return; |
305 | } |
|
306 | ||
307 | /** |
|
308 | * getStats |
|
309 | * <p> |
|
310 | * @return String |
|
311 | */ |
|
312 | public String getStats() |
|
313 | { |
|
314 | 0 | return ""; |
315 | } |
|
316 | ||
317 | /** |
|
318 | * @return Returns the AuxiliaryCacheAttributes. |
|
319 | */ |
|
320 | public AuxiliaryCacheAttributes getAuxiliaryCacheAttributes() |
|
321 | { |
|
322 | 52 | return cattr; |
323 | } |
|
324 | ||
325 | /* |
|
326 | * (non-Javadoc) |
|
327 | * |
|
328 | * @see java.lang.Object#toString() |
|
329 | */ |
|
330 | public String toString() |
|
331 | { |
|
332 | 26 | StringBuffer buf = new StringBuffer(); |
333 | 26 | buf.append( "\n LateralCache " ); |
334 | 26 | buf.append( "\n Cache Name [" + cattr.getCacheName() + "]" ); |
335 | 26 | buf.append( "\n cattr = [" + cattr + "]" ); |
336 | 26 | return buf.toString(); |
337 | } |
|
338 | ||
339 | } |
This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |