KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > statetransfer > DefaultStateTransferGenerator


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.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.loader.NodeData;
18 import org.jboss.cache.loader.NodeDataExceptionMarker;
19
20 import java.io.ObjectOutputStream JavaDoc;
21 import java.util.Map JavaDoc;
22 import java.util.Set JavaDoc;
23
24 public class DefaultStateTransferGenerator implements StateTransferGenerator
25 {
26
27    public static final short STATE_TRANSFER_VERSION = Version.getVersionShort("2.0.0.GA");
28
29    private Log log = LogFactory.getLog(getClass().getName());
30
31    private CacheImpl cache;
32
33    private Set JavaDoc<Fqn> internalFqns;
34
35    protected DefaultStateTransferGenerator(CacheImpl cache)
36    {
37       this.cache = cache;
38       this.internalFqns = cache.getInternalFqns();
39    }
40
41    public void generateState(ObjectOutputStream JavaDoc out, Node rootNode, boolean generateTransient,
42                              boolean generatePersistent, boolean suppressErrors) throws Throwable JavaDoc
43    {
44       Fqn fqn = rootNode.getFqn();
45       try
46       {
47          out.writeShort(STATE_TRANSFER_VERSION);
48          if (generateTransient)
49          {
50             //transient + marker
51
if (log.isTraceEnabled())
52             {
53                log.trace("writing transient state for " + fqn);
54             }
55
56             marshallTransientState((NodeSPI) rootNode, out);
57             out.writeObject(StateTransferManager.STREAMING_DELIMETER_NODE);
58
59             if (log.isTraceEnabled())
60             {
61                log.trace("transient state succesfully written");
62             }
63
64             //associated + marker
65
if (log.isTraceEnabled())
66             {
67                log.trace("writing associated state");
68             }
69
70             marshallAssociatedState(fqn, out);
71             out.writeObject(StateTransferManager.STREAMING_DELIMETER_NODE);
72
73             if (log.isTraceEnabled())
74             {
75                log.trace("associated state succesfully written");
76             }
77
78          }
79          else
80          {
81             //we have to write two markers for transient and associated
82
out.writeObject(StateTransferManager.STREAMING_DELIMETER_NODE);
83             out.writeObject(StateTransferManager.STREAMING_DELIMETER_NODE);
84          }
85
86          CacheLoader cacheLoader = cache.getCacheLoaderManager() == null ? null : cache.getCacheLoaderManager().getCacheLoader();
87          if (cacheLoader != null && generatePersistent)
88          {
89             if (log.isTraceEnabled())
90             {
91                log.trace("writing persistent state for " + fqn + ",using " + cache.getCacheLoaderManager().getCacheLoader().getClass());
92             }
93
94             if (fqn.isRoot())
95             {
96                cacheLoader.loadEntireState(out);
97             }
98             else
99             {
100                cacheLoader.loadState(fqn, out);
101             }
102
103             if (log.isTraceEnabled())
104             {
105                log.trace("persistent state succesfully written");
106             }
107          }
108          out.writeObject(StateTransferManager.STREAMING_DELIMETER_NODE);
109       }
110       catch (Throwable JavaDoc t)
111       {
112          out.writeObject(new NodeDataExceptionMarker(t, cache.getLocalAddress()));
113          throw t;
114       }
115    }
116
117    /**
118     * Do a preorder traversal: visit the node first, then the node's children
119     *
120     * @param out
121     * @throws Exception
122     */

123    protected void marshallTransientState(NodeSPI node, ObjectOutputStream JavaDoc out) throws Exception JavaDoc
124    {
125
126       if (internalFqns.contains(node.getFqn()))
127       {
128          return;
129       }
130
131       Map JavaDoc attrs;
132       NodeData nd;
133
134       // first handle the current node
135
attrs = node.getDataDirect();
136       if (attrs.size() == 0)
137       {
138          nd = new NodeData(node.getFqn());
139       }
140       else
141       {
142          nd = new NodeData(node.getFqn(), attrs);
143       }
144       out.writeObject(nd);
145
146       // then visit the children
147
for (NodeSPI child : node.getChildrenDirect())
148       {
149          marshallTransientState(child, out);
150       }
151    }
152
153    /**
154     * Does nothing in this base class; can be overridden in a subclass.
155     */

156    protected void marshallAssociatedState(Fqn fqn, ObjectOutputStream JavaDoc baos) throws Exception JavaDoc
157    {
158       // no-op in this base class
159
}
160
161    protected CacheImpl getTreeCache()
162    {
163       return cache;
164    }
165
166 }
167
Popular Tags