KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > web > tomcat > tc6 > session > JBossCacheWrapper


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.web.tomcat.tc6.session;
23
24 import java.util.Iterator JavaDoc;
25 import java.util.Map JavaDoc;
26 import java.util.Set JavaDoc;
27
28 import org.jboss.cache.Cache;
29 import org.jboss.cache.Fqn;
30 import org.jboss.cache.InvocationContext;
31 import org.jboss.cache.Node;
32 import org.jboss.cache.config.Option;
33 import org.jboss.cache.lock.TimeoutException;
34 import org.jboss.cache.pojo.PojoCache;
35
36 public class JBossCacheWrapper
37 {
38    static final Option GRAVITATE_OPTION = new Option();
39    static final Option LOCAL_OPTION = new Option();
40    static
41    {
42       GRAVITATE_OPTION.setForceDataGravitation(true);
43       LOCAL_OPTION.setCacheModeLocal(true);
44    }
45    
46    private static final int RETRY = 3;
47    private PojoCache pojoCache_;
48    private Cache plainCache_;
49    
50    JBossCacheWrapper(PojoCache cache)
51    {
52       pojoCache_ = cache;
53       plainCache_ = pojoCache_.getCache();
54    }
55
56    /**
57     * Wrapper to embed retyr logic.
58     *
59     * @param fqn
60     * @param id
61     * @return
62     */

63    Object JavaDoc get(Fqn fqn, String JavaDoc id)
64    {
65       return get(fqn, id, false);
66    }
67
68    /**
69     * Wrapper to embed retyr logic.
70     *
71     * @param fqn
72     * @param id
73     * @return
74     */

75    Object JavaDoc get(Fqn fqn, String JavaDoc id, boolean gravitate)
76    {
77       Exception JavaDoc ex = null;
78       for (int i = 0; i < RETRY; i++)
79       {
80          InvocationContext ctx = null;
81          Option existing = null;
82          try
83          {
84             
85             Object JavaDoc value = null;
86             if (gravitate)
87             {
88                ctx = plainCache_.getInvocationContext();
89                existing = ctx.getOptionOverrides();
90                ctx.setOptionOverrides(GRAVITATE_OPTION);
91                value = plainCache_.get(fqn, id);
92             }
93             else
94             {
95                value = plainCache_.get(fqn, id);
96             }
97             return value;
98          }
99          catch (TimeoutException e)
100          {
101             ex = e;
102          }
103          catch (Exception JavaDoc e)
104          {
105             if (e instanceof RuntimeException JavaDoc)
106                throw (RuntimeException JavaDoc) e;
107             throw new RuntimeException JavaDoc("JBossCacheService: exception occurred in cache get ... ", e);
108          }
109          finally
110          {
111             if (ctx != null)
112                ctx.setOptionOverrides(existing);
113          }
114       }
115       throw new RuntimeException JavaDoc("JBossCacheService: exception occurred in cache get after retry ... ", ex);
116    }
117
118    /**
119     * Wrapper to embed retry logic.
120     *
121     * @param fqn
122     * @param id
123     * @param value
124     * @return
125     */

126    void put(Fqn fqn, String JavaDoc id, Object JavaDoc value)
127    {
128       Exception JavaDoc ex = null;
129       for (int i = 0; i < RETRY; i++)
130       {
131          try
132          {
133             plainCache_.put(fqn, id, value);
134             return;
135          }
136          catch (TimeoutException e)
137          {
138             ex = e;
139          }
140          catch (Exception JavaDoc e)
141          {
142             throw new RuntimeException JavaDoc("JBossCacheService: exception occurred in cache put ... ", e);
143          }
144       }
145       throw new RuntimeException JavaDoc("JBossCacheService: exception occurred in cache put after retry ... ", ex);
146    }
147
148
149    /**
150     * Wrapper to embed retry logic.
151     *
152     * @param fqn
153     * @param map
154     */

155    void put(Fqn fqn, Map JavaDoc map)
156    {
157       Exception JavaDoc ex = null;
158       for (int i = 0; i < RETRY; i++)
159       {
160          try
161          {
162             plainCache_.put(fqn, map);
163             return;
164          }
165          catch (TimeoutException e)
166          {
167             ex = e;
168          }
169          catch (Exception JavaDoc e)
170          {
171             throw new RuntimeException JavaDoc("JBossCacheService: exception occurred in cache put ... ", e);
172          }
173       }
174       throw new RuntimeException JavaDoc("JBossCacheService: exception occurred in cache put after retry ... ", ex);
175    }
176
177    /**
178     * Wrapper to embed retyr logic.
179     *
180     * @param fqn
181     * @param id
182     * @return
183     */

184    Object JavaDoc remove(Fqn fqn, String JavaDoc id)
185    {
186       Exception JavaDoc ex = null;
187       for (int i = 0; i < RETRY; i++)
188       {
189          try
190          {
191             return plainCache_.remove(fqn, id);
192          }
193          catch (TimeoutException e)
194          {
195             ex = e;
196          }
197          catch (Exception JavaDoc e)
198          {
199             throw new RuntimeException JavaDoc("JBossCacheService: exception occurred in cache remove ... ", e);
200          }
201       }
202       throw new RuntimeException JavaDoc("JBossCacheService: exception occurred in cache remove after retry ... ", ex);
203    }
204
205    /**
206     * Wrapper to embed retry logic.
207     *
208     * @param fqn
209     */

210    void remove(Fqn fqn)
211    {
212       Exception JavaDoc ex = null;
213       for (int i = 0; i < RETRY; i++)
214       {
215          try
216          {
217             plainCache_.removeNode(fqn);
218             return;
219          }
220          catch (TimeoutException e)
221          {
222             ex = e;
223          }
224          catch (Exception JavaDoc e)
225          {
226             throw new RuntimeException JavaDoc("JBossCacheService: exception occurred in cache remove ... ", e);
227          }
228       }
229       throw new RuntimeException JavaDoc("JBossCacheService: exception occurred in cache remove after retry ... ", ex);
230    }
231    
232    /**
233     * Wrapper to embed retry logic.
234     *
235     * @param fqn
236     */

237    void removeLocal(Fqn fqn)
238    {
239       Exception JavaDoc ex = null;
240       for (int i = 0; i < RETRY; i++)
241       {
242          InvocationContext ctx = plainCache_.getInvocationContext();
243          Option existing = ctx.getOptionOverrides();
244          try
245          {
246             ctx.setOptionOverrides(LOCAL_OPTION);
247             plainCache_.removeNode(fqn);
248             return;
249          }
250          catch (TimeoutException e)
251          {
252             ex = e;
253          }
254          catch (Exception JavaDoc e)
255          {
256             throw new RuntimeException JavaDoc("JBossCacheService: exception occurred in cache removeLocal ... ", e);
257          }
258          finally
259          {
260             ctx.setOptionOverrides(existing);
261          }
262       }
263       throw new RuntimeException JavaDoc("JBossCacheService: exception occurred in cache removeLocal after retry ... ", ex);
264    }
265
266    /**
267     * Wrapper to embed retry logic.
268     *
269     * @param fqn
270     */

271    void evict(Fqn fqn)
272    {
273       Exception JavaDoc ex = null;
274       for (int i = 0; i < RETRY; i++)
275       {
276          try
277          {
278             plainCache_.evict(fqn, false);
279             return;
280          }
281          catch (TimeoutException e)
282          {
283             ex = e;
284          }
285          catch (Exception JavaDoc e)
286          {
287             throw new RuntimeException JavaDoc("JBossCacheService: exception occurred in cache evict ... ", e);
288          }
289       }
290       throw new RuntimeException JavaDoc("JBossCacheService: exception occurred in cache evict after retry ... ", ex);
291    }
292    
293    void evictSubtree(Fqn fqn)
294    {
295       Exception JavaDoc ex = null;
296       for (int i = 0; i < RETRY; i++)
297       {
298          try
299          {
300 // // Evict the node itself first, since if it stores a Pojo
301
// // that will do everything
302
// plainCache_.evict(fqn);
303
//
304
// // next do a depth first removal; this ensure all nodes
305
// // are removed, not just their data map
306
// Set children = plainCache_.getChildrenNames(fqn);
307
// if (children != null)
308
// {
309
// for (Iterator it = children.iterator(); it.hasNext(); )
310
// {
311
// Fqn child = new Fqn(fqn, it.next());
312
// plainCache_.evict(child);
313
// }
314
//
315
// plainCache_.evict(fqn);
316
// }
317
// return;
318
plainCache_.evict(fqn, true);
319             
320          }
321          catch (TimeoutException e)
322          {
323             ex = e;
324          }
325          catch (Exception JavaDoc e)
326          {
327             throw new RuntimeException JavaDoc("JBossCacheService: exception occurred in cache evict ... ", e);
328          }
329       }
330       throw new RuntimeException JavaDoc("JBossCacheService: exception occurred in cache evictSubtree after retry ... ", ex);
331    }
332    
333    void removeLocalSubtree(Fqn fqn)
334    {
335       // First remove the node itself. If it is the root of an AOP
336
// object tree, this will do everything
337
removeLocal(fqn);
338
339       // Next, clear any children
340
Exception JavaDoc ex = null;
341       for (int i = 0; i < RETRY; i++)
342       {
343          try
344          {
345             // Evict the node itself first, since if it stores a Pojo
346
// that will do everything
347
removeLocal(fqn);
348             
349             // next do a depth first removal; this ensures all nodes
350
// are removed, not just their data map
351
Node base = plainCache_.getChild(fqn);
352             if (base != null)
353             {
354                Set JavaDoc children = base.getChildren();
355                if (children != null)
356                {
357                   for (Iterator JavaDoc it = children.iterator(); it.hasNext(); )
358                   {
359                      Node child = (Node) it.next();
360                      removeLocalSubtree(child.getFqn());
361                   }
362                   
363                   removeLocal(fqn);
364                }
365             }
366             return;
367             
368          }
369          catch (TimeoutException e)
370          {
371             ex = e;
372          }
373          catch (Exception JavaDoc e)
374          {
375             throw new RuntimeException JavaDoc("JBossCacheService: exception occurred in cache removeLocal ... ", e);
376          }
377       }
378       throw new RuntimeException JavaDoc("JBossCacheService: exception occurred in cache removeLocalSubtree after retry ... ", ex);
379    }
380
381 }
382
Popular Tags