1 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 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 _user) throws 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 obj ) throws IOException { 56 try { 57 copyStream.reset(); 58 59 currentCommand = (DbCommand)copyObject( obj, false ); 61 currentCommand.env = env; 62 currentCommand.setProxyObjectGate(getProxyObjectGate()); 63 env.transactionManager.handleCommand(currentCommand,user); 64 } catch (Exception e) { 65 throw new IOException( e.toString() ); 66 } 67 } 68 69 70 public synchronized Object receive() throws IOException, ClassNotFoundException { 71 if (currentCommand == null) { 72 throw new IllegalStateException ( "Attempt to receive() without prior send()." ); 73 } 74 Object result = copyObject( currentCommand.result, true ); 75 currentCommand = null; 76 return result; 77 } 78 79 80 84 protected synchronized Object copyObject( Object 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 if (updateLinks) { 97 OzoneProxy.linkTable.addForKey( db, in ); 98 } 99 100 Object result = in.readObject(); 101 102 return result; 103 } catch (Exception 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 ( "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 ( "Method not implemented." ); 125 } 126 127 128 public void onDeconnect() throws IOException { 129 throw new RuntimeException ( "Method not implemented." ); 130 } 131 132 133 public ObjectInputStream inputStream() { 134 throw new RuntimeException ( "Method not implemented." ); 135 } 136 137 138 public ObjectOutputStream outputStream() { 139 throw new RuntimeException ( "Method not implemented." ); 140 } 141 142 145 public ProxyObjectGate getProxyObjectGate() { 146 return proxyObjectGate; 147 } 148 } 149 | Popular Tags |