List of usage examples for org.apache.commons.vfs FileObject exists
public boolean exists() throws FileSystemException;
From source file:com.panet.imeta.trans.steps.scriptvalues_mod.ScriptValuesAddedFunctions.java
public static String getShortFilename(Context actualContext, Scriptable actualObject, Object[] ArgList, Function FunctionContext) { try {//from w w w. j ava 2s .com if (ArgList.length == 1 && !isNull(ArgList[0]) && !isUndefined(ArgList[0])) { if (ArgList[0].equals(null)) return null; FileObject file = null; try { // Source file file = KettleVFS.getFileObject(Context.toString(ArgList[0])); String Filename = null; if (file.exists()) { Filename = file.getName().getBaseName().toString(); } else { Context.reportRuntimeError("file [" + Context.toString(ArgList[0]) + "] can not be found!"); } return Filename; } catch (IOException e) { throw Context.reportRuntimeError( "The function call getShortFilename throw an error : " + e.toString()); } finally { if (file != null) try { file.close(); } catch (Exception e) { } } } else { throw Context.reportRuntimeError("The function call getShortFilename is not valid."); } } catch (Exception e) { throw Context.reportRuntimeError(e.toString()); } }
From source file:com.panet.imeta.trans.steps.scriptvalues_mod.ScriptValuesAddedFunctions.java
public static boolean isFile(Context actualContext, Scriptable actualObject, Object[] ArgList, Function FunctionContext) { try {/*from w w w . j a v a 2 s . c o m*/ if (ArgList.length == 1 && !isNull(ArgList[0]) && !isUndefined(ArgList[0])) { if (ArgList[0].equals(null)) return false; FileObject file = null; try { // Source file file = KettleVFS.getFileObject(Context.toString(ArgList[0])); boolean isafile = false; if (file.exists()) { if (file.getType().equals(FileType.FILE)) isafile = true; else Context.reportRuntimeError("[" + Context.toString(ArgList[0]) + "] is not a file!"); } else { Context.reportRuntimeError("file [" + Context.toString(ArgList[0]) + "] can not be found!"); } return isafile; } catch (IOException e) { throw Context.reportRuntimeError("The function call is File throw an error : " + e.toString()); } finally { if (file != null) try { file.close(); } catch (Exception e) { } } } else { throw Context.reportRuntimeError("The function call isFile is not valid."); } } catch (Exception e) { throw Context.reportRuntimeError(e.toString()); } }
From source file:com.panet.imeta.trans.steps.scriptvalues_mod.ScriptValuesAddedFunctions.java
public static String getFileExtension(Context actualContext, Scriptable actualObject, Object[] ArgList, Function FunctionContext) { try {// w w w . j a v a 2 s . c om if (ArgList.length == 1 && !isNull(ArgList[0]) && !isUndefined(ArgList[0])) { if (ArgList[0].equals(null)) return null; FileObject file = null; try { // Source file file = KettleVFS.getFileObject(Context.toString(ArgList[0])); String Extension = null; if (file.exists()) { Extension = file.getName().getExtension().toString(); } else { Context.reportRuntimeError("file [" + Context.toString(ArgList[0]) + "] can not be found!"); } return Extension; } catch (IOException e) { throw Context.reportRuntimeError( "The function call getFileExtension throw an error : " + e.toString()); } finally { if (file != null) try { file.close(); } catch (Exception e) { } } } else { throw Context.reportRuntimeError("The function call getFileExtension is not valid."); } } catch (Exception e) { throw Context.reportRuntimeError(e.toString()); } }
From source file:com.panet.imeta.trans.steps.scriptvalues_mod.ScriptValuesAddedFunctions.java
public static String getParentFoldername(Context actualContext, Scriptable actualObject, Object[] ArgList, Function FunctionContext) { try {// w w w .j a va2 s .com if (ArgList.length == 1 && !isNull(ArgList[0]) && !isUndefined(ArgList[0])) { if (ArgList[0].equals(null)) return null; FileObject file = null; try { // Source file file = KettleVFS.getFileObject(Context.toString(ArgList[0])); String foldername = null; if (file.exists()) { foldername = KettleVFS.getFilename(file.getParent()); } else { Context.reportRuntimeError("file [" + Context.toString(ArgList[0]) + "] can not be found!"); } return foldername; } catch (IOException e) { throw Context.reportRuntimeError( "The function call getParentFoldername throw an error : " + e.toString()); } finally { if (file != null) try { file.close(); } catch (Exception e) { } } } else { throw Context.reportRuntimeError("The function call getParentFoldername is not valid."); } } catch (Exception e) { throw Context.reportRuntimeError(e.toString()); } }
From source file:com.panet.imeta.trans.steps.scriptvalues_mod.ScriptValuesAddedFunctions.java
public static boolean isFolder(Context actualContext, Scriptable actualObject, Object[] ArgList, Function FunctionContext) { try {/*from w w w . j av a 2 s .c o m*/ if (ArgList.length == 1 && !isNull(ArgList[0]) && !isUndefined(ArgList[0])) { if (ArgList[0].equals(null)) return false; FileObject file = null; try { // Source file file = KettleVFS.getFileObject(Context.toString(ArgList[0])); boolean isafolder = false; if (file.exists()) { if (file.getType().equals(FileType.FOLDER)) isafolder = true; else Context.reportRuntimeError("[" + Context.toString(ArgList[0]) + "] is not a folder!"); } else { Context.reportRuntimeError( "folder [" + Context.toString(ArgList[0]) + "] can not be found!"); } return isafolder; } catch (IOException e) { throw Context.reportRuntimeError("The function call isFolder throw an error : " + e.toString()); } finally { if (file != null) try { file.close(); } catch (Exception e) { } } } else { throw Context.reportRuntimeError("The function call isFolder is not valid."); } } catch (Exception e) { throw Context.reportRuntimeError(e.toString()); } }
From source file:com.panet.imeta.job.entries.unzip.JobEntryUnZip.java
public Result execute(Result previousResult, int nr, Repository rep, Job parentJob) { LogWriter log = LogWriter.getInstance(); Result result = previousResult; result.setResult(false);// ww w . j ava 2 s. c o m result.setEntryNr(1); List<RowMetaAndData> rows = result.getRows(); RowMetaAndData resultRow = null; String realFilenameSource = environmentSubstitute(zipFilename); String realWildcardSource = environmentSubstitute(wildcardSource); String realWildcard = environmentSubstitute(wildcard); String realWildcardExclude = environmentSubstitute(wildcardexclude); String realTargetdirectory = environmentSubstitute(targetdirectory); String realMovetodirectory = environmentSubstitute(movetodirectory); limitFiles = Const.toInt(environmentSubstitute(getLimit()), 10); NrErrors = 0; NrSuccess = 0; successConditionBroken = false; successConditionBrokenExit = false; if (isfromprevious) { if (log.isDetailed()) log.logDetailed(toString(), Messages.getString("JobUnZip.Log.ArgFromPrevious.Found", (rows != null ? rows.size() : 0) + "")); if (rows.size() == 0) return result; } else { if (Const.isEmpty(zipFilename)) { // Zip file/folder is missing log.logError(toString(), Messages.getString("JobUnZip.No_ZipFile_Defined.Label")); return result; } } FileObject fileObject = null; FileObject targetdir = null; FileObject movetodir = null; try { // Let's make some checks here, before running job entry ... boolean exitjobentry = false; // Target folder targetdir = KettleVFS.getFileObject(realTargetdirectory); if (!targetdir.exists()) { if (createfolder) { targetdir.createFolder(); if (log.isDetailed()) log.logDetailed(toString(), Messages.getString("JobUnZip.Log.TargetFolderCreated", realTargetdirectory)); } else { log.logError(Messages.getString("JobUnZip.Error.Label"), Messages.getString("JobUnZip.TargetFolderNotFound.Label")); exitjobentry = true; } } else { if (!(targetdir.getType() == FileType.FOLDER)) { log.logError(Messages.getString("JobUnZip.Error.Label"), Messages.getString("JobUnZip.TargetFolderNotFolder.Label", realTargetdirectory)); exitjobentry = true; } else { if (log.isDetailed()) log.logDetailed(toString(), Messages.getString("JobUnZip.TargetFolderExists.Label", realTargetdirectory)); } } // If user want to move zip files after process // movetodirectory must be provided if (afterunzip == 2) { if (Const.isEmpty(movetodirectory)) { log.logError(Messages.getString("JobUnZip.Error.Label"), Messages.getString("JobUnZip.MoveToDirectoryEmpty.Label")); exitjobentry = true; } else { movetodir = KettleVFS.getFileObject(realMovetodirectory); if (!(movetodir.exists()) || !(movetodir.getType() == FileType.FOLDER)) { if (createMoveToDirectory) { movetodir.createFolder(); if (log.isDetailed()) log.logDetailed(toString(), Messages.getString("JobUnZip.Log.MoveToFolderCreated", realMovetodirectory)); } else { log.logError(Messages.getString("JobUnZip.Error.Label"), Messages.getString("JobUnZip.MoveToDirectoryNotExists.Label")); exitjobentry = true; } } } } // We found errors...now exit if (exitjobentry) return result; if (isfromprevious) { if (rows != null) // Copy the input row to the (command line) // arguments { for (int iteration = 0; iteration < rows.size() && !parentJob.isStopped(); iteration++) { if (successConditionBroken) { if (!successConditionBrokenExit) { log.logError(toString(), Messages.getString("JobUnZip.Error.SuccessConditionbroken", "" + NrErrors)); successConditionBrokenExit = true; } result.setEntryNr(NrErrors); return result; } resultRow = rows.get(iteration); // Get sourcefile/folder and wildcard realFilenameSource = resultRow.getString(0, null); realWildcardSource = resultRow.getString(1, null); fileObject = KettleVFS.getFileObject(realFilenameSource); if (fileObject.exists()) { processOneFile(log, result, parentJob, fileObject, realTargetdirectory, realWildcard, realWildcardExclude, movetodir, realMovetodirectory, realWildcardSource); } else { updateErrors(); log.logError(toString(), Messages.getString("JobUnZip.Error.CanNotFindFile", realFilenameSource)); } } } } else { fileObject = KettleVFS.getFileObject(realFilenameSource); if (!fileObject.exists()) { log.logError(Messages.getString("JobUnZip.Error.Label"), Messages.getString("JobUnZip.ZipFile.NotExists.Label", realFilenameSource)); return result; } if (log.isDetailed()) log.logDetailed(toString(), Messages.getString("JobUnZip.Zip_FileExists.Label", realFilenameSource)); if (Const.isEmpty(targetdirectory)) { log.logError(Messages.getString("JobUnZip.Error.Label"), Messages.getString("JobUnZip.TargetFolderNotFound.Label")); return result; } processOneFile(log, result, parentJob, fileObject, realTargetdirectory, realWildcard, realWildcardExclude, movetodir, realMovetodirectory, realWildcardSource); } } catch (Exception e) { log.logError(Messages.getString("JobUnZip.Error.Label"), Messages.getString("JobUnZip.ErrorUnzip.Label", realFilenameSource, e.getMessage())); updateErrors(); } finally { if (fileObject != null) { try { fileObject.close(); } catch (IOException ex) { } ; } if (targetdir != null) { try { targetdir.close(); } catch (IOException ex) { } ; } if (movetodir != null) { try { movetodir.close(); } catch (IOException ex) { } ; } } result.setNrErrors(NrErrors); result.setNrLinesWritten(NrSuccess); if (getSuccessStatus()) result.setResult(true); displayResults(log); return result; }
From source file:com.panet.imeta.job.entries.mail.JobEntryMail.java
public Result execute(Result result, int nr, Repository rep, Job parentJob) { LogWriter log = LogWriter.getInstance(); File masterZipfile = null;/* w w w . j av a2s.c o m*/ // Send an e-mail... // create some properties and get the default Session Properties props = new Properties(); if (Const.isEmpty(server)) { log.logError(toString(), Messages.getString("JobMail.Error.HostNotSpecified")); result.setNrErrors(1L); result.setResult(false); return result; } String protocol = "smtp"; if (usingSecureAuthentication) { if (secureConnectionType.equals("TLS")) { // Allow TLS authentication props.put("mail.smtp.starttls.enable", "true"); } else { protocol = "smtps"; // required to get rid of a SSL exception : // nested exception is: // javax.net.ssl.SSLException: Unsupported record version // Unknown props.put("mail.smtps.quitwait", "false"); } } props.put("mail." + protocol + ".host", environmentSubstitute(server)); if (!Const.isEmpty(port)) props.put("mail." + protocol + ".port", environmentSubstitute(port)); boolean debug = log.getLogLevel() >= LogWriter.LOG_LEVEL_DEBUG; if (debug) props.put("mail.debug", "true"); if (usingAuthentication) { props.put("mail." + protocol + ".auth", "true"); /* * authenticator = new Authenticator() { protected * PasswordAuthentication getPasswordAuthentication() { return new * PasswordAuthentication( * StringUtil.environmentSubstitute(Const.NVL(authenticationUser, * "")), * StringUtil.environmentSubstitute(Const.NVL(authenticationPassword * , "")) ); } }; */ } Session session = Session.getInstance(props); session.setDebug(debug); try { // create a message Message msg = new MimeMessage(session); // set message priority if (usePriority) { String priority_int = "1"; if (priority.equals("low")) { priority_int = "3"; } if (priority.equals("normal")) { priority_int = "2"; } msg.setHeader("X-Priority", priority_int); // (String)int // between 1= high // and 3 = low. msg.setHeader("Importance", importance); // seems to be needed for MS Outlook. // where it returns a string of high /normal /low. } // Set Mail sender (From) String sender_address = environmentSubstitute(replyAddress); if (!Const.isEmpty(sender_address)) { String sender_name = environmentSubstitute(replyName); if (!Const.isEmpty(sender_name)) sender_address = sender_name + '<' + sender_address + '>'; msg.setFrom(new InternetAddress(sender_address)); } else { throw new MessagingException(Messages.getString("JobMail.Error.ReplyEmailNotFilled")); } // set Reply to addresses String reply_to_address = environmentSubstitute(replyToAddresses); if (!Const.isEmpty(reply_to_address)) { // Split the mail-address: space separated String[] reply_Address_List = environmentSubstitute(reply_to_address).split(" "); InternetAddress[] address = new InternetAddress[reply_Address_List.length]; for (int i = 0; i < reply_Address_List.length; i++) address[i] = new InternetAddress(reply_Address_List[i]); msg.setReplyTo(address); } // Split the mail-address: space separated String destinations[] = environmentSubstitute(destination).split(" "); InternetAddress[] address = new InternetAddress[destinations.length]; for (int i = 0; i < destinations.length; i++) address[i] = new InternetAddress(destinations[i]); msg.setRecipients(Message.RecipientType.TO, address); if (!Const.isEmpty(destinationCc)) { // Split the mail-address Cc: space separated String destinationsCc[] = environmentSubstitute(destinationCc).split(" "); InternetAddress[] addressCc = new InternetAddress[destinationsCc.length]; for (int i = 0; i < destinationsCc.length; i++) addressCc[i] = new InternetAddress(destinationsCc[i]); msg.setRecipients(Message.RecipientType.CC, addressCc); } if (!Const.isEmpty(destinationBCc)) { // Split the mail-address BCc: space separated String destinationsBCc[] = environmentSubstitute(destinationBCc).split(" "); InternetAddress[] addressBCc = new InternetAddress[destinationsBCc.length]; for (int i = 0; i < destinationsBCc.length; i++) addressBCc[i] = new InternetAddress(destinationsBCc[i]); msg.setRecipients(Message.RecipientType.BCC, addressBCc); } String realSubject = environmentSubstitute(subject); if (!Const.isEmpty(realSubject)) { msg.setSubject(realSubject); } msg.setSentDate(new Date()); StringBuffer messageText = new StringBuffer(); if (comment != null) { messageText.append(environmentSubstitute(comment)).append(Const.CR).append(Const.CR); } if (!onlySendComment) { messageText.append(Messages.getString("JobMail.Log.Comment.Job")).append(Const.CR); messageText.append("-----").append(Const.CR); messageText.append(Messages.getString("JobMail.Log.Comment.JobName") + " : ") .append(parentJob.getJobMeta().getName()).append(Const.CR); messageText.append(Messages.getString("JobMail.Log.Comment.JobDirectory") + " : ") .append(parentJob.getJobMeta().getDirectory()).append(Const.CR); messageText.append(Messages.getString("JobMail.Log.Comment.JobEntry") + " : ").append(getName()) .append(Const.CR); messageText.append(Const.CR); } if (includeDate) { messageText.append(Const.CR).append(Messages.getString("JobMail.Log.Comment.MsgDate") + ": ") .append(XMLHandler.date2string(new Date())).append(Const.CR).append(Const.CR); } if (!onlySendComment && result != null) { messageText.append(Messages.getString("JobMail.Log.Comment.PreviousResult") + ":").append(Const.CR); messageText.append("-----------------").append(Const.CR); messageText.append(Messages.getString("JobMail.Log.Comment.JobEntryNr") + " : ") .append(result.getEntryNr()).append(Const.CR); messageText.append(Messages.getString("JobMail.Log.Comment.Errors") + " : ") .append(result.getNrErrors()).append(Const.CR); messageText.append(Messages.getString("JobMail.Log.Comment.LinesRead") + " : ") .append(result.getNrLinesRead()).append(Const.CR); messageText.append(Messages.getString("JobMail.Log.Comment.LinesWritten") + " : ") .append(result.getNrLinesWritten()).append(Const.CR); messageText.append(Messages.getString("JobMail.Log.Comment.LinesInput") + " : ") .append(result.getNrLinesInput()).append(Const.CR); messageText.append(Messages.getString("JobMail.Log.Comment.LinesOutput") + " : ") .append(result.getNrLinesOutput()).append(Const.CR); messageText.append(Messages.getString("JobMail.Log.Comment.LinesUpdated") + " : ") .append(result.getNrLinesUpdated()).append(Const.CR); messageText.append(Messages.getString("JobMail.Log.Comment.Status") + " : ") .append(result.getExitStatus()).append(Const.CR); messageText.append(Messages.getString("JobMail.Log.Comment.Result") + " : ") .append(result.getResult()).append(Const.CR); messageText.append(Const.CR); } if (!onlySendComment && (!Const.isEmpty(environmentSubstitute(contactPerson)) || !Const.isEmpty(environmentSubstitute(contactPhone)))) { messageText.append(Messages.getString("JobMail.Log.Comment.ContactInfo") + " :").append(Const.CR); messageText.append("---------------------").append(Const.CR); messageText.append(Messages.getString("JobMail.Log.Comment.PersonToContact") + " : ") .append(environmentSubstitute(contactPerson)).append(Const.CR); messageText.append(Messages.getString("JobMail.Log.Comment.Tel") + " : ") .append(environmentSubstitute(contactPhone)).append(Const.CR); messageText.append(Const.CR); } // Include the path to this job entry... if (!onlySendComment) { JobTracker jobTracker = parentJob.getJobTracker(); if (jobTracker != null) { messageText.append(Messages.getString("JobMail.Log.Comment.PathToJobentry") + ":") .append(Const.CR); messageText.append("------------------------").append(Const.CR); addBacktracking(jobTracker, messageText); } } Multipart parts = new MimeMultipart(); MimeBodyPart part1 = new MimeBodyPart(); // put the text in the // 1st part if (useHTML) { if (!Const.isEmpty(getEncoding())) { part1.setContent(messageText.toString(), "text/html; " + "charset=" + getEncoding()); } else { part1.setContent(messageText.toString(), "text/html; " + "charset=ISO-8859-1"); } } else part1.setText(messageText.toString()); parts.addBodyPart(part1); if (includingFiles && result != null) { List<ResultFile> resultFiles = result.getResultFilesList(); if (resultFiles != null && !resultFiles.isEmpty()) { if (!zipFiles) { // Add all files to the message... // for (ResultFile resultFile : resultFiles) { FileObject file = resultFile.getFile(); if (file != null && file.exists()) { boolean found = false; for (int i = 0; i < fileType.length; i++) { if (fileType[i] == resultFile.getType()) found = true; } if (found) { // create a data source MimeBodyPart files = new MimeBodyPart(); URLDataSource fds = new URLDataSource(file.getURL()); // get a data Handler to manipulate this // file type; files.setDataHandler(new DataHandler(fds)); // include the file in the data source files.setFileName(file.getName().getBaseName()); // add the part with the file in the // BodyPart(); parts.addBodyPart(files); log.logBasic(toString(), "Added file '" + fds.getName() + "' to the mail message."); } } } } else { // create a single ZIP archive of all files masterZipfile = new File(System.getProperty("java.io.tmpdir") + Const.FILE_SEPARATOR + environmentSubstitute(zipFilename)); ZipOutputStream zipOutputStream = null; try { zipOutputStream = new ZipOutputStream(new FileOutputStream(masterZipfile)); for (ResultFile resultFile : resultFiles) { boolean found = false; for (int i = 0; i < fileType.length; i++) { if (fileType[i] == resultFile.getType()) found = true; } if (found) { FileObject file = resultFile.getFile(); ZipEntry zipEntry = new ZipEntry(file.getName().getBaseName()); zipOutputStream.putNextEntry(zipEntry); // Now put the content of this file into // this archive... BufferedInputStream inputStream = new BufferedInputStream( KettleVFS.getInputStream(file)); int c; while ((c = inputStream.read()) >= 0) { zipOutputStream.write(c); } inputStream.close(); zipOutputStream.closeEntry(); log.logBasic(toString(), "Added file '" + file.getName().getURI() + "' to the mail message in a zip archive."); } } } catch (Exception e) { log.logError(toString(), "Error zipping attachement files into file [" + masterZipfile.getPath() + "] : " + e.toString()); log.logError(toString(), Const.getStackTracker(e)); result.setNrErrors(1); } finally { if (zipOutputStream != null) { try { zipOutputStream.finish(); zipOutputStream.close(); } catch (IOException e) { log.logError(toString(), "Unable to close attachement zip file archive : " + e.toString()); log.logError(toString(), Const.getStackTracker(e)); result.setNrErrors(1); } } } // Now attach the master zip file to the message. if (result.getNrErrors() == 0) { // create a data source MimeBodyPart files = new MimeBodyPart(); FileDataSource fds = new FileDataSource(masterZipfile); // get a data Handler to manipulate this file type; files.setDataHandler(new DataHandler(fds)); // include the file in th e data source files.setFileName(fds.getName()); // add the part with the file in the BodyPart(); parts.addBodyPart(files); } } } } msg.setContent(parts); Transport transport = null; try { transport = session.getTransport(protocol); if (usingAuthentication) { if (!Const.isEmpty(port)) { transport.connect(environmentSubstitute(Const.NVL(server, "")), Integer.parseInt(environmentSubstitute(Const.NVL(port, ""))), environmentSubstitute(Const.NVL(authenticationUser, "")), environmentSubstitute(Const.NVL(authenticationPassword, ""))); } else { transport.connect(environmentSubstitute(Const.NVL(server, "")), environmentSubstitute(Const.NVL(authenticationUser, "")), environmentSubstitute(Const.NVL(authenticationPassword, ""))); } } else { transport.connect(); } transport.sendMessage(msg, msg.getAllRecipients()); } finally { if (transport != null) transport.close(); } } catch (IOException e) { log.logError(toString(), "Problem while sending message: " + e.toString()); result.setNrErrors(1); } catch (MessagingException mex) { log.logError(toString(), "Problem while sending message: " + mex.toString()); result.setNrErrors(1); Exception ex = mex; do { if (ex instanceof SendFailedException) { SendFailedException sfex = (SendFailedException) ex; Address[] invalid = sfex.getInvalidAddresses(); if (invalid != null) { log.logError(toString(), " ** Invalid Addresses"); for (int i = 0; i < invalid.length; i++) { log.logError(toString(), " " + invalid[i]); result.setNrErrors(1); } } Address[] validUnsent = sfex.getValidUnsentAddresses(); if (validUnsent != null) { log.logError(toString(), " ** ValidUnsent Addresses"); for (int i = 0; i < validUnsent.length; i++) { log.logError(toString(), " " + validUnsent[i]); result.setNrErrors(1); } } Address[] validSent = sfex.getValidSentAddresses(); if (validSent != null) { // System.out.println(" ** ValidSent Addresses"); for (int i = 0; i < validSent.length; i++) { log.logError(toString(), " " + validSent[i]); result.setNrErrors(1); } } } if (ex instanceof MessagingException) { ex = ((MessagingException) ex).getNextException(); } else { ex = null; } } while (ex != null); } finally { if (masterZipfile != null && masterZipfile.exists()) { masterZipfile.delete(); } } if (result.getNrErrors() > 0) { result.setResult(false); } else { result.setResult(true); } return result; }
From source file:com.panet.imeta.job.entries.zipfile.JobEntryZipFile.java
public boolean processRowFile(Job parentJob, Result result, String realZipfilename, String realWildcard, String realWildcardExclude, String realTargetdirectory, String realMovetodirectory, boolean createparentfolder) { LogWriter log = LogWriter.getInstance(); boolean Fileexists = false; File tempFile = null;// w w 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; FileOutputStream dest = null; BufferedOutputStream buff = null; org.apache.tools.zip.ZipOutputStream out = null; org.apache.tools.zip.ZipEntry entry = null; try { OriginFile = KettleVFS.getFileObject(realTargetdirectory); orginexist = OriginFile.exists(); } catch (Exception e) { } finally { if (OriginFile != null) { try { OriginFile.close(); } catch (IOException ex) { } ; } } if (realZipfilename != null && orginexist) { FileObject fileObject = null; try { fileObject = KettleVFS.getFileObject(realZipfilename); // Check if Zip File exists if (fileObject.exists()) { Fileexists = true; if (log.isDebug()) log.logDebug(toString(), Messages.getString("JobZipFiles.Zip_FileExists1.Label") + realZipfilename + Messages.getString("JobZipFiles.Zip_FileExists2.Label")); } // Let's see if we need to create parent folder of destination // zip filename if (createparentfolder) { createParentFolder(realZipfilename); } // 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, KettleVFS.getFileObject(realZipfilename), 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; log.logError(toString(), Messages.getString("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 String[] filelist = null; File f = new File(realTargetdirectory); if (f.isDirectory()) { // Target is a directory // Get all the files in the directory... filelist = f.list(); } else { // Target is a file filelist = new String[1]; filelist[0] = f.getName(); } if (filelist.length == 0) { resultat = false; log.logError(toString(), Messages.getString("JobZipFiles.Log.FolderIsEmpty", realTargetdirectory)); } else if (!checkContainsFile(realTargetdirectory, filelist)) { resultat = false; log.logError(toString(), Messages.getString("JobZipFiles.Log.NoFilesInFolder", realTargetdirectory)); } 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 (realZipfilename.toLowerCase().endsWith(".zip")) { // strip this off realZipfilename = realZipfilename.substring(0, realZipfilename.length() - 4); } realZipfilename = realZipfilename + "_" + StringUtil.getFormattedDateTimeNow(true) + ".zip"; if (log.isDebug()) log.logDebug(toString(), Messages.getString("JobZipFiles.Zip_FileNameChange1.Label") + realZipfilename + Messages.getString("JobZipFiles.Zip_FileNameChange1.Label")); } else if (ifzipfileexists == 1 && Fileexists) { // the zip file exists and user want to append // get a temp file fileZip = new File(realZipfilename); tempFile = File.createTempFile(fileZip.getName(), null); // delete it, otherwise we cannot rename existing // zip to it. tempFile.delete(); renameOk = fileZip.renameTo(tempFile); if (!renameOk) { log.logError(toString(), Messages.getString("JobZipFiles.Cant_Rename_Temp1.Label") + fileZip.getAbsolutePath() + Messages.getString("JobZipFiles.Cant_Rename_Temp2.Label") + tempFile.getAbsolutePath() + Messages.getString("JobZipFiles.Cant_Rename_Temp3.Label")); } if (log.isDebug()) log.logDebug(toString(), Messages.getString("JobZipFiles.Zip_FileAppend1.Label") + realZipfilename + Messages.getString("JobZipFiles.Zip_FileAppend2.Label")); } if (log.isDetailed()) log.logDetailed(toString(), Messages.getString("JobZipFiles.Files_Found1.Label") + filelist.length + Messages.getString("JobZipFiles.Files_Found2.Label") + realTargetdirectory + Messages.getString("JobZipFiles.Files_Found3.Label")); Pattern pattern = null; Pattern patternexclude = null; // Let's prepare pattern..only if target is a folder ! if (f.isDirectory()) { if (!Const.isEmpty(realWildcard)) { pattern = Pattern.compile(realWildcard); } if (!Const.isEmpty(realWildcardExclude)) { patternexclude = Pattern.compile(realWildcardExclude); } } // Prepare Zip File buffer = new byte[18024]; dest = new FileOutputStream(realZipfilename); buff = new BufferedOutputStream(dest); out = new org.apache.tools.zip.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 = (ZipEntry) 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 = (ZipEntry) zin.getNextEntry(); } // Close the streams zin.close(); } // Set the method out.setMethod(org.apache.tools.zip.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...) String[] ZippedFiles = new String[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 (f.isDirectory()) { if (pattern != null) { Matcher matcher = pattern.matcher(filelist[i]); getIt = matcher.matches(); } if (patternexclude != null) { Matcher matcherexclude = patternexclude.matcher(filelist[i]); getItexclude = matcherexclude.matches(); } } // Get processing File String targetFilename = realTargetdirectory + Const.FILE_SEPARATOR + filelist[i]; if (f.isFile()) targetFilename = realTargetdirectory; File file = new File(targetFilename); if (getIt && !getItexclude && !file.isDirectory() && !fileSet.contains(filelist[i])) { // We can add the file to the Zip Archive if (log.isDebug()) log.logDebug(toString(), Messages.getString("JobZipFiles.Add_FilesToZip1.Label") + filelist[i] + Messages.getString("JobZipFiles.Add_FilesToZip2.Label") + realTargetdirectory + Messages.getString("JobZipFiles.Add_FilesToZip3.Label")); // Associate a file input stream for the current // file FileInputStream in = new FileInputStream(targetFilename); // Add ZIP entry to output stream. out.putNextEntry(new ZipEntry(filelist[i])); 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()) log.logBasic(toString(), Messages.getString("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 File FileObject fileObjectd = KettleVFS.getFileObject( realTargetdirectory + Const.FILE_SEPARATOR + ZippedFiles[i]); if (f.isFile()) fileObjectd = KettleVFS.getFileObject(realTargetdirectory); // Here gc() is explicitly called if e.g. // createfile is used in the same // job for the same file. The problem is // that after creating the file the // file object is not properly garbaged // collected and thus the file cannot // be deleted anymore. This is a known // problem in the JVM. System.gc(); // Here we can move, delete files if (afterzip == 1) { // Delete File boolean deleted = fileObjectd.delete(); if (!deleted) { resultat = false; log.logError(toString(), Messages .getString("JobZipFiles.Cant_Delete_File1.Label") + realTargetdirectory + Const.FILE_SEPARATOR + ZippedFiles[i] + Messages.getString("JobZipFiles.Cant_Delete_File2.Label")); } // File deleted if (log.isDebug()) log.logDebug(toString(), Messages.getString("JobZipFiles.File_Deleted1.Label") + realTargetdirectory + Const.FILE_SEPARATOR + ZippedFiles[i] + Messages .getString("JobZipFiles.File_Deleted2.Label")); } else if (afterzip == 2) { // Move File try { FileObject fileObjectm = KettleVFS.getFileObject( realMovetodirectory + Const.FILE_SEPARATOR + ZippedFiles[i]); fileObjectd.moveTo(fileObjectm); } catch (IOException e) { log.logError(toString(), Messages.getString("JobZipFiles.Cant_Move_File1.Label") + ZippedFiles[i] + Messages .getString("JobZipFiles.Cant_Move_File2.Label") + e.getMessage()); resultat = false; } // File moved if (log.isDebug()) log.logDebug(toString(), Messages.getString("JobZipFiles.File_Moved1.Label") + ZippedFiles[i] + Messages.getString("JobZipFiles.File_Moved2.Label")); } } } } if (addfiletoresult) { // Add file to result files name ResultFile resultFile = new ResultFile(ResultFile.FILE_TYPE_GENERAL, KettleVFS.getFileObject(realZipfilename), parentJob.getJobname(), toString()); result.getResultFiles().put(resultFile.getFile().toString(), resultFile); } resultat = true; } } } catch (Exception e) { log.logError(toString(), Messages.getString("JobZipFiles.Cant_CreateZipFile1.Label") + realZipfilename + Messages.getString("JobZipFiles.Cant_CreateZipFile2.Label") + e.getMessage()); // result.setResult( false ); // result.setNrErrors(1); resultat = false; } finally { if (fileObject != null) { try { fileObject.close(); } catch (IOException ex) { } ; } // Close the ZipOutPutStream 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) { } ; } } else { resultat = true; if (realZipfilename == null) log.logError(toString(), Messages.getString("JobZipFiles.No_ZipFile_Defined.Label")); if (!orginexist) log.logError(toString(), Messages.getString("JobZipFiles.No_FolderCible_Defined.Label", realTargetdirectory)); } // return a verifier return resultat; }
From source file:com.panet.imeta.job.entries.movefiles.JobEntryMoveFiles.java
private boolean MoveFile(String shortfilename, FileObject sourcefilename, FileObject destinationfilename, FileObject movetofolderfolder, LogWriter log, Job parentJob, Result result) { FileObject destinationfile = null; boolean retval = false; try {/* w w w . j a v a 2 s . c om*/ if (!destinationfilename.exists()) { if (!simulate) sourcefilename.moveTo(destinationfilename); if (log.isDetailed()) log.logDetailed(toString(), Messages.getString("JobMoveFiles.Log.FileMoved", sourcefilename.getName().toString(), destinationfilename.getName().toString())); // add filename to result filename if (add_result_filesname && !iffileexists.equals("fail") && !iffileexists.equals("do_nothing")) addFileToResultFilenames(destinationfilename.toString(), log, result, parentJob); updateSuccess(); } else { if (log.isDetailed()) log.logDetailed(toString(), Messages.getString("JobMoveFiles.Log.FileExists", destinationfilename.toString())); if (iffileexists.equals("overwrite_file")) { if (!simulate) sourcefilename.moveTo(destinationfilename); if (log.isDetailed()) log.logDetailed(toString(), Messages.getString("JobMoveFiles.Log.FileOverwrite", destinationfilename.getName().toString())); // add filename to result filename if (add_result_filesname && !iffileexists.equals("fail") && !iffileexists.equals("do_nothing")) addFileToResultFilenames(destinationfilename.toString(), log, result, parentJob); updateSuccess(); } else if (iffileexists.equals("unique_name")) { String short_filename = shortfilename; // return destination short filename try { short_filename = getMoveDestinationFilename(short_filename, "ddMMyyyy_HHmmssSSS"); } catch (Exception e) { log.logError(toString(), Messages.getString( Messages.getString("JobMoveFiles.Error.GettingFilename", short_filename))); return retval; } String movetofilenamefull = destinationfilename.getParent().toString() + Const.FILE_SEPARATOR + short_filename; destinationfile = KettleVFS.getFileObject(movetofilenamefull); if (!simulate) sourcefilename.moveTo(destinationfile); if (log.isDetailed()) log.logDetailed(toString(), Messages.getString("JobMoveFiles.Log.FileMoved", sourcefilename.getName().toString(), destinationfile.getName().toString())); // add filename to result filename if (add_result_filesname && !iffileexists.equals("fail") && !iffileexists.equals("do_nothing")) addFileToResultFilenames(destinationfile.toString(), log, result, parentJob); updateSuccess(); } else if (iffileexists.equals("delete_file")) { if (!simulate) destinationfilename.delete(); if (log.isDetailed()) log.logDetailed(toString(), Messages.getString("JobMoveFiles.Log.FileDeleted", destinationfilename.getName().toString())); } else if (iffileexists.equals("move_file")) { String short_filename = shortfilename; // return destination short filename try { short_filename = getMoveDestinationFilename(short_filename, null); } catch (Exception e) { log.logError(toString(), Messages.getString( Messages.getString("JobMoveFiles.Error.GettingFilename", short_filename))); return retval; } String movetofilenamefull = movetofolderfolder.toString() + Const.FILE_SEPARATOR + short_filename; destinationfile = KettleVFS.getFileObject(movetofilenamefull); if (!destinationfile.exists()) { if (!simulate) sourcefilename.moveTo(destinationfile); if (log.isDetailed()) log.logDetailed(toString(), Messages.getString("JobMoveFiles.Log.FileMoved", sourcefilename.getName().toString(), destinationfile.getName().toString())); // add filename to result filename if (add_result_filesname && !iffileexists.equals("fail") && !iffileexists.equals("do_nothing")) addFileToResultFilenames(destinationfile.toString(), log, result, parentJob); } else { if (ifmovedfileexists.equals("overwrite_file")) { if (!simulate) sourcefilename.moveTo(destinationfile); if (log.isDetailed()) log.logDetailed(toString(), Messages.getString("JobMoveFiles.Log.FileOverwrite", destinationfile.getName().toString())); // add filename to result filename if (add_result_filesname && !iffileexists.equals("fail") && !iffileexists.equals("do_nothing")) addFileToResultFilenames(destinationfile.toString(), log, result, parentJob); updateSuccess(); } else if (ifmovedfileexists.equals("unique_name")) { SimpleDateFormat daf = new SimpleDateFormat(); Date now = new Date(); daf.applyPattern("ddMMyyyy_HHmmssSSS"); String dt = daf.format(now); short_filename += "_" + dt; String destinationfilenamefull = movetofolderfolder.toString() + Const.FILE_SEPARATOR + short_filename; destinationfile = KettleVFS.getFileObject(destinationfilenamefull); if (!simulate) sourcefilename.moveTo(destinationfile); if (log.isDetailed()) log.logDetailed(toString(), Messages.getString("JobMoveFiles.Log.FileMoved", destinationfile.getName().toString())); // add filename to result filename if (add_result_filesname && !iffileexists.equals("fail") && !iffileexists.equals("do_nothing")) addFileToResultFilenames(destinationfile.toString(), log, result, parentJob); updateSuccess(); } else if (ifmovedfileexists.equals("fail")) { // Update Errors updateErrors(); } } } else if (iffileexists.equals("fail")) { // Update Errors updateErrors(); } } } catch (Exception e) { log.logError(toString(), Messages.getString("JobMoveFiles.Error.Exception.MoveProcessError", sourcefilename.toString(), destinationfilename.toString(), e.getMessage())); } finally { if (destinationfile != null) { try { destinationfile.close(); } catch (IOException ex) { } ; } } return retval; }
From source file:com.akretion.kettle.steps.terminatooor.ScriptValuesAddedFunctions.java
public static void copyFile(ScriptEngine actualContext, Bindings actualObject, Object[] ArgList, Object FunctionContext) { try {//from w w w .j a va 2 s . co m if (ArgList.length == 3 && !isNull(ArgList[0]) && !isNull(ArgList[1]) && !isUndefined(ArgList[0]) && !isUndefined(ArgList[1])) { FileObject fileSource = null, fileDestination = null; try { // Source file to copy fileSource = KettleVFS.getFileObject((String) ArgList[0]); // Destination filename fileDestination = KettleVFS.getFileObject((String) ArgList[1]); if (fileSource.exists()) { // Source file exists... if (fileSource.getType() == FileType.FILE) { // Great..source is a file ... boolean overwrite = false; if (!ArgList[1].equals(null)) overwrite = (Boolean) ArgList[2]; boolean destinationExists = fileDestination.exists(); // Let's copy the file... if ((destinationExists && overwrite) || !destinationExists) FileUtil.copyContent(fileSource, fileDestination); } } else { new RuntimeException("file to copy [" + (String) ArgList[0] + "] can not be found!"); } } catch (IOException e) { throw new RuntimeException("The function call copyFile throw an error : " + e.toString()); } finally { if (fileSource != null) try { fileSource.close(); } catch (Exception e) { } if (fileDestination != null) try { fileDestination.close(); } catch (Exception e) { } } } else { throw new RuntimeException("The function call copyFileis not valid."); } } catch (Exception e) { throw new RuntimeException(e.toString()); } }