/**
   * Load the shared objects up front, replace them in the xforms/jobs loaded from XML. We do this
   * for performance reasons.
   *
   * @throws KettleException
   */
  protected void loadSharedObjects() throws KettleException {
    sharedObjects = new SharedObjects();

    for (ObjectId id : rep.getDatabaseIDs(false)) {
      DatabaseMeta databaseMeta = rep.loadDatabaseMeta(id, null);
      validateImportedElement(importRules, databaseMeta);
      sharedObjects.storeObject(databaseMeta);
    }

    ObjectId[] slaveIDs = rep.getSlaveIDs(false);
    List<SlaveServer> slaveServers = new ArrayList<SlaveServer>(slaveIDs.length);
    for (ObjectId id : slaveIDs) {
      SlaveServer slaveServer = rep.loadSlaveServer(id, null);
      validateImportedElement(importRules, slaveServer);
      sharedObjects.storeObject(slaveServer);
      slaveServers.add(slaveServer);
    }

    for (ObjectId id : rep.getClusterIDs(false)) {
      ClusterSchema clusterSchema = rep.loadClusterSchema(id, slaveServers, null);
      validateImportedElement(importRules, clusterSchema);
      sharedObjects.storeObject(clusterSchema);
    }
    for (ObjectId id : rep.getPartitionSchemaIDs(false)) {
      PartitionSchema partitionSchema = rep.loadPartitionSchema(id, null);
      validateImportedElement(importRules, partitionSchema);
      sharedObjects.storeObject(partitionSchema);
    }
  }
  public static List<DatabaseMeta> loadSharedDatabases() {
    List<DatabaseMeta> sharedDatabases = new ArrayList<DatabaseMeta>();
    try {
      SharedObjects sharedObjects = new SharedObjects();
      for (SharedObjectInterface sharedObject : sharedObjects.getObjectsMap().values()) {
        if (sharedObject instanceof DatabaseMeta) {
          sharedDatabases.add((DatabaseMeta) sharedObject);
        }
      }
    } catch (Exception e) {
      LogChannel.GENERAL.logError("Unable to load shared objects", e);
    }

    return sharedDatabases;
  }
 @SuppressWarnings("unchecked")
 protected <T extends SharedObjectInterface> List<T> getSharedObjects(Class<T> clazz) {
   List<T> result = new ArrayList<T>();
   for (SharedObjectInterface sharedObject : sharedObjects.getObjectsMap().values()) {
     if (clazz.isInstance(sharedObject)) {
       result.add((T) sharedObject);
     }
   }
   return result;
 }
 /**
  * Add a database to the list of shared databases in ~/.kettle/shared.xml
  *
  * @param databaseMeta
  * @throws KettleException in case there is an error
  * @throws KettleObjectExistsException if a database with the same name already exists
  */
 public static void addSharedDatabase(DatabaseMeta databaseMeta)
     throws KettleObjectExistsException, KettleException {
   // First verify existence...
   //
   List<DatabaseMeta> sharedDatabases = loadSharedDatabases();
   DatabaseMeta found = DatabaseMeta.findDatabase(sharedDatabases, databaseMeta.getName());
   if (found != null) {
     throw new KettleObjectExistsException(
         "A database with name '"
             + databaseMeta.getName()
             + "' already exists in the shared databases list.");
   }
   try {
     SharedObjects sharedObjects = new SharedObjects();
     sharedObjects.storeObject(databaseMeta);
     sharedObjects.saveToFile();
   } catch (Exception e) {
     throw new KettleException(
         "It was not possible to add database '"
             + databaseMeta.getName()
             + "' to the shared.xml file");
   }
 }