KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * JBoss, Home of Professional Open Source
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.CacheException;
10 import org.jboss.cache.CacheSPI;
11 import org.jboss.cache.Fqn;
12 import org.jboss.cache.Modification;
13 import org.jboss.cache.RegionManager;
14 import org.jboss.cache.buddyreplication.BuddyManager;
15 import org.jboss.cache.util.MapCopy;
16
17 import java.io.ObjectInputStream JavaDoc;
18 import java.io.ObjectOutputStream JavaDoc;
19 import java.util.List JavaDoc;
20 import java.util.Map JavaDoc;
21 import java.util.Set JavaDoc;
22
23 /**
24  * A convenience abstract implementation of a {@link CacheLoader}
25  *
26  * @author <a HREF="mailto:manik@jboss.org">Manik Surtani (manik@jboss.org)</a>
27  * @since 2.0.0
28  */

29 public abstract class AbstractCacheLoader implements CacheLoader
30 {
31    protected CacheSPI cache;
32    protected RegionManager regionManager;
33
34    public void put(Fqn fqn, Map JavaDoc<Object JavaDoc, Object JavaDoc> attributes, boolean erase) throws Exception JavaDoc
35    {
36       if (erase)
37       {
38          removeData(fqn);
39       }
40
41       // JBCACHE-769 -- make a defensive copy
42
Map JavaDoc<Object JavaDoc, Object JavaDoc> attrs = (attributes == null ? null : new MapCopy<Object JavaDoc, Object JavaDoc>(attributes));
43       put(fqn, attrs);
44    }
45
46    public void storeEntireState(ObjectInputStream JavaDoc is) throws Exception JavaDoc
47    {
48       storeState(Fqn.ROOT, is);
49    }
50
51    public void storeState(Fqn subtree, ObjectInputStream JavaDoc in) throws Exception JavaDoc
52    {
53       ClassLoader JavaDoc currentCL = Thread.currentThread().getContextClassLoader();
54       try
55       {
56          // Set the TCCL to any classloader registered for subtree
57
setUnmarshallingClassLoader(subtree);
58
59          // remove entire existing state
60
this.remove(subtree);
61
62          boolean moveToBuddy = subtree.isChildOf(BuddyManager.BUDDY_BACKUP_SUBTREE_FQN) && subtree.size() > 1;
63
64          // store new state
65
Fqn fqn = null;
66          NodeData nd = null;
67
68          for (nd = (NodeData) in.readObject(); nd != null && !nd.isMarker(); nd = (NodeData) in.readObject())
69          {
70             if (nd.isExceptionMarker())
71             {
72                NodeDataExceptionMarker ndem = (NodeDataExceptionMarker) nd;
73                throw new CacheException("State provider cacheloader at node " + ndem.getCacheNodeIdentity()
74                        + " threw exception during loadState (see Caused by)", ndem.getCause());
75             }
76
77             if (moveToBuddy)
78             {
79                fqn = BuddyManager.getBackupFqn(subtree, nd.fqn);
80             }
81             else
82             {
83                fqn = nd.fqn;
84             }
85
86             if (nd.attrs != null)
87             {
88                this.put(fqn, nd.attrs, true); // creates a node with 0 or more attributes
89
}
90             else
91             {
92                this.put(fqn, null); // creates a node with null attributes
93
}
94          }
95       }
96       finally
97       {
98          Thread.currentThread().setContextClassLoader(currentCL);
99       }
100    }
101
102    public void loadEntireState(ObjectOutputStream JavaDoc os) throws Exception JavaDoc
103    {
104       loadState(Fqn.ROOT, os);
105    }
106
107    public void loadState(Fqn subtree, ObjectOutputStream JavaDoc os) throws Exception JavaDoc
108    {
109       ClassLoader JavaDoc currentCL = Thread.currentThread().getContextClassLoader();
110       try
111       {
112          // Set the TCCL to any classloader registered for subtree
113
setUnmarshallingClassLoader(subtree);
114          loadStateHelper(subtree, os);
115       }
116       finally
117       {
118          Thread.currentThread().setContextClassLoader(currentCL);
119       }
120    }
121
122
123    public void setCache(CacheSPI c)
124    {
125       this.cache = c;
126    }
127
128    public void setRegionManager(RegionManager regionManager)
129    {
130       this.regionManager = regionManager;
131    }
132
133    /**
134     * Checks the ERegionManager for a classloader registered for the
135     * given, and if found sets it as the TCCL
136     *
137     * @param subtree
138     */

139    protected void setUnmarshallingClassLoader(Fqn subtree)
140    {
141       if (regionManager != null)
142       {
143          regionManager.setContextClassLoaderAsCurrent(subtree);
144       }
145    }
146
147    /**
148     * Do a preorder traversal: visit the node first, then the node's children
149     *
150     * @param fqn Start node
151     * @param out
152     * @throws Exception
153     */

154    protected void loadStateHelper(Fqn fqn, ObjectOutputStream JavaDoc out) throws Exception JavaDoc
155    {
156       Map JavaDoc<Object JavaDoc, Object JavaDoc> attrs;
157       Set JavaDoc<? extends Object JavaDoc> children_names;
158       String JavaDoc child_name;
159       Fqn tmp_fqn;
160       NodeData nd;
161
162       // first handle the current node
163
attrs = get(fqn);
164       if (attrs == null || attrs.size() == 0)
165       {
166          nd = new NodeData(fqn);
167       }
168       else
169       {
170          nd = new NodeData(fqn, attrs);
171       }
172       out.writeObject(nd);
173
174       // then visit the children
175
children_names = getChildrenNames(fqn);
176       if (children_names == null)
177       {
178          return;
179       }
180       for (Object JavaDoc children_name : children_names)
181       {
182          child_name = (String JavaDoc) children_name;
183          tmp_fqn = new Fqn(fqn, child_name);
184          loadStateHelper(tmp_fqn, out);
185       }
186    }
187
188    public void put(List JavaDoc<Modification> modifications) throws Exception JavaDoc
189    {
190       for (Modification m : modifications)
191       {
192          switch (m.getType())
193          {
194             case PUT_DATA:
195                put(m.getFqn(), m.getData());
196                break;
197             case PUT_DATA_ERASE:
198                removeData(m.getFqn());
199                put(m.getFqn(), m.getData());
200                break;
201             case PUT_KEY_VALUE:
202                put(m.getFqn(), m.getKey(), m.getValue());
203                break;
204             case REMOVE_DATA:
205                removeData(m.getFqn());
206                break;
207             case REMOVE_KEY_VALUE:
208                remove(m.getFqn(), m.getKey());
209                break;
210             case REMOVE_NODE:
211                remove(m.getFqn());
212                break;
213             case MOVE:
214 // involve moving all children too,
215
_move(m.getFqn(), m.getFqn2());
216                break;
217             default:
218                throw new CacheException("Unknown modificatiobn " + m.getType());
219          }
220       }
221    }
222
223    private void _move(Fqn fqn, Fqn parent) throws Exception JavaDoc
224    {
225       Object JavaDoc name = fqn.getLastElement();
226       Fqn newFqn = new Fqn(parent, name);
227
228       // start deep.
229
for (Object JavaDoc c : getChildrenNames(fqn))
230       {
231          _move(new Fqn(fqn, c), newFqn);
232       }
233       // get data for node.
234
Map JavaDoc<Object JavaDoc, Object JavaDoc> data = get(fqn);
235       if (data != null) // if null, then the node never existed. Don't bother removing?
236
{
237          remove(fqn);
238          put(newFqn, data);
239       }
240    }
241
242 }
243
Popular Tags