/**
  * Get a list of repository connections that share the same connector.
  *
  * @param className is the class name of the connector.
  * @return the repository connections that use that connector.
  */
 @Override
 public String[] findConnectionsForConnector(String className) throws ManifoldCFException {
   StringSetBuffer ssb = new StringSetBuffer();
   ssb.add(getRepositoryConnectionsKey());
   StringSet localCacheKeys = new StringSet(ssb);
   ArrayList params = new ArrayList();
   String query =
       buildConjunctionClause(
           params, new ClauseDescription[] {new UnitaryClause(classNameField, className)});
   IResultSet set =
       performQuery(
           "SELECT " + nameField + " FROM " + getTableName() + " WHERE " + query,
           params,
           localCacheKeys,
           null);
   String[] rval = new String[set.getRowCount()];
   int i = 0;
   while (i < rval.length) {
     IResultRow row = set.getRow(i);
     rval[i] = (String) row.getValue(nameField);
     i++;
   }
   java.util.Arrays.sort(rval);
   return rval;
 }
 /**
  * Check if underlying connector exists.
  *
  * @param name is the name of the connection to check.
  * @return true if the underlying connector is registered.
  */
 @Override
 public boolean checkConnectorExists(String name) throws ManifoldCFException {
   beginTransaction();
   try {
     StringSetBuffer ssb = new StringSetBuffer();
     ssb.add(getRepositoryConnectionKey(name));
     StringSet localCacheKeys = new StringSet(ssb);
     ArrayList params = new ArrayList();
     String query =
         buildConjunctionClause(
             params, new ClauseDescription[] {new UnitaryClause(nameField, name)});
     IResultSet set =
         performQuery(
             "SELECT " + classNameField + " FROM " + getTableName() + " WHERE " + query,
             params,
             localCacheKeys,
             null);
     if (set.getRowCount() == 0)
       throw new ManifoldCFException("No such connection: '" + name + "'");
     IResultRow row = set.getRow(0);
     String className = (String) row.getValue(classNameField);
     IConnectorManager cm = ConnectorManagerFactory.make(threadContext);
     return cm.isInstalled(className);
   } catch (ManifoldCFException e) {
     signalRollback();
     throw e;
   } catch (Error e) {
     signalRollback();
     throw e;
   } finally {
     endTransaction();
   }
 }
 /**
  * Return true if the specified authority name is referenced.
  *
  * @param groupName is the group name.
  * @return true if referenced, false otherwise.
  */
 @Override
 public boolean isGroupReferenced(String groupName) throws ManifoldCFException {
   StringSetBuffer ssb = new StringSetBuffer();
   ssb.add(getRepositoryConnectionsKey());
   StringSet localCacheKeys = new StringSet(ssb);
   ArrayList params = new ArrayList();
   String query =
       buildConjunctionClause(
           params, new ClauseDescription[] {new UnitaryClause(groupNameField, groupName)});
   IResultSet set =
       performQuery(
           "SELECT " + nameField + " FROM " + getTableName() + " WHERE " + query,
           params,
           localCacheKeys,
           null);
   return set.getRowCount() > 0;
 }
  /**
   * Load multiple repository connections by name.
   *
   * @param names are the names to load.
   * @return the loaded connection objects.
   */
  public IRepositoryConnection[] loadMultiple(String[] names) throws ManifoldCFException {
    // Build description objects
    RepositoryConnectionDescription[] objectDescriptions =
        new RepositoryConnectionDescription[names.length];
    int i = 0;
    StringSetBuffer ssb = new StringSetBuffer();
    while (i < names.length) {
      ssb.clear();
      ssb.add(getRepositoryConnectionKey(names[i]));
      objectDescriptions[i] = new RepositoryConnectionDescription(names[i], new StringSet(ssb));
      i++;
    }

    RepositoryConnectionExecutor exec = new RepositoryConnectionExecutor(this, objectDescriptions);
    cacheManager.findObjectsAndExecute(objectDescriptions, null, exec, getTransactionID());
    return exec.getResults();
  }
  /**
   * Delete a repository connection.
   *
   * @param name is the name of the connection to delete. If the name does not exist, no error is
   *     returned.
   */
  public void delete(String name) throws ManifoldCFException {
    // Grab a job manager handle.  We will need to check if any jobs refer to this connection.
    IJobManager jobManager = JobManagerFactory.make(threadContext);

    StringSetBuffer ssb = new StringSetBuffer();
    ssb.add(getRepositoryConnectionsKey());
    ssb.add(getRepositoryConnectionKey(name));
    StringSet cacheKeys = new StringSet(ssb);
    ICacheHandle ch = cacheManager.enterCache(null, cacheKeys, getTransactionID());
    try {
      beginTransaction();
      try {
        // Check if any jobs refer to this connection name
        if (jobManager.checkIfReference(name))
          throw new ManifoldCFException(
              "Can't delete repository connection '" + name + "': existing jobs refer to it");
        ManifoldCF.noteConfigurationChange();
        throttleSpecManager.deleteRows(name);
        historyManager.deleteOwner(name);
        ArrayList params = new ArrayList();
        String query =
            buildConjunctionClause(
                params, new ClauseDescription[] {new UnitaryClause(nameField, name)});
        performDelete("WHERE " + query, params, null);
        cacheManager.invalidateKeys(ch);
      } catch (ManifoldCFException e) {
        signalRollback();
        throw e;
      } catch (Error e) {
        signalRollback();
        throw e;
      } finally {
        endTransaction();
      }
    } finally {
      cacheManager.leaveCache(ch);
    }
  }
 /**
  * Obtain a list of the repository connections, ordered by name.
  *
  * @return an array of connection objects.
  */
 public IRepositoryConnection[] getAllConnections() throws ManifoldCFException {
   beginTransaction();
   try {
     // Read all the tools
     StringSetBuffer ssb = new StringSetBuffer();
     ssb.add(getRepositoryConnectionsKey());
     StringSet localCacheKeys = new StringSet(ssb);
     IResultSet set =
         performQuery(
             "SELECT "
                 + nameField
                 + ",lower("
                 + nameField
                 + ") AS sortfield FROM "
                 + getTableName()
                 + " ORDER BY sortfield ASC",
             null,
             localCacheKeys,
             null);
     String[] names = new String[set.getRowCount()];
     int i = 0;
     while (i < names.length) {
       IResultRow row = set.getRow(i);
       names[i] = row.getValue(nameField).toString();
       i++;
     }
     return loadMultiple(names);
   } catch (ManifoldCFException e) {
     signalRollback();
     throw e;
   } catch (Error e) {
     signalRollback();
     throw e;
   } finally {
     endTransaction();
   }
 }
  /**
   * Save a repository connection object.
   *
   * @param object is the object to save.
   * @return true if the object was created, false otherwise.
   */
  public boolean save(IRepositoryConnection object) throws ManifoldCFException {
    StringSetBuffer ssb = new StringSetBuffer();
    ssb.add(getRepositoryConnectionsKey());
    ssb.add(getRepositoryConnectionKey(object.getName()));
    StringSet cacheKeys = new StringSet(ssb);
    while (true) {
      // Catch deadlock condition
      long sleepAmt = 0L;
      try {
        ICacheHandle ch = cacheManager.enterCache(null, cacheKeys, getTransactionID());
        try {
          beginTransaction();
          try {
            // performLock();
            // Notify of a change to the configuration
            ManifoldCF.noteConfigurationChange();
            boolean isNew = object.getIsNew();
            // See whether the instance exists
            ArrayList params = new ArrayList();
            String query =
                buildConjunctionClause(
                    params,
                    new ClauseDescription[] {new UnitaryClause(nameField, object.getName())});
            IResultSet set =
                performQuery(
                    "SELECT * FROM " + getTableName() + " WHERE " + query + " FOR UPDATE",
                    params,
                    null,
                    null);
            HashMap values = new HashMap();
            values.put(descriptionField, object.getDescription());
            values.put(classNameField, object.getClassName());
            values.put(groupNameField, object.getACLAuthority());
            values.put(maxCountField, new Long((long) object.getMaxConnections()));
            String configXML = object.getConfigParams().toXML();
            values.put(configField, configXML);
            boolean notificationNeeded = false;
            boolean isCreated;

            if (set.getRowCount() > 0) {
              // If the object is supposedly new, it is bad that we found one that already exists.
              if (isNew)
                throw new ManifoldCFException(
                    "Repository connection '" + object.getName() + "' already exists");
              isCreated = false;
              IResultRow row = set.getRow(0);
              String oldXML = (String) row.getValue(configField);
              if (oldXML == null || !oldXML.equals(configXML)) notificationNeeded = true;

              // Update
              params.clear();
              query =
                  buildConjunctionClause(
                      params,
                      new ClauseDescription[] {new UnitaryClause(nameField, object.getName())});
              performUpdate(values, " WHERE " + query, params, null);
              throttleSpecManager.deleteRows(object.getName());
            } else {
              // If the object is not supposed to be new, it is bad that we did not find one.
              if (!isNew)
                throw new ManifoldCFException(
                    "Repository connection '" + object.getName() + "' no longer exists");
              isCreated = true;
              // Insert
              values.put(nameField, object.getName());
              // We only need the general key because this is new.
              performInsert(values, null);
            }

            // Write secondary table stuff
            throttleSpecManager.writeRows(object.getName(), object);

            // If notification required, do it.
            if (notificationNeeded) {
              IJobManager jobManager = JobManagerFactory.make(threadContext);
              jobManager.noteConnectionChange(object.getName());
            }

            cacheManager.invalidateKeys(ch);
            return isCreated;
          } catch (ManifoldCFException e) {
            signalRollback();
            throw e;
          } catch (Error e) {
            signalRollback();
            throw e;
          } finally {
            endTransaction();
          }
        } finally {
          cacheManager.leaveCache(ch);
        }
      } catch (ManifoldCFException e) {
        // Is this a deadlock exception?  If so, we want to try again.
        if (e.getErrorCode() != ManifoldCFException.DATABASE_TRANSACTION_ABORT) throw e;
        sleepAmt = getSleepAmt();
      } finally {
        sleepFor(sleepAmt);
      }
    }
  }