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);
}
}
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();
}
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();
}
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();
}
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);
}
}
}
}
Aggregations