示例#1
0
  @Test
  public void testConstructor() throws Exception {
    ITransactionManager txMgr = EasyMock.createStrictMock(ITransactionManager.class);

    // constructor with txManager
    EasyMock.reset(txMgr);
    EasyMock.expect(txMgr.executeWithConnection(null)).andReturn(null);
    EasyMock.replay(txMgr);
    TestAdminBase testBase = new TestAdminBase(txMgr);
    testBase.test();
    EasyMock.verify(txMgr);
    assertEquals("TX mgr not stored in admin base.", txMgr, testBase.getTransactionManager());
  }
示例#2
0
 /**
  * Log all policies that are using the set of removed topology objects
  *
  * @param operation Which operation are we performing wrt the topology object
  * @param topologyType The type of topology object to log against.
  * @param deletedNodes Collection of the id's of the Topology Nodes that were deleted.
  * @param constraintName The name of the constraint within a user set for the topology type
  *     specified in the topologyType property
  */
 protected void logPoliciesUsingRemovedTopologyObjs(
     final String operation,
     final String topologyType,
     final Collection<Integer> deletedNodes,
     final String constraintName) {
   ManagementContainer mc = ManagementContainer.getInstance();
   ITransactionManager pool;
   pool = mc.getPool(ManagementContainer.POOL_NAME);
   pool.executeWithConnection(
       new ConnectionExecuteFunction<Object>() {
         public Object execute(Connection c) throws SQLException {
           logPoliciesUsingRemovedTopologyObjs(
               operation, topologyType, deletedNodes, constraintName, c);
           return null;
         }
       });
 }
示例#3
0
 /** Purge all topology objects that are marked as deleted and the purge time has passed */
 public static void purgeDeletedToplogyObjects() {
   ManagementContainer mc = ManagementContainer.getInstance();
   ITransactionManager pool;
   pool = mc.getPool(ManagementContainer.POOL_NAME);
   Set<Integer> custIds =
       pool.executeWithConnection(
           new ConnectionExecuteFunction<Set<Integer>>() {
             public Set<Integer> execute(Connection c) throws SQLException {
               return purgeDeletedToplogyObjects(c);
             }
           });
   // must run the following refresh queries after the transaction above is committed
   if (!CollectionsUtils.isNullOrEmpty(custIds)) {
     for (Integer custID : custIds) {
       ManagementContainer.getInstance()
           .getPolicyMetaManager()
           .notifyOfExternalPolicyChanges(custID);
     }
   }
 }
    @SuppressWarnings("unchecked")
    public static <T> void execute(ITransactionManager tm, final Connection c) throws SQLException {

      tm.executeWithConnectionWithThrow(EasyMock.anyObject(ConnectionExecuteFunction.class));

      EasyMock.expectLastCall()
          .andAnswer(
              new IAnswer<T>() {
                public T answer() throws Throwable {
                  Object[] currentArguments = EasyMock.getCurrentArguments();
                  Object o = currentArguments[0];
                  return ((ConnectionExecuteFunction<T>) o).execute(c);
                }
              });
    }
示例#5
0
  public void purgeLogEvents(final List<SearchConstraint> constraints, final int daysToKeep) {
    blockHereIfBadNfs();

    StringBuffer sql =
        new StringBuffer(
            "select u.mail_directory || '/logs/' || e.event_id || '.log' "
                + "from dat_client_log_events e, dat_user_account u "
                + "where event_time < current_timestamp - (? || ' days')::interval "
                + "and e.has_log_file != 0 "
                + "and e.user_id = u.object_id");
    appendWhereConstraints(sql, constraints, s_propToColumnMap);
    m_logCategory.info("Purge event logs query is\n" + sql);

    StringBuffer sql2 =
        new StringBuffer(
            "delete from dat_client_log_events "
                + "where event_time < current_timestamp - (? || ' days')::interval");
    if (!constraints.isEmpty()) {
      sql2.append(" and event_id in (select e.event_id from dat_client_log_events e ");
      appendWhere(sql2, constraints, s_propToColumnMap);
      sql2.append(")");
    }
    m_logCategory.info("Purge event logs query is\n" + sql2);

    try {
      Connection c = m_txManager.getConnection();
      PreparedStatement stmt = null;
      ResultSet rs = null;
      boolean needsRollback = true;
      try {
        stmt = c.prepareStatement(sql.toString());
        stmt.setInt(1, daysToKeep);
        rs = stmt.executeQuery();
        while (rs.next()) {
          File logFile = new File(getNfsRoot(), rs.getString(1));
          // nfs usage, but DB transaction takes no locks.
          if (logFile.exists()) {
            boolean ok = logFile.delete();
            if (!ok) {
              m_logger
                  .log("Unable to delete")
                  .param(LoggingConsts.FILENAME, logFile.getAbsolutePath())
                  .warn();
            }
          } // file exists.
        } // each row

        // Below, no nfs usage occurs.  We can use the same connection.
        // the 'sql delete' may use DB locks.
        stmt = c.prepareStatement(sql2.toString());
        stmt.setInt(1, daysToKeep);
        stmt.executeUpdate();
        c.commit();
        needsRollback = false;
      } finally {
        DbUtils.safeClose(rs);
        DbUtils.safeClose(stmt);
        if (needsRollback) {
          c.rollback();
        }
        m_txManager.returnConnection(c);
      }
    } catch (Exception ex) {
      m_logger.log("Error in purgeLogEvents").warn(ex);
    }
  }