public void run(int portNumber) {
   try {
     listen(portNumber);
   } catch (IOException ex) {
     Ganymede.logError(ex);
   }
 }
  /** This DBObjectDeltaRec constructor is used to load a delta record from a Journal stream. */
  public DBObjectDeltaRec(DataInput in) throws IOException {
    short fieldcode;
    short typecode;
    boolean scalar;
    DBObjectBase baseDef;
    DBObjectBaseField fieldDef;
    String fieldName = null;
    String status = null;
    DBObject obj = null;
    /* -- */

    status = "Reading invid";

    boolean debug = true;

    try {
      invid = Invid.createInvid(in);
      baseDef = Ganymede.db.getObjectBase(invid.getType());
      obj = Ganymede.db.viewDBObject(invid);

      if (debug) {
        System.err.println(
            "\n>*> Reading delta rec for " + baseDef.getName() + " <" + invid.getNum() + ">");
      }

      status = "Reading field count";
      int fieldcount = in.readInt();

      if (debug) {
        System.err.println(
            ">> DBObjectDeltaRec(): " + fieldcount + " fields in on-disk delta rec.");
      }

      for (int i = 0; i < fieldcount; i++) {
        status = "\nReading field code for field " + i;
        if (debug) {
          System.err.println(status);
        }
        fieldcode = in.readShort();
        fieldDef = (DBObjectBaseField) baseDef.getField(fieldcode);
        typecode = fieldDef.getType();
        fieldName = fieldDef.getName();
        status = "Reading deletion boolean for field " + i;
        if (in.readBoolean()) {
          // we're deleting this field
          if (debug) {
            System.err.println(
                "Reading field deletion record field ("
                    + fieldName
                    + ":"
                    + fieldcode
                    + ") for field "
                    + i);
          }

          fieldRecs.addElement(new fieldDeltaRec(fieldcode, null));
          continue;
        }

        // okay, we've got a field redefinition.. check the type
        // code to make sure we don't have an incompatible journal
        // entry
        status = "Reading type code for field " + i;
        if (in.readShort() != typecode) {
          throw new RuntimeException("Error, field type mismatch in journal file");
        }

        // ok.. now, is it a total redefinition, or a vector delta record?
        status = "Reading scalar/vector delta boolean for field " + i;
        scalar = in.readBoolean();

        if (scalar) {
          fieldDeltaRec f_r = null;
          status = "Reading field (" + fieldName + ":" + fieldcode + ") for field " + i;
          if (debug) {
            System.err.println(status);
          }

          f_r = new fieldDeltaRec(fieldcode, DBField.readField(obj, in, fieldDef));
          fieldRecs.addElement(f_r);
          if (debug) {
            System.err.println("Value: " + f_r.toString());
          }
        } else {
          // ok, we have a vector delta chunk
          fieldDeltaRec fieldRec = new fieldDeltaRec(fieldcode);
          Object value = null;

          // read in the additions
          status = "Reading vector addition count for field " + fieldName + "(" + i + ")";
          if (debug) {
            System.err.println(status);
          }

          int size = in.readInt();

          if (debug) {
            System.err.println(">> DBObjectDeltaRec(): reading " + size + " additions.");
          }

          for (int j = 0; j < size; j++) {
            // we only support 3 vector field types
            switch (typecode) {
              case STRING:
                status = "Reading string addition " + j + " for field " + i + ":" + fieldName;
                if (debug) {
                  System.err.println(status);
                }
                value = in.readUTF();
                break;

              case INVID:
                status = "Reading invid addition " + j + " for field " + i + ":" + fieldName;
                if (debug) {
                  System.err.println(status);
                }
                value = Invid.createInvid(in);
                break;

              case IP:
                status = "Reading ip addition " + j + " for field " + i + ":" + fieldName;
                if (debug) {
                  System.err.println(status);
                }
                byte bytelength = in.readByte();
                Byte[] bytes = new Byte[bytelength];
                for (int k = 0; k < bytelength; k++) {
                  bytes[k] = Byte.valueOf(in.readByte());
                }
                value = bytes;
            }

            fieldRec.addValue(value);
          }

          // read in the deletions
          status = "Reading vector deletion count for field " + i;
          if (debug) {
            System.err.println(status);
          }

          size = in.readInt();
          if (debug) {
            System.err.println(">> DBObjectDeltaRec(): reading " + size + " deletions.");
          }

          for (int j = 0; j < size; j++) {
            // we only support 3 vector field types

            switch (typecode) {
              case STRING:
                status = "Reading string deletion " + j + " for field " + i + ":" + fieldName;
                value = in.readUTF();
                break;

              case INVID:
                status = "Reading invid deletion " + j + " for field " + i + ":" + fieldName;
                value = Invid.createInvid(in);
                break;

              case IP:
                status = "Reading IP deletion " + j + " for field " + i + ":" + fieldName;
                byte bytelength = in.readByte();
                Byte[] bytes = new Byte[bytelength];
                for (int k = 0; k < bytelength; k++) {
                  bytes[k] = Byte.valueOf(in.readByte());
                }
                value = bytes;
            }
            fieldRec.delValue(value);
          }
          // and save this field
          fieldRecs.addElement(fieldRec);
        }
      }
    } catch (IOException ex) {
      System.err.println("DBObjectDeltaRec constructor: IOException in state " + status);
      Ganymede.logError(ex);
      throw ex;
    }
  }
  /**
   * This method is used to remove a Category Node from under us.
   *
   * @see arlut.csd.ganymede.rmi.Category
   */
  public synchronized void removeNode(CategoryNode node) throws RemoteException {
    int i, index = -1;

    /* -- */

    if (node == null) {
      throw new IllegalArgumentException("Can't remove a null node");
    }

    // find our deletion point

    if (debug) {
      try {
        Ganymede.debug("DBBaseCategory (" + getName() + ").removeNode(" + node.getPath() + ")");
      } catch (RemoteException ex) {
        Ganymede.logError(ex);
        throw new RuntimeException("rmi local failure?" + ex.getMessage());
      }
    }

    for (i = 0; i < contents.size(); i++) {
      if (debug) {
        try {
          Ganymede.debug(" examining: " + ((CategoryNode) contents.elementAt(i)).getPath());
        } catch (RemoteException ex) {
          Ganymede.logError(ex);
          throw new RuntimeException("rmi local failure?" + ex.getMessage());
        }
      }

      if (contents.elementAt(i).equals(node)) {
        index = i;
      }
    }

    if (index == -1) {
      throw new IllegalArgumentException("can't delete a node that's not in the category");
    }

    // remove our node from our content list

    contents.removeElementAt(index);

    if (false) {
      if (node instanceof DBObjectBase) {
        DBObjectBase base = (DBObjectBase) node;

        if (!base.isEditing()) {
          System.err.println(
              "DBBaseCategory.removeNode(): " + base.getName() + " has a null editor!");
        } else {
          System.err.println(
              "DBBaseCategory.removeNode(): " + base.getName() + " has a non-null editor!");
        }
      }
    }

    // Sorry, kid, yer on your own now!

    node.setCategory(null);
  }
  /**
   * This method can be used to move a Category from another Category to this Category, or to move a
   * Category around within this Category.
   *
   * @param catPath the fully specified path of the node to be moved
   * @param prevNodeName the name of the node that the catPath node is to be placed after in this
   *     category, or null if the node is to be placed at the first element of this category
   * @see arlut.csd.ganymede.rmi.Category
   */
  public synchronized void moveCategoryNode(String catPath, String prevNodeName) {
    if (debug) {
      System.err.println("DBBaseCategory.moveCategoryNode(" + catPath + "," + prevNodeName + ")");
    }

    CategoryNode categoryNode;
    DBBaseCategory oldCategory;

    try {
      categoryNode = editor.getCategoryNode(catPath);
      oldCategory = (DBBaseCategory) categoryNode.getCategory();
    } catch (RemoteException ex) {
      Ganymede.logError(ex);
      throw new RuntimeException("wow, surprising remote local exception");
    }

    if (oldCategory == this) {
      if (debug) {
        System.err.println("DBBaseCategory.moveCategoryNode(): moving node within category");
      }

      contents.removeElement(categoryNode);
    } else {
      if (debug) {
        System.err.println(
            "DBBaseCategory.moveCategoryNode(): moving node from "
                + oldCategory.getPath()
                + " to "
                + getPath());
      }

      try {
        oldCategory.removeNode(categoryNode);
      } catch (RemoteException ex) {
        throw new RuntimeException("Local category threw a remote exception.. ? " + ex);
      }
    }

    try {
      categoryNode.setCategory(this);
    } catch (RemoteException ex) {
      throw new RuntimeException("Local category node threw a remote exception.. ? " + ex);
    }

    if (prevNodeName == null) {
      contents.insertElementAt(categoryNode, 0);
    } else {
      for (int i = 0; i < contents.size(); i++) {
        CategoryNode cNode = (CategoryNode) contents.elementAt(i);

        try {
          if (cNode.getName().equals(prevNodeName)) {
            contents.insertElementAt(categoryNode, i + 1);
            return;
          }
        } catch (RemoteException ex) {
        }
      }

      throw new RuntimeException(
          "Couldn't move category node " + catPath + " after non-existent " + prevNodeName);
    }
  }
  public void run() {
    try {
      OutputStream rawOutput = socket.getOutputStream();
      InputStream rawInput = socket.getInputStream();
      PrintWriter out = new PrintWriter(rawOutput, true);
      BufferedReader in = new BufferedReader(new InputStreamReader(rawInput));

      String inputLine, outputLine;

      /* Check to make sure that this telnet session is from localhost */
      InetAddress clientAddress = socket.getInetAddress();
      if (!clientAddress.equals(java.net.InetAddress.getByName(Ganymede.serverHostProperty))
          && !clientAddress.getHostAddress().equals("127.0.0.1")) {
        // Permission denied
        out.println(ts.l("run.denied_not_localhost"));
        out.flush();
        socket.close();
        return;
      }

      /* Check to see if logins are allowed */

      String error = Ganymede.server.lSemaphore.increment();

      if (error != null) {
        if (error.equals("shutdown")) {
          // "ERROR: The server is currently waiting to shut down.  No logins will be accepted until
          // the server has restarted"
          out.print(ts.l("run.nologins_shutdown"));
        } else {
          // "ERROR: Can't log in to the Ganymede server.. semaphore disabled: {0}"
          out.print(ts.l("run.nologins_semaphore", error));
        }

        out.print("\n");
        out.flush();
        socket.close();

        return;
      }

      try {
        // "Username"
        out.print(ts.l("run.username"));
        out.print(": ");
        out.flush();
        String loginName = in.readLine();

        /* Telnet terminal codes */

        /* IAC WILL ECHO */
        byte[] echoOff = {(byte) 255, (byte) 251, (byte) 1};
        /* IAC DO ECHO */
        byte[] echoOffResponse = {(byte) 255, (byte) 253, (byte) 1};
        /* IAC WONT ECHO */
        byte[] echoOn = {(byte) 255, (byte) 252, (byte) 1};
        /* IAC DONT ECHO */
        byte[] echoOnResponse = {(byte) 255, (byte) 254, (byte) 1};
        /* Holds the client response to each terminal code */
        byte[] responseBuffer = new byte[3];

        // "Password"
        out.print(ts.l("run.password"));
        out.print(": ");
        out.flush();

        /* Disable client-side character echo while the user types in
         * the password  */
        rawOutput.write(echoOff);
        rawOutput.flush();

        int chars_read = 0;

        while (chars_read < 3) {
          chars_read += rawInput.read(responseBuffer, chars_read, 3 - chars_read);
        }

        if (!Arrays.equals(responseBuffer, echoOffResponse)) {
          out.print("Your telnet client won't properly suppress character echo.");
          out.flush();
          socket.close();
          return;
        }

        String password = in.readLine();

        /* Now re-enable client-side character echo so we can conduct
         * business as usual */
        rawOutput.write(echoOn);
        rawOutput.flush();

        chars_read = 0;

        while (chars_read < 3) {
          chars_read += rawInput.read(responseBuffer, chars_read, 3 - chars_read);
        }

        if (!Arrays.equals(responseBuffer, echoOnResponse)) {
          out.print("Your telnet client won't properly resume character echo.");
          out.flush();
          socket.close();
          return;
        }

        /* Authenticate the user */
        int validationResult = Ganymede.server.validateAdminUser(loginName, password);

        /* A result of 3 means that this user has interpreter access
         * privileges. Anything else means that we give 'em the boot. */
        if (validationResult != 3) {
          try {
            Thread.currentThread().sleep(3000);
          } catch (InterruptedException ex) {
            /* Move along */
          }

          // "Permission denied."
          out.print(ts.l("run.denied"));
          out.print("\n");
          out.flush();
          socket.close();
          return;
        }

        /* Send the HELO */
        outputLine = protocol.processInput(null);
        out.print(outputLine);
        out.flush();

        /* Setup the interpreter session variable */
        protocol.createSession(loginName);

        /* Here is the read-eval-print loop */
        while ((inputLine = in.readLine()) != null) {
          outputLine = protocol.processInput(inputLine);
          out.print(outputLine);
          out.flush();
          if (outputLine.equals(JythonServerProtocol.doneString)) break;
        }
        out.close();
        in.close();
        socket.close();
      } finally {
        Ganymede.server.lSemaphore.decrement();

        /* Make sure to register the logout */

        try {
          protocol.session.logout();
        } catch (Exception e) {
          Ganymede.logError(e); // Move along
        }
      }
    } catch (IOException e) {
      Ganymede.logError(e);
    }
  }