use of org.apache.commons.vfs2.FileObject in project pentaho-kettle by pentaho.
the class ScriptAddedFunctions method getFileSize.
public static double getFileSize(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 0;
}
FileObject file = null;
try {
// Source file
file = KettleVFS.getFileObject((String) ArgList[0]);
long filesize = 0;
if (file.exists()) {
if (file.getType().equals(FileType.FILE)) {
filesize = file.getContent().getSize();
} else {
throw new RuntimeException("[" + ArgList[0] + "] is not a file!");
}
} else {
throw new RuntimeException("file [" + ArgList[0] + "] can not be found!");
}
return filesize;
} catch (IOException e) {
throw new RuntimeException("The function call getFileSize throw an error : " + e.toString());
} finally {
if (file != null) {
try {
file.close();
} catch (Exception e) {
// Ignore errors
}
}
}
} else {
throw new RuntimeException("The function call getFileSize 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 FTPSConnectionTest method testEnforceProtPOnPut.
@Test
public void testEnforceProtPOnPut() throws Exception {
FileObject file = KettleVFS.createTempFile("FTPSConnectionTest_testEnforceProtPOnPut", KettleVFS.Suffix.TMP);
file.createFile();
try {
FTPSTestConnection connection = spy(new FTPSTestConnection(FTPSConnection.CONNECTION_TYPE_FTP_IMPLICIT_TLS_WITH_CRYPTED, "the.perfect.host", 2010, "warwickw", "julia", null));
connection.replies.put("PWD", new Reply(Arrays.asList("257 \"/la\" is current directory")));
connection.connect();
connection.uploadFile(file.getPublicURIString(), "uploaded-file");
assertEquals("buffer not set", "PBSZ 0\r\n", connection.commands.get(0).toString());
assertEquals("data privacy not set", "PROT P\r\n", connection.commands.get(1).toString());
} finally {
file.delete();
}
}
use of org.apache.commons.vfs2.FileObject in project pentaho-kettle by pentaho.
the class TextFileOutputTest method testsIterate.
@Test
public void testsIterate() {
FileObject resultFile = null;
FileObject contentFile;
String content = null;
int i = 0;
for (Boolean fileExists : BOOL_VALUE_LIST) {
for (Boolean dataReceived : BOOL_VALUE_LIST) {
for (Boolean isDoNotOpenNewFileInit : BOOL_VALUE_LIST) {
for (Boolean endLineExists : BOOL_VALUE_LIST) {
for (Boolean append : BOOL_VALUE_LIST) {
try {
resultFile = helpTestInit(fileExists, dataReceived, isDoNotOpenNewFileInit, endLineExists, append);
content = (String) contents.toArray()[i++];
contentFile = createTemplateFile(content);
if (resultFile.exists()) {
assertTrue(IOUtils.contentEquals(resultFile.getContent().getInputStream(), contentFile.getContent().getInputStream()));
} else {
assertFalse(contentFile.exists());
}
} catch (Exception e) {
fail(e.getMessage() + "\n FileExists = " + fileExists + "\n DataReceived = " + dataReceived + "\n isDoNotOpenNewFileInit = " + isDoNotOpenNewFileInit + "\n EndLineExists = " + endLineExists + "\n Append = " + append + "\n Content = " + (content != null ? content : "<null>") + "\n resultFile = " + (resultFile != null ? resultFile : "<null>"));
}
}
}
}
}
}
}
use of org.apache.commons.vfs2.FileObject in project pentaho-kettle by pentaho.
the class TextFileOutputTest method helpTestInit.
private FileObject helpTestInit(Boolean fileExists, Boolean dataReceived, Boolean isDoNotOpenNewFileInit, Boolean endLineExists, Boolean append) throws Exception {
FileObject f = createTemplateFile(fileExists ? TEST_PREVIOUS_DATA : null);
List<Object[]> rows = dataReceived ? this.rows : this.emptyRows;
String endLine = endLineExists ? END_LINE : null;
List<Throwable> errors = doOutput(textFileFields, rows, f.getName().getURI(), endLine, false, isDoNotOpenNewFileInit, append);
if (!errors.isEmpty()) {
StringBuilder str = new StringBuilder();
for (Throwable thr : errors) {
str.append(thr);
}
fail(str.toString());
}
return f;
}
use of org.apache.commons.vfs2.FileObject in project pentaho-kettle by pentaho.
the class VFSDirectory method create.
public static VFSDirectory create(String parent, FileObject fileObject, String connection, String domain) {
VFSDirectory vfsDirectory = new VFSDirectory();
vfsDirectory.setName(fileObject.getName().getBaseName());
vfsDirectory.setPath(fileObject.getName().getURI());
vfsDirectory.setParent(parent);
if (connection != null) {
vfsDirectory.setConnection(connection);
vfsDirectory.setRoot(VFSFileProvider.NAME);
}
vfsDirectory.setDomain(domain != null ? domain : "");
vfsDirectory.setCanEdit(true);
vfsDirectory.setHasChildren(true);
vfsDirectory.setCanAddChildren(true);
try {
vfsDirectory.setDate(new Date(fileObject.getContent().getLastModifiedTime()));
} catch (FileSystemException e) {
vfsDirectory.setDate(new Date());
}
return vfsDirectory;
}
Aggregations