View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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   * An interceptor which makes a serialized copy of all parameters and return values.  This
31   * is useful when testing remote services to ensure that all parameter/return types
32   * are in fact serializable/deserializable.
33   * @since 1.0
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  }