KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > burlap > client > BurlapProxy


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 "Burlap", "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.burlap.client;
50
51 import com.caucho.burlap.io.AbstractBurlapInput;
52 import com.caucho.burlap.io.BurlapOutput;
53
54 import java.io.FileNotFoundException JavaDoc;
55 import java.io.IOException JavaDoc;
56 import java.io.InputStream JavaDoc;
57 import java.io.OutputStream JavaDoc;
58 import java.lang.reflect.InvocationHandler JavaDoc;
59 import java.lang.reflect.Method JavaDoc;
60 import java.lang.reflect.Proxy JavaDoc;
61 import java.net.HttpURLConnection JavaDoc;
62 import java.net.URL JavaDoc;
63 import java.net.URLConnection JavaDoc;
64
65 /**
66  * Proxy implementation for Burlap clients. Applications will generally
67  * use BurlapProxyFactory to create proxy clients.
68  */

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

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

94   public Object JavaDoc invoke(Object JavaDoc proxy, Method JavaDoc method, Object JavaDoc []args)
95     throws Throwable JavaDoc
96   {
97     String JavaDoc methodName = method.getName();
98     Class JavaDoc []params = method.getParameterTypes();
99
100     // equals and hashCode are special cased
101
if (methodName.equals("equals") &&
102         params.length == 1 && params[0].equals(Object JavaDoc.class)) {
103       Object JavaDoc value = args[0];
104       if (value == null || ! Proxy.isProxyClass(value.getClass()))
105         return new Boolean JavaDoc(false);
106
107       BurlapProxy handler = (BurlapProxy) Proxy.getInvocationHandler(value);
108
109       return new Boolean JavaDoc(_url.equals(handler.getURL()));
110     }
111     else if (methodName.equals("hashCode") && params.length == 0)
112       return new Integer JavaDoc(_url.hashCode());
113     else if (methodName.equals("getBurlapType"))
114       return proxy.getClass().getInterfaces()[0].getName();
115     else if (methodName.equals("getBurlapURL"))
116       return _url.toString();
117     else if (methodName.equals("toString") && params.length == 0)
118       return "[BurlapProxy " + _url + "]";
119
120     InputStream JavaDoc is = null;
121     
122     URLConnection JavaDoc conn = null;
123     HttpURLConnection JavaDoc httpConn = null;
124     try {
125       conn = _factory.openConnection(_url);
126       conn.setRequestProperty("Content-Type", "text/xml");
127     
128       OutputStream JavaDoc os;
129
130       try {
131     os = conn.getOutputStream();
132       } catch (Exception JavaDoc e) {
133     throw new BurlapRuntimeException(e);
134       }
135
136       BurlapOutput out = _factory.getBurlapOutput(os);
137
138       if (! _factory.isOverloadEnabled()) {
139       }
140       else if (args != null)
141         methodName = methodName + "__" + args.length;
142       else
143         methodName = methodName + "__0";
144
145       out.call(methodName, args);
146
147       try {
148     os.flush();
149       } catch (Exception JavaDoc e) {
150     throw new BurlapRuntimeException(e);
151       }
152
153       if (conn instanceof HttpURLConnection JavaDoc) {
154     httpConn = (HttpURLConnection JavaDoc) conn;
155         int code = 500;
156
157         try {
158           code = httpConn.getResponseCode();
159         } catch (Exception JavaDoc e) {
160         }
161
162         if (code != 200) {
163           StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
164           int ch;
165
166           try {
167             is = httpConn.getInputStream();
168
169             if (is != null) {
170               while ((ch = is.read()) >= 0)
171                 sb.append((char) ch);
172
173               is.close();
174             }
175
176             is = httpConn.getErrorStream();
177             if (is != null) {
178               while ((ch = is.read()) >= 0)
179                 sb.append((char) ch);
180             }
181           } catch (FileNotFoundException JavaDoc e) {
182             throw new BurlapRuntimeException(code + ": " + String.valueOf(e));
183           } catch (IOException JavaDoc e) {
184           }
185
186           if (is != null)
187             is.close();
188
189           throw new BurlapProtocolException(code + ": " + sb.toString());
190         }
191       }
192
193       is = conn.getInputStream();
194
195       AbstractBurlapInput in = _factory.getBurlapInput(is);
196
197       return in.readReply(method.getReturnType());
198     } catch (BurlapProtocolException e) {
199       throw new BurlapRuntimeException(e);
200     } finally {
201       try {
202     if (is != null)
203       is.close();
204       } catch (IOException JavaDoc e) {
205       }
206       
207       if (httpConn != null)
208     httpConn.disconnect();
209     }
210   }
211 }
212
Popular Tags