KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > server > admin > HessianHmuxProxy


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.server.admin;
31
32 import com.caucho.hessian.io.AbstractHessianInput;
33 import com.caucho.hessian.io.AbstractHessianOutput;
34 import com.caucho.hessian.io.Hessian2Input;
35 import com.caucho.hessian.io.HessianOutput;
36 import com.caucho.hessian.io.HessianProtocolException;
37 import com.caucho.util.CharBuffer;
38 import com.caucho.vfs.Path;
39 import com.caucho.vfs.ReadStream;
40 import com.caucho.vfs.ReadWritePair;
41 import com.caucho.vfs.WriteStream;
42
43 import java.io.IOException JavaDoc;
44 import java.lang.reflect.InvocationHandler JavaDoc;
45 import java.lang.reflect.Method JavaDoc;
46 import java.lang.reflect.Proxy JavaDoc;
47
48 /**
49  * Proxy implementation for Hessian clients. Applications will generally
50  * use HessianProxyFactory to create proxy clients.
51  */

52 public class HessianHmuxProxy implements InvocationHandler JavaDoc {
53   private Path _path;
54   
55   private HessianHmuxProxy(Path url)
56   {
57     _path = url;
58   }
59   
60   public static <X> X create(Path url, Class JavaDoc<X> api)
61   {
62     Thread JavaDoc thread = Thread.currentThread();
63     
64     return (X) Proxy.newProxyInstance(thread.getContextClassLoader(),
65                       new Class JavaDoc[] { api },
66                       new HessianHmuxProxy(url));
67   }
68
69   /**
70    * Handles the object invocation.
71    *
72    * @param proxy the proxy object to invoke
73    * @param method the method to call
74    * @param args the arguments to the proxy object
75    */

76   public Object JavaDoc invoke(Object JavaDoc proxy, Method JavaDoc method, Object JavaDoc []args)
77     throws Throwable JavaDoc
78   {
79     String JavaDoc methodName = method.getName();
80     Class JavaDoc []params = method.getParameterTypes();
81
82     // equals and hashCode are special cased
83
if (methodName.equals("equals") &&
84         params.length == 1 && params[0].equals(Object JavaDoc.class)) {
85       Object JavaDoc value = args[0];
86       if (value == null || ! Proxy.isProxyClass(value.getClass()))
87         return new Boolean JavaDoc(false);
88
89       HessianHmuxProxy handler = (HessianHmuxProxy) Proxy.getInvocationHandler(value);
90
91       return new Boolean JavaDoc(_path.equals(handler._path));
92     }
93     else if (methodName.equals("hashCode") && params.length == 0)
94       return new Integer JavaDoc(_path.hashCode());
95     else if (methodName.equals("getHessianType"))
96       return proxy.getClass().getInterfaces()[0].getName();
97     else if (methodName.equals("getHessianURL"))
98       return _path.toString();
99     else if (methodName.equals("toString") && params.length == 0)
100       return "HessianHmuxProxy[" + _path + "]";
101
102     ReadStream is = null;
103     
104     try {
105       if (args != null)
106         methodName = methodName + "__" + args.length;
107       else
108         methodName = methodName + "__0";
109
110       is = sendRequest(methodName, args);
111
112       String JavaDoc code = (String JavaDoc) is.getAttribute("status");
113
114       if (! "200".equals(code)) {
115     CharBuffer sb = new CharBuffer();
116
117     while (is.readLine(sb)) {
118     }
119
120     throw new HessianProtocolException(code + ": " + sb);
121       }
122
123       AbstractHessianInput in = new Hessian2Input(is);
124
125       return in.readReply(method.getReturnType());
126     } catch (HessianProtocolException e) {
127       throw new RuntimeException JavaDoc(e);
128     } finally {
129       try {
130     if (is != null)
131       is.close();
132       } catch (Throwable JavaDoc e) {
133       }
134     }
135   }
136
137   private ReadStream sendRequest(String JavaDoc methodName, Object JavaDoc []args)
138     throws IOException JavaDoc
139   {
140     ReadWritePair pair = _path.openReadWrite();
141
142     ReadStream is = pair.getReadStream();
143     WriteStream os = pair.getWriteStream();
144
145     try {
146       AbstractHessianOutput out = new HessianOutput(os);
147
148       out.call(methodName, args);
149       out.flush();
150
151       return is;
152     } catch (IOException JavaDoc e) {
153       try {
154     os.close();
155       } catch (Exception JavaDoc e1) {
156       }
157       
158       try {
159     is.close();
160       } catch (Exception JavaDoc e1) {
161       }
162
163       throw e;
164     } catch (RuntimeException JavaDoc e) {
165       try {
166     os.close();
167       } catch (Exception JavaDoc e1) {
168       }
169       
170       try {
171     is.close();
172       } catch (Exception JavaDoc e1) {
173       }
174
175       throw e;
176     }
177   }
178 }
179
Popular Tags