use of org.apache.commons.vfs2.FileObject in project pentaho-kettle by pentaho.
the class SharedObjects method writeToFile.
/**
* Write sharedObjects to file. In case of an exception are caught save backup file instead of new one.
*
* @param fileObject is file for writing
* @param backupFileName is backup file name
* @throws IOException
* @throws KettleException
*/
@VisibleForTesting
protected void writeToFile(FileObject fileObject, String backupFileName) throws IOException, KettleException {
OutputStream outputStream = null;
PrintStream out = null;
try {
outputStream = initOutputStreamUsingKettleVFS(fileObject);
out = new PrintStream(outputStream);
out.print(XMLHandler.getXMLHeader(Const.XML_ENCODING));
out.println("<" + XML_TAG + ">");
Collection<SharedObjectInterface> collection = objectsMap.values();
for (SharedObjectInterface sharedObject : collection) {
String xmlContent = sharedObject.getXML();
out.println(xmlContent);
}
out.println("</" + XML_TAG + ">");
} catch (Exception e) {
// restore file if something wrong
boolean isRestored = false;
if (backupFileName != null) {
restoreFileFromBackup(backupFileName);
isRestored = true;
}
throw new KettleException(BaseMessages.getString(PKG, "SharedOjects.WriteToFile.ErrorWritingFile", isRestored), e);
} finally {
if (out != null) {
out.flush();
}
if (out != null) {
out.close();
}
if (out != null) {
outputStream.close();
}
}
}
use of org.apache.commons.vfs2.FileObject in project pentaho-kettle by pentaho.
the class SharedObjects method saveToFile.
public void saveToFile() throws IOException, KettleException {
FileObject fileObject = getFileObjectFromKettleVFS(filename);
String backupFileName = createOrGetFileBackup(fileObject);
writeToFile(fileObject, backupFileName);
}
use of org.apache.commons.vfs2.FileObject in project pentaho-kettle by pentaho.
the class ScriptAddedFunctions method getFileExtension.
public static String getFileExtension(ScriptEngine actualContext, Bindings actualObject, Object[] ArgList, Object FunctionContext) {
try {
if (ArgList.length == 1 && !isNull(ArgList[0]) && !isUndefined(ArgList[0])) {
if (ArgList[0].equals(null)) {
return null;
}
FileObject file = null;
try {
// Source file
file = KettleVFS.getFileObject((String) ArgList[0]);
String Extension = null;
if (file.exists()) {
Extension = file.getName().getExtension();
} else {
throw new RuntimeException("file [" + ArgList[0] + "] can not be found!");
}
return Extension;
} catch (IOException e) {
throw new RuntimeException("The function call getFileExtension throw an error : " + e.toString());
} finally {
if (file != null) {
try {
file.close();
} catch (Exception e) {
// Ignore errors
}
}
}
} else {
throw new RuntimeException("The function call getFileExtension is not valid.");
}
} catch (Exception e) {
throw new RuntimeException(e.toString());
}
}
use of org.apache.commons.vfs2.FileObject in project pentaho-kettle by pentaho.
the class ScriptAddedFunctions method getShortFilename.
public static String getShortFilename(ScriptEngine actualContext, Bindings actualObject, Object[] ArgList, Object FunctionContext) {
try {
if (ArgList.length == 1 && !isNull(ArgList[0]) && !isUndefined(ArgList[0])) {
if (ArgList[0].equals(null)) {
return null;
}
FileObject file = null;
try {
// Source file
file = KettleVFS.getFileObject((String) ArgList[0]);
String Filename = null;
if (file.exists()) {
Filename = file.getName().getBaseName();
} else {
throw new RuntimeException("file [" + ArgList[0] + "] can not be found!");
}
return Filename;
} catch (IOException e) {
throw new RuntimeException("The function call getShortFilename throw an error : " + e.toString());
} finally {
if (file != null) {
try {
file.close();
} catch (Exception e) {
// Ignore errors
}
}
}
} else {
throw new RuntimeException("The function call getShortFilename is not valid.");
}
} catch (Exception e) {
throw new RuntimeException(e.toString());
}
}
use of org.apache.commons.vfs2.FileObject in project pentaho-kettle by pentaho.
the class ScriptAddedFunctions method copyFile.
public static void copyFile(ScriptEngine actualContext, Bindings actualObject, Object[] ArgList, Object FunctionContext) {
try {
if (ArgList.length == 3 && !isNull(ArgList[0]) && !isNull(ArgList[1]) && !isUndefined(ArgList[0]) && !isUndefined(ArgList[1])) {
FileObject fileSource = null, fileDestination = null;
try {
// Source file to copy
fileSource = KettleVFS.getFileObject((String) ArgList[0]);
// Destination filename
fileDestination = KettleVFS.getFileObject((String) ArgList[1]);
if (fileSource.exists()) {
// Source file exists...
if (fileSource.getType() == FileType.FILE) {
// Great..source is a file ...
boolean overwrite = false;
if (!ArgList[1].equals(null)) {
overwrite = (Boolean) ArgList[2];
}
boolean destinationExists = fileDestination.exists();
// Let's copy the file...
if ((destinationExists && overwrite) || !destinationExists) {
FileUtil.copyContent(fileSource, fileDestination);
}
}
} else {
throw new RuntimeException("file to copy [" + ArgList[0] + "] can not be found!");
}
} catch (IOException e) {
throw new RuntimeException("The function call copyFile throw an error : " + e.toString());
} finally {
if (fileSource != null) {
try {
fileSource.close();
} catch (Exception e) {
// Ignore errors
}
}
if (fileDestination != null) {
try {
fileDestination.close();
} catch (Exception e) {
// Ignore errors
}
}
}
} else {
throw new RuntimeException("The function call copyFileis not valid.");
}
} catch (Exception e) {
throw new RuntimeException(e.toString());
}
}
Aggregations