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.RegionManager; 12 import org.jgroups.blocks.RpcDispatcher; 13 14 import java.io.ByteArrayOutputStream ; 15 import java.io.ObjectInputStream ; 16 import java.io.ObjectOutputStream ; 17 import java.util.HashMap ; 18 import java.util.Map ; 19 import java.util.StringTokenizer ; 20 21 27 public class VersionAwareMarshaller implements RpcDispatcher.Marshaller 28 { 29 private RegionManager manager; 30 private boolean defaultInactive, useRegionBasedMarshalling; 31 private Log log = LogFactory.getLog(VersionAwareMarshaller.class); 32 33 Marshaller defaultMarshaller; 34 Map <Integer , Marshaller> marshallers = new HashMap <Integer , Marshaller>(); 35 36 private static final int VERSION_200 = 20; 37 38 private int versionInt; 39 40 41 public VersionAwareMarshaller(RegionManager manager, boolean defaultInactive, boolean useRegionBasedMarshalling, String version) 42 { 43 this.manager = manager; 44 this.defaultInactive = defaultInactive; 45 this.useRegionBasedMarshalling = useRegionBasedMarshalling; 46 47 50 versionInt = toMinorVersionInt(version); 51 52 switch (versionInt) 53 { 54 case VERSION_200: 55 default: 56 defaultMarshaller = new CacheMarshaller200(manager, defaultInactive, useRegionBasedMarshalling); 57 marshallers.put(VERSION_200, defaultMarshaller); 58 break; 59 } 60 61 if (log.isDebugEnabled()) 62 { 63 log.debug("Initialised with version " + version + " and versionInt " + versionInt); 64 log.debug("Using default marshaller " + defaultMarshaller.getClass()); 65 } 66 } 67 68 84 private int toMinorVersionInt(String version) 85 { 86 try 87 { 88 StringTokenizer strtok = new StringTokenizer (version, "."); 89 90 String [] versionComponents = {null, null, null, null}; 92 int i = 0; 93 while (strtok.hasMoreTokens()) 94 { 95 versionComponents[i++] = strtok.nextToken(); 96 } 97 98 int major = Integer.parseInt(versionComponents[0]); 99 int minor = Integer.parseInt(versionComponents[1]); 100 101 return (major > 1 || minor > 3) ? (10 * major) + minor : 1; 102 } 103 catch (Exception e) 104 { 105 throw new IllegalArgumentException ("Unsupported replication version string " + version); 106 } 107 } 108 109 117 public byte[] objectToByteBuffer(Object obj) throws Exception 118 { 119 ByteArrayOutputStream bos = new ByteArrayOutputStream (); 120 ObjectOutputStream out; 121 122 out = ObjectSerializationFactory.createObjectOutputStream(bos); 124 out.writeShort(versionInt); 125 126 defaultMarshaller.objectToStream(obj, out); 128 out.close(); 129 130 return bos.toByteArray(); 132 } 133 134 142 public Object objectFromByteBuffer(byte[] buf) throws Exception 143 { 144 Marshaller marshaller; 145 int versionId; 146 ObjectInputStream in; 147 try 148 { 149 in = ObjectSerializationFactory.createObjectInputStream(buf); 152 versionId = in.readShort(); 153 } 154 catch (Exception e) 155 { 156 log.error("Unable to read version id from first two bytes of stream, barfing."); 157 throw e; 158 } 159 160 marshaller = getMarshaller(versionId); 161 162 return marshaller.objectFromStream(in); 163 } 164 165 171 Marshaller getMarshaller(int versionId) 172 { 173 Marshaller marshaller; 174 switch (versionId) 175 { 176 case VERSION_200: 177 default: 178 marshaller = marshallers.get(VERSION_200); 179 if (marshaller == null) 180 { 181 marshaller = new CacheMarshaller200(manager, defaultInactive, useRegionBasedMarshalling); 182 marshallers.put(VERSION_200, marshaller); 183 } 184 break; 185 } 186 return marshaller; 187 } 188 189 195 public boolean isInactive(String fqn) 196 { 197 return defaultMarshaller.isInactive(fqn); 198 } 199 200 206 public ClassLoader getClassLoader(String fqn) 207 { 208 return defaultMarshaller.getClassLoader(fqn); 209 } 210 } 211 | Popular Tags |