use of com.axway.ats.environment.EnvironmentUnit in project ats-framework by Axway.
the class EnvironmentConfigurator method apply.
@Override
public void apply() throws ConfigurationException {
for (Properties dbProperties : dbPropertiesList) {
int dbConfigurationIndex = -1;
if (dbProperties.get(DB_CONFIGURATION_INDEX) != null) {
dbConfigurationIndex = (Integer) dbProperties.get(DB_CONFIGURATION_INDEX);
}
ComponentRepository componentRepository = ComponentRepository.getInstance();
ComponentEnvironment componentEnvironment;
try {
componentEnvironment = componentRepository.getComponentEnvironment(component);
} catch (NoSuchComponentException nsce) {
throw new ConfigurationException("Error changing environment configuration. CAUSE: " + nsce.getMessage());
}
int foundDbConfigurationIndex = 0;
for (EnvironmentUnit environmentUnit : componentEnvironment.getEnvironmentUnits()) {
if (environmentUnit instanceof DatabaseEnvironmentUnit) {
if (foundDbConfigurationIndex == dbConfigurationIndex) {
DatabaseEnvironmentUnit dbEnvironmentUnit = (DatabaseEnvironmentUnit) environmentUnit;
DbConnection dbConnection = dbEnvironmentUnit.getDbConnection();
// the database port is kept in a list of custom properties
Map<String, Object> customProperties = dbConnection.getCustomProperties();
// get the new configuration properties
String newDbHost = chooseNewProperty(dbProperties.getProperty(DB_HOST), dbConnection.getHost());
String newDbName = chooseNewProperty(dbProperties.getProperty(DB_NAME), dbConnection.getDb());
String newDbUserName = chooseNewProperty(dbProperties.getProperty(DB_USER_NAME), dbConnection.getUser());
String newDbUserPassword = chooseNewProperty(dbProperties.getProperty(DB_USER_PASSWORD), dbConnection.getPassword());
Object newDbPort = chooseDbPort(dbProperties.get(DB_PORT), customProperties.get(DbKeys.PORT_KEY));
// create a new connection object
customProperties.put(DbKeys.PORT_KEY, newDbPort);
DbConnection newDbConnection = DatabaseProviderFactory.createDbConnection(dbConnection.getDbType(), newDbHost, newDbName, newDbUserName, newDbUserPassword, customProperties);
// apply the changes
dbEnvironmentUnit.setDbConnection(newDbConnection);
log.info("Database configuration for index " + dbConfigurationIndex + " is changed. DbConnection: " + newDbConnection.getDescription());
return;
} else {
// still searching the exact database configuration
foundDbConfigurationIndex++;
}
}
}
throw new ConfigurationException("Database configuration with index " + dbConfigurationIndex + " is not available");
}
}
use of com.axway.ats.environment.EnvironmentUnit in project ats-framework by Axway.
the class ConfigurationParser method parseEnvironment.
/**
* Parse the environment node in the Agent descriptor
*
* @param environmentNode the environment node
* @throws AgentException on error
*/
private void parseEnvironment(Node environmentNode) throws AgentException {
NodeList environmentChildNodes = environmentNode.getChildNodes();
//read the environment name
String environmentName = parseEnvironmentName(environmentNode);
//read the backup folder
String backupFolder = parseBackupFolder(environmentNode);
//now read the individual entries in the environment
//these can be databases, files, processes, etc.
List<EnvironmentUnit> environmentUnits = new ArrayList<EnvironmentUnit>();
for (int i = 0; i < environmentChildNodes.getLength(); i++) {
Node individualEnvironmentNode = environmentChildNodes.item(i);
if (individualEnvironmentNode.getNodeName().equals(DATABASE)) {
environmentUnits.add(parseDbEnvironment(individualEnvironmentNode, backupFolder));
} else if (individualEnvironmentNode.getNodeName().equals(FILE)) {
environmentUnits.add(parseFileEnvironment(individualEnvironmentNode, backupFolder));
} else if (individualEnvironmentNode.getNodeName().equals(DIRECTORY)) {
environmentUnits.add(parseDirectoryEnvironment(individualEnvironmentNode, backupFolder));
}
}
environments.add(new ComponentEnvironment(componentName, environmentName, environmentUnits, backupFolder));
}
use of com.axway.ats.environment.EnvironmentUnit in project ats-framework by Axway.
the class ComponentEnvironment method backupOnlyIfNotAlreadyDone.
public void backupOnlyIfNotAlreadyDone() throws AgentException {
try {
File backupDir = new File(backupFolder);
String[] fileList = backupDir.list();
if (backupDir.isDirectory() && fileList != null && fileList.length > 0) {
log.info("Backup directory '" + backupDir.getAbsolutePath() + "' already exists and the backup will be skipped.");
} else if (backupDir.exists() && !backupDir.isDirectory()) {
throw new AgentException("Could not create backup directory '" + backupDir.getAbsolutePath() + "'. File with this name already exists.");
} else {
log.info("Creating backup for component " + componentName);
for (EnvironmentUnit environmentUnit : environmentUnits) {
environmentUnit.backup();
}
}
} catch (EnvironmentCleanupException ece) {
throw new AgentException("Could not backup environment for component '" + componentName + "'" + recurseCauses(ece), ece);
}
}
use of com.axway.ats.environment.EnvironmentUnit in project ats-framework by Axway.
the class ComponentEnvironment method restore.
public void restore(String folderPath) throws AgentException {
// All additional actions are implicitly added to the Additional Actions Queue Instance
try {
for (EnvironmentUnit environmentUnit : environmentUnits) {
environmentUnit.setTempBackupDir(folderPath);
environmentUnit.restore();
}
} catch (EnvironmentCleanupException ece) {
throw new AgentException("Could not restore environment for component '" + this.componentName + "'" + recurseCauses(ece), ece);
}
// Now execute the additional actions and clean the queue
try {
AdditionalActionsQueue.getInstance().flushAllActions();
} catch (EnvironmentCleanupException ece) {
throw new AgentException("Could not restore environment for component '" + this.componentName + "'" + recurseCauses(ece), ece);
}
}
use of com.axway.ats.environment.EnvironmentUnit in project ats-framework by Axway.
the class Test_DirectoryEnvironmentUnit method restore_aNewFileMustBeDeleted.
// TODO - investigate failing error. It is just on CI machine, only when run from Jenkins
@Ignore
@Test
public void restore_aNewFileMustBeDeleted() throws EnvironmentCleanupException, IOException {
EnvironmentUnit dirEnvUnit = new DirectoryEnvironmentUnit(restoreDirPath, backupDirPath, backupDirName);
Assert.assertTrue(dirEnvUnit.restore());
new File(restoreDirPath + "newFile.txt").createNewFile();
// now the restore is not needed
Assert.assertTrue(dirEnvUnit.restore());
}
Aggregations