List of usage examples for org.apache.commons.vfs FileObject close
public void close() throws FileSystemException;
From source file:org.pentaho.di.core.row.ValueDataUtil.java
public static Long ChecksumCRC32(ValueMetaInterface metaA, Object dataA) { long checksum = 0; FileObject file = null; try {/*w ww . j a v a2s. c o m*/ file = KettleVFS.getFileObject(dataA.toString()); CheckedInputStream cis = null; // Computer CRC32 checksum cis = new CheckedInputStream(((LocalFile) file).getInputStream(), new CRC32()); byte[] buf = new byte[128]; int readSize = 0; do { readSize = cis.read(buf); } while (readSize >= 0); checksum = cis.getChecksum().getValue(); } catch (Exception e) { // ignore - should likely log the exception } finally { if (file != null) { try { file.close(); file = null; } catch (Exception e) { // Ignore } } } return checksum; }
From source file:org.pentaho.di.core.row.ValueDataUtil.java
public static Long ChecksumAdler32(ValueMetaInterface metaA, Object dataA) { long checksum = 0; FileObject file = null; try {/*w ww.j a v a 2 s . c o m*/ file = KettleVFS.getFileObject(dataA.toString()); CheckedInputStream cis = null; // Computer Adler-32 checksum cis = new CheckedInputStream(((LocalFile) file).getInputStream(), new Adler32()); byte[] buf = new byte[128]; int readSize = 0; do { readSize = cis.read(buf); } while (readSize >= 0); checksum = cis.getChecksum().getValue(); } catch (Exception e) { // throw new Exception(e); } finally { if (file != null) { try { file.close(); file = null; } catch (Exception e) { // Ignore } } } return checksum; }
From source file:org.pentaho.di.core.row.ValueDataUtil.java
public static Object loadFileContentInBinary(ValueMetaInterface metaA, Object dataA) throws KettleValueException { if (dataA == null) { return null; }/* ww w .j a va 2s .co m*/ FileObject file = null; FileInputStream fis = null; try { file = KettleVFS.getFileObject(dataA.toString()); fis = (FileInputStream) ((LocalFile) file).getInputStream(); int fileSize = (int) file.getContent().getSize(); byte[] content = Const.createByteArray(fileSize); fis.read(content, 0, fileSize); return content; } catch (Exception e) { throw new KettleValueException(e); } finally { try { if (fis != null) { fis.close(); } fis = null; if (file != null) { file.close(); } file = null; } catch (Exception e) { // Ignore } } }
From source file:org.pentaho.di.core.row.ValueDataUtil.java
/** * Checks an xml file is well formed.// ww w . j a v a 2s .co m * * @param metaA * The ValueMetaInterface * @param dataA * The value (filename) * @return true if the file is well formed. */ public static boolean isXMLFileWellFormed(ValueMetaInterface metaA, Object dataA) { if (dataA == null) { return false; } String filename = dataA.toString(); FileObject file = null; try { file = KettleVFS.getFileObject(filename); return XMLCheck.isXMLFileWellFormed(file); } catch (Exception e) { // ignore - we'll return false although would be nice to log it. } finally { if (file != null) { try { file.close(); } catch (Exception e) { // Ignore } } } return false; }
From source file:org.pentaho.di.core.vfs.KettleVFS.java
public static boolean fileExists(String vfsFilename, VariableSpace space) throws KettleFileException { FileObject fileObject = null; try {//from ww w.j a v a 2s. c o m fileObject = getFileObject(vfsFilename, space); return fileObject.exists(); } catch (IOException e) { throw new KettleFileException(e); } finally { if (fileObject != null) { try { fileObject.close(); } catch (Exception e) { /* Ignore */ } } } }
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 {//from ww w. j a v a 2 s. c om 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 v a2 s .co m 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 2s .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 w ww . ja v a2 s .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 ww. ja v a 2 s .c om 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; }