KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nanocontainer > reflection > DefaultContainerRecorder


1 /*****************************************************************************
2  * Copyright (C) NanoContainer Organization. All rights reserved. *
3  * ------------------------------------------------------------------------- *
4  * The software in this package is published under the terms of the BSD *
5  * style license a copy of which has been included with this distribution in *
6  * the LICENSE.txt file. *
7  * *
8  *****************************************************************************/

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 JavaDoc;
16 import java.io.ObjectInputStream JavaDoc;
17 import java.io.ObjectOutputStream JavaDoc;
18 import java.io.Serializable JavaDoc;
19 import java.lang.reflect.InvocationHandler JavaDoc;
20 import java.lang.reflect.InvocationTargetException JavaDoc;
21 import java.lang.reflect.Method JavaDoc;
22 import java.lang.reflect.Proxy JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.List JavaDoc;
26
27 /**
28  * This class is serializable. The original container will not be serialized
29  * (for performance reasons), but the invocations will, so they can be replayed at the
30  * other end of the wire.
31  *
32  * @author Konstantin Pribluda ( konstantin.pribluda(at)infodesire.com )
33  * @author Aslak Hellesøy
34  * @author Mauro Talevi
35  */

36 public class DefaultContainerRecorder implements Serializable JavaDoc, ContainerRecorder {
37
38     private final List JavaDoc invocations = new ArrayList JavaDoc();
39     private transient MutablePicoContainer container;
40
41     private final InvocationHandler JavaDoc 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 JavaDoc[]{MutablePicoContainer.class}, invocationRecorder);
50     }
51
52     public void replay(MutablePicoContainer target) {
53         for (Iterator JavaDoc iter = invocations.iterator(); iter.hasNext();) {
54             Invocation invocation = (Invocation) iter.next();
55             try {
56                 invocation.invoke(target);
57             } catch (IllegalAccessException JavaDoc e) {
58                 throw new PicoException(e) {
59                 };
60             } catch (InvocationTargetException JavaDoc e) {
61                 throw new PicoException(e) {
62                 };
63             }
64         }
65     }
66
67     private class Invocation implements Serializable JavaDoc {
68         private transient Method JavaDoc method;
69         private Object JavaDoc[] args;
70
71         Invocation(Method JavaDoc method, Object JavaDoc[] args) {
72             this.method = method;
73             this.args = args;
74         }
75
76         private void writeObject(ObjectOutputStream JavaDoc out) throws IOException JavaDoc {
77             out.defaultWriteObject();
78             out.writeUTF(method.getName());
79             out.writeObject(method.getDeclaringClass());
80             Class JavaDoc[] 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 JavaDoc in) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
88             in.defaultReadObject();
89             String JavaDoc methodName = in.readUTF();
90             Class JavaDoc declaringClass = (Class JavaDoc) in.readObject();
91             int n = in.readInt();
92             Class JavaDoc[] parameterTypes = new Class JavaDoc[n];
93             for (int i = 0; i < n; i++) {
94                 parameterTypes[i] = (Class JavaDoc) in.readObject();
95             }
96             try {
97                 method = declaringClass.getMethod(methodName, parameterTypes);
98             } catch (NoSuchMethodException JavaDoc e) {
99                 throw new IOException JavaDoc("Couldn't load method " + methodName);
100             }
101         }
102
103         public void invoke(Object JavaDoc target) throws IllegalAccessException JavaDoc, InvocationTargetException JavaDoc {
104             method.invoke(target, args);
105         }
106     }
107
108     private class InvocationRecorder implements InvocationHandler JavaDoc, Serializable JavaDoc {
109         /**
110          * Record invocation and invoke on underlying container
111          */

112         public Object JavaDoc invoke(Object JavaDoc proxy, Method JavaDoc method, Object JavaDoc[] args) throws IllegalAccessException JavaDoc, InvocationTargetException JavaDoc {
113             invocations.add(new Invocation(method, args));
114             return method.invoke(container, args);
115         }
116     };
117
118 }
119
Popular Tags