Esempio n. 1
0
  /**
   * Calling clearLogs posits that these EventLogs were successfully saved, and so this method may
   * raise an event signalling such. This could eventually be reworked to be fully within the
   * security system.
   */
  void saveLogs(boolean readOnly, Session session) {

    // Grabbing a copy to prevent ConcurrentModificationEx
    final List<EventLog> logs = new ArrayList<EventLog>(secSys.getLogs());
    secSys.clearLogs();

    if (logs == null || logs.size() == 0) {
      return; // EARLY EXIT
    }

    if (readOnly) {
      // If we reach here, we have logs when we shouldn't.
      StringBuilder sb = new StringBuilder();
      sb.append("EventLogs in readOnly transaction:\n");
      for (EventLog eventLog : logs) {
        sb.append(eventLog.getAction());
        sb.append(" ");
        sb.append(eventLog);
        sb.append(eventLog.getEntityType());
        sb.append(" ");
        sb.append(eventLog.getEntityId());
        sb.append("\b");
      }
      throw new InternalException(sb.toString());
    }

    try {
      long lastValue = sql.nextValue("seq_eventlog", logs.size());
      long id = lastValue - logs.size() + 1;
      List<Object[]> batchData = new ArrayList<Object[]>();
      for (EventLog l : logs) {
        Event e = l.getEvent();
        if (e.getId() == null) {
          throw new RuntimeException("Transient event");
        }
        batchData.add(
            new Object[] {
              id++, -35L, l.getEntityId(), l.getEntityType(), l.getAction(), l.getEvent().getId()
            });
      }

      sql.insertLogs(batchData);

    } catch (Exception ex) {
      log.error("Error saving event logs: " + logs, ex);
    }

    if (secSys.getLogs().size() > 0) {
      throw new InternalException("More logs present after saveLogs()");
    }
  }
Esempio n. 2
0
  public void addLog(String action, Class klass, Long id) {

    Assert.notNull(action);
    Assert.notNull(klass);
    Assert.notNull(id);

    if (Event.class.isAssignableFrom(klass) || EventLog.class.isAssignableFrom(klass)) {
      if (log.isDebugEnabled()) {
        log.debug("Not logging creation of logging type:" + klass);
      }
      return; // EARLY EXIT
    } else {
      if (!isReady()) {
        throw new InternalException("Not ready to add EventLog");
      }
    }

    if (log.isInfoEnabled()) {
      log.info("Adding log:" + action + "," + klass + "," + id);
    }

    BasicEventContext c = current();
    List<EventLog> list = current().getLogs();
    if (list == null) {
      list = new ArrayList<EventLog>();
      c.setLogs(list);
    }

    EventLog l = new EventLog();
    l.setAction(action);
    l.setEntityType(klass.getName()); // TODO could be id to Type entity
    l.setEntityId(id);
    l.setEvent(c.getEvent());
    Details d = Details.create();
    d.setPermissions(new Permissions());
    l.getDetails().copy(d);
    list.add(l);
  }