01: package org.mandarax.jdbc.client.local;
02:
03: /*
04: * Copyright (C) 1999-2004 <a href="mailto:mandarax@jbdietrich.com">Jens Dietrich</a>
05: *
06: * This library is free software; you can redistribute it and/or
07: * modify it under the terms of the GNU Lesser General Public
08: * License as published by the Free Software Foundation; either
09: * version 2 of the License, or (at your option) any later version.
10: *
11: * This library is distributed in the hope that it will be useful,
12: * but WITHOUT ANY WARRANTY; without even the implied warranty of
13: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14: * Lesser General Public License for more details.
15: *
16: * You should have received a copy of the GNU Lesser General Public
17: * License along with this library; if not, write to the Free Software
18: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19: */
20:
21: import java.io.*;
22: import org.mandarax.jdbc.server.*;
23: import org.mandarax.jdbc.server.local.*;
24: import org.mandarax.jdbc.rpc.*;
25:
26: /**
27: * A dummy that uses local method calls. Mainly used for testing the jdbc
28: * client-server implementation.
29: * @author <A HREF="mailto:mandarax@jbdietrich.com">Jens Dietrich</A>
30: * @version 3.3.2 <29 December 2004>
31: * @since 3.0
32: */
33: public class LocalTransport implements Transport {
34: private Serializer serializer = new XMLSerializer();
35: private ServerFacade serverFacade = new LocalServerFacade();
36:
37: /**
38: * Constructor.
39: */
40: public LocalTransport() {
41: super ();
42: }
43:
44: /**
45: * Invoke a call.
46: * @param call a call
47: * @throws CallException
48: * @throws java.io.IOException
49: */
50: public Object perform(Call call) throws CallException {
51: try {
52: // serialize, use a byte buffer for simulation - this is client code
53: ByteArrayOutputStream out = new ByteArrayOutputStream();
54: serializer.write(call, out);
55: out.close();
56:
57: // simulated transport happens here - this is server code
58: byte[] data = out.toByteArray();
59: ByteArrayInputStream in = new ByteArrayInputStream(data);
60: Call receivedCall = (Call) serializer.read(in);
61:
62: // invoke call - this is server code
63: CallResult result = serverFacade.perform(receivedCall);
64: out = new ByteArrayOutputStream();
65: serializer.write(result, out);
66: out.close();
67:
68: // deserialize - this is client code
69: data = out.toByteArray();
70: in = new ByteArrayInputStream(data);
71: CallResult receivedResult = (CallResult) serializer
72: .read(in);
73: in.close();
74:
75: if (receivedResult instanceof ExceptionResult) {
76: throw new CallException(
77: ((ExceptionResult) receivedResult).getMessage());
78: }
79: if (receivedResult instanceof ReturnValue) {
80: return ((ReturnValue) receivedResult).getValue();
81: }
82: return null;
83: } catch (IOException x) {
84: throw new CallException(x.getMessage());
85: }
86: }
87:
88: }
|