List of usage examples for org.apache.commons.vfs FileObject exists
public boolean exists() throws FileSystemException;
From source file:org.pentaho.di.core.vfs.VfsCoreTest.java
public void testWriteReadFile() throws Exception { // Write a text ///*from w w w. j ava2 s . c o m*/ FileObject tempFile = KettleVFS.createTempFile("prefix", "suffix", tmpDir); OutputStream outputStream = KettleVFS.getOutputStream(tempFile, false); OutputStreamWriter writer = new OutputStreamWriter(outputStream); writer.write(content); writer.close(); outputStream.close(); // Read it back... // InputStream inputStream = KettleVFS.getInputStream(tempFile); StringBuffer buffer = new StringBuffer(); int c; while ((c = inputStream.read()) >= 0) { buffer.append((char) c); } inputStream.close(); assertEquals(content, buffer.toString()); // Now open the data as a regular file... // String url = tempFile.getName().getURI(); String textFileContent = KettleVFS.getTextFileContent(url, Const.XML_ENCODING); // Now delete the file... // tempFile.delete(); assertEquals(false, tempFile.exists()); assertEquals(content, textFileContent); }
From source file:org.pentaho.di.job.entries.addresultfilenames.JobEntryAddResultFilenames.java
private boolean processFile(String filename, String wildcard, Job parentJob, Result result) { boolean rcode = true; FileObject filefolder = null; String realFilefoldername = environmentSubstitute(filename); String realwildcard = environmentSubstitute(wildcard); try {// w w w.j a v a2s. com filefolder = KettleVFS.getFileObject(realFilefoldername, this); if (filefolder.exists()) { // the file or folder exists if (filefolder.getType() == FileType.FILE) { // Add filename to Resultfilenames ... if (log.isDetailed()) { logDetailed(BaseMessages.getString(PKG, "JobEntryAddResultFilenames.AddingFileToResult", filefolder.toString())); } ResultFile resultFile = new ResultFile(ResultFile.FILE_TYPE_GENERAL, KettleVFS.getFileObject(filefolder.toString(), this), parentJob.getJobname(), toString()); result.getResultFiles().put(resultFile.getFile().toString(), resultFile); } else { FileObject[] list = filefolder .findFiles(new TextFileSelector(filefolder.toString(), realwildcard)); for (int i = 0; i < list.length && !parentJob.isStopped(); i++) { // Add filename to Resultfilenames ... if (log.isDetailed()) { logDetailed(BaseMessages.getString(PKG, "JobEntryAddResultFilenames.AddingFileToResult", list[i].toString())); } ResultFile resultFile = new ResultFile(ResultFile.FILE_TYPE_GENERAL, KettleVFS.getFileObject(list[i].toString(), this), parentJob.getJobname(), toString()); result.getResultFiles().put(resultFile.getFile().toString(), resultFile); } } } else { // File can not be found if (log.isBasic()) { logBasic(BaseMessages.getString(PKG, "JobEntryAddResultFilenames.FileCanNotbeFound", realFilefoldername)); } rcode = false; } } catch (Exception e) { rcode = false; logError(BaseMessages.getString(PKG, "JobEntryAddResultFilenames.CouldNotProcess", realFilefoldername, e.getMessage()), e); } finally { if (filefolder != null) { try { filefolder.close(); filefolder = null; } catch (IOException ex) { // Ignore } } } return rcode; }
From source file:org.pentaho.di.job.entries.checkfilelocked.JobEntryCheckFilesLocked.java
private void ProcessFile(String filename, String wildcard) { FileObject filefolder = null; String realFilefoldername = environmentSubstitute(filename); String realwilcard = environmentSubstitute(wildcard); try {/* w w w. j a va 2 s.c om*/ filefolder = KettleVFS.getFileObject(realFilefoldername); FileObject[] files = new FileObject[] { filefolder }; if (filefolder.exists()) { // the file or folder exists if (filefolder.getType() == FileType.FOLDER) { // It's a folder if (isDetailed()) { logDetailed(BaseMessages.getString(PKG, "JobEntryCheckFilesLocked.ProcessingFolder", realFilefoldername)); } // Retrieve all files files = filefolder.findFiles(new TextFileSelector(filefolder.toString(), realwilcard)); if (isDetailed()) { logDetailed(BaseMessages.getString(PKG, "JobEntryCheckFilesLocked.TotalFilesToCheck", String.valueOf(files.length))); } } else { // It's a file if (isDetailed()) { logDetailed(BaseMessages.getString(PKG, "JobEntryCheckFilesLocked.ProcessingFile", realFilefoldername)); } } // Check files locked checkFilesLocked(files); } else { // We can not find thsi file logBasic(BaseMessages.getString(PKG, "JobEntryCheckFilesLocked.FileNotExist", realFilefoldername)); } } catch (Exception e) { logError(BaseMessages.getString(PKG, "JobEntryCheckFilesLocked.CouldNotProcess", realFilefoldername, e.getMessage())); } finally { if (filefolder != null) { try { filefolder.close(); } catch (IOException ex) { // Ignore } } } }
From source file:org.pentaho.di.job.entries.checkfilelocked.LockFile.java
/** * Checks if a file is locked In order to check is a file is locked we will use a dummy renaming exercise * * @param filename/*from w w w . j a v a 2 s . c o m*/ * @throws KettleException */ public LockFile(String filename) throws KettleException { setFilename(filename); setLocked(false); // In order to check is a file is locked // we will use a dummy renaming exercise FileObject file = null; FileObject dummyfile = null; try { file = KettleVFS.getFileObject(filename); if (file.exists()) { dummyfile = KettleVFS.getFileObject(filename); // move file to itself! file.moveTo(dummyfile); } } catch (Exception e) { // We got an exception // The is locked by another process setLocked(true); } finally { if (file != null) { try { file.close(); } catch (Exception e) { /* Ignore */ } } if (dummyfile != null) { try { file.close(); } catch (Exception e) { /* Ignore */ } } } }
From source file:org.pentaho.di.job.entries.copyfiles.JobEntryCopyFiles.java
private 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();//from ww w .j av a 2s . c o m 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()) { // Check if destination folder/parent folder exists ! // If user wanted and if destination folder does not exist // 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", realSourceFilefoldername, 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", sourcefilefolder.getName().toString(), destinationfilefolder.getName().toString())); } } 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", sourcefilefolder.toString())); } 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(); removeFile = null; // re=null each iteration // 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", fileremoventry)); } else { if (isDetailed()) { logDetailed(" " + BaseMessages.getString(PKG, "JobCopyFiles.Log.FileFolderRemoved", 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(); addFile = null; // re=null each iteration // 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", fileaddentry)); } } } } } entrystatus = true; } else { // Destination Folder or Parent folder is missing logError(BaseMessages.getString(PKG, "JobCopyFiles.Error.DestinationFolderNotFound", realDestinationFilefoldername)); } } else { logError(BaseMessages.getString(PKG, "JobCopyFiles.Error.SourceFileNotExists", 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", realSourceFilefoldername, 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; }
From source file:org.pentaho.di.job.entries.copyfiles.JobEntryCopyFiles.java
private boolean CreateDestinationFolder(FileObject filefolder) { FileObject folder = null; try {/*from w w w . ja v a2 s . c o m*/ if (destination_is_a_file) { folder = filefolder.getParent(); } else { folder = filefolder; } if (!folder.exists()) { if (create_destination_folder) { if (isDetailed()) { logDetailed("Folder " + folder.getName() + " does not exist !"); } folder.createFolder(); if (isDetailed()) { logDetailed("Folder parent was created."); } } else { logError("Folder " + folder.getName() + " does not exist !"); return false; } } return true; } catch (Exception e) { logError("Couldn't created parent folder " + folder.getName(), e); } finally { if (folder != null) { try { folder.close(); folder = null; } catch (Exception ex) { /* Ignore */ } } } return false; }
From source file:org.pentaho.di.job.entries.copymoveresultfilenames.JobEntryCopyMoveResultFilenames.java
public Result execute(Result previousResult, int nr) { Result result = previousResult; result.setNrErrors(1);/*from ww w. j a v a 2s . c o m*/ result.setResult(false); boolean deleteFile = getAction().equals("delete"); String realdestinationFolder = null; if (!deleteFile) { realdestinationFolder = environmentSubstitute(getDestinationFolder()); if (!CreateDestinationFolder(realdestinationFolder)) { return result; } } if (!Const.isEmpty(wildcard)) { wildcardPattern = Pattern.compile(environmentSubstitute(wildcard)); } if (!Const.isEmpty(wildcardexclude)) { wildcardExcludePattern = Pattern.compile(environmentSubstitute(wildcardexclude)); } if (previousResult != null) { NrErrors = 0; limitFiles = Const.toInt(environmentSubstitute(getNrErrorsLessThan()), 10); NrErrors = 0; NrSuccess = 0; successConditionBroken = false; successConditionBrokenExit = false; FileObject file = null; try { int size = result.getResultFiles().size(); if (log.isBasic()) { logBasic(BaseMessages.getString(PKG, "JobEntryCopyMoveResultFilenames.log.FilesFound", "" + size)); } List<ResultFile> resultFiles = result.getResultFilesList(); if (resultFiles != null && resultFiles.size() > 0) { for (Iterator<ResultFile> it = resultFiles.iterator(); it.hasNext() && !parentJob.isStopped();) { if (successConditionBroken) { logError(BaseMessages.getString(PKG, "JobEntryCopyMoveResultFilenames.Error.SuccessConditionbroken", "" + NrErrors)); throw new Exception(BaseMessages.getString(PKG, "JobEntryCopyMoveResultFilenames.Error.SuccessConditionbroken", "" + NrErrors)); } ResultFile resultFile = it.next(); file = resultFile.getFile(); if (file != null && file.exists()) { if (!specifywildcard || (CheckFileWildcard(file.getName().getBaseName(), wildcardPattern, true) && !CheckFileWildcard(file.getName().getBaseName(), wildcardExcludePattern, false) && specifywildcard)) { // Copy or Move file if (!processFile(file, realdestinationFolder, result, parentJob, deleteFile)) { // Update Errors updateErrors(); } } } else { logError(BaseMessages.getString(PKG, "JobEntryCopyMoveResultFilenames.log.ErrorCanNotFindFile", file.toString())); // Update Errors updateErrors(); } } // end for } } catch (Exception e) { logError(BaseMessages.getString(PKG, "JobEntryCopyMoveResultFilenames.Error", e.toString())); } finally { if (file != null) { try { file.close(); file = null; } catch (Exception ex) { /* Ignore */ } } } } // Success Condition result.setNrErrors(NrErrors); result.setNrLinesWritten(NrSuccess); if (getSuccessStatus()) { result.setResult(true); } return result; }
From source file:org.pentaho.di.job.entries.copymoveresultfilenames.JobEntryCopyMoveResultFilenames.java
private boolean CreateDestinationFolder(String foldername) { FileObject folder = null; try {/*from w ww . jav a2s .c o m*/ folder = KettleVFS.getFileObject(foldername, this); if (!folder.exists()) { logError(BaseMessages.getString(PKG, "JobEntryCopyMoveResultFilenames.Log.FolderNotExists", foldername)); if (isCreateDestinationFolder()) { folder.createFolder(); } else { return false; } if (log.isBasic()) { logBasic(BaseMessages.getString(PKG, "JobEntryCopyMoveResultFilenames.Log.FolderCreated", foldername)); } } else { if (log.isDetailed()) { logDetailed(BaseMessages.getString(PKG, "JobEntryCopyMoveResultFilenames.Log.FolderExists", foldername)); } } return true; } catch (Exception e) { logError(BaseMessages.getString(PKG, "JobEntryCopyMoveResultFilenames.Log.CanNotCreatedFolder", foldername, e.toString())); } finally { if (folder != null) { try { folder.close(); folder = null; } catch (Exception ex) { /* Ignore */ } } } return false; }
From source file:org.pentaho.di.job.entries.copymoveresultfilenames.JobEntryCopyMoveResultFilenames.java
private boolean processFile(FileObject sourcefile, String destinationFolder, Result result, Job parentJob, boolean deleteFile) { boolean retval = false; try {/*from w w w . java2s . co m*/ if (deleteFile) { // delete file if (sourcefile.delete()) { if (log.isDetailed()) { logDetailed(BaseMessages.getString(PKG, "JobEntryCopyMoveResultFilenames.log.DeletedFile", sourcefile.toString())); } // Remove source file from result files list result.getResultFiles().remove(sourcefile.toString()); if (log.isDetailed()) { logDetailed(BaseMessages.getString(PKG, "JobEntryCopyMoveResultFilenames.RemovedFileFromResult", sourcefile.toString())); } } else { logError(BaseMessages.getString(PKG, "JobEntryCopyMoveResultFilenames.CanNotDeletedFile", sourcefile.toString())); } } else { // return destination short filename String shortfilename = getDestinationFilename(sourcefile.getName().getBaseName()); // build full destination filename String destinationFilename = destinationFolder + Const.FILE_SEPARATOR + shortfilename; FileObject destinationfile = KettleVFS.getFileObject(destinationFilename, this); boolean filexists = destinationfile.exists(); if (filexists) { if (log.isDetailed()) { logDetailed(BaseMessages.getString(PKG, "JobEntryCopyMoveResultFilenames.Log.FileExists", destinationFilename)); } } if ((!filexists) || (filexists && isOverwriteFile())) { if (getAction().equals("copy")) { // Copy file FileUtil.copyContent(sourcefile, destinationfile); if (log.isDetailed()) { logDetailed( BaseMessages.getString(PKG, "JobEntryCopyMoveResultFilenames.log.CopiedFile", sourcefile.toString(), destinationFolder)); } } else { // Move file sourcefile.moveTo(destinationfile); if (log.isDetailed()) { logDetailed(BaseMessages.getString(PKG, "JobEntryCopyMoveResultFilenames.log.MovedFile", sourcefile.toString(), destinationFolder)); } } if (isRemovedSourceFilename()) { // Remove source file from result files list result.getResultFiles().remove(sourcefile.toString()); if (log.isDetailed()) { logDetailed(BaseMessages.getString(PKG, "JobEntryCopyMoveResultFilenames.RemovedFileFromResult", sourcefile.toString())); } } if (isAddDestinationFilename()) { // Add destination filename to Resultfilenames ... ResultFile resultFile = new ResultFile(ResultFile.FILE_TYPE_GENERAL, KettleVFS.getFileObject(destinationfile.toString(), this), parentJob.getJobname(), toString()); result.getResultFiles().put(resultFile.getFile().toString(), resultFile); if (log.isDetailed()) { logDetailed( BaseMessages.getString(PKG, "JobEntryCopyMoveResultFilenames.AddedFileToResult", destinationfile.toString())); } } } } retval = true; } catch (Exception e) { logError(BaseMessages.getString(PKG, "JobEntryCopyMoveResultFilenames.Log.ErrorProcessing", e.toString())); } return retval; }
From source file:org.pentaho.di.job.entries.createfile.JobEntryCreateFile.java
public Result execute(Result previousResult, int nr) throws KettleException { Result result = previousResult; result.setResult(false);//from w w w .j a v a2s. c o m if (filename != null) { String realFilename = getRealFilename(); FileObject fileObject = null; try { fileObject = KettleVFS.getFileObject(realFilename, this); if (fileObject.exists()) { if (isFailIfFileExists()) { // File exists and fail flag is on. result.setResult(false); logError("File [" + realFilename + "] exists, failing."); } else { // File already exists, no reason to try to create it result.setResult(true); logBasic("File [" + realFilename + "] already exists, not recreating."); } // add filename to result filenames if needed if (isAddFilenameToResult()) { addFilenameToResult(realFilename, result, parentJob); } } else { // No file yet, create an empty file. fileObject.createFile(); logBasic("File [" + realFilename + "] created!"); // add filename to result filenames if needed if (isAddFilenameToResult()) { addFilenameToResult(realFilename, result, parentJob); } result.setResult(true); } } catch (IOException e) { logError("Could not create file [" + realFilename + "], exception: " + e.getMessage()); result.setResult(false); result.setNrErrors(1); } finally { if (fileObject != null) { try { fileObject.close(); fileObject = null; } catch (IOException ex) { // Ignore } } } } else { logError("No filename is defined."); } return result; }