List of usage examples for org.apache.commons.vfs FileObject close
public void close() throws FileSystemException;
From source file:org.pentaho.di.job.entries.waitforfile.JobEntryWaitForFile.java
public Result execute(Result previousResult, int nr) { Result result = previousResult; result.setResult(false);/* w w w .j a va 2 s. c o m*/ // starttime (in seconds) long timeStart = System.currentTimeMillis() / 1000; if (filename != null) { FileObject fileObject = null; String realFilename = getRealFilename(); try { fileObject = KettleVFS.getFileObject(realFilename, this); long iMaximumTimeout = Const.toInt(getRealMaximumTimeout(), Const.toInt(DEFAULT_MAXIMUM_TIMEOUT, 0)); long iCycleTime = Const.toInt(getRealCheckCycleTime(), Const.toInt(DEFAULT_CHECK_CYCLE_TIME, 0)); // // Sanity check on some values, and complain on insanity // if (iMaximumTimeout < 0) { iMaximumTimeout = Const.toInt(DEFAULT_MAXIMUM_TIMEOUT, 0); if (log.isBasic()) { logBasic("Maximum timeout invalid, reset to " + iMaximumTimeout); } } if (iCycleTime < 1) { // If lower than 1 set to the default iCycleTime = Const.toInt(DEFAULT_CHECK_CYCLE_TIME, 1); if (log.isBasic()) { logBasic("Check cycle time invalid, reset to " + iCycleTime); } } if (iMaximumTimeout == 0) { if (log.isBasic()) { logBasic("Waiting indefinitely for file [" + realFilename + "]"); } } else { if (log.isBasic()) { logBasic("Waiting " + iMaximumTimeout + " seconds for file [" + realFilename + "]"); } } boolean continueLoop = true; while (continueLoop && !parentJob.isStopped()) { fileObject = KettleVFS.getFileObject(realFilename, this); if (fileObject.exists()) { // file exists, we're happy to exit if (log.isBasic()) { logBasic("Detected file [" + realFilename + "] within timeout"); } result.setResult(true); continueLoop = false; // add filename to result filenames if (addFilenameToResult && fileObject.getType() == FileType.FILE) { ResultFile resultFile = new ResultFile(ResultFile.FILE_TYPE_GENERAL, fileObject, parentJob.getJobname(), toString()); resultFile.setComment(BaseMessages.getString(PKG, "JobWaitForFile.FilenameAdded")); result.getResultFiles().put(resultFile.getFile().toString(), resultFile); } } else { long now = System.currentTimeMillis() / 1000; if ((iMaximumTimeout > 0) && (now > (timeStart + iMaximumTimeout))) { continueLoop = false; // file doesn't exist after timeout, either true or false if (isSuccessOnTimeout()) { if (log.isBasic()) { logBasic("Didn't detect file [" + realFilename + "] before timeout, success"); } result.setResult(true); } else { if (log.isBasic()) { logBasic("Didn't detect file [" + realFilename + "] before timeout, failure"); } result.setResult(false); } } // sleep algorithm long sleepTime = 0; if (iMaximumTimeout == 0) { sleepTime = iCycleTime; } else { if ((now + iCycleTime) < (timeStart + iMaximumTimeout)) { sleepTime = iCycleTime; } else { sleepTime = iCycleTime - ((now + iCycleTime) - (timeStart + iMaximumTimeout)); } } try { if (sleepTime > 0) { if (log.isDetailed()) { logDetailed("Sleeping " + sleepTime + " seconds before next check for file [" + realFilename + "]"); } Thread.sleep(sleepTime * 1000); } } catch (InterruptedException e) { // something strange happened result.setResult(false); continueLoop = false; } } } if (!parentJob.isStopped() && fileObject.exists() && isFileSizeCheck()) { long oldSize = -1; long newSize = fileObject.getContent().getSize(); if (log.isDetailed()) { logDetailed("File [" + realFilename + "] is " + newSize + " bytes long"); } if (log.isBasic()) { logBasic("Waiting until file [" + realFilename + "] stops growing for " + iCycleTime + " seconds"); } while (oldSize != newSize && !parentJob.isStopped()) { try { if (log.isDetailed()) { logDetailed("Sleeping " + iCycleTime + " seconds, waiting for file [" + realFilename + "] to stop growing"); } Thread.sleep(iCycleTime * 1000); } catch (InterruptedException e) { // something strange happened result.setResult(false); continueLoop = false; } oldSize = newSize; newSize = fileObject.getContent().getSize(); if (log.isDetailed()) { logDetailed("File [" + realFilename + "] is " + newSize + " bytes long"); } } if (log.isBasic()) { logBasic("Stopped waiting for file [" + realFilename + "] to stop growing"); } } if (parentJob.isStopped()) { result.setResult(false); } } catch (Exception e) { logBasic("Exception while waiting for file [" + realFilename + "] to stop growing", e); } finally { if (fileObject != null) { try { fileObject.close(); } catch (Exception e) { // Ignore errors } } } } else { logError("No filename is defined."); } return result; }
From source file:org.pentaho.di.job.entries.writetofile.JobEntryWriteToFile.java
private void createParentFolder(String realFilename) throws KettleException { FileObject parent = null; try {//from w w w . jav a 2s.c om parent = KettleVFS.getFileObject(realFilename).getParent(); if (!parent.exists()) { if (isCreateParentFolder()) { if (isDetailed()) { logDetailed(BaseMessages.getString(PKG, "JobWriteToFile.Log.ParentFoldetNotExist", parent.getName().toString())); } parent.createFolder(); if (isDetailed()) { logDetailed(BaseMessages.getString(PKG, "JobWriteToFile.Log.ParentFolderCreated", parent.getName().toString())); } } else { throw new KettleException(BaseMessages.getString(PKG, "JobWriteToFile.Log.ParentFoldetNotExist", parent.getName().toString())); } } } catch (Exception e) { throw new KettleException( BaseMessages.getString(PKG, "JobWriteToFile.Error.CheckingParentFolder", realFilename), e); } finally { if (parent != null) { try { parent.close(); } catch (Exception e) { /* Ignore */ } } } }
From source file:org.pentaho.di.job.entries.xmlwellformed.JobEntryXMLWellFormed.java
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 (Const.isEmpty(realSourceFilefoldername)) { logError(BaseMessages.getString(PKG, "JobXMLWellFormed.log.FileFolderEmpty", sourcefilefoldername)); // Update Errors updateErrors();/* www . j a v a 2 s.co m*/ 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; }
From source file:org.pentaho.di.job.entries.xsdvalidator.JobEntryXSDValidator.java
public Result execute(Result previousResult, int nr) { Result result = previousResult; result.setResult(false);/* w w w. j av a 2 s .c o m*/ String realxmlfilename = getRealxmlfilename(); String realxsdfilename = getRealxsdfilename(); FileObject xmlfile = null; FileObject xsdfile = null; try { if (xmlfilename != null && xsdfilename != null) { xmlfile = KettleVFS.getFileObject(realxmlfilename, this); xsdfile = KettleVFS.getFileObject(realxsdfilename, this); if (xmlfile.exists() && xsdfile.exists()) { SchemaFactory factorytXSDValidator_1 = SchemaFactory .newInstance("http://www.w3.org/2001/XMLSchema"); // Get XSD File File XSDFile = new File(KettleVFS.getFilename(xsdfile)); Schema SchematXSD = factorytXSDValidator_1.newSchema(XSDFile); Validator XSDValidator = SchematXSD.newValidator(); // Get XML File File xmlfiletXSDValidator_1 = new File(KettleVFS.getFilename(xmlfile)); Source sourcetXSDValidator_1 = new StreamSource(xmlfiletXSDValidator_1); XSDValidator.validate(sourcetXSDValidator_1); // Everything is OK result.setResult(true); } else { if (!xmlfile.exists()) { logError(BaseMessages.getString(PKG, "JobEntryXSDValidator.FileDoesNotExist1.Label") + realxmlfilename + BaseMessages.getString(PKG, "JobEntryXSDValidator.FileDoesNotExist2.Label")); } if (!xsdfile.exists()) { logError(BaseMessages.getString(PKG, "JobEntryXSDValidator.FileDoesNotExist1.Label") + realxsdfilename + BaseMessages.getString(PKG, "JobEntryXSDValidator.FileDoesNotExist2.Label")); } result.setResult(false); result.setNrErrors(1); } } else { logError(BaseMessages.getString(PKG, "JobEntryXSDValidator.AllFilesNotNull.Label")); result.setResult(false); result.setNrErrors(1); } } catch (SAXException ex) { logError("Error :" + ex.getMessage()); } catch (Exception e) { logError(BaseMessages.getString(PKG, "JobEntryXSDValidator.ErrorXSDValidator.Label") + BaseMessages.getString(PKG, "JobEntryXSDValidator.ErrorXML1.Label") + realxmlfilename + BaseMessages.getString(PKG, "JobEntryXSDValidator.ErrorXML2.Label") + BaseMessages.getString(PKG, "JobEntryXSDValidator.ErrorXSD1.Label") + realxsdfilename + BaseMessages.getString(PKG, "JobEntryXSDValidator.ErrorXSD2.Label") + e.getMessage()); result.setResult(false); result.setNrErrors(1); } finally { try { if (xmlfile != null) { xmlfile.close(); } if (xsdfile != null) { xsdfile.close(); } } catch (IOException e) { // Ignore errors } } return result; }
From source file:org.pentaho.di.job.entries.xslt.JobEntryXSLT.java
private boolean processOneXMLFile(String xmlfilename, String xslfilename, String outputfilename, Result result, Job parentJob) {// w w w. j a v a 2 s .c o m boolean retval = false; FileObject xmlfile = null; FileObject xslfile = null; FileObject outputfile = null; try { xmlfile = KettleVFS.getFileObject(xmlfilename, this); xslfile = KettleVFS.getFileObject(xslfilename, this); outputfile = KettleVFS.getFileObject(outputfilename, this); if (xmlfile.exists() && xslfile.exists()) { if (outputfile.exists() && iffileexists == 2) { // Output file exists // User want to fail logError(BaseMessages.getString(PKG, "JobEntryXSLT.OuputFileExists1.Label") + outputfilename + BaseMessages.getString(PKG, "JobEntryXSLT.OuputFileExists2.Label")); return retval; } else if (outputfile.exists() && iffileexists == 1) { // Do nothing if (log.isDebug()) { logDebug(BaseMessages.getString(PKG, "JobEntryXSLT.OuputFileExists1.Label") + outputfilename + BaseMessages.getString(PKG, "JobEntryXSLT.OuputFileExists2.Label")); } retval = true; return retval; } else { if (outputfile.exists() && iffileexists == 0) { // the output file exists and user want to create new one with unique name // Format Date // Try to clean filename (without wildcard) String wildcard = outputfilename.substring(outputfilename.length() - 4, outputfilename.length()); if (wildcard.substring(0, 1).equals(".")) { // Find wildcard outputfilename = outputfilename.substring(0, outputfilename.length() - 4) + "_" + StringUtil.getFormattedDateTimeNow(true) + wildcard; } else { // did not find wildcard outputfilename = outputfilename + "_" + StringUtil.getFormattedDateTimeNow(true); } if (log.isDebug()) { logDebug(BaseMessages.getString(PKG, "JobEntryXSLT.OuputFileExists1.Label") + outputfilename + BaseMessages.getString(PKG, "JobEntryXSLT.OuputFileExists2.Label")); logDebug(BaseMessages.getString(PKG, "JobEntryXSLT.OuputFileNameChange1.Label") + outputfilename + BaseMessages.getString(PKG, "JobEntryXSLT.OuputFileNameChange2.Label")); } } // Create transformer factory TransformerFactory factory = TransformerFactory.newInstance(); if (xsltfactory.equals(FACTORY_SAXON)) { // Set the TransformerFactory to the SAXON implementation. factory = new net.sf.saxon.TransformerFactoryImpl(); } if (log.isDetailed()) { log.logDetailed(BaseMessages.getString(PKG, "JobEntryXSL.Log.TransformerFactoryInfos"), BaseMessages.getString(PKG, "JobEntryXSL.Log.TransformerFactory", factory.getClass().getName())); } InputStream xslInputStream = KettleVFS.getInputStream(xslfile); InputStream xmlInputStream = KettleVFS.getInputStream(xmlfile); OutputStream os = null; try { // Use the factory to create a template containing the xsl file Templates template = factory.newTemplates(new StreamSource(xslInputStream)); // Use the template to create a transformer Transformer xformer = template.newTransformer(); if (log.isDetailed()) { log.logDetailed(BaseMessages.getString(PKG, "JobEntryXSL.Log.TransformerClassInfos"), BaseMessages.getString(PKG, "JobEntryXSL.Log.TransformerClass", xformer.getClass().getName())); } // Do we need to set output properties? if (setOutputProperties) { xformer.setOutputProperties(outputProperties); } // Do we need to pass parameters? if (useParameters) { for (int i = 0; i < nrParams; i++) { xformer.setParameter(nameOfParams[i], valueOfParams[i]); } } // Prepare the input and output files Source source = new StreamSource(xmlInputStream); os = KettleVFS.getOutputStream(outputfile, false); StreamResult resultat = new StreamResult(os); // Apply the xsl file to the source file and write the result to the output file xformer.transform(source, resultat); if (isAddFileToResult()) { // Add output filename to output files ResultFile resultFile = new ResultFile(ResultFile.FILE_TYPE_GENERAL, KettleVFS.getFileObject(outputfilename, this), parentJob.getJobname(), toString()); result.getResultFiles().put(resultFile.getFile().toString(), resultFile); } // Everything is OK retval = true; } finally { try { xslInputStream.close(); } catch (IOException ignored) { // ignore IO Exception on close } try { xmlInputStream.close(); } catch (IOException ignored) { // ignore IO Exception on close } try { if (os != null) { os.close(); } } catch (IOException ignored) { // ignore IO Exception on close } } } } else { if (!xmlfile.exists()) { logError(BaseMessages.getString(PKG, "JobEntryXSLT.FileDoesNotExist1.Label") + xmlfilename + BaseMessages.getString(PKG, "JobEntryXSLT.FileDoesNotExist2.Label")); } if (!xslfile.exists()) { logError(BaseMessages.getString(PKG, "JobEntryXSLT.FileDoesNotExist1.Label") + xmlfilename + BaseMessages.getString(PKG, "JobEntryXSLT.FileDoesNotExist2.Label")); } } } catch (Exception e) { logError(BaseMessages.getString(PKG, "JobEntryXSLT.ErrorXLST.Label") + BaseMessages.getString(PKG, "JobEntryXSLT.ErrorXLSTXML1.Label") + xmlfilename + BaseMessages.getString(PKG, "JobEntryXSLT.ErrorXLSTXML2.Label") + BaseMessages.getString(PKG, "JobEntryXSLT.ErrorXLSTXSL1.Label") + xslfilename + BaseMessages.getString(PKG, "JobEntryXSLT.ErrorXLSTXSL2.Label") + e.getMessage()); } finally { try { if (xmlfile != null) { xmlfile.close(); } if (xslfile != null) { xslfile.close(); } if (outputfile != null) { outputfile.close(); } } catch (IOException e) { logError("Unable to close file", e); } } return retval; }
From source file:org.pentaho.di.job.entries.zipfile.JobEntryZipFile.java
private boolean createParentFolder(String filename) { // Check for parent folder FileObject parentfolder = null; boolean result = false; try {//from w ww . jav a2s . co m // Get parent folder parentfolder = KettleVFS.getFileObject(filename, this).getParent(); if (!parentfolder.exists()) { if (log.isDetailed()) { logDetailed(BaseMessages.getString(PKG, "JobEntryZipFile.CanNotFindFolder", "" + parentfolder.getName())); } parentfolder.createFolder(); if (log.isDetailed()) { logDetailed(BaseMessages.getString(PKG, "JobEntryZipFile.FolderCreated", "" + parentfolder.getName())); } } else { if (log.isDetailed()) { logDetailed(BaseMessages.getString(PKG, "JobEntryZipFile.FolderExists", "" + parentfolder.getName())); } } result = true; } catch (Exception e) { logError(BaseMessages.getString(PKG, "JobEntryZipFile.CanNotCreateFolder", "" + parentfolder.getName()), e); } finally { if (parentfolder != null) { try { parentfolder.close(); parentfolder = null; } catch (Exception ex) { // Ignore } } } return result; }
From source file:org.pentaho.di.job.entries.zipfile.JobEntryZipFile.java
public boolean processRowFile(Job parentJob, Result result, String realZipfilename, String realWildcard, String realWildcardExclude, String realSourceDirectoryOrFile, String realMovetodirectory, boolean createparentfolder) { boolean Fileexists = false; File tempFile = null;/*from ww w. j a v a 2 s . c o m*/ File fileZip = null; boolean resultat = false; boolean renameOk = false; boolean orginExist = false; // Check if target file/folder exists! FileObject originFile = null; ZipInputStream zin = null; byte[] buffer = null; OutputStream dest = null; BufferedOutputStream buff = null; ZipOutputStream out = null; ZipEntry entry = null; String localSourceFilename = realSourceDirectoryOrFile; try { originFile = KettleVFS.getFileObject(realSourceDirectoryOrFile, this); localSourceFilename = KettleVFS.getFilename(originFile); orginExist = originFile.exists(); } catch (Exception e) { // Ignore errors } finally { if (originFile != null) { try { originFile.close(); } catch (IOException ex) { logError("Error closing file '" + originFile.toString() + "'", ex); } } } String localrealZipfilename = realZipfilename; if (realZipfilename != null && orginExist) { FileObject fileObject = null; try { fileObject = KettleVFS.getFileObject(localrealZipfilename, this); localrealZipfilename = KettleVFS.getFilename(fileObject); // Check if Zip File exists if (fileObject.exists()) { Fileexists = true; if (log.isDebug()) { logDebug(BaseMessages.getString(PKG, "JobZipFiles.Zip_FileExists1.Label") + localrealZipfilename + BaseMessages.getString(PKG, "JobZipFiles.Zip_FileExists2.Label")); } } // Let's see if we need to create parent folder of destination zip filename if (createparentfolder) { createParentFolder(localrealZipfilename); } // Let's start the process now if (ifZipFileExists == 3 && Fileexists) { // the zip file exists and user want to Fail resultat = false; } else if (ifZipFileExists == 2 && Fileexists) { // the zip file exists and user want to do nothing if (addFileToResult) { // Add file to result files name ResultFile resultFile = new ResultFile(ResultFile.FILE_TYPE_GENERAL, fileObject, parentJob.getJobname(), toString()); result.getResultFiles().put(resultFile.getFile().toString(), resultFile); } resultat = true; } else if (afterZip == 2 && realMovetodirectory == null) { // After Zip, Move files..User must give a destination Folder resultat = false; logError( BaseMessages.getString(PKG, "JobZipFiles.AfterZip_No_DestinationFolder_Defined.Label")); } else { // After Zip, Move files..User must give a destination Folder // Let's see if we deal with file or folder FileObject[] fileList = null; FileObject sourceFileOrFolder = KettleVFS.getFileObject(localSourceFilename); boolean isSourceDirectory = sourceFileOrFolder.getType().equals(FileType.FOLDER); final Pattern pattern; final Pattern patternexclude; if (isSourceDirectory) { // Let's prepare the pattern matcher for performance reasons. // We only do this if the target is a folder ! // if (!Const.isEmpty(realWildcard)) { pattern = Pattern.compile(realWildcard); } else { pattern = null; } if (!Const.isEmpty(realWildcardExclude)) { patternexclude = Pattern.compile(realWildcardExclude); } else { patternexclude = null; } // Target is a directory // Get all the files in the directory... // if (includingSubFolders) { fileList = sourceFileOrFolder.findFiles(new FileSelector() { public boolean traverseDescendents(FileSelectInfo fileInfo) throws Exception { return true; } public boolean includeFile(FileSelectInfo fileInfo) throws Exception { boolean include; // Only include files in the sub-folders... // When we include sub-folders we match the whole filename, not just the base-name // if (fileInfo.getFile().getType().equals(FileType.FILE)) { include = true; if (pattern != null) { String name = fileInfo.getFile().getName().getPath(); include = pattern.matcher(name).matches(); } if (include && patternexclude != null) { String name = fileInfo.getFile().getName().getPath(); include = !pattern.matcher(name).matches(); } } else { include = false; } return include; } }); } else { fileList = sourceFileOrFolder.getChildren(); } } else { pattern = null; patternexclude = null; // Target is a file fileList = new FileObject[] { sourceFileOrFolder }; } if (fileList.length == 0) { resultat = false; logError(BaseMessages.getString(PKG, "JobZipFiles.Log.FolderIsEmpty", localSourceFilename)); } else if (!checkContainsFile(localSourceFilename, fileList, isSourceDirectory)) { resultat = false; logError(BaseMessages.getString(PKG, "JobZipFiles.Log.NoFilesInFolder", localSourceFilename)); } else { if (ifZipFileExists == 0 && Fileexists) { // the zip file exists and user want to create new one with unique name // Format Date // do we have already a .zip at the end? if (localrealZipfilename.toLowerCase().endsWith(".zip")) { // strip this off localrealZipfilename = localrealZipfilename.substring(0, localrealZipfilename.length() - 4); } localrealZipfilename += "_" + StringUtil.getFormattedDateTimeNow(true) + ".zip"; if (log.isDebug()) { logDebug(BaseMessages.getString(PKG, "JobZipFiles.Zip_FileNameChange1.Label") + localrealZipfilename + BaseMessages.getString(PKG, "JobZipFiles.Zip_FileNameChange1.Label")); } } else if (ifZipFileExists == 1 && Fileexists) { // the zip file exists and user want to append // get a temp file fileZip = getFile(localrealZipfilename); tempFile = File.createTempFile(fileZip.getName(), null); // delete it, otherwise we cannot rename existing zip to it. tempFile.delete(); renameOk = fileZip.renameTo(tempFile); if (!renameOk) { logError(BaseMessages.getString(PKG, "JobZipFiles.Cant_Rename_Temp1.Label") + fileZip.getAbsolutePath() + BaseMessages.getString(PKG, "JobZipFiles.Cant_Rename_Temp2.Label") + tempFile.getAbsolutePath() + BaseMessages.getString(PKG, "JobZipFiles.Cant_Rename_Temp3.Label")); } if (log.isDebug()) { logDebug(BaseMessages.getString(PKG, "JobZipFiles.Zip_FileAppend1.Label") + localrealZipfilename + BaseMessages.getString(PKG, "JobZipFiles.Zip_FileAppend2.Label")); } } if (log.isDetailed()) { logDetailed( BaseMessages.getString(PKG, "JobZipFiles.Files_Found1.Label") + fileList.length + BaseMessages.getString(PKG, "JobZipFiles.Files_Found2.Label") + localSourceFilename + BaseMessages.getString(PKG, "JobZipFiles.Files_Found3.Label")); } // Prepare Zip File buffer = new byte[18024]; dest = KettleVFS.getOutputStream(localrealZipfilename, false); buff = new BufferedOutputStream(dest); out = new ZipOutputStream(buff); HashSet<String> fileSet = new HashSet<String>(); if (renameOk) { // User want to append files to existing Zip file // The idea is to rename the existing zip file to a temporary file // and then adds all entries in the existing zip along with the new files, // excluding the zip entries that have the same name as one of the new files. zin = new ZipInputStream(new FileInputStream(tempFile)); entry = zin.getNextEntry(); while (entry != null) { String name = entry.getName(); if (!fileSet.contains(name)) { // Add ZIP entry to output stream. out.putNextEntry(new ZipEntry(name)); // Transfer bytes from the ZIP file to the output file int len; while ((len = zin.read(buffer)) > 0) { out.write(buffer, 0, len); } fileSet.add(name); } entry = zin.getNextEntry(); } // Close the streams zin.close(); } // Set the method out.setMethod(ZipOutputStream.DEFLATED); // Set the compression level if (compressionRate == 0) { out.setLevel(Deflater.NO_COMPRESSION); } else if (compressionRate == 1) { out.setLevel(Deflater.DEFAULT_COMPRESSION); } if (compressionRate == 2) { out.setLevel(Deflater.BEST_COMPRESSION); } if (compressionRate == 3) { out.setLevel(Deflater.BEST_SPEED); } // Specify Zipped files (After that we will move,delete them...) FileObject[] zippedFiles = new FileObject[fileList.length]; int fileNum = 0; // Get the files in the list... for (int i = 0; i < fileList.length && !parentJob.isStopped(); i++) { boolean getIt = true; boolean getItexclude = false; // First see if the file matches the regular expression! // ..only if target is a folder ! if (isSourceDirectory) { // If we include sub-folders, we match on the whole name, not just the basename // String filename; if (includingSubFolders) { filename = fileList[i].getName().getPath(); } else { filename = fileList[i].getName().getBaseName(); } if (pattern != null) { // Matches the base name of the file (backward compatible!) // Matcher matcher = pattern.matcher(filename); getIt = matcher.matches(); } if (patternexclude != null) { Matcher matcherexclude = patternexclude.matcher(filename); getItexclude = matcherexclude.matches(); } } // Get processing File String targetFilename = KettleVFS.getFilename(fileList[i]); if (sourceFileOrFolder.getType().equals(FileType.FILE)) { targetFilename = localSourceFilename; } FileObject file = KettleVFS.getFileObject(targetFilename); boolean isTargetDirectory = file.exists() && file.getType().equals(FileType.FOLDER); if (getIt && !getItexclude && !isTargetDirectory && !fileSet.contains(targetFilename)) { // We can add the file to the Zip Archive if (log.isDebug()) { logDebug(BaseMessages.getString(PKG, "JobZipFiles.Add_FilesToZip1.Label") + fileList[i] + BaseMessages.getString(PKG, "JobZipFiles.Add_FilesToZip2.Label") + localSourceFilename + BaseMessages.getString(PKG, "JobZipFiles.Add_FilesToZip3.Label")); } // Associate a file input stream for the current file InputStream in = KettleVFS.getInputStream(file); // Add ZIP entry to output stream. // String relativeName; String fullName = fileList[i].getName().getPath(); String basePath = sourceFileOrFolder.getName().getPath(); if (isSourceDirectory) { if (fullName.startsWith(basePath)) { relativeName = fullName.substring(basePath.length() + 1); } else { relativeName = fullName; } } else if (isFromPrevious) { int depth = determineDepth(environmentSubstitute(storedSourcePathDepth)); relativeName = determineZipfilenameForDepth(fullName, depth); } else { relativeName = fileList[i].getName().getBaseName(); } out.putNextEntry(new ZipEntry(relativeName)); int len; while ((len = in.read(buffer)) > 0) { out.write(buffer, 0, len); } out.flush(); out.closeEntry(); // Close the current file input stream in.close(); // Get Zipped File zippedFiles[fileNum] = fileList[i]; fileNum = fileNum + 1; } } // Close the ZipOutPutStream out.close(); buff.close(); dest.close(); if (log.isBasic()) { logBasic(BaseMessages.getString(PKG, "JobZipFiles.Log.TotalZippedFiles", "" + zippedFiles.length)); } // Delete Temp File if (tempFile != null) { tempFile.delete(); } // -----Get the list of Zipped Files and Move or Delete Them if (afterZip == 1 || afterZip == 2) { // iterate through the array of Zipped files for (int i = 0; i < zippedFiles.length; i++) { if (zippedFiles[i] != null) { // Delete, Move File FileObject fileObjectd = zippedFiles[i]; if (!isSourceDirectory) { fileObjectd = KettleVFS.getFileObject(localSourceFilename); } // Here we can move, delete files if (afterZip == 1) { // Delete File boolean deleted = fileObjectd.delete(); if (!deleted) { resultat = false; logError(BaseMessages.getString(PKG, "JobZipFiles.Cant_Delete_File1.Label") + localSourceFilename + Const.FILE_SEPARATOR + zippedFiles[i] + BaseMessages .getString(PKG, "JobZipFiles.Cant_Delete_File2.Label")); } // File deleted if (log.isDebug()) { logDebug(BaseMessages.getString(PKG, "JobZipFiles.File_Deleted1.Label") + localSourceFilename + Const.FILE_SEPARATOR + zippedFiles[i] + BaseMessages.getString(PKG, "JobZipFiles.File_Deleted2.Label")); } } else if (afterZip == 2) { // Move File FileObject fileObjectm = null; try { fileObjectm = KettleVFS.getFileObject(realMovetodirectory + Const.FILE_SEPARATOR + fileObjectd.getName().getBaseName()); fileObjectd.moveTo(fileObjectm); } catch (IOException e) { logError( BaseMessages.getString(PKG, "JobZipFiles.Cant_Move_File1.Label") + zippedFiles[i] + BaseMessages.getString(PKG, "JobZipFiles.Cant_Move_File2.Label") + e.getMessage()); resultat = false; } finally { try { if (fileObjectm != null) { fileObjectm.close(); } } catch (Exception e) { if (fileObjectm != null) { logError("Error closing file '" + fileObjectm.toString() + "'", e); } } } // File moved if (log.isDebug()) { logDebug(BaseMessages.getString(PKG, "JobZipFiles.File_Moved1.Label") + zippedFiles[i] + BaseMessages.getString(PKG, "JobZipFiles.File_Moved2.Label")); } } } } } if (addFileToResult) { // Add file to result files name ResultFile resultFile = new ResultFile(ResultFile.FILE_TYPE_GENERAL, fileObject, parentJob.getJobname(), toString()); result.getResultFiles().put(resultFile.getFile().toString(), resultFile); } resultat = true; } } } catch (Exception e) { logError(BaseMessages.getString(PKG, "JobZipFiles.Cant_CreateZipFile1.Label") + localrealZipfilename + BaseMessages.getString(PKG, "JobZipFiles.Cant_CreateZipFile2.Label"), e); resultat = false; } finally { if (fileObject != null) { try { fileObject.close(); fileObject = null; } catch (IOException ex) { logError("Error closing file '" + fileObject.toString() + "'", ex); } } try { if (out != null) { out.close(); } if (buff != null) { buff.close(); } if (dest != null) { dest.close(); } if (zin != null) { zin.close(); } if (entry != null) { entry = null; } } catch (IOException ex) { logError("Error closing zip file entry for file '" + originFile.toString() + "'", ex); } } } else { resultat = true; if (localrealZipfilename == null) { logError(BaseMessages.getString(PKG, "JobZipFiles.No_ZipFile_Defined.Label")); } if (!orginExist) { logError(BaseMessages.getString(PKG, "JobZipFiles.No_FolderCible_Defined.Label", localSourceFilename)); } } // return a verifier return resultat; }
From source file:org.pentaho.di.job.entries.zipfile.JobEntryZipFile.java
public Result execute(Result previousResult, int nr) { Result result = previousResult; List<RowMetaAndData> rows = result.getRows(); // reset values String realZipfilename = null; String realWildcard = null;//from w w w.j a v a 2s . c o m String realWildcardExclude = null; String realTargetdirectory = null; String realMovetodirectory = environmentSubstitute(movetoDirectory); // Sanity check boolean SanityControlOK = true; if (afterZip == 2) { if (Const.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(); moveToDirectory = null; } catch (Exception e) { logError("Error moving to directory", e); result.setResult(false); result.setNrErrors(1); } } } } } if (!SanityControlOK) { result.setNrErrors(1); result.setResult(false); return result; } // arguments from previous 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 (!Const.isEmpty(realTargetdirectory)) { // get wildcard to include if (!Const.isEmpty(resultRow.getString(1, null))) { realWildcard = resultRow.getString(1, null); } // get wildcard to exclude if (!Const.isEmpty(resultRow.getString(2, null))) { realWildcardExclude = resultRow.getString(2, null); } // get destination zip file realZipfilename = resultRow.getString(3, null); if (!Const.isEmpty(realZipfilename)) { if (!processRowFile(parentJob, result, realZipfilename, realWildcard, realWildcardExclude, realTargetdirectory, realMovetodirectory, createParentFolder)) { result.setResult(false); return 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 (!Const.isEmpty(sourceDirectory)) { // get values from job entry realZipfilename = getFullFilename(environmentSubstitute(zipFilename), addDate, addTime, specifyFormat, dateTimeFormat); realWildcard = environmentSubstitute(wildCard); realWildcardExclude = environmentSubstitute(excludeWildCard); realTargetdirectory = environmentSubstitute(sourceDirectory); result.setResult(processRowFile(parentJob, result, realZipfilename, realWildcard, realWildcardExclude, realTargetdirectory, realMovetodirectory, createParentFolder)); } else { logError("Source folder/file is empty! Ignoring row..."); } } // End return result; }
From source file:org.pentaho.di.job.entry.validator.FileDoesNotExistValidator.java
public boolean validate(CheckResultSourceInterface source, String propertyName, List<CheckResultInterface> remarks, ValidatorContext context) { String filename = ValidatorUtils.getValueAsString(source, propertyName); VariableSpace variableSpace = getVariableSpace(source, propertyName, remarks, context); boolean failIfExists = getFailIfExists(source, propertyName, remarks, context); if (null == variableSpace) { return false; }/*w w w . ja v a 2 s . c om*/ String realFileName = variableSpace.environmentSubstitute(filename); FileObject fileObject = null; try { fileObject = KettleVFS.getFileObject(realFileName, variableSpace); if (fileObject.exists() && failIfExists) { JobEntryValidatorUtils.addFailureRemark(source, propertyName, VALIDATOR_NAME, remarks, JobEntryValidatorUtils.getLevelOnFail(context, VALIDATOR_NAME)); return false; } try { fileObject.close(); // Just being paranoid } catch (IOException ignored) { // Ignore close errors } } catch (Exception e) { JobEntryValidatorUtils.addExceptionRemark(source, propertyName, VALIDATOR_NAME, remarks, e); return false; } return true; }
From source file:org.pentaho.di.job.entry.validator.FileExistsValidator.java
public boolean validate(CheckResultSourceInterface source, String propertyName, List<CheckResultInterface> remarks, ValidatorContext context) { String filename = ValidatorUtils.getValueAsString(source, propertyName); VariableSpace variableSpace = getVariableSpace(source, propertyName, remarks, context); boolean failIfDoesNotExist = getFailIfDoesNotExist(source, propertyName, remarks, context); if (null == variableSpace) { return false; }//from www .ja v a 2 s . com String realFileName = variableSpace.environmentSubstitute(filename); FileObject fileObject = null; try { fileObject = KettleVFS.getFileObject(realFileName, variableSpace); if (fileObject == null || (fileObject != null && !fileObject.exists() && failIfDoesNotExist)) { JobEntryValidatorUtils.addFailureRemark(source, propertyName, VALIDATOR_NAME, remarks, JobEntryValidatorUtils.getLevelOnFail(context, VALIDATOR_NAME)); return false; } try { fileObject.close(); // Just being paranoid } catch (IOException ignored) { // Ignore close errors } } catch (Exception e) { JobEntryValidatorUtils.addExceptionRemark(source, propertyName, VALIDATOR_NAME, remarks, e); return false; } return true; }