Search in sources :

Example 1 with EnvironmentCleanupException

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);
    }
}
Also used : AgentException(com.axway.ats.agent.core.exceptions.AgentException) EnvironmentCleanupException(com.axway.ats.environment.EnvironmentCleanupException) EnvironmentUnit(com.axway.ats.environment.EnvironmentUnit) FileEnvironmentUnit(com.axway.ats.environment.file.FileEnvironmentUnit) DatabaseEnvironmentUnit(com.axway.ats.environment.database.DatabaseEnvironmentUnit) DirectoryEnvironmentUnit(com.axway.ats.environment.file.DirectoryEnvironmentUnit) File(java.io.File)

Example 2 with EnvironmentCleanupException

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);
    }
}
Also used : EnvironmentCleanupException(com.axway.ats.environment.EnvironmentCleanupException) AgentException(com.axway.ats.agent.core.exceptions.AgentException) EnvironmentUnit(com.axway.ats.environment.EnvironmentUnit) FileEnvironmentUnit(com.axway.ats.environment.file.FileEnvironmentUnit) DatabaseEnvironmentUnit(com.axway.ats.environment.database.DatabaseEnvironmentUnit) DirectoryEnvironmentUnit(com.axway.ats.environment.file.DirectoryEnvironmentUnit)

Example 3 with EnvironmentCleanupException

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);
    }
}
Also used : EnvironmentCleanupException(com.axway.ats.environment.EnvironmentCleanupException) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) File(java.io.File) PublicAtsApi(com.axway.ats.common.PublicAtsApi)

Example 4 with EnvironmentCleanupException

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;
    }
}
Also used : EnvironmentCleanupException(com.axway.ats.environment.EnvironmentCleanupException) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) File(java.io.File)

Example 5 with EnvironmentCleanupException

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;
}
Also used : EnvironmentCleanupException(com.axway.ats.environment.EnvironmentCleanupException) File(java.io.File)

Aggregations

EnvironmentCleanupException (com.axway.ats.environment.EnvironmentCleanupException)9 File (java.io.File)7 AgentException (com.axway.ats.agent.core.exceptions.AgentException)3 EnvironmentUnit (com.axway.ats.environment.EnvironmentUnit)3 DatabaseEnvironmentUnit (com.axway.ats.environment.database.DatabaseEnvironmentUnit)3 DirectoryEnvironmentUnit (com.axway.ats.environment.file.DirectoryEnvironmentUnit)3 FileEnvironmentUnit (com.axway.ats.environment.file.FileEnvironmentUnit)3 FileNotFoundException (java.io.FileNotFoundException)2 IOException (java.io.IOException)2 PublicAtsApi (com.axway.ats.common.PublicAtsApi)1 LocalProcessExecutor (com.axway.ats.core.process.LocalProcessExecutor)1 Date (java.util.Date)1