KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > dynaop > remote > StubInterceptor


1 package dynaop.remote;
2
3 import java.io.IOException JavaDoc;
4 import java.io.ObjectInputStream JavaDoc;
5 import java.io.ObjectOutputStream JavaDoc;
6 import java.net.URL JavaDoc;
7 import java.net.URLConnection JavaDoc;
8 import java.rmi.RemoteException JavaDoc;
9
10 import dynaop.Interceptor;
11 import dynaop.Invocation;
12
13 /**
14  *
15  *
16  * @author Bob Lee (crazybob@crazybob.org)
17  */

18 class StubInterceptor implements Interceptor {
19     
20     public Object JavaDoc intercept(Invocation invocation) throws Throwable JavaDoc {
21         InvocationHandle handle =
22             new InvocationHandle(invocation.getMethod(),
23                 invocation.getArguments());
24
25         Object JavaDoc result;
26         URL JavaDoc url = (URL JavaDoc) invocation.getProxy().getProxyContext().unwrap();
27         URLConnection JavaDoc connection;
28         try {
29             connection = url.openConnection();
30             connection.setDoOutput(true);
31             connection.setDoInput(true);
32             connection.setUseCaches(false);
33
34             // output session invocation object.
35
ObjectOutputStream JavaDoc out =
36                 new ObjectOutputStream JavaDoc(connection.getOutputStream());
37             out.writeObject(handle);
38             out.flush();
39             out.close();
40
41             // input result.
42
ObjectInputStream JavaDoc in =
43                 new ObjectInputStream JavaDoc(connection.getInputStream());
44             result = in.readObject();
45             in.close();
46
47             // unwrap and throw application exceptions.
48
if (result instanceof ThrowableWrapper)
49                 throw ((ThrowableWrapper) result).getThrowable();
50             
51             return result;
52         }
53         catch (IOException JavaDoc e) {
54             throw new RemoteException JavaDoc("Error invoking remote service.", e);
55         }
56     }
57 }
58
Popular Tags