use of org.apache.commons.vfs2.VFS in project jackrabbit by apache.
the class VFSDataStore method init.
@Override
public void init(String homeDir) throws RepositoryException {
overridePropertiesFromConfig();
if (baseFolderUri == null) {
throw new RepositoryException("VFS base folder URI must be set.");
}
fileSystemManager = createFileSystemManager();
FileName baseFolderName = null;
try {
baseFolderName = fileSystemManager.resolveURI(baseFolderUri);
FileSystemOptions fso = getFileSystemOptions();
if (fso != null) {
baseFolder = fileSystemManager.resolveFile(baseFolderUri, fso);
} else {
baseFolder = fileSystemManager.resolveFile(baseFolderUri);
}
baseFolder.createFolder();
} catch (FileSystemException e) {
throw new RepositoryException("Could not initialize the VFS base folder at '" + (baseFolderName == null ? "" : baseFolderName.getFriendlyURI()) + "'.", e);
}
super.init(homeDir);
}
use of org.apache.commons.vfs2.VFS in project pentaho-kettle by pentaho.
the class SwtSvgImageUtil method loadFromBasedVFS.
/**
* Internal image loading from Kettle's user.dir VFS.
*/
private static SwtUniversalImage loadFromBasedVFS(Display display, String location) {
try {
FileObject imageFileObject = KettleVFS.getInstance().getFileSystemManager().resolveFile(base, location);
InputStream s = KettleVFS.getInputStream(imageFileObject);
if (s == null) {
return null;
}
try {
return loadImage(display, s, location);
} finally {
IOUtils.closeQuietly(s);
}
} catch (FileSystemException ex) {
return null;
}
}
use of org.apache.commons.vfs2.VFS in project pentaho-kettle by pentaho.
the class JobEntryUnZip method unzipFile.
private boolean unzipFile(FileObject sourceFileObject, String realTargetdirectory, String realWildcard, String realWildcardExclude, Result result, Job parentJob, FileObject movetodir, String realMovetodirectory) {
boolean retval = false;
String unzipToFolder = realTargetdirectory;
try {
if (log.isDetailed()) {
logDetailed(BaseMessages.getString(PKG, "JobUnZip.Log.ProcessingFile", sourceFileObject.toString()));
}
//
if (rootzip) {
String shortSourceFilename = sourceFileObject.getName().getBaseName();
int lenstring = shortSourceFilename.length();
int lastindexOfDot = shortSourceFilename.lastIndexOf('.');
if (lastindexOfDot == -1) {
lastindexOfDot = lenstring;
}
String foldername = realTargetdirectory + "/" + shortSourceFilename.substring(0, lastindexOfDot);
FileObject rootfolder = KettleVFS.getFileObject(foldername, this);
if (!rootfolder.exists()) {
try {
rootfolder.createFolder();
if (log.isDetailed()) {
logDetailed(BaseMessages.getString(PKG, "JobUnZip.Log.RootFolderCreated", foldername));
}
} catch (Exception e) {
throw new Exception(BaseMessages.getString(PKG, "JobUnZip.Error.CanNotCreateRootFolder", foldername), e);
}
}
unzipToFolder = foldername;
}
// Try to read the entries from the VFS object...
//
String zipFilename = "zip:" + sourceFileObject.getName().getFriendlyURI();
FileObject zipFile = KettleVFS.getFileObject(zipFilename, this);
FileObject[] items = zipFile.findFiles(new AllFileSelector() {
public boolean traverseDescendents(FileSelectInfo info) {
return true;
}
public boolean includeFile(FileSelectInfo info) {
// Never return the parent directory of a file list.
if (info.getDepth() == 0) {
return false;
}
FileObject fileObject = info.getFile();
return fileObject != null;
}
});
Pattern pattern = null;
if (!Utils.isEmpty(realWildcard)) {
pattern = Pattern.compile(realWildcard);
}
Pattern patternexclude = null;
if (!Utils.isEmpty(realWildcardExclude)) {
patternexclude = Pattern.compile(realWildcardExclude);
}
for (FileObject item : items) {
if (successConditionBroken) {
if (!successConditionBrokenExit) {
logError(BaseMessages.getString(PKG, "JobUnZip.Error.SuccessConditionbroken", "" + NrErrors));
successConditionBrokenExit = true;
}
return false;
}
synchronized (KettleVFS.getInstance().getFileSystemManager()) {
FileObject newFileObject = null;
try {
if (log.isDetailed()) {
logDetailed(BaseMessages.getString(PKG, "JobUnZip.Log.ProcessingZipEntry", item.getName().getURI(), sourceFileObject.toString()));
}
// get real destination filename
//
String newFileName = unzipToFolder + Const.FILE_SEPARATOR + getTargetFilename(item);
newFileObject = KettleVFS.getFileObject(newFileName, this);
if (item.getType().equals(FileType.FOLDER)) {
//
if (log.isDetailed()) {
logDetailed(BaseMessages.getString(PKG, "JobUnZip.CreatingDirectory.Label", newFileName));
}
//
if (!newFileObject.exists()) {
newFileObject.createFolder();
}
} else {
// File
//
boolean getIt = true;
boolean getItexclude = false;
//
if (pattern != null) {
Matcher matcher = pattern.matcher(item.getName().getURI());
getIt = matcher.matches();
}
if (patternexclude != null) {
Matcher matcherexclude = patternexclude.matcher(item.getName().getURI());
getItexclude = matcherexclude.matches();
}
boolean take = takeThisFile(item, newFileName);
if (getIt && !getItexclude && take) {
if (log.isDetailed()) {
logDetailed(BaseMessages.getString(PKG, "JobUnZip.ExtractingEntry.Label", item.getName().getURI(), newFileName));
}
if (iffileexist == IF_FILE_EXISTS_UNIQ) {
// Create file with unique name
int lenstring = newFileName.length();
int lastindexOfDot = newFileName.lastIndexOf('.');
if (lastindexOfDot == -1) {
lastindexOfDot = lenstring;
}
newFileName = newFileName.substring(0, lastindexOfDot) + StringUtil.getFormattedDateTimeNow(true) + newFileName.substring(lastindexOfDot, lenstring);
if (log.isDebug()) {
logDebug(BaseMessages.getString(PKG, "JobUnZip.Log.CreatingUniqFile", newFileName));
}
}
//
if (!newFileObject.getParent().exists()) {
// creates the whole path.
newFileObject.getParent().createFolder();
}
InputStream is = null;
OutputStream os = null;
try {
is = KettleVFS.getInputStream(item);
os = KettleVFS.getOutputStream(newFileObject, false);
if (is != null) {
byte[] buff = new byte[2048];
int len;
while ((len = is.read(buff)) > 0) {
os.write(buff, 0, len);
}
// Add filename to result filenames
addFilenameToResultFilenames(result, parentJob, newFileName);
}
} finally {
if (is != null) {
is.close();
}
if (os != null) {
os.close();
}
}
}
// end if take
}
} catch (Exception e) {
updateErrors();
logError(BaseMessages.getString(PKG, "JobUnZip.Error.CanNotProcessZipEntry", item.getName().getURI(), sourceFileObject.toString()), e);
} finally {
if (newFileObject != null) {
try {
newFileObject.close();
if (setOriginalModificationDate) {
// Change last modification date
newFileObject.getContent().setLastModifiedTime(item.getContent().getLastModifiedTime());
}
} catch (Exception e) {
/* Ignore */
}
// ignore this
}
// Close file object
// close() does not release resources!
KettleVFS.getInstance().getFileSystemManager().closeFileSystem(item.getFileSystem());
if (items != null) {
items = null;
}
}
}
// Synchronized block on KettleVFS.getInstance().getFileSystemManager()
}
// Unzip done...
if (afterunzip > 0) {
doUnzipPostProcessing(sourceFileObject, movetodir, realMovetodirectory);
}
retval = true;
} catch (Exception e) {
updateErrors();
log.logError(BaseMessages.getString(PKG, "JobUnZip.ErrorUnzip.Label", sourceFileObject.toString(), e.getMessage()), e);
}
return retval;
}
use of org.apache.commons.vfs2.VFS in project pentaho-kettle by pentaho.
the class JobEntryZipFile method execute.
public Result execute(Result previousResult, int nr) {
Result result = previousResult;
List<RowMetaAndData> rows = result.getRows();
// reset values
String realZipfilename;
String realWildcard = null;
String realWildcardExclude = null;
String realTargetdirectory;
String realMovetodirectory = environmentSubstitute(movetoDirectory);
// Set Embedded NamedCluter MetatStore Provider Key so that it can be passed to VFS
if (parentJobMeta.getNamedClusterEmbedManager() != null) {
parentJobMeta.getNamedClusterEmbedManager().passEmbeddedMetastoreKey(this, parentJobMeta.getEmbeddedMetastoreProviderKey());
}
// Sanity check
boolean SanityControlOK = true;
if (afterZip == 2) {
if (Utils.isEmpty(realMovetodirectory)) {
SanityControlOK = false;
logError(BaseMessages.getString(PKG, "JobZipFiles.AfterZip_No_DestinationFolder_Defined.Label"));
} else {
FileObject moveToDirectory = null;
try {
moveToDirectory = KettleVFS.getFileObject(realMovetodirectory, this);
if (moveToDirectory.exists()) {
if (moveToDirectory.getType() == FileType.FOLDER) {
if (log.isDetailed()) {
logDetailed(BaseMessages.getString(PKG, "JobZipFiles.Log.MoveToFolderExist", realMovetodirectory));
}
} else {
SanityControlOK = false;
logError(BaseMessages.getString(PKG, "JobZipFiles.Log.MoveToFolderNotFolder", realMovetodirectory));
}
} else {
if (log.isDetailed()) {
logDetailed(BaseMessages.getString(PKG, "JobZipFiles.Log.MoveToFolderNotNotExist", realMovetodirectory));
}
if (createMoveToDirectory) {
moveToDirectory.createFolder();
if (log.isDetailed()) {
logDetailed(BaseMessages.getString(PKG, "JobZipFiles.Log.MoveToFolderCreaterd", realMovetodirectory));
}
} else {
SanityControlOK = false;
logError(BaseMessages.getString(PKG, "JobZipFiles.Log.MoveToFolderNotNotExist", realMovetodirectory));
}
}
} catch (Exception e) {
SanityControlOK = false;
logError(BaseMessages.getString(PKG, "JobZipFiles.ErrorGettingMoveToFolder.Label", realMovetodirectory), e);
} finally {
if (moveToDirectory != null) {
realMovetodirectory = KettleVFS.getFilename(moveToDirectory);
try {
moveToDirectory.close();
} catch (Exception e) {
logError("Error moving to directory", e);
SanityControlOK = false;
}
}
}
}
}
if (!SanityControlOK) {
return errorResult(result);
}
if (isFromPrevious) {
if (log.isDetailed()) {
logDetailed(BaseMessages.getString(PKG, "JobZipFiles.ArgFromPrevious.Found", (rows != null ? rows.size() : 0) + ""));
}
}
if (isFromPrevious && rows != null) {
try {
for (int iteration = 0; iteration < rows.size() && !parentJob.isStopped(); iteration++) {
// get arguments from previous job entry
RowMetaAndData resultRow = rows.get(iteration);
// get target directory
realTargetdirectory = resultRow.getString(0, null);
if (!Utils.isEmpty(realTargetdirectory)) {
// get wildcard to include
if (!Utils.isEmpty(resultRow.getString(1, null))) {
realWildcard = resultRow.getString(1, null);
}
// get wildcard to exclude
if (!Utils.isEmpty(resultRow.getString(2, null))) {
realWildcardExclude = resultRow.getString(2, null);
}
// get destination zip file
realZipfilename = resultRow.getString(3, null);
if (!Utils.isEmpty(realZipfilename)) {
if (!processRowFile(parentJob, result, realZipfilename, realWildcard, realWildcardExclude, realTargetdirectory, realMovetodirectory, createParentFolder)) {
return errorResult(result);
}
} else {
logError("destination zip filename is empty! Ignoring row...");
}
} else {
logError("Target directory is empty! Ignoring row...");
}
}
} catch (Exception e) {
logError("Erreur during process!", e);
result.setResult(false);
result.setNrErrors(1);
}
} else if (!isFromPrevious) {
if (!Utils.isEmpty(sourceDirectory)) {
// get values from job entry
realZipfilename = getFullFilename(environmentSubstitute(zipFilename), addDate, addTime, specifyFormat, dateTimeFormat);
realWildcard = environmentSubstitute(wildCard);
realWildcardExclude = environmentSubstitute(excludeWildCard);
realTargetdirectory = environmentSubstitute(sourceDirectory);
boolean success = processRowFile(parentJob, result, realZipfilename, realWildcard, realWildcardExclude, realTargetdirectory, realMovetodirectory, createParentFolder);
if (success) {
result.setResult(true);
} else {
errorResult(result);
}
} else {
logError("Source folder/file is empty! Ignoring row...");
}
}
// End
return result;
}
use of org.apache.commons.vfs2.VFS in project pentaho-kettle by pentaho.
the class JobEntryCopyFiles method processFileFolder.
boolean processFileFolder(String sourcefilefoldername, String destinationfilefoldername, String wildcard, Job parentJob, Result result) {
boolean entrystatus = false;
FileObject sourcefilefolder = null;
FileObject destinationfilefolder = null;
// Clear list files to remove after copy process
// This list is also added to result files name
list_files_remove.clear();
list_add_result.clear();
// 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 (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 COPY FOLDER TO FILE !!!
logError(BaseMessages.getString(PKG, "JobCopyFiles.Log.CanNotCopyFolderToFile", KettleVFS.getFriendlyURI(realSourceFilefoldername), KettleVFS.getFriendlyURI(realDestinationFilefoldername)));
NbrFail++;
} else {
if (destinationfilefolder.getType().equals(FileType.FOLDER) && sourcefilefolder.getType().equals(FileType.FILE)) {
// Source is a file, destination is a folder
// Copy the file to the destination folder
destinationfilefolder.copyFrom(sourcefilefolder.getParent(), new TextOneFileSelector(sourcefilefolder.getParent().toString(), sourcefilefolder.getName().getBaseName(), destinationfilefolder.toString()));
if (isDetailed()) {
logDetailed(BaseMessages.getString(PKG, "JobCopyFiles.Log.FileCopied", KettleVFS.getFriendlyURI(sourcefilefolder), KettleVFS.getFriendlyURI(destinationfilefolder)));
}
} else if (sourcefilefolder.getType().equals(FileType.FILE) && destination_is_a_file) {
// Source is a file, destination is a file
destinationfilefolder.copyFrom(sourcefilefolder, new TextOneToOneFileSelector(destinationfilefolder));
} else {
// Both source and destination are folders
if (isDetailed()) {
logDetailed(" ");
logDetailed(BaseMessages.getString(PKG, "JobCopyFiles.Log.FetchFolder", KettleVFS.getFriendlyURI(sourcefilefolder)));
}
TextFileSelector textFileSelector = new TextFileSelector(sourcefilefolder, destinationfilefolder, realWildcard, parentJob);
try {
destinationfilefolder.copyFrom(sourcefilefolder, textFileSelector);
} finally {
textFileSelector.shutdown();
}
}
// Remove Files if needed
if (remove_source_files && !list_files_remove.isEmpty()) {
String sourceFilefoldername = sourcefilefolder.toString();
int trimPathLength = sourceFilefoldername.length() + 1;
FileObject removeFile;
for (Iterator<String> iter = list_files_remove.iterator(); iter.hasNext() && !parentJob.isStopped(); ) {
String fileremoventry = iter.next();
// re=null each iteration
removeFile = null;
// Try to get the file relative to the existing connection
if (fileremoventry.startsWith(sourceFilefoldername)) {
if (trimPathLength < fileremoventry.length()) {
removeFile = sourcefilefolder.getChild(fileremoventry.substring(trimPathLength));
}
}
// Unable to retrieve file through existing connection; Get the file through a new VFS connection
if (removeFile == null) {
removeFile = KettleVFS.getFileObject(fileremoventry, this);
}
// Remove ONLY Files
if (removeFile.getType() == FileType.FILE) {
boolean deletefile = removeFile.delete();
logBasic(" ------ ");
if (!deletefile) {
logError(" " + BaseMessages.getString(PKG, "JobCopyFiles.Error.Exception.CanRemoveFileFolder", KettleVFS.getFriendlyURI(fileremoventry)));
} else {
if (isDetailed()) {
logDetailed(" " + BaseMessages.getString(PKG, "JobCopyFiles.Log.FileFolderRemoved", KettleVFS.getFriendlyURI(fileremoventry)));
}
}
}
}
}
// Add files to result files name
if (add_result_filesname && !list_add_result.isEmpty()) {
String destinationFilefoldername = destinationfilefolder.toString();
int trimPathLength = destinationFilefoldername.length() + 1;
FileObject addFile;
for (Iterator<String> iter = list_add_result.iterator(); iter.hasNext(); ) {
String fileaddentry = iter.next();
// re=null each iteration
addFile = null;
// Try to get the file relative to the existing connection
if (fileaddentry.startsWith(destinationFilefoldername)) {
if (trimPathLength < fileaddentry.length()) {
addFile = destinationfilefolder.getChild(fileaddentry.substring(trimPathLength));
}
}
// Unable to retrieve file through existing connection; Get the file through a new VFS connection
if (addFile == null) {
addFile = KettleVFS.getFileObject(fileaddentry, this);
}
// Add ONLY Files
if (addFile.getType() == FileType.FILE) {
ResultFile resultFile = new ResultFile(ResultFile.FILE_TYPE_GENERAL, addFile, parentJob.getJobname(), toString());
result.getResultFiles().put(resultFile.getFile().toString(), resultFile);
if (isDetailed()) {
logDetailed(" ------ ");
logDetailed(" " + BaseMessages.getString(PKG, "JobCopyFiles.Log.FileAddedToResultFilesName", KettleVFS.getFriendlyURI(fileaddentry)));
}
}
}
}
}
entrystatus = true;
} else {
// Destination Folder or Parent folder is missing
logError(BaseMessages.getString(PKG, "JobCopyFiles.Error.DestinationFolderNotFound", KettleVFS.getFriendlyURI(realDestinationFilefoldername)));
}
} else {
logError(BaseMessages.getString(PKG, "JobCopyFiles.Error.SourceFileNotExists", KettleVFS.getFriendlyURI(realSourceFilefoldername)));
}
} catch (FileSystemException fse) {
logError(BaseMessages.getString(PKG, "JobCopyFiles.Error.Exception.CopyProcessFileSystemException", fse.getMessage()));
Throwable throwable = fse.getCause();
while (throwable != null) {
logError(BaseMessages.getString(PKG, "JobCopyFiles.Log.CausedBy", throwable.getMessage()));
throwable = throwable.getCause();
}
} catch (Exception e) {
logError(BaseMessages.getString(PKG, "JobCopyFiles.Error.Exception.CopyProcess", KettleVFS.getFriendlyURI(realSourceFilefoldername), KettleVFS.getFriendlyURI(realDestinationFilefoldername), e.getMessage()), e);
} finally {
if (sourcefilefolder != null) {
try {
sourcefilefolder.close();
sourcefilefolder = null;
} catch (IOException ex) {
/* Ignore */
}
}
if (destinationfilefolder != null) {
try {
destinationfilefolder.close();
destinationfilefolder = null;
} catch (IOException ex) {
/* Ignore */
}
}
}
return entrystatus;
}
Aggregations