KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ozoneDB > core > DbRemote > DbLocalClient


1 // You can redistribute this software and/or modify it under the terms of
2
// the Ozone Core License version 1 published by ozone-db.org.
3
//
4
// The original code and portions created by SMB are
5
// Copyright (C) 1997-@year@ by SMB GmbH. All rights reserved.
6
//
7
// $Id: DbLocalClient.java,v 1.7 2003/03/12 12:30:23 per_nyfelt Exp $
8

9 package org.ozoneDB.core.DbRemote;
10
11 import org.ozoneDB.*;
12 import org.ozoneDB.io.stream.ResolvingObjectInputStream;
13 import org.ozoneDB.core.Env;
14 import org.ozoneDB.core.User;
15
16 import java.io.*;
17
18
19 /**
20  * Note: the entire connection is synchronized when ExternalDatabase is sending
21  * a command through; so synchronization of send/receive is not strictly needed
22  *
23  * @author <a HREF="http://www.softwarebuero.de/">SMB</a>
24  * @version $Revision: 1.7 $Date: 2003/03/12 12:30:23 $
25  */

26 public class DbLocalClient extends DbClient {
27
28     protected Env env;
29
30     protected User user;
31
32     protected DbCommand currentCommand = null;
33
34     protected ByteArrayOutputStream copyStream;
35
36     protected ProxyObjectGate proxyObjectGate;
37
38     public DbLocalClient(ExternalDatabase _db,Env _env,String JavaDoc _user) throws /*Exception*/ org.ozoneDB.core.UserManagerException,org.ozoneDB.PermissionDeniedException{
39         super( _db, _user );
40         env = _env;
41
42         user = env.userManager.userForName( _user );
43         if (user == null) {
44             throw new PermissionDeniedException( "No such user." );
45         }
46
47         copyStream = new ByteArrayOutputStream( 4 * 1024 );
48
49         proxyObjectGate = new ProxyObjectGate();
50
51         env.getLocalClientTracker().addClient(this);
52     }
53
54
55     public synchronized void send( Object JavaDoc obj ) throws IOException {
56         try {
57             copyStream.reset();
58
59             // FIXME: copy only args of DbInvoke
60
currentCommand = (DbCommand)copyObject( obj, false );
61             currentCommand.env = env;
62             currentCommand.setProxyObjectGate(getProxyObjectGate());
63             env.transactionManager.handleCommand(currentCommand,user/*this*/);
64         } catch (Exception JavaDoc e) {
65             throw new IOException( e.toString() );
66         }
67     }
68
69
70     public synchronized Object JavaDoc receive() throws IOException, ClassNotFoundException JavaDoc {
71         if (currentCommand == null) {
72             throw new IllegalStateException JavaDoc( "Attempt to receive() without prior send()." );
73         }
74         Object JavaDoc result = copyObject( currentCommand.result, true );
75         currentCommand = null;
76         return result;
77     }
78
79
80     /**
81      * Copy the given object using ByteArrayStreams so all streamable
82      * objects (all database objects) can be copied.
83      */

84     protected synchronized Object JavaDoc copyObject( Object JavaDoc obj, boolean updateLinks ) throws IOException {
85         ObjectInputStream in = null;
86         try {
87             copyStream.reset();
88
89             ObjectOutputStream out = new ObjectOutputStream( copyStream );
90             out.writeObject( obj );
91             out.close();
92
93             in = new ResolvingObjectInputStream( new ByteArrayInputStream( copyStream.toByteArray() ) );
94
95             // remove in finally clause
96
if (updateLinks) {
97                 OzoneProxy.linkTable.addForKey( db, in );
98             }
99
100             Object JavaDoc result = in.readObject();
101
102             return result;
103         } catch (Exception JavaDoc e) {
104             throw new IOException( e.toString() );
105         } finally {
106             if (updateLinks && in != null) {
107                 OzoneProxy.linkTable.removeForKey( in );
108             }
109         }
110
111     }
112
113
114     public boolean objectAvailable() {
115         throw new RuntimeException JavaDoc( "Method not implemented." );
116     }
117
118
119     public void close() throws IOException {
120     }
121
122
123     public void onConnect() throws IOException {
124         throw new RuntimeException JavaDoc( "Method not implemented." );
125     }
126
127
128     public void onDeconnect() throws IOException {
129         throw new RuntimeException JavaDoc( "Method not implemented." );
130     }
131
132
133     public ObjectInputStream inputStream() {
134         throw new RuntimeException JavaDoc( "Method not implemented." );
135     }
136
137
138     public ObjectOutputStream outputStream() {
139         throw new RuntimeException JavaDoc( "Method not implemented." );
140     }
141
142     /**
143         Returns the ProxyObjectGate for this Client.
144     */

145     public ProxyObjectGate getProxyObjectGate() {
146         return proxyObjectGate;
147     }
148 }
149
Popular Tags