Search in sources :

Example 6 with FileSystemOperationException

use of com.axway.ats.common.filesystem.FileSystemOperationException in project ats-framework by Axway.

the class RemoteFileSystemOperations method copyFileFrom.

public void copyFileFrom(String fromFile, String toFile, boolean failOnError) {
    try {
        Integer copyFileStartPort = getCopyFilePortProperty(ActionLibraryConfigurator.getInstance().getCopyFileStartPort());
        Integer copyFileEndPort = getCopyFilePortProperty(ActionLibraryConfigurator.getInstance().getCopyFileEndPort());
        if (copyFileStartPort != null && copyFileStartPort > 0 && copyFileEndPort != null && copyFileEndPort > 0) {
            localFileSystemOperations.setCopyFilePortRange(copyFileStartPort, copyFileEndPort);
        }
        int port = localFileSystemOperations.openFileTransferSocket();
        remoteFileSystemOperations.sendFileTo(fromFile, toFile, HostUtils.getPublicLocalHostIp(this.atsAgent), port, failOnError);
        localFileSystemOperations.waitForFileTransferCompletion(port);
    } catch (Exception e) {
        String message = new StringBuilder().append("Unable to copy file ").append(fromFile).append(" from ").append(this.atsAgent).append(" to file ").append(toFile).append(" on the local host").toString();
        throw new FileSystemOperationException(message, e);
    }
}
Also used : FileSystemOperationException(com.axway.ats.common.filesystem.FileSystemOperationException) FileSystemOperationException(com.axway.ats.common.filesystem.FileSystemOperationException)

Example 7 with FileSystemOperationException

use of com.axway.ats.common.filesystem.FileSystemOperationException in project ats-framework by Axway.

the class Test_FileSystemOperations method testDeleteDirectoryException.

/**
 * Test case
 * @throws Exception
 */
@Test(expected = FileSystemOperationException.class)
public void testDeleteDirectoryException() throws Exception {
    // setup expectations
    expectNew(LocalFileSystemOperations.class).andReturn(localFSOperationsMock);
    localFSOperationsMock.deleteDirectory(SOURCE_DIRECTORY_NAME_VALID, true);
    expectLastCall().andThrow(new FileSystemOperationException("Test"));
    replayAll();
    // execute operation
    fileSystemOperationsLocal.deleteDirectory(SOURCE_DIRECTORY_NAME_VALID);
    // verify results
    verifyAll();
}
Also used : LocalFileSystemOperations(com.axway.ats.core.filesystem.LocalFileSystemOperations) FileSystemOperationException(com.axway.ats.common.filesystem.FileSystemOperationException) BaseTest(com.axway.ats.action.BaseTest) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 8 with FileSystemOperationException

use of com.axway.ats.common.filesystem.FileSystemOperationException in project ats-framework by Axway.

the class Test_FileSystemOperations method testPurgeDirectoryException.

/**
 * Test case
 * @throws Exception
 */
@Test(expected = FileSystemOperationException.class)
public void testPurgeDirectoryException() throws Exception {
    // setup expectations
    expectNew(LocalFileSystemOperations.class).andReturn(localFSOperationsMock);
    localFSOperationsMock.purgeDirectoryContents(SOURCE_DIRECTORY_NAME_VALID);
    expectLastCall().andThrow(new FileSystemOperationException("Test"));
    replayAll();
    // execute operation
    fileSystemOperationsLocal.deleteDirectoryContent(SOURCE_DIRECTORY_NAME_VALID);
    // verify results
    verifyAll();
}
Also used : LocalFileSystemOperations(com.axway.ats.core.filesystem.LocalFileSystemOperations) FileSystemOperationException(com.axway.ats.common.filesystem.FileSystemOperationException) BaseTest(com.axway.ats.action.BaseTest) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 9 with FileSystemOperationException

use of com.axway.ats.common.filesystem.FileSystemOperationException in project ats-framework by Axway.

the class Test_FileSystemOperations method computeMD5NegativeExceptionLocal.

/**
 * Test case
 * @throws Exception
 */
@Test(expected = FileSystemOperationException.class)
public void computeMD5NegativeExceptionLocal() throws Exception {
    // setup expectations
    expectNew(LocalFileSystemOperations.class).andReturn(localFSOperationsMock);
    expect(localFSOperationsMock.computeMd5Sum(SOURCE_FILE_NAME_VALID, Md5SumMode.BINARY)).andThrow(new FileSystemOperationException("Test"));
    replayAll();
    // execute operation
    fileSystemOperationsLocal.computeMd5Sum(SOURCE_FILE_NAME_VALID, Md5SumMode.BINARY);
    // verify results
    verifyAll();
}
Also used : LocalFileSystemOperations(com.axway.ats.core.filesystem.LocalFileSystemOperations) FileSystemOperationException(com.axway.ats.common.filesystem.FileSystemOperationException) BaseTest(com.axway.ats.action.BaseTest) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 10 with FileSystemOperationException

