1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.proxy.interceptor;
19
20 import org.apache.commons.proxy.Interceptor;
21 import org.apache.commons.proxy.Invocation;
22
23 import java.io.ByteArrayInputStream;
24 import java.io.ByteArrayOutputStream;
25 import java.io.IOException;
26 import java.io.ObjectInputStream;
27 import java.io.ObjectOutputStream;
28
29
30
31
32
33
34
35 public class SerializingInterceptor implements Interceptor
36 {
37 public Object intercept(Invocation invocation) throws Throwable
38 {
39 Object[] arguments = invocation.getArguments();
40 for (int i = 0; i < arguments.length; i++)
41 {
42 arguments[i] = serializedCopy(arguments[i]);
43 }
44 return serializedCopy(invocation.proceed());
45 }
46
47 private Object serializedCopy(Object original)
48 {
49 try
50 {
51 final ByteArrayOutputStream bout = new ByteArrayOutputStream();
52 final ObjectOutputStream oout = new ObjectOutputStream(bout);
53 oout.writeObject(original);
54 oout.close();
55 bout.close();
56 final ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
57 final ObjectInputStream oin = new ObjectInputStream(bin);
58 final Object copy = oin.readObject();
59 oin.close();
60 bin.close();
61 return copy;
62 }
63 catch (IOException e)
64 {
65 throw new RuntimeException( "Unable to make serialized copy of " +
66 original.getClass().getName() + " object.", e );
67 }
68 catch (ClassNotFoundException e)
69 {
70 throw new RuntimeException( "Unable to make serialized copy of " +
71 original.getClass().getName() + " object.", e );
72 }
73 }
74 }