KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > loader > LocalDelegatingCacheLoader


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.cache.loader;
8
9 import org.jboss.cache.CacheImpl;
10 import org.jboss.cache.Fqn;
11 import org.jboss.cache.NodeSPI;
12 import org.jboss.cache.config.CacheLoaderConfig.IndividualCacheLoaderConfig;
13
14 import java.io.ObjectInputStream JavaDoc;
15 import java.io.ObjectOutputStream JavaDoc;
16 import java.util.HashMap JavaDoc;
17 import java.util.Map JavaDoc;
18 import java.util.Set JavaDoc;
19
20 /**
21  * DelegatingCacheLoader implementation which delegates to a local (in the same VM) CacheImpl. Sample code:
22  * <pre>
23  * CacheImpl firstLevel=new CacheImpl();
24  * CacheImpl secondLevel=new CacheImpl();
25  * DelegatingCacheLoader l=new DelegatingCacheLoader(secondLevel);
26  * l.setCache(firstLevel);
27  * firstLevel.setCacheLoader(l);
28  * secondLevel.start();
29  * firstLevel.start();
30  * </pre>
31  *
32  * @author Bela Ban
33  * @author Daniel Gredler
34  * @version $Id: LocalDelegatingCacheLoader.java,v 1.14 2007/01/04 05:35:37 msurtani Exp $
35  */

36 public class LocalDelegatingCacheLoader extends DelegatingCacheLoader
37 {
38
39    IndividualCacheLoaderConfig config;
40    CacheImpl delegate = null;
41
42    public LocalDelegatingCacheLoader()
43    {
44    }
45
46    public LocalDelegatingCacheLoader(CacheImpl delegate)
47    {
48       this.delegate = delegate;
49    }
50
51    public void setConfig(IndividualCacheLoaderConfig config)
52    {
53       this.config = config;
54    }
55
56    public IndividualCacheLoaderConfig getConfig()
57    {
58       return config;
59    }
60
61    protected Set JavaDoc delegateGetChildrenNames(Fqn fqn) throws Exception JavaDoc
62    {
63       return delegate.getChildrenNames(fqn);
64    }
65
66    // See http://jira.jboss.com/jira/browse/JBCACHE-118 for why this is commented out.
67
// protected Object delegateGet(Fqn name, Object key) throws Exception {
68
// return delegate.get(name, key);
69
// }
70

71    protected Map JavaDoc delegateGet(Fqn name) throws Exception JavaDoc
72    {
73       NodeSPI n = (NodeSPI) delegate.get(name);
74       if (n == null) return null;
75       // after this stage we know that the node exists. So never return a null - at worst, an empty map.
76
Map JavaDoc m = n.getDataDirect();
77       if (m == null) m = new HashMap JavaDoc(0);
78       return m;
79    }
80
81    protected void setDelegateCache(CacheImpl delegate)
82    {
83       this.delegate = delegate;
84    }
85
86    protected boolean delegateExists(Fqn name) throws Exception JavaDoc
87    {
88       return delegate.exists(name);
89    }
90
91    protected Object JavaDoc delegatePut(Fqn name, Object JavaDoc key, Object JavaDoc value) throws Exception JavaDoc
92    {
93       return delegate.put(name, key, value);
94    }
95
96    protected void delegatePut(Fqn name, Map JavaDoc attributes) throws Exception JavaDoc
97    {
98       delegate.put(name, attributes);
99    }
100
101    protected Object JavaDoc delegateRemove(Fqn name, Object JavaDoc key) throws Exception JavaDoc
102    {
103       return delegate.remove(name, key);
104    }
105
106    protected void delegateRemove(Fqn name) throws Exception JavaDoc
107    {
108       delegate.remove(name);
109    }
110
111    protected void delegateRemoveData(Fqn name) throws Exception JavaDoc
112    {
113       delegate.removeData(name);
114    }
115
116    protected void delegateLoadEntireState(ObjectOutputStream JavaDoc os) throws Exception JavaDoc
117    {
118       try
119       {
120          // // We use the lock acquisition timeout rather than the
121
// // state transfer timeout, otherwise we'd never try
122
// // to break locks before the requesting node gives up
123
// return cache._getState(Fqn.fromString(SEPARATOR),
124
// cache.getLockAcquisitionTimeout(),
125
// true,
126
// false);
127
// Until flush is in place, use the old mechanism
128
// where we wait the full state retrieval timeout
129
delegate.getStateTransferManager().getState(os, Fqn.ROOT, delegate.getConfiguration().getInitialStateRetrievalTimeout(), true, false);
130       }
131       catch (Exception JavaDoc e)
132       {
133          throw e;
134       }
135       catch (Throwable JavaDoc t)
136       {
137          throw new RuntimeException JavaDoc("Caught exception getting state from delegate", t);
138       }
139    }
140
141    protected void delegateLoadState(Fqn subtree, ObjectOutputStream JavaDoc os) throws Exception JavaDoc
142    {
143       throw new UnsupportedOperationException JavaDoc("setting and loading state for specific Fqns not supported");
144    }
145
146    protected void delegateStoreEntireState(ObjectInputStream JavaDoc is) throws Exception JavaDoc
147    {
148       delegate.getStateTransferManager().setState(is, Fqn.ROOT, null);
149
150    }
151
152    protected void delegateStoreState(Fqn subtree, ObjectInputStream JavaDoc is) throws Exception JavaDoc
153    {
154       throw new UnsupportedOperationException JavaDoc("setting and loading state for specific Fqns not supported");
155    }
156
157 }
158
Popular Tags