1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| package org.jboss.cache.statetransfer; |
8 |
| |
9 |
| import org.apache.commons.logging.Log; |
10 |
| import org.apache.commons.logging.LogFactory; |
11 |
| import org.jboss.cache.CacheImpl; |
12 |
| import org.jboss.cache.Fqn; |
13 |
| import org.jboss.cache.Node; |
14 |
| import org.jboss.cache.NodeSPI; |
15 |
| import org.jboss.cache.Version; |
16 |
| import org.jboss.cache.loader.CacheLoader; |
17 |
| import org.jboss.cache.marshall.NodeData; |
18 |
| import org.jboss.cache.marshall.NodeDataExceptionMarker; |
19 |
| |
20 |
| import java.io.IOException; |
21 |
| import java.io.ObjectOutputStream; |
22 |
| import java.util.LinkedList; |
23 |
| import java.util.List; |
24 |
| import java.util.Map; |
25 |
| import java.util.Set; |
26 |
| |
27 |
| public class DefaultStateTransferGenerator implements StateTransferGenerator |
28 |
| { |
29 |
| |
30 |
| public static final short STATE_TRANSFER_VERSION = Version.getVersionShort("2.0.0.GA"); |
31 |
| |
32 |
| private Log log = LogFactory.getLog(getClass().getName()); |
33 |
| |
34 |
| private CacheImpl cache; |
35 |
| |
36 |
| private Set<Fqn> internalFqns; |
37 |
| |
38 |
728
| protected DefaultStateTransferGenerator(CacheImpl cache)
|
39 |
| { |
40 |
728
| this.cache = cache;
|
41 |
728
| this.internalFqns = cache.getInternalFqns();
|
42 |
| } |
43 |
| |
44 |
722
| public void generateState(ObjectOutputStream out, Node rootNode, boolean generateTransient,
|
45 |
| boolean generatePersistent, boolean suppressErrors) throws Throwable |
46 |
| { |
47 |
722
| Fqn fqn = rootNode.getFqn();
|
48 |
722
| try
|
49 |
| { |
50 |
722
| cache.getMarshaller().objectToObjectStream(STATE_TRANSFER_VERSION, out);
|
51 |
722
| if (generateTransient)
|
52 |
| { |
53 |
| |
54 |
718
| if (log.isTraceEnabled())
|
55 |
| { |
56 |
0
| log.trace("writing transient state for " + fqn);
|
57 |
| } |
58 |
718
| marshallTransientState((NodeSPI) rootNode, out);
|
59 |
718
| delimitStream(out);
|
60 |
| |
61 |
718
| if (log.isTraceEnabled())
|
62 |
| { |
63 |
0
| log.trace("transient state succesfully written");
|
64 |
| } |
65 |
| |
66 |
| |
67 |
718
| if (log.isTraceEnabled())
|
68 |
| { |
69 |
0
| log.trace("writing associated state");
|
70 |
| } |
71 |
| |
72 |
718
| marshallAssociatedState(fqn, out);
|
73 |
718
| delimitStream(out);
|
74 |
| |
75 |
718
| if (log.isTraceEnabled())
|
76 |
| { |
77 |
0
| log.trace("associated state succesfully written");
|
78 |
| } |
79 |
| |
80 |
| } |
81 |
| else |
82 |
| { |
83 |
| |
84 |
4
| delimitStream(out);
|
85 |
4
| delimitStream(out);
|
86 |
| } |
87 |
| |
88 |
722
| CacheLoader cacheLoader = cache.getCacheLoaderManager() == null ? null : cache.getCacheLoaderManager().getCacheLoader();
|
89 |
722
| if (cacheLoader != null && generatePersistent)
|
90 |
| { |
91 |
54
| if (log.isTraceEnabled())
|
92 |
| { |
93 |
0
| log.trace("writing persistent state for " + fqn + ",using " + cache.getCacheLoaderManager().getCacheLoader().getClass());
|
94 |
| } |
95 |
| |
96 |
54
| if (fqn.isRoot())
|
97 |
| { |
98 |
38
| cacheLoader.loadEntireState(out);
|
99 |
| } |
100 |
| else |
101 |
| { |
102 |
16
| cacheLoader.loadState(fqn, out);
|
103 |
| } |
104 |
| |
105 |
52
| if (log.isTraceEnabled())
|
106 |
| { |
107 |
0
| log.trace("persistent state succesfully written");
|
108 |
| } |
109 |
| } |
110 |
720
| delimitStream(out);
|
111 |
| } |
112 |
| catch (Throwable t) |
113 |
| { |
114 |
2
| cache.getMarshaller().objectToObjectStream(new NodeDataExceptionMarker(t, cache.getLocalAddress()), out);
|
115 |
2
| throw t;
|
116 |
| } |
117 |
| } |
118 |
| |
119 |
| |
120 |
| |
121 |
| |
122 |
| |
123 |
| |
124 |
| |
125 |
2164
| protected void delimitStream(ObjectOutputStream out) throws Exception
|
126 |
| { |
127 |
2164
| cache.getMarshaller().objectToObjectStream(StateTransferManager.STREAMING_DELIMITER_NODE, out);
|
128 |
| } |
129 |
| |
130 |
| |
131 |
| |
132 |
| |
133 |
| |
134 |
| |
135 |
| |
136 |
718
| protected void marshallTransientState(NodeSPI node, ObjectOutputStream out) throws Exception
|
137 |
| { |
138 |
718
| List<NodeData> nodeData = new LinkedList<NodeData>();
|
139 |
718
| generateNodeDataList(node, nodeData);
|
140 |
718
| cache.getMarshaller().objectToObjectStream(nodeData, out, node.getFqn());
|
141 |
| } |
142 |
| |
143 |
11194
| protected void generateNodeDataList(NodeSPI<?, ?> node, List<NodeData> list) throws Exception
|
144 |
| { |
145 |
11194
| if (internalFqns.contains(node.getFqn()))
|
146 |
| { |
147 |
68
| return;
|
148 |
| } |
149 |
| |
150 |
11126
| Map attrs;
|
151 |
11126
| NodeData nd;
|
152 |
| |
153 |
| |
154 |
11126
| attrs = node.getDataDirect();
|
155 |
11126
| if (attrs.size() == 0)
|
156 |
| { |
157 |
811
| nd = new NodeData(node.getFqn());
|
158 |
| } |
159 |
| else |
160 |
| { |
161 |
10315
| nd = new NodeData(node.getFqn(), attrs);
|
162 |
| } |
163 |
| |
164 |
11126
| list.add(nd);
|
165 |
| |
166 |
| |
167 |
11126
| for (NodeSPI child : node.getChildrenDirect())
|
168 |
| { |
169 |
| |
170 |
10476
| generateNodeDataList(child, list);
|
171 |
| } |
172 |
| } |
173 |
| |
174 |
| |
175 |
| |
176 |
| |
177 |
718
| protected void marshallAssociatedState(Fqn fqn, ObjectOutputStream baos) throws Exception
|
178 |
| { |
179 |
| |
180 |
| } |
181 |
| |
182 |
0
| protected CacheImpl getTreeCache()
|
183 |
| { |
184 |
0
| return cache;
|
185 |
| } |
186 |
| |
187 |
| } |