1 9 package org.nanocontainer.reflection; 10 11 import org.nanocontainer.integrationkit.ContainerRecorder; 12 import org.picocontainer.MutablePicoContainer; 13 import org.picocontainer.PicoException; 14 15 import java.io.IOException ; 16 import java.io.ObjectInputStream ; 17 import java.io.ObjectOutputStream ; 18 import java.io.Serializable ; 19 import java.lang.reflect.InvocationHandler ; 20 import java.lang.reflect.InvocationTargetException ; 21 import java.lang.reflect.Method ; 22 import java.lang.reflect.Proxy ; 23 import java.util.ArrayList ; 24 import java.util.Iterator ; 25 import java.util.List ; 26 27 36 public class DefaultContainerRecorder implements Serializable , ContainerRecorder { 37 38 private final List invocations = new ArrayList (); 39 private transient MutablePicoContainer container; 40 41 private final InvocationHandler invocationRecorder = new InvocationRecorder(); 42 43 public DefaultContainerRecorder(MutablePicoContainer container) { 44 this.container = container; 45 } 46 47 public MutablePicoContainer getContainerProxy() { 48 return (MutablePicoContainer) Proxy.newProxyInstance(getClass().getClassLoader(), 49 new Class []{MutablePicoContainer.class}, invocationRecorder); 50 } 51 52 public void replay(MutablePicoContainer target) { 53 for (Iterator iter = invocations.iterator(); iter.hasNext();) { 54 Invocation invocation = (Invocation) iter.next(); 55 try { 56 invocation.invoke(target); 57 } catch (IllegalAccessException e) { 58 throw new PicoException(e) { 59 }; 60 } catch (InvocationTargetException e) { 61 throw new PicoException(e) { 62 }; 63 } 64 } 65 } 66 67 private class Invocation implements Serializable { 68 private transient Method method; 69 private Object [] args; 70 71 Invocation(Method method, Object [] args) { 72 this.method = method; 73 this.args = args; 74 } 75 76 private void writeObject(ObjectOutputStream out) throws IOException { 77 out.defaultWriteObject(); 78 out.writeUTF(method.getName()); 79 out.writeObject(method.getDeclaringClass()); 80 Class [] parameterTypes = method.getParameterTypes(); 81 out.writeInt(parameterTypes.length); 82 for (int i = 0; i < parameterTypes.length; i++) { 83 out.writeObject(parameterTypes[i]); 84 } 85 } 86 87 private void readObject(ObjectInputStream in) throws IOException , ClassNotFoundException { 88 in.defaultReadObject(); 89 String methodName = in.readUTF(); 90 Class declaringClass = (Class ) in.readObject(); 91 int n = in.readInt(); 92 Class [] parameterTypes = new Class [n]; 93 for (int i = 0; i < n; i++) { 94 parameterTypes[i] = (Class ) in.readObject(); 95 } 96 try { 97 method = declaringClass.getMethod(methodName, parameterTypes); 98 } catch (NoSuchMethodException e) { 99 throw new IOException ("Couldn't load method " + methodName); 100 } 101 } 102 103 public void invoke(Object target) throws IllegalAccessException , InvocationTargetException { 104 method.invoke(target, args); 105 } 106 } 107 108 private class InvocationRecorder implements InvocationHandler , Serializable { 109 112 public Object invoke(Object proxy, Method method, Object [] args) throws IllegalAccessException , InvocationTargetException { 113 invocations.add(new Invocation(method, args)); 114 return method.invoke(container, args); 115 } 116 }; 117 118 } 119 | Popular Tags |