List of usage examples for org.apache.commons.vfs FileObject createFolder
public void createFolder() throws FileSystemException;
From source file:org.pentaho.di.job.entries.hadooptransjobexecutor.DistributedCacheUtilTest.java
@Test public void deleteDirectory() throws Exception { FileObject test = KettleVFS.getFileObject("bin/test/deleteDirectoryTest"); test.createFolder(); DistributedCacheUtil ch = new DistributedCacheUtil(); ch.deleteDirectory(test);/*w w w . j av a 2 s .c o m*/ try { assertFalse(test.exists()); } finally { // Delete the directory with java.io.File if it wasn't removed File f = new File("bin/test/deleteDirectoryTest"); if (f.exists() && !f.delete()) { throw new IOException("unable to delete test directory: " + f.getAbsolutePath()); } } }
From source file:org.pentaho.di.job.entries.job.JobEntryJob.java
private boolean createParentFolder(String filename) { // Check for parent folder FileObject parentfolder = null; boolean resultat = true; try {/* w w w . j a v a2 s. com*/ // Get parent folder parentfolder = KettleVFS.getFileObject(filename, this).getParent(); if (!parentfolder.exists()) { if (createParentFolder) { if (log.isDebug()) { log.logDebug(BaseMessages.getString(PKG, "JobJob.Log.ParentLogFolderNotExist", parentfolder.getName().toString())); } parentfolder.createFolder(); if (log.isDebug()) { log.logDebug(BaseMessages.getString(PKG, "JobJob.Log.ParentLogFolderCreated", parentfolder.getName().toString())); } } else { log.logError(BaseMessages.getString(PKG, "JobJob.Log.ParentLogFolderNotExist", parentfolder.getName().toString())); resultat = false; } } else { if (log.isDebug()) { log.logDebug(BaseMessages.getString(PKG, "JobJob.Log.ParentLogFolderExists", parentfolder.getName().toString())); } } } catch (Exception e) { resultat = false; log.logError(BaseMessages.getString(PKG, "JobJob.Error.ChekingParentLogFolderTitle"), BaseMessages .getString(PKG, "JobJob.Error.ChekingParentLogFolder", parentfolder.getName().toString()), e); } finally { if (parentfolder != null) { try { parentfolder.close(); parentfolder = null; } catch (Exception ex) { // Ignore } } } return resultat; }
From source file:org.pentaho.di.job.entries.movefiles.JobEntryMoveFiles.java
public Result execute(Result previousResult, int nr) throws KettleException { Result result = previousResult; List<RowMetaAndData> rows = result.getRows(); RowMetaAndData resultRow = null;/*from w w w. j a v a 2s .c om*/ result.setNrErrors(1); result.setResult(false); NrErrors = 0; NrSuccess = 0; successConditionBroken = false; successConditionBrokenExit = false; limitFiles = Const.toInt(environmentSubstitute(getNrErrorsLessThan()), 10); if (log.isDetailed()) { if (simulate) { logDetailed(BaseMessages.getString(PKG, "JobMoveFiles.Log.SimulationOn")); } if (include_subfolders) { logDetailed(BaseMessages.getString(PKG, "JobMoveFiles.Log.IncludeSubFoldersOn")); } } String MoveToFolder = environmentSubstitute(destinationFolder); // Get source and destination files, also wildcard String[] vsourcefilefolder = source_filefolder; String[] vdestinationfilefolder = destination_filefolder; String[] vwildcard = wildcard; if (iffileexists.equals("move_file")) { if (Const.isEmpty(MoveToFolder)) { logError(BaseMessages.getString(PKG, "JobMoveFiles.Log.Error.MoveToFolderMissing")); return result; } FileObject folder = null; try { folder = KettleVFS.getFileObject(MoveToFolder, this); if (!folder.exists()) { if (log.isDetailed()) { logDetailed( BaseMessages.getString(PKG, "JobMoveFiles.Log.Error.FolderMissing", MoveToFolder)); } if (create_move_to_folder) { folder.createFolder(); } else { logError(BaseMessages.getString(PKG, "JobMoveFiles.Log.Error.FolderMissing", MoveToFolder)); return result; } } if (!folder.getType().equals(FileType.FOLDER)) { logError(BaseMessages.getString(PKG, "JobMoveFiles.Log.Error.NotFolder", MoveToFolder)); return result; } } catch (Exception e) { logError(BaseMessages.getString(PKG, "JobMoveFiles.Log.Error.GettingMoveToFolder", MoveToFolder, e.getMessage())); return result; } finally { if (folder != null) { try { folder.close(); } catch (IOException ex) { /* Ignore */ } } } } if (arg_from_previous) { if (log.isDetailed()) { logDetailed(BaseMessages.getString(PKG, "JobMoveFiles.Log.ArgFromPrevious.Found", (rows != null ? rows.size() : 0) + "")); } } if (arg_from_previous && rows != null) { for (int iteration = 0; iteration < rows.size() && !parentJob.isStopped(); iteration++) { // Success condition broken? if (successConditionBroken) { if (!successConditionBrokenExit) { logError(BaseMessages.getString(PKG, "JobMoveFiles.Error.SuccessConditionbroken", "" + NrErrors)); successConditionBrokenExit = true; } result.setNrErrors(NrErrors); displayResults(); return result; } resultRow = rows.get(iteration); // Get source and destination file names, also wildcard String vsourcefilefolder_previous = resultRow.getString(0, null); String vdestinationfilefolder_previous = resultRow.getString(1, null); String vwildcard_previous = resultRow.getString(2, null); if (!Const.isEmpty(vsourcefilefolder_previous) && !Const.isEmpty(vdestinationfilefolder_previous)) { if (log.isDetailed()) { logDetailed(BaseMessages.getString(PKG, "JobMoveFiles.Log.ProcessingRow", vsourcefilefolder_previous, vdestinationfilefolder_previous, vwildcard_previous)); } if (!ProcessFileFolder(vsourcefilefolder_previous, vdestinationfilefolder_previous, vwildcard_previous, parentJob, result, MoveToFolder)) { // The move process fail // Update Errors updateErrors(); } } else { if (log.isDetailed()) { logDetailed(BaseMessages.getString(PKG, "JobMoveFiles.Log.IgnoringRow", vsourcefilefolder[iteration], vdestinationfilefolder[iteration], vwildcard[iteration])); } } } } else if (vsourcefilefolder != null && vdestinationfilefolder != null) { for (int i = 0; i < vsourcefilefolder.length && !parentJob.isStopped(); i++) { // Success condition broken? if (successConditionBroken) { if (!successConditionBrokenExit) { logError(BaseMessages.getString(PKG, "JobMoveFiles.Error.SuccessConditionbroken", "" + NrErrors)); successConditionBrokenExit = true; } result.setNrErrors(NrErrors); displayResults(); return result; } if (!Const.isEmpty(vsourcefilefolder[i]) && !Const.isEmpty(vdestinationfilefolder[i])) { // ok we can process this file/folder if (log.isDetailed()) { logDetailed(BaseMessages.getString(PKG, "JobMoveFiles.Log.ProcessingRow", vsourcefilefolder[i], vdestinationfilefolder[i], vwildcard[i])); } if (!ProcessFileFolder(vsourcefilefolder[i], vdestinationfilefolder[i], vwildcard[i], parentJob, result, MoveToFolder)) { // Update Errors updateErrors(); } } else { if (log.isDetailed()) { logDetailed(BaseMessages.getString(PKG, "JobMoveFiles.Log.IgnoringRow", vsourcefilefolder[i], vdestinationfilefolder[i], vwildcard[i])); } } } } // Success Condition result.setNrErrors(NrErrors); result.setNrLinesWritten(NrSuccess); if (getSuccessStatus()) { result.setResult(true); } displayResults(); return result; }
From source file:org.pentaho.di.job.entries.movefiles.JobEntryMoveFiles.java
private boolean CreateDestinationFolder(FileObject filefolder) { FileObject folder = null; try {/*w w w . j a 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 (log.isDetailed()) { logDetailed(BaseMessages.getString(PKG, "JobMoveFiles.Log.FolderNotExist", folder.getName().toString())); } folder.createFolder(); if (log.isDetailed()) { logDetailed(BaseMessages.getString(PKG, "JobMoveFiles.Log.FolderWasCreated", folder.getName().toString())); } } else { logError(BaseMessages.getString(PKG, "JobMoveFiles.Log.FolderNotExist", folder.getName().toString())); return false; } } return true; } catch (Exception e) { logError(BaseMessages.getString(PKG, "JobMoveFiles.Log.CanNotCreateParentFolder", folder.getName().toString()), e); } finally { if (folder != null) { try { folder.close(); } catch (Exception ex) { /* Ignore */ } } } return false; }
From source file:org.pentaho.di.job.entries.pgpdecryptfiles.JobEntryPGPDecryptFiles.java
public Result execute(Result previousResult, int nr) throws KettleException { try {// w ww . ja v a 2 s . c o m Result result = previousResult; List<RowMetaAndData> rows = result.getRows(); RowMetaAndData resultRow = null; result.setNrErrors(1); result.setResult(false); NrErrors = 0; NrSuccess = 0; successConditionBroken = false; successConditionBrokenExit = false; limitFiles = Const.toInt(environmentSubstitute(getNrErrorsLessThan()), 10); if (include_subfolders) { if (isDetailed()) { logDetailed(BaseMessages.getString(PKG, "JobPGPDecryptFiles.Log.IncludeSubFoldersOn")); } } String MoveToFolder = environmentSubstitute(destinationFolder); // Get source and destination files, also wildcard String[] vsourcefilefolder = source_filefolder; String[] vpassphrase = passphrase; String[] vdestinationfilefolder = destination_filefolder; String[] vwildcard = wildcard; if (iffileexists.equals("move_file")) { if (Const.isEmpty(MoveToFolder)) { logError(BaseMessages.getString(PKG, "JobPGPDecryptFiles.Log.Error.MoveToFolderMissing")); return result; } FileObject folder = null; try { folder = KettleVFS.getFileObject(MoveToFolder); if (!folder.exists()) { if (isDetailed()) { logDetailed(BaseMessages.getString(PKG, "JobPGPDecryptFiles.Log.Error.FolderMissing", MoveToFolder)); } if (create_move_to_folder) { folder.createFolder(); } else { logError(BaseMessages.getString(PKG, "JobPGPDecryptFiles.Log.Error.FolderMissing", MoveToFolder)); return result; } } if (!folder.getType().equals(FileType.FOLDER)) { logError(BaseMessages.getString(PKG, "JobPGPDecryptFiles.Log.Error.NotFolder", MoveToFolder)); return result; } } catch (Exception e) { logError(BaseMessages.getString(PKG, "JobPGPDecryptFiles.Log.Error.GettingMoveToFolder", MoveToFolder, e.getMessage())); return result; } finally { if (folder != null) { try { folder.close(); } catch (IOException ex) { /* Ignore */ } } } } gpg = new GPG(environmentSubstitute(gpglocation), log); if (arg_from_previous) { if (isDetailed()) { logDetailed(BaseMessages.getString(PKG, "JobPGPDecryptFiles.Log.ArgFromPrevious.Found", (rows != null ? rows.size() : 0) + "")); } } if (arg_from_previous && rows != null) { for (int iteration = 0; iteration < rows.size(); iteration++) { // Success condition broken? if (successConditionBroken) { if (!successConditionBrokenExit) { logError(BaseMessages.getString(PKG, "JobPGPDecryptFiles.Error.SuccessConditionbroken", "" + NrErrors)); successConditionBrokenExit = true; } result.setNrErrors(NrErrors); displayResults(); return result; } resultRow = rows.get(iteration); // Get source and destination file names, also wildcard String vsourcefilefolder_previous = resultRow.getString(0, null); String vwildcard_previous = Encr.decryptPasswordOptionallyEncrypted( environmentSubstitute(resultRow.getString(1, null))); String vpassphrase_previous = resultRow.getString(2, null); String vdestinationfilefolder_previous = resultRow.getString(3, null); if (!Const.isEmpty(vsourcefilefolder_previous) && !Const.isEmpty(vdestinationfilefolder_previous)) { if (isDetailed()) { logDetailed(BaseMessages.getString(PKG, "JobPGPDecryptFiles.Log.ProcessingRow", vsourcefilefolder_previous, vdestinationfilefolder_previous, vwildcard_previous)); } if (!ProcessFileFolder(vsourcefilefolder_previous, vpassphrase_previous, vdestinationfilefolder_previous, vwildcard_previous, parentJob, result, MoveToFolder)) { // The move process fail // Update Errors updateErrors(); } } else { if (isDetailed()) { logDetailed(BaseMessages.getString(PKG, "JobPGPDecryptFiles.Log.IgnoringRow", vsourcefilefolder[iteration], vdestinationfilefolder[iteration], vwildcard[iteration])); } } } } else if (vsourcefilefolder != null && vdestinationfilefolder != null) { for (int i = 0; i < vsourcefilefolder.length && !parentJob.isStopped(); i++) { // Success condition broken? if (successConditionBroken) { if (!successConditionBrokenExit) { logError(BaseMessages.getString(PKG, "JobPGPDecryptFiles.Error.SuccessConditionbroken", "" + NrErrors)); successConditionBrokenExit = true; } result.setNrErrors(NrErrors); displayResults(); return result; } if (!Const.isEmpty(vsourcefilefolder[i]) && !Const.isEmpty(vdestinationfilefolder[i])) { // ok we can process this file/folder if (isDetailed()) { logDetailed(BaseMessages.getString(PKG, "JobPGPDecryptFiles.Log.ProcessingRow", vsourcefilefolder[i], vdestinationfilefolder[i], vwildcard[i])); } if (!ProcessFileFolder(vsourcefilefolder[i], Encr.decryptPasswordOptionallyEncrypted(environmentSubstitute(vpassphrase[i])), vdestinationfilefolder[i], vwildcard[i], parentJob, result, MoveToFolder)) { // Update Errors updateErrors(); } } else { if (isDetailed()) { logDetailed(BaseMessages.getString(PKG, "JobPGPDecryptFiles.Log.IgnoringRow", vsourcefilefolder[i], vdestinationfilefolder[i], vwildcard[i])); } } } } // Success Condition result.setNrErrors(NrErrors); result.setNrLinesWritten(NrSuccess); if (getSuccessStatus()) { result.setResult(true); } displayResults(); return result; } finally { if (source_filefolder != null) { source_filefolder = null; } if (destination_filefolder != null) { destination_filefolder = null; } } }
From source file:org.pentaho.di.job.entries.pgpdecryptfiles.JobEntryPGPDecryptFiles.java
private boolean CreateDestinationFolder(FileObject filefolder) { FileObject folder = null; try {/* w ww . ja va 2 s. c o m*/ if (destination_is_a_file) { folder = filefolder.getParent(); } else { folder = filefolder; } if (!folder.exists()) { if (create_destination_folder) { if (isDetailed()) { logDetailed(BaseMessages.getString(PKG, "JobPGPDecryptFiles.Log.FolderNotExist", folder.getName().toString())); } folder.createFolder(); if (isDetailed()) { logDetailed(BaseMessages.getString(PKG, "JobPGPDecryptFiles.Log.FolderWasCreated", folder.getName().toString())); } } else { logError(BaseMessages.getString(PKG, "JobPGPDecryptFiles.Log.FolderNotExist", folder.getName().toString())); return false; } } return true; } catch (Exception e) { logError(BaseMessages.getString(PKG, "JobPGPDecryptFiles.Log.CanNotCreateParentFolder", folder.getName().toString()), e); } finally { if (folder != null) { try { folder.close(); } catch (Exception ex) { /* Ignore */ } } } return false; }
From source file:org.pentaho.di.job.entries.pgpencryptfiles.JobEntryPGPEncryptFiles.java
public Result execute(Result previousResult, int nr) { Result result = previousResult; List<RowMetaAndData> rows = result.getRows(); RowMetaAndData resultRow = null;/*from ww w . j a v a2 s .c om*/ result.setNrErrors(1); result.setResult(false); try { NrErrors = 0; NrSuccess = 0; successConditionBroken = false; successConditionBrokenExit = false; limitFiles = Const.toInt(environmentSubstitute(getNrErrorsLessThan()), 10); if (include_subfolders) { if (isDetailed()) { logDetailed(BaseMessages.getString(PKG, "JobPGPEncryptFiles.Log.IncludeSubFoldersOn")); } } String MoveToFolder = environmentSubstitute(destinationFolder); // Get source and destination files, also wildcard String[] vsourcefilefolder = source_filefolder; String[] vuserid = userid; String[] vdestinationfilefolder = destination_filefolder; String[] vwildcard = wildcard; if (iffileexists.equals("move_file")) { if (Const.isEmpty(MoveToFolder)) { logError(toString(), BaseMessages.getString(PKG, "JobPGPEncryptFiles.Log.Error.MoveToFolderMissing")); return result; } FileObject folder = null; try { folder = KettleVFS.getFileObject(MoveToFolder); if (!folder.exists()) { if (isDetailed()) { logDetailed(BaseMessages.getString(PKG, "JobPGPEncryptFiles.Log.Error.FolderMissing", MoveToFolder)); } if (create_move_to_folder) { folder.createFolder(); } else { logError(BaseMessages.getString(PKG, "JobPGPEncryptFiles.Log.Error.FolderMissing", MoveToFolder)); return result; } } if (!folder.getType().equals(FileType.FOLDER)) { logError(BaseMessages.getString(PKG, "JobPGPEncryptFiles.Log.Error.NotFolder", MoveToFolder)); return result; } } catch (Exception e) { logError(BaseMessages.getString(PKG, "JobPGPEncryptFiles.Log.Error.GettingMoveToFolder", MoveToFolder, e.getMessage())); return result; } finally { if (folder != null) { try { folder.close(); } catch (IOException ex) { /* Ignore */ } } } } gpg = new GPG(environmentSubstitute(gpglocation), log); if (arg_from_previous) { if (isDetailed()) { logDetailed(BaseMessages.getString(PKG, "JobPGPEncryptFiles.Log.ArgFromPrevious.Found", (rows != null ? rows.size() : 0) + "")); } } if (arg_from_previous && rows != null) { for (int iteration = 0; iteration < rows.size(); iteration++) { // Success condition broken? if (successConditionBroken) { if (!successConditionBrokenExit) { logError(BaseMessages.getString(PKG, "JobPGPEncryptFiles.Error.SuccessConditionbroken", "" + NrErrors)); successConditionBrokenExit = true; } result.setNrErrors(NrErrors); displayResults(); return result; } resultRow = rows.get(iteration); // Get source and destination file names, also wildcard int vactionType_previous = getActionTypeByCode(resultRow.getString(0, null)); String vsourcefilefolder_previous = resultRow.getString(1, null); String vwildcard_previous = environmentSubstitute(resultRow.getString(2, null)); String vuserid_previous = resultRow.getString(3, null); String vdestinationfilefolder_previous = resultRow.getString(4, null); if (!Const.isEmpty(vsourcefilefolder_previous) && !Const.isEmpty(vdestinationfilefolder_previous)) { if (isDetailed()) { logDetailed(BaseMessages.getString(PKG, "JobPGPEncryptFiles.Log.ProcessingRow", vsourcefilefolder_previous, vdestinationfilefolder_previous, vwildcard_previous)); } if (!ProcessFileFolder(vactionType_previous, vsourcefilefolder_previous, vuserid_previous, vdestinationfilefolder_previous, vwildcard_previous, parentJob, result, MoveToFolder)) { // The process fail // Update Errors updateErrors(); } } else { if (isDetailed()) { logDetailed(BaseMessages.getString(PKG, "JobPGPEncryptFiles.Log.IgnoringRow", vsourcefilefolder[iteration], vdestinationfilefolder[iteration], vwildcard[iteration])); } } } } else if (vsourcefilefolder != null && vdestinationfilefolder != null) { for (int i = 0; i < vsourcefilefolder.length && !parentJob.isStopped(); i++) { // Success condition broken? if (successConditionBroken) { if (!successConditionBrokenExit) { logError(BaseMessages.getString(PKG, "JobPGPEncryptFiles.Error.SuccessConditionbroken", "" + NrErrors)); successConditionBrokenExit = true; } result.setNrErrors(NrErrors); displayResults(); return result; } if (!Const.isEmpty(vsourcefilefolder[i]) && !Const.isEmpty(vdestinationfilefolder[i])) { // ok we can process this file/folder if (isDetailed()) { logDetailed(BaseMessages.getString(PKG, "JobPGPEncryptFiles.Log.ProcessingRow", vsourcefilefolder[i], vdestinationfilefolder[i], vwildcard[i])); } if (!ProcessFileFolder(action_type[i], vsourcefilefolder[i], vuserid[i], vdestinationfilefolder[i], vwildcard[i], parentJob, result, MoveToFolder)) { // Update Errors updateErrors(); } } else { if (isDetailed()) { logDetailed(BaseMessages.getString(PKG, "JobPGPEncryptFiles.Log.IgnoringRow", vsourcefilefolder[i], vdestinationfilefolder[i], vwildcard[i])); } } } } } catch (Exception e) { updateErrors(); logError(BaseMessages.getString("JobPGPEncryptFiles.Error", e.getMessage())); } finally { if (source_filefolder != null) { source_filefolder = null; } if (destination_filefolder != null) { destination_filefolder = null; } } // Success Condition result.setNrErrors(NrErrors); result.setNrLinesWritten(NrSuccess); if (getSuccessStatus()) { result.setResult(true); } displayResults(); return result; }
From source file:org.pentaho.di.job.entries.pgpencryptfiles.JobEntryPGPEncryptFiles.java
private boolean CreateDestinationFolder(FileObject filefolder) { FileObject folder = null; try {//w ww . j ava 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(BaseMessages.getString(PKG, "JobPGPEncryptFiles.Log.FolderNotExist", folder.getName().toString())); } folder.createFolder(); if (isDetailed()) { logDetailed(BaseMessages.getString(PKG, "JobPGPEncryptFiles.Log.FolderWasCreated", folder.getName().toString())); } } else { logError(BaseMessages.getString(PKG, "JobPGPEncryptFiles.Log.FolderNotExist", folder.getName().toString())); return false; } } return true; } catch (Exception e) { logError(BaseMessages.getString(PKG, "JobPGPEncryptFiles.Log.CanNotCreateParentFolder", folder.getName().toString()), e); } finally { if (folder != null) { try { folder.close(); } catch (Exception ex) { /* Ignore */ } } } return false; }
From source file:org.pentaho.di.job.entries.sftp.JobEntrySFTP.java
public Result execute(Result previousResult, int nr) { Result result = previousResult; List<RowMetaAndData> rows = result.getRows(); RowMetaAndData resultRow = null;//from www.j a v a 2s. c om result.setResult(false); long filesRetrieved = 0; if (log.isDetailed()) { logDetailed(BaseMessages.getString(PKG, "JobSFTP.Log.StartJobEntry")); } HashSet<String> list_previous_filenames = new HashSet<String>(); if (copyprevious) { if (rows.size() == 0) { if (log.isDetailed()) { logDetailed(BaseMessages.getString(PKG, "JobSFTP.ArgsFromPreviousNothing")); } result.setResult(true); return result; } try { // Copy the input row to the (command line) arguments for (int iteration = 0; iteration < rows.size(); iteration++) { resultRow = rows.get(iteration); // Get file names String file_previous = resultRow.getString(0, null); if (!Const.isEmpty(file_previous)) { list_previous_filenames.add(file_previous); if (log.isDebug()) { logDebug(BaseMessages.getString(PKG, "JobSFTP.Log.FilenameFromResult", file_previous)); } } } } catch (Exception e) { logError(BaseMessages.getString(PKG, "JobSFTP.Error.ArgFromPrevious")); result.setNrErrors(1); return result; } } SFTPClient sftpclient = null; // String substitution.. String realServerName = environmentSubstitute(serverName); String realServerPort = environmentSubstitute(serverPort); String realUsername = environmentSubstitute(userName); String realPassword = Encr.decryptPasswordOptionallyEncrypted(environmentSubstitute(password)); String realSftpDirString = environmentSubstitute(sftpDirectory); String realWildcard = environmentSubstitute(wildcard); String realTargetDirectory = environmentSubstitute(targetDirectory); String realKeyFilename = null; String realPassPhrase = null; FileObject TargetFolder = null; try { // Let's perform some checks before starting if (isUseKeyFile()) { // We must have here a private keyfilename realKeyFilename = environmentSubstitute(getKeyFilename()); if (Const.isEmpty(realKeyFilename)) { // Error..Missing keyfile logError(BaseMessages.getString(PKG, "JobSFTP.Error.KeyFileMissing")); result.setNrErrors(1); return result; } if (!KettleVFS.fileExists(realKeyFilename)) { // Error.. can not reach keyfile logError(BaseMessages.getString(PKG, "JobSFTP.Error.KeyFileNotFound", realKeyFilename)); result.setNrErrors(1); return result; } realPassPhrase = environmentSubstitute(getKeyPassPhrase()); } if (!Const.isEmpty(realTargetDirectory)) { TargetFolder = KettleVFS.getFileObject(realTargetDirectory, this); boolean TargetFolderExists = TargetFolder.exists(); if (TargetFolderExists) { if (log.isDetailed()) { logDetailed( BaseMessages.getString(PKG, "JobSFTP.Log.TargetFolderExists", realTargetDirectory)); } } else { logError(BaseMessages.getString(PKG, "JobSFTP.Error.TargetFolderNotExists", realTargetDirectory)); if (!createtargetfolder) { // Error..Target folder can not be found ! result.setNrErrors(1); return result; } else { // create target folder TargetFolder.createFolder(); if (log.isDetailed()) { logDetailed(BaseMessages.getString(PKG, "JobSFTP.Log.TargetFolderCreated", realTargetDirectory)); } } } } if (TargetFolder != null) { TargetFolder.close(); TargetFolder = null; } // Create sftp client to host ... sftpclient = new SFTPClient(InetAddress.getByName(realServerName), Const.toInt(realServerPort, DEFAULT_PORT), realUsername, realKeyFilename, realPassPhrase); if (log.isDetailed()) { logDetailed(BaseMessages.getString(PKG, "JobSFTP.Log.OpenedConnection", realServerName, realServerPort, realUsername)); } // Set compression sftpclient.setCompression(getCompression()); // Set proxy? String realProxyHost = environmentSubstitute(getProxyHost()); if (!Const.isEmpty(realProxyHost)) { // Set proxy sftpclient.setProxy(realProxyHost, environmentSubstitute(getProxyPort()), environmentSubstitute(getProxyUsername()), environmentSubstitute(getProxyPassword()), getProxyType()); } // login to ftp host ... sftpclient.login(realPassword); // Passwords should not appear in log files. // logDetailed("logged in using password "+realPassword); // Logging this seems a bad idea! Oh well. // move to spool dir ... if (!Const.isEmpty(realSftpDirString)) { try { sftpclient.chdir(realSftpDirString); } catch (Exception e) { logError( BaseMessages.getString(PKG, "JobSFTP.Error.CanNotFindRemoteFolder", realSftpDirString)); throw new Exception(e); } if (log.isDetailed()) { logDetailed(BaseMessages.getString(PKG, "JobSFTP.Log.ChangedDirectory", realSftpDirString)); } } Pattern pattern = null; // Get all the files in the current directory... String[] filelist = sftpclient.dir(); if (filelist == null) { // Nothing was found !!! exit result.setResult(true); if (log.isDetailed()) { logDetailed(BaseMessages.getString(PKG, "JobSFTP.Log.Found", "" + 0)); } return result; } if (log.isDetailed()) { logDetailed(BaseMessages.getString(PKG, "JobSFTP.Log.Found", "" + filelist.length)); } if (!copyprevious) { if (!Const.isEmpty(realWildcard)) { pattern = Pattern.compile(realWildcard); } } // Get the files in the list... for (int i = 0; i < filelist.length && !parentJob.isStopped(); i++) { boolean getIt = true; if (copyprevious) { // filenames list is send by previous job entry // download if the current file is in this list getIt = list_previous_filenames.contains(filelist[i]); } else { // download files // but before see if the file matches the regular expression! if (pattern != null) { Matcher matcher = pattern.matcher(filelist[i]); getIt = matcher.matches(); } } if (getIt) { if (log.isDebug()) { logDebug(BaseMessages.getString(PKG, "JobSFTP.Log.GettingFiles", filelist[i], realTargetDirectory)); } String targetFilename = realTargetDirectory + Const.FILE_SEPARATOR + filelist[i]; sftpclient.get(targetFilename, filelist[i]); filesRetrieved++; if (isaddresult) { // Add to the result files... ResultFile resultFile = new ResultFile(ResultFile.FILE_TYPE_GENERAL, KettleVFS.getFileObject(targetFilename, this), parentJob.getJobname(), toString()); result.getResultFiles().put(resultFile.getFile().toString(), resultFile); if (log.isDetailed()) { logDetailed(BaseMessages.getString(PKG, "JobSFTP.Log.FilenameAddedToResultFilenames", filelist[i])); } } if (log.isDetailed()) { logDetailed(BaseMessages.getString(PKG, "JobSFTP.Log.TransferedFile", filelist[i])); } // Delete the file if this is needed! if (remove) { sftpclient.delete(filelist[i]); if (log.isDetailed()) { logDetailed(BaseMessages.getString(PKG, "JobSFTP.Log.DeletedFile", filelist[i])); } } } } result.setResult(true); result.setNrFilesRetrieved(filesRetrieved); } catch (Exception e) { result.setNrErrors(1); logError(BaseMessages.getString(PKG, "JobSFTP.Error.GettingFiles", e.getMessage())); logError(Const.getStackTracker(e)); } finally { // close connection, if possible try { if (sftpclient != null) { sftpclient.disconnect(); } } catch (Exception e) { // just ignore this, makes no big difference } try { if (TargetFolder != null) { TargetFolder.close(); TargetFolder = null; } if (list_previous_filenames != null) { list_previous_filenames = null; } } catch (Exception e) { // Ignore errors } } return result; }
From source file:org.pentaho.di.job.entries.sftpput.JobEntrySFTPPUT.java
public Result execute(Result previousResult, int nr) throws KettleException { Result result = previousResult; List<RowMetaAndData> rows = result.getRows(); result.setResult(false);// w w w. jav a 2 s . c o m if (log.isDetailed()) { logDetailed(BaseMessages.getString(PKG, "JobSFTPPUT.Log.StartJobEntry")); } ArrayList<FileObject> myFileList = new ArrayList<FileObject>(); if (copyprevious) { if (rows.size() == 0) { if (log.isDetailed()) { logDetailed(BaseMessages.getString(PKG, "JobSFTPPUT.ArgsFromPreviousNothing")); } result.setResult(true); return result; } try { RowMetaAndData resultRow = null; // Copy the input row to the (command line) arguments for (int iteration = 0; iteration < rows.size(); iteration++) { resultRow = rows.get(iteration); // Get file names String file_previous = resultRow.getString(0, null); if (!Const.isEmpty(file_previous)) { FileObject file = KettleVFS.getFileObject(file_previous, this); if (!file.exists()) { logError(BaseMessages.getString(PKG, "JobSFTPPUT.Log.FilefromPreviousNotFound", file_previous)); } else { myFileList.add(file); if (log.isDebug()) { logDebug(BaseMessages.getString(PKG, "JobSFTPPUT.Log.FilenameFromResult", file_previous)); } } } } } catch (Exception e) { logError(BaseMessages.getString(PKG, "JobSFTPPUT.Error.ArgFromPrevious")); result.setNrErrors(1); // free resource myFileList = null; return result; } } if (copypreviousfiles) { List<ResultFile> resultFiles = result.getResultFilesList(); if (resultFiles == null || resultFiles.size() == 0) { if (log.isDetailed()) { logDetailed(BaseMessages.getString(PKG, "JobSFTPPUT.ArgsFromPreviousNothingFiles")); } result.setResult(true); return result; } try { for (Iterator<ResultFile> it = resultFiles.iterator(); it.hasNext() && !parentJob.isStopped();) { ResultFile resultFile = it.next(); FileObject file = resultFile.getFile(); if (file != null) { if (!file.exists()) { logError(BaseMessages.getString(PKG, "JobSFTPPUT.Log.FilefromPreviousNotFound", file.toString())); } else { myFileList.add(file); if (log.isDebug()) { logDebug(BaseMessages.getString(PKG, "JobSFTPPUT.Log.FilenameFromResult", file.toString())); } } } } } catch (Exception e) { logError(BaseMessages.getString(PKG, "JobSFTPPUT.Error.ArgFromPrevious")); result.setNrErrors(1); // free resource myFileList = null; return result; } } SFTPClient sftpclient = null; // String substitution.. String realServerName = environmentSubstitute(serverName); String realServerPort = environmentSubstitute(serverPort); String realUsername = environmentSubstitute(userName); String realPassword = Encr.decryptPasswordOptionallyEncrypted(environmentSubstitute(password)); String realSftpDirString = environmentSubstitute(sftpDirectory); String realWildcard = environmentSubstitute(wildcard); String realLocalDirectory = environmentSubstitute(localDirectory); String realKeyFilename = null; String realPassPhrase = null; // Destination folder (Move to) String realDestinationFolder = environmentSubstitute(getDestinationFolder()); try { // Let's perform some checks before starting if (getAfterFTPS() == AFTER_FTPSPUT_MOVE) { if (Const.isEmpty(realDestinationFolder)) { logError(BaseMessages.getString(PKG, "JobSSH2PUT.Log.DestinatFolderMissing")); result.setNrErrors(1); return result; } else { FileObject folder = null; try { folder = KettleVFS.getFileObject(realDestinationFolder, this); // Let's check if folder exists... if (!folder.exists()) { // Do we need to create it? if (createDestinationFolder) { folder.createFolder(); } else { logError(BaseMessages.getString(PKG, "JobSSH2PUT.Log.DestinatFolderNotExist", realDestinationFolder)); result.setNrErrors(1); return result; } } realDestinationFolder = KettleVFS.getFilename(folder); } catch (Exception e) { throw new KettleException(e); } finally { if (folder != null) { try { folder.close(); } catch (Exception e) { /* Ignore */ } } } } } if (isUseKeyFile()) { // We must have here a private keyfilename realKeyFilename = environmentSubstitute(getKeyFilename()); if (Const.isEmpty(realKeyFilename)) { // Error..Missing keyfile logError(BaseMessages.getString(PKG, "JobSFTP.Error.KeyFileMissing")); result.setNrErrors(1); return result; } if (!KettleVFS.fileExists(realKeyFilename)) { // Error.. can not reach keyfile logError(BaseMessages.getString(PKG, "JobSFTP.Error.KeyFileNotFound")); result.setNrErrors(1); return result; } realPassPhrase = environmentSubstitute(getKeyPassPhrase()); } // Create sftp client to host ... sftpclient = new SFTPClient(InetAddress.getByName(realServerName), Const.toInt(realServerPort, 22), realUsername, realKeyFilename, realPassPhrase); if (log.isDetailed()) { logDetailed(BaseMessages.getString(PKG, "JobSFTPPUT.Log.OpenedConnection", realServerName, "" + realServerPort, realUsername)); } // Set compression sftpclient.setCompression(getCompression()); // Set proxy? String realProxyHost = environmentSubstitute(getProxyHost()); if (!Const.isEmpty(realProxyHost)) { // Set proxy sftpclient.setProxy(realProxyHost, environmentSubstitute(getProxyPort()), environmentSubstitute(getProxyUsername()), environmentSubstitute(getProxyPassword()), getProxyType()); } // login to ftp host ... sftpclient.login(realPassword); // Don't show the password in the logs, it's not good for security audits // logDetailed("logged in using password "+realPassword); // Logging this seems a bad idea! Oh well. // move to spool dir ... if (!Const.isEmpty(realSftpDirString)) { boolean existfolder = sftpclient.folderExists(realSftpDirString); if (!existfolder) { if (!isCreateRemoteFolder()) { throw new KettleException(BaseMessages.getString(PKG, "JobSFTPPUT.Error.CanNotFindRemoteFolder", realSftpDirString)); } if (log.isDetailed()) { logDetailed(BaseMessages.getString(PKG, "JobSFTPPUT.Error.CanNotFindRemoteFolder", realSftpDirString)); } // Let's create folder sftpclient.createFolder(realSftpDirString); if (log.isDetailed()) { logDetailed(BaseMessages.getString(PKG, "JobSFTPPUT.Log.RemoteFolderCreated", realSftpDirString)); } } sftpclient.chdir(realSftpDirString); if (log.isDetailed()) { logDetailed(BaseMessages.getString(PKG, "JobSFTPPUT.Log.ChangedDirectory", realSftpDirString)); } } // end if if (!copyprevious && !copypreviousfiles) { // Get all the files in the local directory... myFileList = new ArrayList<FileObject>(); FileObject localFiles = KettleVFS.getFileObject(realLocalDirectory, this); FileObject[] children = localFiles.getChildren(); if (children != null) { for (int i = 0; i < children.length; i++) { // Get filename of file or directory if (children[i].getType().equals(FileType.FILE)) { // myFileList.add(children[i].getAbsolutePath()); myFileList.add(children[i]); } } // end for } } if (myFileList == null || myFileList.size() == 0) { if (isSuccessWhenNoFile()) { // Just warn user if (isBasic()) { logBasic(BaseMessages.getString(PKG, "JobSFTPPUT.Error.NoFileToSend")); } } else { // Fail logError(BaseMessages.getString(PKG, "JobSFTPPUT.Error.NoFileToSend")); result.setNrErrors(1); return result; } } if (log.isDetailed()) { logDetailed( BaseMessages.getString(PKG, "JobSFTPPUT.Log.RowsFromPreviousResult", myFileList.size())); } Pattern pattern = null; if (!copyprevious && !copypreviousfiles) { if (!Const.isEmpty(realWildcard)) { pattern = Pattern.compile(realWildcard); } } // Get the files in the list and execute sftp.put() for each file Iterator<FileObject> it = myFileList.iterator(); while (it.hasNext() && !parentJob.isStopped()) { FileObject myFile = it.next(); try { String localFilename = myFile.toString(); String destinationFilename = myFile.getName().getBaseName(); boolean getIt = true; // First see if the file matches the regular expression! if (pattern != null) { Matcher matcher = pattern.matcher(destinationFilename); getIt = matcher.matches(); } if (getIt) { if (log.isDebug()) { logDebug(BaseMessages.getString(PKG, "JobSFTPPUT.Log.PuttingFile", localFilename, realSftpDirString)); } sftpclient.put(myFile, destinationFilename); if (log.isDetailed()) { logDetailed( BaseMessages.getString(PKG, "JobSFTPPUT.Log.TransferedFile", localFilename)); } // We successfully uploaded the file // what's next ... switch (getAfterFTPS()) { case AFTER_FTPSPUT_DELETE: myFile.delete(); if (log.isDetailed()) { logDetailed( BaseMessages.getString(PKG, "JobSFTPPUT.Log.DeletedFile", localFilename)); } break; case AFTER_FTPSPUT_MOVE: FileObject destination = null; try { destination = KettleVFS.getFileObject(realDestinationFolder + Const.FILE_SEPARATOR + myFile.getName().getBaseName(), this); myFile.moveTo(destination); if (log.isDetailed()) { logDetailed(BaseMessages.getString(PKG, "JobSFTPPUT.Log.FileMoved", myFile, destination)); } } finally { if (destination != null) { destination.close(); } } break; default: if (addFilenameResut) { // Add to the result files... ResultFile resultFile = new ResultFile(ResultFile.FILE_TYPE_GENERAL, myFile, parentJob.getJobname(), toString()); result.getResultFiles().put(resultFile.getFile().toString(), resultFile); if (log.isDetailed()) { logDetailed(BaseMessages.getString(PKG, "JobSFTPPUT.Log.FilenameAddedToResultFilenames", localFilename)); } } break; } } } finally { if (myFile != null) { myFile.close(); } } } // end for result.setResult(true); // JKU: no idea if this is needed...! // result.setNrFilesRetrieved(filesRetrieved); } catch (Exception e) { result.setNrErrors(1); logError(BaseMessages.getString(PKG, "JobSFTPPUT.Exception", e.getMessage())); logError(Const.getStackTracker(e)); } finally { // close connection, if possible try { if (sftpclient != null) { sftpclient.disconnect(); } } catch (Exception e) { // just ignore this, makes no big difference } // end catch myFileList = null; } // end finally return result; }