001: // You can redistribute this software and/or modify it under the terms of
002: // the Ozone Core License version 1 published by ozone-db.org.
003: //
004: // The original code and portions created by SMB are
005: // Copyright (C) 1997-@year@ by SMB GmbH. All rights reserved.
006: //
007: // $Id: DbLocalClient.java,v 1.2 2002/06/08 00:49:38 mediumnet Exp $
008:
009: package org.ozoneDB.core.DbRemote;
010:
011: import java.io.*;
012: import org.ozoneDB.DxLib.*;
013: import org.ozoneDB.DxLib.net.DxClient;
014: import org.ozoneDB.*;
015: import org.ozoneDB.core.*;
016: import org.ozoneDB.util.*;
017:
018: /**
019: * Note: the entire connection is synchronized when ExternalDatabase is sending
020: * a command through; so synchronization of send/receive is not strictly needed
021: *
022: * @author <a href="http://www.softwarebuero.de/">SMB</a>
023: * @version $Revision: 1.2 $Date: 2002/06/08 00:49:38 $
024: */
025: public class DbLocalClient extends DbClient {
026:
027: protected Env env;
028:
029: protected User user;
030:
031: protected DbCommand currentCommand = null;
032:
033: protected ByteArrayOutputStream copyStream;
034:
035: protected ProxyObjectGate proxyObjectGate;
036:
037: public DbLocalClient(ExternalDatabase _db, Env _env, String _user)
038: throws Exception {
039: super (_db, _user);
040: env = _env;
041:
042: user = env.userManager.userForName(_user);
043: if (user == null) {
044: throw new PermissionDeniedExc("No such user.");
045: }
046:
047: copyStream = new ByteArrayOutputStream(4 * 1024);
048:
049: proxyObjectGate = new ProxyObjectGate();
050:
051: env.getLocalClientTracker().addClient(this );
052: }
053:
054: public synchronized void send(Object obj) throws IOException {
055: try {
056: copyStream.reset();
057:
058: // FIXME: copy only args of DbInvoke
059: currentCommand = (DbCommand) copyObject(obj, false);
060: currentCommand.env = env;
061: currentCommand.setProxyObjectGate(getProxyObjectGate());
062: env.transactionManager
063: .handleCommand(currentCommand, user/*this*/);
064: } catch (Exception e) {
065: throw new IOException(e.toString());
066: }
067: }
068:
069: public synchronized Object receive() throws IOException,
070: ClassNotFoundException {
071: if (currentCommand == null) {
072: throw new IllegalStateException(
073: "Attempt to receive() without prior send().");
074: }
075: Object result = copyObject(currentCommand.result, true);
076: currentCommand = null;
077: return result;
078: }
079:
080: /**
081: * Copy the given object using ByteArrayStreams so all streamable
082: * objects (all database objects) can be copied.
083: */
084: protected synchronized Object copyObject(Object obj,
085: boolean updateLinks) throws IOException {
086: ObjectInputStream in = null;
087: try {
088: copyStream.reset();
089:
090: ObjectOutputStream out = new ObjectOutputStream(copyStream);
091: out.writeObject(obj);
092: out.close();
093:
094: in = new ObjectInputStream(new ByteArrayInputStream(
095: copyStream.toByteArray()));
096:
097: // remove in finally clause
098: if (updateLinks) {
099: OzoneProxy.linkTable.addForKey(db, in);
100: }
101:
102: Object result = in.readObject();
103:
104: return result;
105: } catch (Exception e) {
106: throw new IOException(e.toString());
107: } finally {
108: if (updateLinks && in != null) {
109: OzoneProxy.linkTable.removeForKey(in);
110: }
111: }
112:
113: }
114:
115: public boolean objectAvailable() {
116: throw new RuntimeException("Method not implemented.");
117: }
118:
119: public void close() throws IOException {
120: }
121:
122: public void onConnect() throws IOException {
123: throw new RuntimeException("Method not implemented.");
124: }
125:
126: public void onDeconnect() throws IOException {
127: throw new RuntimeException("Method not implemented.");
128: }
129:
130: public ObjectInputStream inputStream() {
131: throw new RuntimeException("Method not implemented.");
132: }
133:
134: public ObjectOutputStream outputStream() {
135: throw new RuntimeException("Method not implemented.");
136: }
137:
138: /**
139: Returns the ProxyObjectGate for this Client.
140: */
141: public ProxyObjectGate getProxyObjectGate() {
142: return proxyObjectGate;
143: }
144: }
|