KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > mx4j > tools > remote > caucho > CauchoClientInvoker


1 /*
2  * Copyright (C) The MX4J Contributors.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the MX4J License version 1.0.
6  * See the terms of the MX4J License in the documentation provided with this software.
7  */

8
9 package mx4j.tools.remote.caucho;
10
11 import java.io.BufferedInputStream JavaDoc;
12 import java.io.BufferedOutputStream JavaDoc;
13 import java.io.IOException JavaDoc;
14 import java.io.InputStream JavaDoc;
15 import java.io.OutputStream JavaDoc;
16 import java.lang.reflect.InvocationHandler JavaDoc;
17 import java.lang.reflect.Method JavaDoc;
18 import java.lang.reflect.Proxy JavaDoc;
19 import java.net.URL JavaDoc;
20 import java.net.URLConnection JavaDoc;
21
22 import mx4j.log.Log;
23 import mx4j.log.Logger;
24 import mx4j.tools.remote.http.HTTPClientInvoker;
25 import mx4j.tools.remote.http.HTTPConnection;
26
27 /**
28  * @version $Revision: 1.4 $
29  */

30 public abstract class CauchoClientInvoker extends HTTPClientInvoker
31 {
32    private final String JavaDoc endpoint;
33    private final HTTPConnection service;
34
35    public CauchoClientInvoker(String JavaDoc endpoint)
36    {
37       this.endpoint = endpoint;
38       CauchoServiceProxy proxy = new CauchoServiceProxy();
39       service = (HTTPConnection)Proxy.newProxyInstance(proxy.getClass().getClassLoader(), new Class JavaDoc[]{HTTPConnection.class}, proxy);
40    }
41
42    protected HTTPConnection getService()
43    {
44       return service;
45    }
46
47    protected abstract CauchoInput createCauchoInput(InputStream JavaDoc stream);
48
49    protected abstract CauchoOutput createCauchoOutput(OutputStream JavaDoc stream);
50
51    private class CauchoServiceProxy implements InvocationHandler JavaDoc
52    {
53       public Object JavaDoc invoke(Object JavaDoc proxy, Method JavaDoc method, Object JavaDoc[] args) throws Throwable JavaDoc
54       {
55          URLConnection JavaDoc connection = new URL JavaDoc(endpoint).openConnection();
56          connection.setDoInput(true);
57          connection.setDoOutput(true);
58          connection.setUseCaches(false);
59          OutputStream JavaDoc os = new BufferedOutputStream JavaDoc(connection.getOutputStream());
60          try
61          {
62             CauchoOutput output = createCauchoOutput(os);
63             startCall(output);
64             writeHeaders(output);
65             writeMethod(output, method);
66             writeArguments(output, args);
67             completeCall(output);
68             os.flush();
69
70             InputStream JavaDoc is = new BufferedInputStream JavaDoc(connection.getInputStream());
71             try
72             {
73                CauchoInput input = createCauchoInput(is);
74                input.startReply();
75                Object JavaDoc result = input.readObject(null);
76                input.completeReply();
77                return result;
78             }
79             catch (Throwable JavaDoc x)
80             {
81                Logger logger = Log.getLogger(CauchoClientInvoker.class.getName());
82                if (logger.isEnabledFor(Logger.DEBUG)) logger.debug("", x);
83                throw x;
84             }
85             finally
86             {
87                is.close();
88             }
89          }
90          finally
91          {
92             os.close();
93          }
94       }
95
96       private void startCall(CauchoOutput output) throws IOException JavaDoc
97       {
98          output.startCall();
99       }
100
101       private void writeHeaders(CauchoOutput output) throws IOException JavaDoc
102       {
103          output.writeHeader(CauchoService.CONNECTION_ID_HEADER_NAME);
104          output.writeObject(getConnectionId());
105       }
106
107       private void writeMethod(CauchoOutput output, Method JavaDoc method) throws IOException JavaDoc
108       {
109          String JavaDoc methodName = mangleMethodName(method);
110          output.writeMethod(methodName);
111       }
112
113       private String JavaDoc mangleMethodName(Method JavaDoc method)
114       {
115          return CauchoService.mangleMethodName(method);
116       }
117
118       private void writeArguments(CauchoOutput output, Object JavaDoc[] args) throws IOException JavaDoc
119       {
120          if (args != null) for (int i = 0; i < args.length; ++i) output.writeObject(args[i]);
121       }
122
123       private void completeCall(CauchoOutput output) throws IOException JavaDoc
124       {
125          output.completeCall();
126       }
127    }
128 }
129
Popular Tags