1 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 ; 21 import java.util.Map ; 22 import java.util.Set ; 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 <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 out, Node rootNode, boolean generateTransient, 42 boolean generatePersistent, boolean suppressErrors) throws Throwable 43 { 44 Fqn fqn = rootNode.getFqn(); 45 try 46 { 47 out.writeShort(STATE_TRANSFER_VERSION); 48 if (generateTransient) 49 { 50 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 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 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 t) 111 { 112 out.writeObject(new NodeDataExceptionMarker(t, cache.getLocalAddress())); 113 throw t; 114 } 115 } 116 117 123 protected void marshallTransientState(NodeSPI node, ObjectOutputStream out) throws Exception 124 { 125 126 if (internalFqns.contains(node.getFqn())) 127 { 128 return; 129 } 130 131 Map attrs; 132 NodeData nd; 133 134 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 for (NodeSPI child : node.getChildrenDirect()) 148 { 149 marshallTransientState(child, out); 150 } 151 } 152 153 156 protected void marshallAssociatedState(Fqn fqn, ObjectOutputStream baos) throws Exception 157 { 158 } 160 161 protected CacheImpl getTreeCache() 162 { 163 return cache; 164 } 165 166 } 167 | Popular Tags |