KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > hessian > client > HessianProxy


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  * Copyright (c) 2001-2004 Caucho Technology, Inc. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution, if
19  * any, must include the following acknowlegement:
20  * "This product includes software developed by the
21  * Caucho Technology (http://www.caucho.com/)."
22  * Alternately, this acknowlegement may appear in the software itself,
23  * if and wherever such third-party acknowlegements normally appear.
24  *
25  * 4. The names "Hessian", "Resin", and "Caucho" must not be used to
26  * endorse or promote products derived from this software without prior
27  * written permission. For written permission, please contact
28  * info@caucho.com.
29  *
30  * 5. Products derived from this software may not be called "Resin"
31  * nor may "Resin" appear in their names without prior written
32  * permission of Caucho Technology.
33  *
34  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
35  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
36  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
37  * DISCLAIMED. IN NO EVENT SHALL CAUCHO TECHNOLOGY OR ITS CONTRIBUTORS
38  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
39  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
40  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
41  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
42  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
43  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
44  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45  *
46  * @author Scott Ferguson
47  */

48
49 package com.caucho.hessian.client;
50
51 import com.caucho.hessian.io.AbstractHessianInput;
52 import com.caucho.hessian.io.AbstractHessianOutput;
53 import com.caucho.hessian.io.HessianProtocolException;
54
55 import java.io.FileNotFoundException JavaDoc;
56 import java.io.IOException JavaDoc;
57 import java.io.InputStream JavaDoc;
58 import java.io.OutputStream JavaDoc;
59 import java.lang.reflect.InvocationHandler JavaDoc;
60 import java.lang.reflect.Method JavaDoc;
61 import java.lang.reflect.Proxy JavaDoc;
62 import java.net.HttpURLConnection JavaDoc;
63 import java.net.URL JavaDoc;
64 import java.net.URLConnection JavaDoc;
65
66 /**
67  * Proxy implementation for Hessian clients. Applications will generally
68  * use HessianProxyFactory to create proxy clients.
69  */

70 public class HessianProxy implements InvocationHandler JavaDoc {
71   private HessianProxyFactory _factory;
72   private URL JavaDoc _url;
73   
74   HessianProxy(HessianProxyFactory factory, URL JavaDoc url)
75   {
76     _factory = factory;
77     _url = url;
78   }
79
80   /**
81    * Returns the proxy's URL.
82    */

83   public URL JavaDoc getURL()
84   {
85     return _url;
86   }
87
88   /**
89    * Handles the object invocation.
90    *
91    * @param proxy the proxy object to invoke
92    * @param method the method to call
93    * @param args the arguments to the proxy object
94    */

95   public Object JavaDoc invoke(Object JavaDoc proxy, Method JavaDoc method, Object JavaDoc []args)
96     throws Throwable JavaDoc
97   {
98     String JavaDoc methodName = method.getName();
99     Class JavaDoc []params = method.getParameterTypes();
100
101     // equals and hashCode are special cased
102
if (methodName.equals("equals") &&
103         params.length == 1 && params[0].equals(Object JavaDoc.class)) {
104       Object JavaDoc value = args[0];
105       if (value == null || ! Proxy.isProxyClass(value.getClass()))
106         return new Boolean JavaDoc(false);
107
108       HessianProxy handler = (HessianProxy) Proxy.getInvocationHandler(value);
109
110       return new Boolean JavaDoc(_url.equals(handler.getURL()));
111     }
112     else if (methodName.equals("hashCode") && params.length == 0)
113       return new Integer JavaDoc(_url.hashCode());
114     else if (methodName.equals("getHessianType"))
115       return proxy.getClass().getInterfaces()[0].getName();
116     else if (methodName.equals("getHessianURL"))
117       return _url.toString();
118     else if (methodName.equals("toString") && params.length == 0)
119       return "[HessianProxy " + _url + "]";
120
121     InputStream JavaDoc is = null;
122     URLConnection JavaDoc conn = null;
123     HttpURLConnection JavaDoc httpConn = null;
124     
125     try {
126       if (! _factory.isOverloadEnabled()) {
127       }
128       else if (args != null)
129         methodName = methodName + "__" + args.length;
130       else
131         methodName = methodName + "__0";
132
133       conn = sendRequest(methodName, args);
134
135       if (conn instanceof HttpURLConnection JavaDoc) {
136     httpConn = (HttpURLConnection JavaDoc) conn;
137         int code = 500;
138
139         try {
140           code = httpConn.getResponseCode();
141         } catch (Exception JavaDoc e) {
142         }
143
144         if (code != 200) {
145           StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
146           int ch;
147
148           try {
149             is = httpConn.getInputStream();
150
151             if (is != null) {
152               while ((ch = is.read()) >= 0)
153                 sb.append((char) ch);
154
155               is.close();
156             }
157
158             is = httpConn.getErrorStream();
159             if (is != null) {
160               while ((ch = is.read()) >= 0)
161                 sb.append((char) ch);
162             }
163           } catch (FileNotFoundException JavaDoc e) {
164             throw new HessianRuntimeException(String.valueOf(e));
165           } catch (IOException JavaDoc e) {
166         if (is == null)
167           throw new HessianProtocolException(code + ": " + e, e);
168           }
169
170           if (is != null)
171             is.close();
172
173           throw new HessianProtocolException(code + ": " + sb.toString());
174         }
175       }
176
177       is = conn.getInputStream();
178
179       AbstractHessianInput in = _factory.getHessianInput(is);
180
181       return in.readReply(method.getReturnType());
182     } catch (HessianProtocolException e) {
183       throw new HessianRuntimeException(e);
184     } finally {
185       try {
186     if (is != null)
187       is.close();
188       } catch (Throwable JavaDoc e) {
189       }
190       
191       try {
192     if (httpConn != null)
193       httpConn.disconnect();
194       } catch (Throwable JavaDoc e) {
195       }
196     }
197   }
198
199   private URLConnection JavaDoc sendRequest(String JavaDoc methodName, Object JavaDoc []args)
200     throws IOException JavaDoc
201   {
202     URLConnection JavaDoc conn = null;
203     
204     conn = _factory.openConnection(_url);
205
206     // Used chunked mode when available, i.e. JDK 1.5.
207
if (_factory.isChunkedPost() && conn instanceof HttpURLConnection JavaDoc) {
208       try {
209     HttpURLConnection JavaDoc httpConn = (HttpURLConnection JavaDoc) conn;
210
211     httpConn.setChunkedStreamingMode(8 * 1024);
212       } catch (Throwable JavaDoc e) {
213       }
214     }
215     
216     OutputStream JavaDoc os = null;
217
218     try {
219       os = conn.getOutputStream();
220     } catch (Exception JavaDoc e) {
221       throw new HessianRuntimeException(e);
222     }
223
224     try {
225       AbstractHessianOutput out = _factory.getHessianOutput(os);
226
227       out.call(methodName, args);
228       out.flush();
229
230       return conn;
231     } catch (IOException JavaDoc e) {
232       if (conn instanceof HttpURLConnection JavaDoc)
233     ((HttpURLConnection JavaDoc) conn).disconnect();
234
235       throw e;
236     } catch (RuntimeException JavaDoc e) {
237       if (conn instanceof HttpURLConnection JavaDoc)
238     ((HttpURLConnection JavaDoc) conn).disconnect();
239
240       throw e;
241     }
242   }
243 }
244
Popular Tags