use of com.axway.ats.common.filesystem.FileSystemOperationException in project ats-framework by Axway.
the class LocalFileSystemOperations method replaceTextInFile.
@Override
public void replaceTextInFile(String fileName, Map<String, String> searchTokens, boolean isRegex) {
BufferedReader inFileReader = null;
BufferedWriter outFileWriter = null;
File inputFile = null;
File outputFile = null;
StringBuilder info = new StringBuilder();
for (String token : searchTokens.keySet()) {
info.append(token);
info.append(",");
}
info = info.deleteCharAt(info.length() - 1);
try {
try {
inputFile = new File(fileName);
outputFile = new File(fileName + "_" + System.currentTimeMillis() + ".tmp");
inFileReader = new BufferedReader(new FileReader(inputFile));
outFileWriter = new BufferedWriter(new FileWriter(outputFile, false));
String currentLine = inFileReader.readLine();
while (currentLine != null) {
for (Entry<String, String> tokens : searchTokens.entrySet()) {
if (isRegex) {
currentLine = currentLine.replaceAll(tokens.getKey(), tokens.getValue());
} else {
currentLine = currentLine.replace(tokens.getKey(), tokens.getValue());
}
}
outFileWriter.write(currentLine);
outFileWriter.newLine();
// read a new line
currentLine = inFileReader.readLine();
}
log.info("Successfully replaced all" + (isRegex ? " regular expression" : "") + " instances of '" + info.toString() + "' in file '" + fileName + "'");
} finally {
IoUtils.closeStream(inFileReader);
IoUtils.closeStream(outFileWriter);
}
// after we are finished, rename the temporary file to the original one
if (OperatingSystemType.getCurrentOsType().isUnix()) {
// getting original file permissions before overriding operation
String permissions = getFilePermissions(inputFile.getCanonicalPath());
renameFile(outputFile.getCanonicalPath(), fileName, true);
// restoring file permissions
setFilePermissions(fileName, permissions);
} else {
renameFile(outputFile.getCanonicalPath(), fileName, true);
}
} catch (IOException ioe) {
throw new FileSystemOperationException("Unable to replace" + (isRegex ? " regular expression" : "") + " instances of '" + info.toString() + "' in file '" + fileName + "'", ioe);
}
}
use of com.axway.ats.common.filesystem.FileSystemOperationException in project ats-framework by Axway.
the class LocalFileSystemOperations method appendToFile.
@Override
public void appendToFile(String filePath, String contentToAdd) {
if (!new File(filePath).exists()) {
throw new FileSystemOperationException("Unable to append content to file '" + filePath + "' as it does not exist");
}
if (new File(filePath).isDirectory()) {
throw new FileSystemOperationException("Unable to append content to '" + filePath + "' as it is a directory, but file was expected");
}
BufferedWriter fileWriter = null;
try {
fileWriter = new BufferedWriter(new FileWriter(filePath, true));
fileWriter.write(contentToAdd);
} catch (IOException ioe) {
throw new FileSystemOperationException("Unable to append content to file '" + filePath + "'", ioe);
} finally {
if (fileWriter != null) {
try {
fileWriter.close();
log.info("Successfully appended " + contentToAdd.length() + " characters to file '" + filePath + "'");
} catch (IOException ioe) {
log.error("Could not close file writer", ioe);
}
}
}
}
use of com.axway.ats.common.filesystem.FileSystemOperationException in project ats-framework by Axway.
the class LocalFileSystemOperations method sendFileTo.
/**
* Send file contents to another machine
*
* @param fromFileName the source file name
* @param toFileName the destination file name (Not a directory)
* @param toHost the destination host address
* @param toPort the destination port
* @param failOnError set to true if you want to be thrown an exception,
* if there is still a process writing in the file that is being copied
* @throws FileSystemOperationException
*/
public void sendFileTo(String fromFileName, String toFileName, String toHost, int toPort, boolean failOnError) {
File file = new File(fromFileName);
checkFileExistence(file);
Socket socket = null;
OutputStream sos = null;
try {
socket = new Socket(toHost, toPort);
sos = socket.getOutputStream();
sendFileToSocketStream(file, toFileName, sos, failOnError);
} catch (IOException ioe) {
throw new FileSystemOperationException("Unable to send file '" + fromFileName + "' to '" + toFileName + "' on " + toHost + ":" + toPort, ioe);
} finally {
IoUtils.closeStream(sos);
IoUtils.closeStream(socket, "Could not close the socket for sending file " + fromFileName);
}
}
Aggregations