use of com.axway.ats.environment.EnvironmentCleanupException 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.EnvironmentCleanupException 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.EnvironmentCleanupException in project ats-framework by Axway.
the class FileEnvironmentUnit method backup.
@Override
@PublicAtsApi
public void backup() throws EnvironmentCleanupException {
try {
String backupFileAsString = getBackupFile();
createDirIfNotExist(backupFileAsString);
if (new File(origFileName).exists()) {
copyFile(origFileName, backupFileAsString);
// fix the modification date of the backup file
File origFile = new File(origFileName);
File backupFile = new File(backupFileAsString);
backupFile.setLastModified(origFile.lastModified());
} else if (new File(backupFileAsString).exists()) {
if (!new File(backupFileAsString).delete()) {
throw new EnvironmentCleanupException("File " + backupFileAsString + " must be removed, but the delete operation fails.");
}
}
} catch (FileNotFoundException fnfe) {
log.warn("Cannot backup file: " + origFileName + " Skipping it.", fnfe);
} catch (IOException ioe) {
throw new EnvironmentCleanupException("Could not backup file " + origFileName, ioe);
} finally {
setTempBackupDir(null);
}
}
use of com.axway.ats.environment.EnvironmentCleanupException in project ats-framework by Axway.
the class FileEnvironmentUnit method executeRestoreIfNecessary.
@Override
protected boolean executeRestoreIfNecessary() throws EnvironmentCleanupException {
if (isRestoreNecessary()) {
String backupFileAsString = getBackupFile();
try {
File origFile = new File(origFileName);
File backupFile = new File(backupFileAsString);
if (origFile.exists() && origFile.isDirectory()) {
throw new EnvironmentCleanupException("File " + getFileCanonicalPath(origFile) + " is actually a directory and can not be restored.");
} else if (origFile.exists() && !backupFile.exists()) {
if (origFile.delete()) {
log.info("File " + getFileCanonicalPath(origFile) + " is deleted.");
} else {
throw new EnvironmentCleanupException("File " + getFileCanonicalPath(origFile) + " must be removed, but the delete opeation fails.");
}
} else if (backupFile.exists()) {
createDirIfNotExist(origFileName);
copyFile(backupFileAsString, origFileName);
// fix the modification date of the original file
origFile.setLastModified(backupFile.lastModified());
}
} catch (FileNotFoundException fnfe) {
log.warn("Cannot restore from backup file: " + backupFileAsString + " Skipping it.", fnfe);
} catch (IOException ioe) {
throw new EnvironmentCleanupException("Could not restore file " + origFileName, ioe);
}
return true;
} else {
return false;
}
}
use of com.axway.ats.environment.EnvironmentCleanupException in project ats-framework by Axway.
the class DirectoryEnvironmentUnit method executeRestoreIfNecessary.
@Override
protected boolean executeRestoreIfNecessary() throws EnvironmentCleanupException {
// reset the restored flag
this.restored = false;
File backupDir = new File(getBackupDir());
if (backupDir.isDirectory()) {
File origDir = new File(origDirName);
if (origDir.exists() && !origDir.isDirectory()) {
throw new EnvironmentCleanupException("'" + origDirName + "' exists, but is not a directory");
} else if (!origDir.exists()) {
if (origDir.mkdirs()) {
updateRestoredFlag(true);
log.debug("Created restore folder '" + origDirName + "'");
} else {
log.warn("Can't create folder '" + origDirName + "'");
}
}
Set<String> fileAndDirectoryPaths = getFileAndDirectoryPathsIndex(backupDir);
restoreAllFilesInDirectory(origDir, fileAndDirectoryPaths);
// We should restore them
for (String backupFilePath : fileAndDirectoryPaths) {
String originalFilePath = backupFilePath.replace(getBackupDir(), origDirName);
if (originalFilePath.endsWith("/") || originalFilePath.endsWith("\\")) {
File dir = new File(originalFilePath);
if (!dir.exists() && !dir.mkdirs()) {
log.warn("Can't create folder '" + originalFilePath + "'");
}
} else {
boolean fileRestored = new FileEnvironmentUnit(originalFilePath, IoUtils.getFilePath(backupFilePath), IoUtils.getFileName(backupFilePath)).restore();
updateRestoredFlag(fileRestored);
}
}
}
return this.restored;
}
Aggregations