1 package org.apache.jcs.auxiliary.lateral;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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 private final static Log log = LogFactory.getLog( LateralCache.class );
47
48
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 {
67 this.cacheName = cattr.getCacheName();
68 this.cattr = cattr;
69 this.lateral = lateral;
70 this.monitor = monitor;
71 }
72
73 /***
74 * Constructor for the LateralCache object
75 * <p>
76 * @param cattr
77 */
78 protected LateralCache( ILateralCacheAttributes cattr )
79 {
80 this.cacheName = cattr.getCacheName();
81 this.cattr = cattr;
82 }
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 if ( log.isDebugEnabled() )
96 {
97 log.debug( "update: lateral = [" + lateral + "], " + "LateralCacheInfo.listenerId = "
98 + LateralCacheInfo.listenerId );
99 }
100 lateral.update( ce, LateralCacheInfo.listenerId );
101 }
102 catch ( NullPointerException npe )
103 {
104 log.error( "Failure updating lateral. lateral = " + lateral, npe );
105 handleException( npe, "Failed to put [" + ce.getKey() + "] to " + ce.getCacheName() );
106 return;
107 }
108 catch ( Exception ex )
109 {
110 handleException( ex, "Failed to put [" + ce.getKey() + "] to " + ce.getCacheName() );
111 }
112 }
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 ICacheElement obj = null;
126
127 if ( this.cattr.getPutOnlyMode() )
128 {
129 return null;
130 }
131 try
132 {
133 obj = lateral.get( cacheName, key );
134 }
135 catch ( Exception e )
136 {
137 log.error( e );
138 handleException( e, "Failed to get " + key + " from " + this.cattr.getCacheName() );
139 }
140 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 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 log.debug( "removing key:" + key );
165
166 try
167 {
168 lateral.remove( cacheName, key, LateralCacheInfo.listenerId );
169 }
170 catch ( Exception ex )
171 {
172 handleException( ex, "Failed to remove " + key + " from " + this.cattr.getCacheName() );
173 }
174 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 lateral.removeAll( cacheName, LateralCacheInfo.listenerId );
189 }
190 catch ( Exception ex )
191 {
192 handleException( ex, "Failed to remove all from " + this.cattr.getCacheName() );
193 }
194 }
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 log.debug( "Disposing of lateral cache" );
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221 }
222
223 /***
224 * Returns the cache status.
225 * <p>
226 * @return The status value
227 */
228 public int getStatus()
229 {
230 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 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 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 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 log.error( "Disabling lateral cache due to error " + msg, ex );
274
275 lateral = new ZombieLateralCacheService();
276
277
278
279 monitor.notifyError();
280
281
282 if ( ex instanceof IOException )
283 {
284 throw (IOException) ex;
285 }
286 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 if ( lateral != null )
297 {
298 this.lateral = lateral;
299 }
300 else
301 {
302 log.warn( "Fix cache called with null lateral." );
303 }
304 return;
305 }
306
307 /***
308 * getStats
309 * <p>
310 * @return String
311 */
312 public String getStats()
313 {
314 return "";
315 }
316
317 /***
318 * @return Returns the AuxiliaryCacheAttributes.
319 */
320 public AuxiliaryCacheAttributes getAuxiliaryCacheAttributes()
321 {
322 return cattr;
323 }
324
325
326
327
328
329
330 public String toString()
331 {
332 StringBuffer buf = new StringBuffer();
333 buf.append( "\n LateralCache " );
334 buf.append( "\n Cache Name [" + cattr.getCacheName() + "]" );
335 buf.append( "\n cattr = [" + cattr + "]" );
336 return buf.toString();
337 }
338
339 }