01: /*
02: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
03: *
04: * This file is part of Resin(R) Open Source
05: *
06: * Each copy or derived work must preserve the copyright notice and this
07: * notice unmodified.
08: *
09: * 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.burlap;
30:
31: import com.caucho.burlap.io.BurlapInput;
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;
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 call(Path urlPath, String method, Object arg)
52: throws Throwable {
53: return call(urlPath, method, new Object[] { arg });
54: }
55:
56: /**
57: * Calls an arbitrary method at the given url.
58: *
59: * @param url a path to the remote url
60: * @param method the method to call
61: * @param args any arguments to call
62: */
63: public static Object call(Path urlPath, String method, Object[] args)
64: throws Throwable {
65: ReadWritePair pair = urlPath.openReadWrite();
66:
67: ReadStream is = pair.getReadStream();
68: WriteStream os = pair.getWriteStream();
69:
70: BurlapInput in = new BurlapInput(is);
71: BurlapWriter out = new BurlapWriter(os);
72:
73: try {
74: out.call(method, args);
75:
76: String status = (String) is.getAttribute("status");
77:
78: if (!"200".equals(status)) {
79: CharBuffer msg = new CharBuffer();
80:
81: int ch;
82: while ((ch = is.readChar()) >= 0)
83: msg.append((char) ch);
84:
85: throw new IOException("bad status: " + status + "\n"
86: + msg);
87: }
88:
89: return in.readReply(null);
90: } finally {
91: os.close();
92: is.close();
93: }
94: }
95: }
|