1 54 package org.hibernate.util; 55 56 import java.io.ByteArrayInputStream ; 57 import java.io.ByteArrayOutputStream ; 58 import java.io.IOException ; 59 import java.io.InputStream ; 60 import java.io.ObjectOutputStream ; 61 import java.io.OutputStream ; 62 import java.io.Serializable ; 63 import java.io.ObjectStreamClass ; 64 import java.io.ObjectInputStream ; 65 66 import org.hibernate.type.SerializationException; 67 import org.apache.commons.logging.Log; 68 import org.apache.commons.logging.LogFactory; 69 70 92 public final class SerializationHelper { 93 94 private static final Log log = LogFactory.getLog(SerializationHelper.class); 95 96 private SerializationHelper() {} 97 98 113 public static Object clone(Serializable object) throws SerializationException { 114 log.trace("Starting clone through serialization"); 115 return deserialize( serialize(object) ); 116 } 117 118 135 public static void serialize(Serializable obj, OutputStream outputStream) throws SerializationException { 136 if (outputStream == null) { 137 throw new IllegalArgumentException ("The OutputStream must not be null"); 138 } 139 140 if ( log.isTraceEnabled() ) { 141 log.trace("Starting serialization of object [" + obj + "]"); 142 } 143 144 ObjectOutputStream out = null; 145 try { 146 out = new ObjectOutputStream (outputStream); 148 out.writeObject(obj); 149 150 } 151 catch (IOException ex) { 152 throw new SerializationException("could not serialize", ex); 153 } 154 finally { 155 try { 156 if (out != null) out.close(); 157 } 158 catch (IOException ignored) {} 159 } 160 } 161 162 170 public static byte[] serialize(Serializable obj) throws SerializationException { 171 ByteArrayOutputStream baos = new ByteArrayOutputStream (512); 172 serialize(obj, baos); 173 return baos.toByteArray(); 174 } 175 176 193 public static Object deserialize(InputStream inputStream) throws SerializationException { 194 if (inputStream == null) { 195 throw new IllegalArgumentException ("The InputStream must not be null"); 196 } 197 198 log.trace("Starting deserialization of object"); 199 200 CustomObjectInputStream in = null; 201 try { 202 in = new CustomObjectInputStream(inputStream); 204 return in.readObject(); 205 206 } 207 catch (ClassNotFoundException ex) { 208 throw new SerializationException("could not deserialize", ex); 209 } 210 catch (IOException ex) { 211 throw new SerializationException("could not deserialize", ex); 212 } 213 finally { 214 try { 215 if (in != null) in.close(); 216 } 217 catch (IOException ex) {} 218 } 219 } 220 221 229 public static Object deserialize(byte[] objectData) throws SerializationException { 230 if (objectData == null) { 231 throw new IllegalArgumentException ("The byte[] must not be null"); 232 } 233 ByteArrayInputStream bais = new ByteArrayInputStream (objectData); 234 return deserialize(bais); 235 } 236 237 238 243 private static final class CustomObjectInputStream extends ObjectInputStream { 244 245 public CustomObjectInputStream(InputStream in) throws IOException { 246 super(in); 247 } 248 249 protected Class resolveClass(ObjectStreamClass v) throws IOException , ClassNotFoundException { 250 String className = v.getName(); 251 Class resolvedClass = null; 252 253 log.trace("Attempting to locate class [" + className + "]"); 254 255 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 256 try { 257 resolvedClass = loader.loadClass(className); 258 log.trace("Class resolved through context class loader"); 259 } 260 catch(ClassNotFoundException e) { 261 log.trace("Asking super to resolve"); 262 resolvedClass = super.resolveClass(v); 263 } 264 265 return resolvedClass; 266 } 267 } 268 } 269 | Popular Tags |