1 16 package org.springframework.webflow.execution.repository.continuation; 17 18 import java.io.ByteArrayInputStream ; 19 import java.io.ByteArrayOutputStream ; 20 import java.io.Externalizable ; 21 import java.io.IOException ; 22 import java.io.NotSerializableException ; 23 import java.io.ObjectInput ; 24 import java.io.ObjectInputStream ; 25 import java.io.ObjectOutput ; 26 import java.io.ObjectOutputStream ; 27 import java.util.zip.GZIPInputStream ; 28 import java.util.zip.GZIPOutputStream ; 29 30 import org.springframework.util.FileCopyUtils; 31 import org.springframework.webflow.execution.FlowExecution; 32 33 42 public class SerializedFlowExecutionContinuation extends FlowExecutionContinuation implements Externalizable { 43 44 47 private byte[] flowExecutionData; 48 49 52 private boolean compressed; 53 54 58 public SerializedFlowExecutionContinuation() { 59 } 60 61 67 public SerializedFlowExecutionContinuation(FlowExecution flowExecution, boolean compress) 68 throws ContinuationCreationException { 69 try { 70 flowExecutionData = serialize(flowExecution); 71 if (compress) { 72 flowExecutionData = compress(flowExecutionData); 73 } 74 } 75 catch (NotSerializableException e) { 76 throw new ContinuationCreationException(flowExecution, 77 "Could not serialize flow execution; " + 78 "make sure all objects stored in flow or flash scope are serializable", e); 79 } 80 catch (IOException e) { 81 throw new ContinuationCreationException(flowExecution, 82 "IOException thrown serializing flow execution -- this should not happen!", e); 83 } 84 this.compressed = compress; 85 } 86 87 91 public boolean isCompressed() { 92 return compressed; 93 } 94 95 public FlowExecution unmarshal() throws ContinuationUnmarshalException { 96 try { 97 return deserialize(getFlowExecutionData()); 98 } 99 catch (IOException e) { 100 throw new ContinuationUnmarshalException( 101 "IOException thrown deserializing the flow execution stored in this continuation -- this should not happen!", 102 e); 103 } 104 catch (ClassNotFoundException e) { 105 throw new ContinuationUnmarshalException( 106 "ClassNotFoundException thrown deserializing the flow execution stored in this continuation -- " 107 + "This should not happen! Make sure there are no classloader issues." 108 + "For example, perhaps the Web Flow system is being loaded by a classloader " 109 + "that is a parent of the classloader loading application classes?", e); 110 } 111 } 112 113 public byte[] toByteArray() { 114 try { 115 ByteArrayOutputStream baos = new ByteArrayOutputStream (flowExecutionData.length + 40); 116 ObjectOutputStream oos = new ObjectOutputStream (baos); 117 try { 118 oos.writeObject(this); 119 oos.flush(); 120 } 121 finally { 122 oos.close(); 123 } 124 return baos.toByteArray(); 125 } 126 catch (IOException e) { 127 throw new IllegalStateException (); 128 } 129 } 130 131 133 public void writeExternal(ObjectOutput out) throws IOException { 134 out.writeInt(flowExecutionData.length); 136 out.write(flowExecutionData); 138 out.writeBoolean(compressed); 139 } 140 141 public void readExternal(ObjectInput in) throws IOException , ClassNotFoundException { 142 int length = in.readInt(); 144 flowExecutionData = new byte[length]; 145 in.readFully(flowExecutionData); 147 compressed = in.readBoolean(); 148 } 149 150 152 158 protected byte[] getFlowExecutionData() throws IOException { 159 if (isCompressed()) { 160 return decompress(flowExecutionData); 161 } 162 else { 163 return flowExecutionData; 164 } 165 } 166 167 174 protected byte[] serialize(FlowExecution flowExecution) throws IOException { 175 ByteArrayOutputStream baos = new ByteArrayOutputStream (1024); 176 ObjectOutputStream oos = new ObjectOutputStream (baos); 177 try { 178 oos.writeObject(flowExecution); 179 oos.flush(); 180 return baos.toByteArray(); 181 } 182 finally { 183 oos.close(); 184 } 185 } 186 187 195 protected FlowExecution deserialize(byte[] data) throws IOException , ClassNotFoundException { 196 ObjectInputStream ois = new ObjectInputStream (new ByteArrayInputStream (data)); 197 try { 198 return (FlowExecution)ois.readObject(); 199 } 200 finally { 201 ois.close(); 202 } 203 } 204 205 209 protected byte[] compress(byte[] dataToCompress) throws IOException { 210 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 211 GZIPOutputStream gzipos = new GZIPOutputStream (baos); 212 try { 213 gzipos.write(dataToCompress); 214 gzipos.flush(); 215 } 216 finally { 217 gzipos.close(); 218 } 219 return baos.toByteArray(); 220 } 221 222 226 protected byte[] decompress(byte[] dataToDecompress) throws IOException { 227 GZIPInputStream gzipin = new GZIPInputStream (new ByteArrayInputStream (dataToDecompress)); 228 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 229 try { 230 FileCopyUtils.copy(gzipin, baos); 231 } 232 finally { 233 gzipin.close(); 234 } 235 return baos.toByteArray(); 236 } 237 } | Popular Tags |