public class DbaseLib implements DbaseHandler {

  public DbaseLib() {
    lock = new Object();
  }

  public byte[] get(byte[] key) throws Exception {
    this.reply = null;
    dbGroup.sendGet(this, key);

    synchronized (lock) {
      while (this.reply == null) {
	lock.wait();
      }
    }

    if (reply.length == 4) {
      return (null);
    }

    byte[] b = new byte[reply.length - 4];
    System.arraycopy(reply, 4, b, 0, reply.length - 4);
    return (b);
  }

  public void put(byte[] key, byte[] value) throws Exception {
    this.done = false;
    dbGroup.sendPut(this, key, value);

    synchronized (lock) {
      while (done == false) {
	lock.wait ();
      }
    }
  }

  public void remove (byte[] key) throws Exception {
    this.done = false;
    dbGroup.sendRemove(this, key);

    synchronized (lock) {
      while (done == false) {
	lock.wait ();
      }
    }
  }

  // -- DbaseHandler interface

  public void getDone(byte[] reply, int offset, int length) {
    synchronized (lock) {
      this.reply = reply;
      lock.notify();
    }
  }

  public void putDone() {
    synchronized (lock) {
      this.done = true;
      lock.notify();
    }
  }

  public void removeDone() {
    synchronized (lock) {
      this.done = true;
      lock.notify();
    }
  }

  public int getId() {
    return (this.id);
  }

  // -- Private members

  private int id = System.identityHashCode(this);
  private byte[] reply;
  private boolean done;
  private Object lock;

  private static DbaseGroup dbGroup;

  static {
    try {
      dbGroup = new DbaseGroup();
    } catch (Exception e) {
      throw new ExceptionInInitializerError(e);
    }
  }
}
