KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > ejb > hessian > MetaStub


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  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.ejb.hessian;
30
31 import com.caucho.hessian.io.HessianInput;
32 import com.caucho.util.CharBuffer;
33 import com.caucho.vfs.Path;
34 import com.caucho.vfs.ReadStream;
35 import com.caucho.vfs.ReadWritePair;
36 import com.caucho.vfs.WriteStream;
37
38 import java.io.IOException JavaDoc;
39
40 /**
41  * Utility class to call methods easily.
42  */

43 public class MetaStub {
44   /**
45    * Calls an arbitrary method at the given url.
46    *
47    * @param url a path to the remote url
48    * @param method the method to call
49    * @param arg an argument
50    */

51   public static Object JavaDoc call(Path urlPath, String JavaDoc method, Object JavaDoc arg)
52     throws Throwable JavaDoc
53   {
54     return call(urlPath, method, new Object JavaDoc[] { arg });
55   }
56   
57   /**
58    * Calls an arbitrary method at the given url.
59    *
60    * @param url a path to the remote url
61    * @param method the method to call
62    * @param args any arguments to call
63    */

64   public static Object JavaDoc call(Path urlPath, String JavaDoc method, Object JavaDoc []args)
65     throws Throwable JavaDoc
66   {
67     ReadWritePair pair = urlPath.openReadWrite();
68
69     ReadStream is = pair.getReadStream();
70     WriteStream os = pair.getWriteStream();
71     
72     HessianInput in = new HessianInput(is);
73     HessianWriter out = new HessianWriter(os);
74
75     try {
76       out.call(method, args);
77
78       String JavaDoc status = (String JavaDoc) is.getAttribute("status");
79
80       if (! "200".equals(status)) {
81         CharBuffer msg = new CharBuffer();
82
83         int ch;
84         while ((ch = is.readChar()) >= 0)
85           msg.append((char) ch);
86
87         throw new IOException JavaDoc("bad status: " + status + "\n" + msg);
88       }
89
90       return in.readReply(null);
91     } finally {
92       os.close();
93       is.close();
94     }
95   }
96 }
97
Popular Tags