Ejemplo n.º 1
0
  public void shutdown() {
    synchronized (lock) {
      shutdownRequested = true;
    }

    /* Now close the ServerSocket */
    try {
      new Socket(sock.getInetAddress(), sock.getLocalPort()).close();
    } catch (IOException ex) {
      Ganymede.stackTrace(ex);
    }
  }
Ejemplo n.º 2
0
  public void createSession(String personaName) {
    try {
      /* Snag the appropriate Admin Persona from the database */
      DBObject persona =
          (DBObject) ((DBObjectBase) Ganymede.db.get("Admin Persona")).get(personaName);

      /* If there is a user associated with this persona, snag it */
      DBObject user = null;
      InvidDBField userField = (InvidDBField) persona.get("User");
      if (userField != null) {
        user = (DBObject) userField.getVal();
      }

      /* Now we have all we need to create the session */
      session = new GanymedeSession(personaName, user, persona, false);
    } catch (RemoteException ex) {
      Ganymede.stackTrace(ex);
      return;
    }

    interp.set("session", session);
  }
Ejemplo n.º 3
0
  /**
   * This method is used to scan the persistent log storage for log events that match invid and that
   * have occurred since sinceTime.
   *
   * @param invid If not null, retrieveHistory() will only return events involving this object
   *     invid.
   * @param sinceTime if not null, retrieveHistory() will only return events occuring on or after
   *     the time specified in this Date object.
   * @param beforeTime if not null, retrieveHistory() will only return events occurring on or before
   *     the time specified in this Date object.
   * @param keyOnAdmin if true, rather than returning a string containing events that involved
   *     <invid>, retrieveHistory() will return a string containing events performed on behalf
   *     of the administrator with invid <invid>.
   * @param fullTransactions if true, the buffer returned will include all events in any
   *     transactions that involve the given invid. if false, only those events in a transaction
   *     directly affecting the given invid will be returned.
   * @param getLoginEvents if true, this method will return only login and logout events. if false,
   *     this method will return no login and logout events.
   * @return A human-readable multiline string containing a list of history events
   */
  public StringBuffer retrieveHistory(
      Invid invid,
      Date sinceTime,
      Date beforeTime,
      boolean keyOnAdmin,
      boolean fullTransactions,
      boolean getLoginEvents) {
    if (con == null) {
      throw new IllegalArgumentException("no connection to postgres database");
    }

    StringBuffer buffer = new StringBuffer();

    Date transDate = null;
    String prevTransID = null;
    String transAdmin = null;

    ResultSet rs = null;

    /* -- */

    try {
      if (keyOnAdmin) {
        rs = queryEventsByAdmin(invid, sinceTime, beforeTime);
      } else if (fullTransactions) {
        rs = queryEventsByTransactions(invid, sinceTime, beforeTime);
      } else {
        rs = queryEvents(invid, sinceTime, beforeTime);
      }

      while (rs.next()) {
        long time = rs.getLong(1);
        String token = rs.getString(2);
        String adminName = rs.getString(3);
        String text = rs.getString(4);
        String transactionID = rs.getString(5);

        if (invid.getType() == SchemaConstants.UserBase) {
          if (token.equals("normallogin")
              || token.equals("normallogout")
              || token.equals("abnormallogout")) {
            if (!getLoginEvents) {
              continue; // we don't want to show login/logout activity here
            }
          } else if (getLoginEvents) {
            continue; // we don't want to show non-login/logout activity here
          }
        }

        if (prevTransID != null && !prevTransID.equals(transactionID)) {
          buffer.append(
              "---------- End Transaction "
                  + transDate.toString()
                  + ": "
                  + transAdmin
                  + " ----------\n\n");

          transAdmin = null;
          transDate = null;
          prevTransID = null;
        }

        if (token.equals("starttransaction")) {
          transDate = new Date(time);

          String tmp2 =
              "---------- Transaction " + transDate.toString() + ": " + adminName + " ----------\n";

          // remember our admin name and transaction id so we
          // can put out a notice about closing the previous
          // transaction when we see it

          transAdmin = adminName;
          prevTransID = transactionID;

          buffer.append(tmp2);
        } else if (token.equals("finishtransaction")) {
          String tmp2 =
              "---------- End Transaction "
                  + new Date(time).toString()
                  + ": "
                  + adminName
                  + " ----------\n\n";

          prevTransID = null;
          transAdmin = null;
          transDate = null;

          buffer.append(tmp2);
        } else {
          String tmp = token + "\n" + WordWrap.wrap(text, 78, "\t") + "\n";

          buffer.append(tmp);
        }
      }
    } catch (SQLException ex) {
      Ganymede.debug("SQLException in retrieveHistory: " + ex.getMessage());
      Ganymede.debug("** SQLState: " + ex.getSQLState());
      Ganymede.debug("** SQL Error Code: " + ex.getErrorCode());
      Ganymede.debug(Ganymede.stackTrace(ex));
      return buffer;
    } finally {
      try {
        if (rs != null) {
          rs.close();

          Statement st = rs.getStatement();

          if (st != null) {
            st.close();
          }
        }
      } catch (SQLException ex) {
      }
    }

    return buffer;
  }