use of com.axway.ats.common.filesystem.FileSystemOperationException in project ats-framework by Axway.

the class LocalFileSystemOperations method lockFile.

/**
 * <pre>
 * Acquires an exclusive lock on a file
 *
 * <b>Platform dependencies</b>
 *
 * - In Windows it works as expected
 * - In Linux it depends on the locking mechanism of the system. The file locking types are two - advisory and mandatory:
 *
 *    a) <b>Advisory locking</b> - advisory locking will work, only if the participating process are cooperative.
 *       Advisory locking sometimes also called as "unenforced" locking.
 *
 *    b) <b>Mandatory locking</b> - mandatory locking doesn’t require cooperation from the participating processes.
 *       It causes the kernel to check every open, read and write to verify that the calling process isn’t
 *       violating a lock on the given file. To enable mandatory locking in Linux, you need to enable it on
 *       a file system level and also on the individual files. The steps to be followed are:
 *           1. Mount the file system with "<i>-o mand</i>" option
 *           2. For the lock_file, turn on the set-group-ID bit and turn off the group-execute bit, to enable
 *              mandatory locking on that particular file. (This way has been chosen because when you turn off
 *              the group-execute bit, set-group-ID has no real meaning to it )
 *
 *       How to do mandatory locking:
 *           Note: You need to be root to execute the below command
 *           <i># mount -oremount,mand /</i>
 *           <i># touch mandatory.txt</i>
 *           <i># chmod g+s,g-x mandatory.txt</i>
 * </pre>
 *
 * @param fileName file name
 */
@Override
public void lockFile(String fileName) {
    synchronized (lockedFiles) {
        if (lockedFiles.containsKey(fileName)) {
            log.warn("File '" + fileName + "' is already locked");
        } else {
            try {
                File fileToLock = new File(fileName);
                @SuppressWarnings("resource") FileChannel // keep lock to the file
                channel = new RandomAccessFile(fileToLock, "rw").getChannel();
                FileLock fileLock = channel.lock();
                lockedFiles.put(fileName, fileLock);
            } catch (FileNotFoundException fnfe) {
                throw new FileSystemOperationException("File '" + fileName + "' is not found", fnfe);
            } catch (OverlappingFileLockException ofle) {
                throw new FileSystemOperationException("File '" + fileName + "' is already locked in the current JVM" + ", but not from this class, so we can't unlock it later.", ofle);
            } catch (Exception e) {
                throw new FileSystemOperationException("Could not lock file '" + fileName + "'", e);
            }
        }
    }
}
Also used : RandomAccessFile(java.io.RandomAccessFile) FileSystemOperationException(com.axway.ats.common.filesystem.FileSystemOperationException) FileChannel(java.nio.channels.FileChannel) FileLock(java.nio.channels.FileLock) FileNotFoundException(java.io.FileNotFoundException) RandomAccessFile(java.io.RandomAccessFile) ZipFile(org.apache.commons.compress.archivers.zip.ZipFile) File(java.io.File) OverlappingFileLockException(java.nio.channels.OverlappingFileLockException) OverlappingFileLockException(java.nio.channels.OverlappingFileLockException) FileSystemOperationException(com.axway.ats.common.filesystem.FileSystemOperationException) AttributeNotSupportedException(com.axway.ats.core.filesystem.exceptions.AttributeNotSupportedException) EOFException(java.io.EOFException) FileNotFoundException(java.io.FileNotFoundException) FileDoesNotExistException(com.axway.ats.core.filesystem.exceptions.FileDoesNotExistException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) SocketTimeoutException(java.net.SocketTimeoutException) IOException(java.io.IOException)

Aggregations

FileSystemOperationException (com.axway.ats.common.filesystem.FileSystemOperationException)58 IOException (java.io.IOException)32 File (java.io.File)31 RandomAccessFile (java.io.RandomAccessFile)29 ZipFile (org.apache.commons.compress.archivers.zip.ZipFile)29 FileDoesNotExistException (com.axway.ats.core.filesystem.exceptions.FileDoesNotExistException)16 FileNotFoundException (java.io.FileNotFoundException)16 SocketTimeoutException (java.net.SocketTimeoutException)14 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)14 AttributeNotSupportedException (com.axway.ats.core.filesystem.exceptions.AttributeNotSupportedException)13 EOFException (java.io.EOFException)13 OverlappingFileLockException (java.nio.channels.OverlappingFileLockException)13 FileOutputStream (java.io.FileOutputStream)10 FileInputStream (java.io.FileInputStream)9 BufferedOutputStream (java.io.BufferedOutputStream)8 DataOutputStream (java.io.DataOutputStream)8 BaseTest (com.axway.ats.action.BaseTest)7 LocalFileSystemOperations (com.axway.ats.core.filesystem.LocalFileSystemOperations)7 Test (org.junit.Test)7 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)7