1 7 package org.jboss.cache.marshall; 8 9 import org.apache.commons.logging.Log; 10 import org.apache.commons.logging.LogFactory; 11 import org.jboss.cache.Fqn; 12 import org.jboss.cache.GlobalTransaction; 13 import org.jboss.cache.Region; 14 import org.jboss.cache.RegionManager; 15 import org.jboss.cache.buddyreplication.BuddyManager; 16 17 import java.io.ObjectInputStream ; 18 import java.io.ObjectOutputStream ; 19 import java.util.List ; 20 import java.util.Map ; 21 import java.util.concurrent.ConcurrentHashMap ; 22 23 28 public abstract class Marshaller 29 { 30 protected boolean useRegionBasedMarshalling; 31 protected RegionManager regionManager; 32 protected boolean defaultInactive; 33 private static Log log = LogFactory.getLog(Marshaller.class); 34 35 38 private Map <GlobalTransaction, String > transactions = new ConcurrentHashMap <GlobalTransaction, String >(16); 39 40 protected void init(RegionManager manager, boolean defaultInactive, boolean useRegionBasedMarshalling) 41 { 42 this.useRegionBasedMarshalling = useRegionBasedMarshalling; 43 this.defaultInactive = defaultInactive; 44 this.regionManager = manager; 45 } 46 47 55 public abstract void objectToStream(Object obj, ObjectOutputStream out) throws Exception ; 56 57 63 public abstract Object objectFromStream(ObjectInputStream in) throws Exception ; 64 65 66 71 protected String extractFqnFromMethodCall(MethodCall call) 72 { 73 MethodCall c0 = (MethodCall) call.getArgs()[0]; 74 return extractFqn(c0); 75 } 76 77 82 protected String extractFqnFromListOfMethodCall(MethodCall call) 83 { 84 Object [] args = call.getArgs(); 85 MethodCall c0 = (MethodCall) ((List ) args[0]).get(0); 87 return extractFqn(c0); 88 } 89 90 protected String extractFqn(MethodCall methodCall) 91 { 92 if (methodCall == null) 93 { 94 throw new NullPointerException ("method call is null"); 95 } 96 97 String fqnStr = null; 98 Object [] args = methodCall.getArgs(); 99 switch (methodCall.getMethodId()) 100 { 101 case MethodDeclarations.optimisticPrepareMethod_id: 102 case MethodDeclarations.prepareMethod_id: 103 List modifications = (List ) args[1]; 105 fqnStr = extractFqn((MethodCall) modifications.get(0)); 106 107 boolean one_phase_commit = (Boolean ) args[args.length - 1]; 109 110 if (!one_phase_commit) 113 { 114 transactions.put((GlobalTransaction) args[0], fqnStr); 115 } 116 break; 117 case MethodDeclarations.rollbackMethod_id: 118 case MethodDeclarations.commitMethod_id: 119 fqnStr = transactions.remove(args[0]); 121 break; 122 case MethodDeclarations.getPartialStateMethod_id: 123 case MethodDeclarations.dataGravitationMethod_id: 124 case MethodDeclarations.evictNodeMethodLocal_id: 125 case MethodDeclarations.evictVersionedNodeMethodLocal_id: 126 Fqn fqn = (Fqn) args[0]; 127 fqnStr = fqn.toString(); 128 break; 129 case MethodDeclarations.dataGravitationCleanupMethod_id: 130 Fqn fqn1 = (Fqn) args[1]; 131 fqnStr = fqn1.toString(); 132 break; 133 case MethodDeclarations.remoteAnnounceBuddyPoolNameMethod_id: 134 case MethodDeclarations.remoteAssignToBuddyGroupMethod_id: 135 case MethodDeclarations.remoteRemoveFromBuddyGroupMethod_id: 136 break; 137 case MethodDeclarations.replicateMethod_id: 138 fqnStr = extractFqn((MethodCall) args[0]); 140 break; 141 default: 142 if (MethodDeclarations.isCrudMethod(methodCall.getMethodId())) 143 { 144 Fqn fqn2 = (Fqn) args[1]; 145 fqnStr = fqn2.toString(); 146 } 147 else 148 { 149 throw new IllegalArgumentException ("Marshaller.extractFqn(): Unknown id in method call: " + methodCall); 150 } 151 break; 152 153 } 154 155 if (log.isTraceEnabled()) 156 { 157 log.trace("extract(): received " + methodCall + "extracted fqn: " + fqnStr); 158 } 159 160 return fqnStr; 161 } 162 163 169 protected Region getRegion(String fqnString) 170 { 171 Fqn fqn = Fqn.fromString(fqnString); 172 173 if (BuddyManager.isBackupFqn(fqn)) 174 { 175 fqn = fqn.getFqnChild(2, fqn.size()); 177 } 178 return regionManager.getRegion(fqn, false); 179 } 180 181 189 public ClassLoader getClassLoader(String fqn) 190 { 191 ClassLoader result = null; 192 Region region = regionManager.getRegion(Fqn.fromString(fqn), false); 193 if (region != null) result = region.getClassLoader(); 194 return result; 195 } 196 197 205 public boolean isInactive(String fqn) 206 { 207 Region region = regionManager.getRegion(Fqn.fromString(fqn), false); 208 return region == null ? defaultInactive : !region.isActive(); 209 } 210 } 211 | Popular Tags |