1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| package org.jboss.cache.loader; |
8 |
| |
9 |
| import org.apache.commons.logging.Log; |
10 |
| import org.apache.commons.logging.LogFactory; |
11 |
| import org.jboss.cache.CacheException; |
12 |
| import org.jboss.cache.CacheSPI; |
13 |
| import org.jboss.cache.Fqn; |
14 |
| import org.jboss.cache.Modification; |
15 |
| import org.jboss.cache.RegionManager; |
16 |
| import org.jboss.cache.buddyreplication.BuddyManager; |
17 |
| import org.jboss.cache.marshall.Marshaller; |
18 |
| import org.jboss.cache.marshall.NodeData; |
19 |
| import org.jboss.cache.marshall.NodeDataExceptionMarker; |
20 |
| import org.jboss.cache.marshall.NodeDataMarker; |
21 |
| import org.jboss.cache.util.MapCopy; |
22 |
| |
23 |
| import java.io.ObjectInputStream; |
24 |
| import java.io.ObjectOutputStream; |
25 |
| import java.util.LinkedList; |
26 |
| import java.util.List; |
27 |
| import java.util.Map; |
28 |
| import java.util.Set; |
29 |
| |
30 |
| |
31 |
| |
32 |
| |
33 |
| |
34 |
| |
35 |
| |
36 |
| |
37 |
| |
38 |
| |
39 |
| |
40 |
| |
41 |
| |
42 |
| |
43 |
| public abstract class AbstractCacheLoader implements CacheLoader |
44 |
| { |
45 |
| protected CacheSPI cache; |
46 |
| protected RegionManager regionManager; |
47 |
| private static final Log log = LogFactory.getLog(AbstractCacheLoader.class); |
48 |
| |
49 |
55
| public void put(Fqn fqn, Map<Object, Object> attributes, boolean erase) throws Exception
|
50 |
| { |
51 |
55
| if (erase)
|
52 |
| { |
53 |
55
| removeData(fqn);
|
54 |
| } |
55 |
| |
56 |
| |
57 |
55
| Map<Object, Object> attrs = (attributes == null ? null : new MapCopy<Object, Object>(attributes));
|
58 |
55
| put(fqn, attrs);
|
59 |
| } |
60 |
| |
61 |
60
| public void storeEntireState(ObjectInputStream is) throws Exception
|
62 |
| { |
63 |
60
| storeState(Fqn.ROOT, is);
|
64 |
| } |
65 |
| |
66 |
128
| public void storeState(Fqn subtree, ObjectInputStream in) throws Exception
|
67 |
| { |
68 |
| |
69 |
| |
70 |
| |
71 |
| |
72 |
| |
73 |
| |
74 |
| |
75 |
128
| this.remove(subtree);
|
76 |
| |
77 |
128
| boolean moveToBuddy = subtree.isChildOf(BuddyManager.BUDDY_BACKUP_SUBTREE_FQN) && subtree.size() > 1;
|
78 |
| |
79 |
| |
80 |
128
| Fqn fqn = null;
|
81 |
| |
82 |
128
| Object objectFromStream = cache.getMarshaller().objectFromObjectStream(in);
|
83 |
128
| if (objectFromStream instanceof NodeDataMarker)
|
84 |
| { |
85 |
| |
86 |
41
| return;
|
87 |
| } |
88 |
87
| List nodeData = (List) objectFromStream;
|
89 |
| |
90 |
| |
91 |
85
| for (Object o : nodeData)
|
92 |
| { |
93 |
240
| NodeData nd = (NodeData) o;
|
94 |
0
| if (nd.isMarker()) break;
|
95 |
| |
96 |
240
| if (nd.isExceptionMarker())
|
97 |
| { |
98 |
0
| NodeDataExceptionMarker ndem = (NodeDataExceptionMarker) nd;
|
99 |
0
| throw new CacheException("State provider cacheloader at node " + ndem.getCacheNodeIdentity()
|
100 |
| + " threw exception during loadState (see Caused by)", ndem.getCause()); |
101 |
| } |
102 |
| |
103 |
240
| if (moveToBuddy)
|
104 |
| { |
105 |
26
| fqn = BuddyManager.getBackupFqn(subtree, nd.getFqn());
|
106 |
| } |
107 |
| else |
108 |
| { |
109 |
214
| fqn = nd.getFqn();
|
110 |
| } |
111 |
| |
112 |
240
| if (nd.getAttributes() != null)
|
113 |
| { |
114 |
141
| this.put(fqn, nd.getAttributes(), true);
|
115 |
| } |
116 |
| else |
117 |
| { |
118 |
99
| this.put(fqn, null);
|
119 |
| } |
120 |
| } |
121 |
| |
122 |
| |
123 |
| |
124 |
| |
125 |
| |
126 |
| |
127 |
| |
128 |
| |
129 |
| } |
130 |
| |
131 |
94
| public void loadEntireState(ObjectOutputStream os) throws Exception
|
132 |
| { |
133 |
94
| loadState(Fqn.ROOT, os);
|
134 |
| } |
135 |
| |
136 |
125
| public void loadState(Fqn subtree, ObjectOutputStream os) throws Exception
|
137 |
| { |
138 |
| |
139 |
| |
140 |
| |
141 |
| |
142 |
| |
143 |
125
| loadStateHelper(subtree, os);
|
144 |
| |
145 |
| |
146 |
| |
147 |
| |
148 |
| |
149 |
| } |
150 |
| |
151 |
| |
152 |
1191
| public void setCache(CacheSPI c)
|
153 |
| { |
154 |
1191
| this.cache = c;
|
155 |
| } |
156 |
| |
157 |
17
| public void setRegionManager(RegionManager regionManager)
|
158 |
| { |
159 |
17
| this.regionManager = regionManager;
|
160 |
| } |
161 |
| |
162 |
| |
163 |
| |
164 |
| |
165 |
| |
166 |
| |
167 |
| |
168 |
| |
169 |
125
| protected void loadStateHelper(Fqn fqn, ObjectOutputStream out) throws Exception
|
170 |
| { |
171 |
125
| List<NodeData> list = new LinkedList<NodeData>();
|
172 |
125
| getNodeDataList(fqn, list);
|
173 |
0
| if (log.isTraceEnabled()) log.trace("Loading state of " + list.size() + " nodes into stream");
|
174 |
125
| cache.getMarshaller().objectToObjectStream(list, out, fqn);
|
175 |
| } |
176 |
| |
177 |
237
| protected void getNodeDataList(Fqn fqn, List<NodeData> list) throws Exception
|
178 |
| { |
179 |
237
| Map<Object, Object> attrs;
|
180 |
237
| Set<? extends Object> children_names;
|
181 |
237
| String child_name;
|
182 |
237
| Fqn tmp_fqn;
|
183 |
237
| NodeData nd;
|
184 |
| |
185 |
| |
186 |
237
| attrs = get(fqn);
|
187 |
237
| if (attrs == null || attrs.size() == 0)
|
188 |
| { |
189 |
118
| nd = new NodeData(fqn);
|
190 |
| } |
191 |
| else |
192 |
| { |
193 |
119
| nd = new NodeData(fqn, attrs);
|
194 |
| } |
195 |
| |
196 |
237
| list.add(nd);
|
197 |
| |
198 |
| |
199 |
237
| children_names = getChildrenNames(fqn);
|
200 |
237
| if (children_names == null)
|
201 |
| { |
202 |
152
| return;
|
203 |
| } |
204 |
85
| for (Object children_name : children_names)
|
205 |
| { |
206 |
136
| child_name = (String) children_name;
|
207 |
136
| tmp_fqn = new Fqn(fqn, child_name);
|
208 |
| |
209 |
136
| getNodeDataList(tmp_fqn, list);
|
210 |
| } |
211 |
| } |
212 |
| |
213 |
801
| public void put(List<Modification> modifications) throws Exception
|
214 |
| { |
215 |
801
| for (Modification m : modifications)
|
216 |
| { |
217 |
1135
| switch (m.getType())
|
218 |
| { |
219 |
607
| case PUT_DATA:
|
220 |
607
| put(m.getFqn(), m.getData());
|
221 |
607
| break;
|
222 |
0
| case PUT_DATA_ERASE:
|
223 |
0
| removeData(m.getFqn());
|
224 |
0
| put(m.getFqn(), m.getData());
|
225 |
0
| break;
|
226 |
414
| case PUT_KEY_VALUE:
|
227 |
414
| put(m.getFqn(), m.getKey(), m.getValue());
|
228 |
414
| break;
|
229 |
24
| case REMOVE_DATA:
|
230 |
24
| removeData(m.getFqn());
|
231 |
24
| break;
|
232 |
25
| case REMOVE_KEY_VALUE:
|
233 |
25
| remove(m.getFqn(), m.getKey());
|
234 |
25
| break;
|
235 |
59
| case REMOVE_NODE:
|
236 |
59
| remove(m.getFqn());
|
237 |
59
| break;
|
238 |
6
| case MOVE:
|
239 |
| |
240 |
6
| _move(m.getFqn(), m.getFqn2());
|
241 |
6
| break;
|
242 |
0
| default:
|
243 |
0
| throw new CacheException("Unknown modificatiobn " + m.getType());
|
244 |
| } |
245 |
| } |
246 |
| } |
247 |
| |
248 |
12
| private void _move(Fqn fqn, Fqn parent) throws Exception
|
249 |
| { |
250 |
12
| Object name = fqn.getLastElement();
|
251 |
12
| Fqn newFqn = new Fqn(parent, name);
|
252 |
| |
253 |
| |
254 |
12
| Set childrenNames = getChildrenNames(fqn);
|
255 |
12
| if (childrenNames != null)
|
256 |
| { |
257 |
6
| for (Object c : childrenNames)
|
258 |
| { |
259 |
6
| _move(new Fqn(fqn, c), newFqn);
|
260 |
| } |
261 |
| } |
262 |
| |
263 |
12
| Map<Object, Object> data = get(fqn);
|
264 |
12
| if (data != null)
|
265 |
| { |
266 |
12
| remove(fqn);
|
267 |
12
| put(newFqn, data);
|
268 |
| } |
269 |
| } |
270 |
| |
271 |
68989
| protected Marshaller getMarshaller()
|
272 |
| { |
273 |
68989
| return cache.getMarshaller();
|
274 |
| } |
275 |
| } |