Ejemplo n.º 4
0
  /**
   * This method writes the given event to the persistent storage managed by this controller.
   *
   * @param event The DBLogEvent to be recorded
   */
  public synchronized void writeEvent(DBLogEvent event) {
    if (con == null) {
      throw new IllegalArgumentException("no connection to postgres database");
    }

    try {
      if (statement == null) {
        statement =
            con.prepareStatement(
                "insert into event (event_id, javatime, sqltime, classtoken, "
                    + "admin_invid, admin_name, trans_id, text) "
                    + "values (?,?,?,?,?,?,?,?)");
      }

      if (event.eventClassToken.equals("starttransaction")) {
        transactionID = event.transactionID;
      }

      primaryKey++;
      statement.setInt(1, primaryKey);
      statement.setLong(2, event.time.getTime());
      statement.setTimestamp(3, new java.sql.Timestamp(event.time.getTime()));
      statement.setString(4, event.eventClassToken);

      if (event.admin != null) {
        statement.setString(5, event.admin.toString());
      } else {
        statement.setNull(5, java.sql.Types.VARCHAR);
      }

      if (event.adminName != null) {
        statement.setString(6, event.adminName);
      } else {
        statement.setNull(5, java.sql.Types.VARCHAR);
      }

      if (event.transactionID != null) {
        statement.setString(7, event.transactionID);
      } else {
        statement.setNull(5, java.sql.Types.VARCHAR);
      }

      if (event.description != null) {
        statement.setString(8, event.description);
      } else {
        statement.setNull(5, java.sql.Types.VARCHAR);
      }

      statement.addBatch();

      for (String address : event.getMailTargets()) {
        statement.addBatch(
            "insert into email (event_id, address) values(" + primaryKey + ", '" + address + "')");
      }

      for (Invid invid : event.getInvids()) {
        statement.addBatch(
            "insert into invids (invid, event_id) values('"
                + invid.toString()
                + "',"
                + primaryKey
                + ")");
      }

      // if we had successfully logged our entire transaction,
      // record a mapping in the transactions table from each
      // invid in the transaction (which are recorded in the
      // starttransaction line) to the transaction id
      //
      // if we get a transactionID mismatch we won't record anything
      //
      // we reset the transactionID and transInvids List
      // pointer in any event

      if (event.eventClassToken.equals("finishtransaction")) {
        if (transactionID != null && transactionID.equals(event.transactionID)) {
          for (Invid invid : event.getInvids()) {
            statement.addBatch(
                "insert into transactions (invid, trans_id) values('"
                    + invid.toString()
                    + "','"
                    + transactionID
                    + "')");
          }
        }

        transactionID = null;
      }

      statement.executeBatch();
      statement.clearParameters();
    } catch (SQLException ex) {
      Ganymede.debug("SQLException in spinLog: " + ex.getMessage());
      Ganymede.debug("** SQLState: " + ex.getSQLState());
      Ganymede.debug("** SQL Error Code: " + ex.getErrorCode());
      Ganymede.debug(Ganymede.stackTrace(ex));
      Ganymede.debug("Event was " + event.toString());
      return;
    }
  }