001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    
018    package org.apache.commons.proxy.interceptor;
019    
020    import org.apache.commons.proxy.Interceptor;
021    import org.apache.commons.proxy.Invocation;
022    
023    import java.io.ByteArrayInputStream;
024    import java.io.ByteArrayOutputStream;
025    import java.io.IOException;
026    import java.io.ObjectInputStream;
027    import java.io.ObjectOutputStream;
028    import java.io.Serializable;
029    
030    /**
031     * An interceptor which makes a serialized copy of all parameters and return values.  This
032     * is useful when testing remote services to ensure that all parameter/return types
033     * are in fact serializable/deserializable.
034     *
035     * @since 1.0
036     */
037    public class SerializingInterceptor implements Interceptor, Serializable
038    {
039    //**********************************************************************************************************************
040    // Interceptor Implementation
041    //**********************************************************************************************************************
042    
043        public Object intercept( Invocation invocation ) throws Throwable
044        {
045            Object[] arguments = invocation.getArguments();
046            for( int i = 0; i < arguments.length; i++ )
047            {
048                arguments[i] = serializedCopy(arguments[i]);
049            }
050            return serializedCopy(invocation.proceed());
051        }
052    
053    //**********************************************************************************************************************
054    // Other Methods
055    //**********************************************************************************************************************
056    
057        private Object serializedCopy( Object original )
058        {
059            try
060            {
061                final ByteArrayOutputStream bout = new ByteArrayOutputStream();
062                final ObjectOutputStream oout = new ObjectOutputStream(bout);
063                oout.writeObject(original);
064                oout.close();
065                bout.close();
066                final ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
067                final ObjectInputStream oin = new ObjectInputStream(bin);
068                final Object copy = oin.readObject();
069                oin.close();
070                bin.close();
071                return copy;
072            }
073            catch( IOException e )
074            {
075                throw new RuntimeException("Unable to make serialized copy of " +
076                        original.getClass().getName() + " object.", e);
077            }
078            catch( ClassNotFoundException e )
079            {
080                throw new RuntimeException("Unable to make serialized copy of " +
081                        original.getClass().getName() + " object.", e);
082            }
083        }
084    }