use of org.apache.commons.vfs2.FileSelectInfo in project pentaho-kettle by pentaho.
the class BasePluginType method findPluginFiles.
protected List<FileObject> findPluginFiles(String folder, final String regex) {
List<FileObject> list = new ArrayList<>();
try {
FileObject folderObject = KettleVFS.getFileObject(folder);
FileObject[] files = folderObject.findFiles(new FileSelector() {
@Override
public boolean traverseDescendents(FileSelectInfo fileSelectInfo) throws Exception {
return true;
}
@Override
public boolean includeFile(FileSelectInfo fileSelectInfo) throws Exception {
return fileSelectInfo.getFile().toString().matches(regex);
}
});
if (files != null) {
Collections.addAll(list, files);
}
} catch (Exception e) {
// ignore this: unknown folder, insufficient permissions, etc
}
return list;
}
use of org.apache.commons.vfs2.FileSelectInfo in project pentaho-kettle by pentaho.
the class JobEntryMoveFiles method ProcessFileFolder.
private boolean ProcessFileFolder(String sourcefilefoldername, String destinationfilefoldername, String wildcard, Job parentJob, Result result, String MoveToFolder) {
boolean entrystatus = false;
FileObject sourcefilefolder = null;
FileObject destinationfilefolder = null;
FileObject movetofolderfolder = null;
FileObject Currentfile = null;
// Get real source, destination file and wildcard
String realSourceFilefoldername = environmentSubstitute(sourcefilefoldername);
String realDestinationFilefoldername = environmentSubstitute(destinationfilefoldername);
String realWildcard = environmentSubstitute(wildcard);
try {
sourcefilefolder = KettleVFS.getFileObject(realSourceFilefoldername, this);
destinationfilefolder = KettleVFS.getFileObject(realDestinationFilefoldername, this);
if (!Utils.isEmpty(MoveToFolder)) {
movetofolderfolder = KettleVFS.getFileObject(MoveToFolder, this);
}
if (sourcefilefolder.exists()) {
// PDI will create it
if (CreateDestinationFolder(destinationfilefolder)) {
// Basic Tests
if (sourcefilefolder.getType().equals(FileType.FOLDER) && destination_is_a_file) {
// Source is a folder, destination is a file
// WARNING !!! CAN NOT MOVE FOLDER TO FILE !!!
log.logError(BaseMessages.getString(PKG, "JobMoveFiles.Log.Forbidden"), BaseMessages.getString(PKG, "JobMoveFiles.Log.CanNotMoveFolderToFile", realSourceFilefoldername, realDestinationFilefoldername));
// Update Errors
updateErrors();
} else {
if (destinationfilefolder.getType().equals(FileType.FOLDER) && sourcefilefolder.getType().equals(FileType.FILE)) {
// Source is a file, destination is a folder
// return destination short filename
String shortfilename = sourcefilefolder.getName().getBaseName();
try {
shortfilename = getDestinationFilename(shortfilename);
} catch (Exception e) {
logError(BaseMessages.getString(PKG, BaseMessages.getString(PKG, "JobMoveFiles.Error.GettingFilename", sourcefilefolder.getName().getBaseName(), e.toString())));
return entrystatus;
}
// Move the file to the destination folder
String destinationfilenamefull = KettleVFS.getFilename(destinationfilefolder) + Const.FILE_SEPARATOR + shortfilename;
FileObject destinationfile = KettleVFS.getFileObject(destinationfilenamefull, this);
entrystatus = MoveFile(shortfilename, sourcefilefolder, destinationfile, movetofolderfolder, parentJob, result);
return entrystatus;
} else if (sourcefilefolder.getType().equals(FileType.FILE) && destination_is_a_file) {
// Source is a file, destination is a file
FileObject destinationfile = KettleVFS.getFileObject(realDestinationFilefoldername, this);
// return destination short filename
String shortfilename = destinationfile.getName().getBaseName();
try {
shortfilename = getDestinationFilename(shortfilename);
} catch (Exception e) {
logError(BaseMessages.getString(PKG, BaseMessages.getString(PKG, "JobMoveFiles.Error.GettingFilename", sourcefilefolder.getName().getBaseName(), e.toString())));
return entrystatus;
}
String destinationfilenamefull = KettleVFS.getFilename(destinationfile.getParent()) + Const.FILE_SEPARATOR + shortfilename;
destinationfile = KettleVFS.getFileObject(destinationfilenamefull, this);
entrystatus = MoveFile(shortfilename, sourcefilefolder, destinationfile, movetofolderfolder, parentJob, result);
return entrystatus;
} else {
// Both source and destination are folders
if (log.isDetailed()) {
logDetailed(" ");
logDetailed(BaseMessages.getString(PKG, "JobMoveFiles.Log.FetchFolder", sourcefilefolder.toString()));
}
FileObject[] fileObjects = sourcefilefolder.findFiles(new AllFileSelector() {
public boolean traverseDescendents(FileSelectInfo info) {
return true;
}
public boolean includeFile(FileSelectInfo info) {
FileObject fileObject = info.getFile();
try {
if (fileObject == null) {
return false;
}
} catch (Exception ex) {
// Upon error don't process the file.
return false;
} finally {
if (fileObject != null) {
try {
fileObject.close();
} catch (IOException ex) {
/* Ignore */
}
}
}
return true;
}
});
if (fileObjects != null) {
for (int j = 0; j < fileObjects.length && !parentJob.isStopped(); j++) {
// Success condition broken?
if (successConditionBroken) {
if (!successConditionBrokenExit) {
logError(BaseMessages.getString(PKG, "JobMoveFiles.Error.SuccessConditionbroken", "" + NrErrors));
successConditionBrokenExit = true;
}
return false;
}
// Fetch files in list one after one ...
Currentfile = fileObjects[j];
if (!MoveOneFile(Currentfile, sourcefilefolder, realDestinationFilefoldername, realWildcard, parentJob, result, movetofolderfolder)) {
// Update Errors
updateErrors();
}
}
}
}
}
entrystatus = true;
} else {
// Destination Folder or Parent folder is missing
logError(BaseMessages.getString(PKG, "JobMoveFiles.Error.DestinationFolderNotFound", realDestinationFilefoldername));
}
} else {
logError(BaseMessages.getString(PKG, "JobMoveFiles.Error.SourceFileNotExists", realSourceFilefoldername));
}
} catch (Exception e) {
logError(BaseMessages.getString(PKG, "JobMoveFiles.Error.Exception.MoveProcess", realSourceFilefoldername.toString(), destinationfilefolder.toString(), e.getMessage()));
} finally {
if (sourcefilefolder != null) {
try {
sourcefilefolder.close();
} catch (IOException ex) {
/* Ignore */
}
}
if (destinationfilefolder != null) {
try {
destinationfilefolder.close();
} catch (IOException ex) {
/* Ignore */
}
}
if (Currentfile != null) {
try {
Currentfile.close();
} catch (IOException ex) {
/* Ignore */
}
}
if (movetofolderfolder != null) {
try {
movetofolderfolder.close();
} catch (IOException ex) {
/* Ignore */
}
}
}
return entrystatus;
}
use of org.apache.commons.vfs2.FileSelectInfo in project pentaho-kettle by pentaho.
the class JobEntryPGPDecryptFiles method ProcessFileFolder.
private boolean ProcessFileFolder(String sourcefilefoldername, String passPhrase, String destinationfilefoldername, String wildcard, Job parentJob, Result result, String MoveToFolder) {
boolean entrystatus = false;
FileObject sourcefilefolder = null;
FileObject destinationfilefolder = null;
FileObject movetofolderfolder = null;
FileObject Currentfile = null;
// Get real source, destination file and wildcard
String realSourceFilefoldername = environmentSubstitute(sourcefilefoldername);
String realDestinationFilefoldername = environmentSubstitute(destinationfilefoldername);
String realWildcard = environmentSubstitute(wildcard);
try {
sourcefilefolder = KettleVFS.getFileObject(realSourceFilefoldername);
destinationfilefolder = KettleVFS.getFileObject(realDestinationFilefoldername);
if (!Utils.isEmpty(MoveToFolder)) {
movetofolderfolder = KettleVFS.getFileObject(MoveToFolder);
}
if (sourcefilefolder.exists()) {
// PDI will create it
if (CreateDestinationFolder(destinationfilefolder)) {
// Basic Tests
if (sourcefilefolder.getType().equals(FileType.FOLDER) && destination_is_a_file) {
// Source is a folder, destination is a file
// WARNING !!! CAN NOT MOVE FOLDER TO FILE !!!
logError(BaseMessages.getString(PKG, "JobPGPDecryptFiles.Log.Forbidden"), BaseMessages.getString(PKG, "JobPGPDecryptFiles.Log.CanNotMoveFolderToFile", realSourceFilefoldername, realDestinationFilefoldername));
// Update Errors
updateErrors();
} else {
if (destinationfilefolder.getType().equals(FileType.FOLDER) && sourcefilefolder.getType().equals(FileType.FILE)) {
// Source is a file, destination is a folder
// return destination short filename
String shortfilename = sourcefilefolder.getName().getBaseName();
try {
shortfilename = getDestinationFilename(sourcefilefolder.getName().getBaseName());
} catch (Exception e) {
logError(BaseMessages.getString(PKG, "JobPGPDecryptFiles.Error.GettingFilename", sourcefilefolder.getName().getBaseName(), e.toString()));
return entrystatus;
}
// Move the file to the destination folder
String destinationfilenamefull = destinationfilefolder.toString() + Const.FILE_SEPARATOR + shortfilename;
FileObject destinationfile = KettleVFS.getFileObject(destinationfilenamefull);
entrystatus = DecryptFile(shortfilename, sourcefilefolder, passPhrase, destinationfile, movetofolderfolder, parentJob, result);
} else if (sourcefilefolder.getType().equals(FileType.FILE) && destination_is_a_file) {
// Source is a file, destination is a file
FileObject destinationfile = KettleVFS.getFileObject(realDestinationFilefoldername);
// return destination short filename
String shortfilename = destinationfile.getName().getBaseName();
try {
shortfilename = getDestinationFilename(destinationfile.getName().getBaseName());
} catch (Exception e) {
logError(BaseMessages.getString(PKG, "JobPGPDecryptFiles.Error.GettingFilename", sourcefilefolder.getName().getBaseName(), e.toString()));
return entrystatus;
}
String destinationfilenamefull = destinationfilefolder.getParent().toString() + Const.FILE_SEPARATOR + shortfilename;
destinationfile = KettleVFS.getFileObject(destinationfilenamefull);
entrystatus = DecryptFile(shortfilename, sourcefilefolder, passPhrase, destinationfile, movetofolderfolder, parentJob, result);
} else {
// Both source and destination are folders
if (isDetailed()) {
logDetailed(" ");
logDetailed(BaseMessages.getString(PKG, "JobPGPDecryptFiles.Log.FetchFolder", sourcefilefolder.toString()));
}
FileObject[] fileObjects = sourcefilefolder.findFiles(new AllFileSelector() {
public boolean traverseDescendents(FileSelectInfo info) {
return info.getDepth() == 0 || include_subfolders;
}
public boolean includeFile(FileSelectInfo info) {
FileObject fileObject = info.getFile();
try {
if (fileObject == null) {
return false;
}
} catch (Exception ex) {
// Upon error don't process the file.
return false;
} finally {
if (fileObject != null) {
try {
fileObject.close();
fileObject = null;
} catch (IOException ex) {
/* Ignore */
}
}
}
return true;
}
});
if (fileObjects != null) {
for (int j = 0; j < fileObjects.length && !parentJob.isStopped(); j++) {
// Success condition broken?
if (successConditionBroken) {
if (!successConditionBrokenExit) {
logError(BaseMessages.getString(PKG, "JobPGPDecryptFiles.Error.SuccessConditionbroken", "" + NrErrors));
successConditionBrokenExit = true;
}
return false;
}
// Fetch files in list one after one ...
Currentfile = fileObjects[j];
if (!DecryptOneFile(Currentfile, sourcefilefolder, passPhrase, realDestinationFilefoldername, realWildcard, parentJob, result, movetofolderfolder)) {
// Update Errors
updateErrors();
}
}
}
}
}
entrystatus = true;
} else {
// Destination Folder or Parent folder is missing
logError(BaseMessages.getString(PKG, "JobPGPDecryptFiles.Error.DestinationFolderNotFound", realDestinationFilefoldername));
}
} else {
logError(BaseMessages.getString(PKG, "JobPGPDecryptFiles.Error.SourceFileNotExists", realSourceFilefoldername));
}
} catch (Exception e) {
logError(BaseMessages.getString(PKG, "JobPGPDecryptFiles.Error.Exception.MoveProcess", realSourceFilefoldername.toString(), destinationfilefolder.toString(), e.getMessage()));
// Update Errors
updateErrors();
} finally {
if (sourcefilefolder != null) {
try {
sourcefilefolder.close();
} catch (IOException ex) {
/* Ignore */
}
}
if (destinationfilefolder != null) {
try {
destinationfilefolder.close();
} catch (IOException ex) {
/* Ignore */
}
}
if (Currentfile != null) {
try {
Currentfile.close();
} catch (IOException ex) {
/* Ignore */
}
}
if (movetofolderfolder != null) {
try {
movetofolderfolder.close();
} catch (IOException ex) {
/* Ignore */
}
}
}
return entrystatus;
}
use of org.apache.commons.vfs2.FileSelectInfo in project pentaho-kettle by pentaho.
the class JobEntryXMLWellFormed method processFileFolder.
private boolean processFileFolder(String sourcefilefoldername, String wildcard, Job parentJob, Result result) {
boolean entrystatus = false;
FileObject sourcefilefolder = null;
FileObject CurrentFile = null;
// Get real source file and wilcard
String realSourceFilefoldername = environmentSubstitute(sourcefilefoldername);
if (Utils.isEmpty(realSourceFilefoldername)) {
logError(BaseMessages.getString(PKG, "JobXMLWellFormed.log.FileFolderEmpty", sourcefilefoldername));
// Update Errors
updateErrors();
return entrystatus;
}
String realWildcard = environmentSubstitute(wildcard);
try {
sourcefilefolder = KettleVFS.getFileObject(realSourceFilefoldername, this);
if (sourcefilefolder.exists()) {
if (log.isDetailed()) {
logDetailed(BaseMessages.getString(PKG, "JobXMLWellFormed.Log.FileExists", sourcefilefolder.toString()));
}
if (sourcefilefolder.getType() == FileType.FILE) {
entrystatus = checkOneFile(sourcefilefolder, result, parentJob);
} else if (sourcefilefolder.getType() == FileType.FOLDER) {
FileObject[] fileObjects = sourcefilefolder.findFiles(new AllFileSelector() {
public boolean traverseDescendents(FileSelectInfo info) {
return true;
}
public boolean includeFile(FileSelectInfo info) {
FileObject fileObject = info.getFile();
try {
if (fileObject == null) {
return false;
}
if (fileObject.getType() != FileType.FILE) {
return false;
}
} catch (Exception ex) {
// Upon error don't process the file.
return false;
} finally {
if (fileObject != null) {
try {
fileObject.close();
} catch (IOException ex) {
/* Ignore */
}
}
}
return true;
}
});
if (fileObjects != null) {
for (int j = 0; j < fileObjects.length && !parentJob.isStopped(); j++) {
if (successConditionBroken) {
if (!successConditionBrokenExit) {
logError(BaseMessages.getString(PKG, "JobXMLWellFormed.Error.SuccessConditionbroken", "" + NrAllErrors));
successConditionBrokenExit = true;
}
return false;
}
// Fetch files in list one after one ...
CurrentFile = fileObjects[j];
if (!CurrentFile.getParent().toString().equals(sourcefilefolder.toString())) {
// Not in the Base Folder..Only if include sub folders
if (include_subfolders) {
if (GetFileWildcard(CurrentFile.toString(), realWildcard)) {
checkOneFile(CurrentFile, result, parentJob);
}
}
} else {
// In the base folder
if (GetFileWildcard(CurrentFile.toString(), realWildcard)) {
checkOneFile(CurrentFile, result, parentJob);
}
}
}
}
} else {
logError(BaseMessages.getString(PKG, "JobXMLWellFormed.Error.UnknowFileFormat", sourcefilefolder.toString()));
// Update Errors
updateErrors();
}
} else {
logError(BaseMessages.getString(PKG, "JobXMLWellFormed.Error.SourceFileNotExists", realSourceFilefoldername));
// Update Errors
updateErrors();
}
} catch (Exception e) {
logError(BaseMessages.getString(PKG, "JobXMLWellFormed.Error.Exception.Processing", realSourceFilefoldername.toString(), e));
// Update Errors
updateErrors();
} finally {
if (sourcefilefolder != null) {
try {
sourcefilefolder.close();
} catch (IOException ex) {
/* Ignore */
}
}
if (CurrentFile != null) {
try {
CurrentFile.close();
} catch (IOException ex) {
/* Ignore */
}
}
}
return entrystatus;
}
use of org.apache.commons.vfs2.FileSelectInfo in project pentaho-kettle by pentaho.
the class KettleDatabaseRepositoryIT method verifyJobSamples.
protected void verifyJobSamples(RepositoryDirectoryInterface samplesDirectory) throws Exception {
FileObject jobSamplesFolder = KettleVFS.getFileObject("samples/jobs/");
FileObject[] files = jobSamplesFolder.findFiles(new FileSelector() {
@Override
public boolean traverseDescendents(FileSelectInfo arg0) throws Exception {
return true;
}
@Override
public boolean includeFile(FileSelectInfo info) throws Exception {
return info.getFile().getName().getExtension().equalsIgnoreCase("kjb");
}
});
List<FileObject> filesList = Arrays.asList(files);
Collections.sort(filesList, new Comparator<FileObject>() {
@Override
public int compare(FileObject o1, FileObject o2) {
return o1.getName().getPath().compareTo(o2.getName().getPath());
}
});
// test the storage of jobMeta attributes in the Kettle DB Repo
if (filesList.size() > 0) {
FileObject file = filesList.get(0);
String jobFilename = file.getName().getPath();
System.out.println("Storing/Loading/validating job attributes");
// Load the JobMeta object...
//
JobMeta jobMeta = new JobMeta(jobFilename, repository);
// set some attributes
jobMeta.setAttribute("group", "key", "value");
jobMeta.setAttribute("test-group", "test-key-1", "test-value");
jobMeta.setAttribute("test-group", "test-key-2", "test-value");
jobMeta.setAttribute("test-group", "test-key-3", "test-value-3");
// Save it in the repository in the samples folder
//
jobMeta.setRepositoryDirectory(samplesDirectory);
repository.save(jobMeta, "unit testing");
assertNotNull(jobMeta.getObjectId());
// Load it back up again...
//
JobMeta repJobMeta = repository.loadJob(jobMeta.getObjectId(), null);
String value = repJobMeta.getAttribute("group", "key");
String value1 = repJobMeta.getAttribute("test-group", "test-key-1");
String value2 = repJobMeta.getAttribute("test-group", "test-key-2");
String value3 = repJobMeta.getAttribute("test-group", "test-key-3");
assertEquals("value", value);
assertEquals("test-value", value1);
assertEquals("test-value", value2);
assertEquals("test-value-3", value3);
}
for (FileObject file : filesList) {
String jobFilename = file.getName().getPath();
System.out.println("Storing/Loading/validating job '" + jobFilename + "'");
// Load the JobMeta object...
//
JobMeta jobMeta = new JobMeta(jobFilename, repository);
if (Utils.isEmpty(jobMeta.getName())) {
jobMeta.setName(Const.createName(file.getName().getBaseName()));
}
// Save it in the repository in the samples folder
//
jobMeta.setRepositoryDirectory(samplesDirectory);
repository.save(jobMeta, "unit testing");
assertNotNull(jobMeta.getObjectId());
// Load it back up again...
//
JobMeta repJobMeta = repository.loadJob(jobMeta.getObjectId(), null);
String oneXml = repJobMeta.getXML();
// Save & load it again
//
repository.save(jobMeta, "unit testing");
repJobMeta = repository.loadJob(jobMeta.getObjectId(), null);
String twoXml = repJobMeta.getXML();
// The XML needs to be identical after loading
//
// storeFile(oneXml, "/tmp/one.ktr");
// storeFile(twoXml, "/tmp/two.ktr");
//
assertEquals(oneXml, twoXml);
}
// Verify the number of stored files, see if we can find them all again.
//
System.out.println("Stored " + files.length + " job samples in folder " + samplesDirectory.getPath());
String[] jobNames = repository.getJobNames(samplesDirectory.getObjectId(), false);
assertEquals(files.length, jobNames.length);
}
Aggregations