use of com.enterprisedt.net.ftp.FTPException in project pentaho-kettle by pentaho.
the class JobEntryFTPPUT method execute.
public Result execute(Result previousResult, int nr) {
Result result = previousResult;
result.setResult(false);
long filesput = 0;
if (log.isDetailed()) {
logDetailed(BaseMessages.getString(PKG, "JobFTPPUT.Log.Starting"));
}
FTPClient ftpclient = null;
try {
// Create ftp client to host:port ...
ftpclient = createAndSetUpFtpClient();
// login to ftp host ...
String realUsername = environmentSubstitute(userName);
String realPassword = Encr.decryptPasswordOptionallyEncrypted(environmentSubstitute(password));
ftpclient.connect();
ftpclient.login(realUsername, realPassword);
// set BINARY
if (binaryMode) {
ftpclient.setType(FTPTransferType.BINARY);
if (log.isDetailed()) {
logDetailed(BaseMessages.getString(PKG, "JobFTPPUT.Log.BinaryMode"));
}
}
// Remove password from logging, you don't know where it ends up.
if (log.isDetailed()) {
logDetailed(BaseMessages.getString(PKG, "JobFTPPUT.Log.Logged", realUsername));
}
// Fix for PDI-2534 - add auxilliary FTP File List parsers to the ftpclient object.
this.hookInOtherParsers(ftpclient);
// move to spool dir ...
String realRemoteDirectory = environmentSubstitute(remoteDirectory);
if (!Utils.isEmpty(realRemoteDirectory)) {
ftpclient.chdir(realRemoteDirectory);
if (log.isDetailed()) {
logDetailed(BaseMessages.getString(PKG, "JobFTPPUT.Log.ChangedDirectory", realRemoteDirectory));
}
}
String realLocalDirectory = environmentSubstitute(localDirectory);
if (realLocalDirectory == null) {
throw new FTPException(BaseMessages.getString(PKG, "JobFTPPUT.LocalDir.NotSpecified"));
} else {
// handle file:/// prefix
if (realLocalDirectory.startsWith("file:")) {
realLocalDirectory = new URI(realLocalDirectory).getPath();
}
}
final List<String> files;
File localFiles = new File(realLocalDirectory);
File[] children = localFiles.listFiles();
if (children == null) {
files = Collections.emptyList();
} else {
files = new ArrayList<String>(children.length);
for (File child : children) {
// Get filename of file or directory
if (!child.isDirectory()) {
files.add(child.getName());
}
}
}
if (log.isDetailed()) {
logDetailed(BaseMessages.getString(PKG, "JobFTPPUT.Log.FoundFileLocalDirectory", "" + files.size(), realLocalDirectory));
}
String realWildcard = environmentSubstitute(wildcard);
Pattern pattern;
if (!Utils.isEmpty(realWildcard)) {
pattern = Pattern.compile(realWildcard);
} else {
pattern = null;
}
for (String file : files) {
if (parentJob.isStopped()) {
break;
}
boolean toBeProcessed = true;
// First see if the file matches the regular expression!
if (pattern != null) {
Matcher matcher = pattern.matcher(file);
toBeProcessed = matcher.matches();
}
if (toBeProcessed) {
// File exists?
boolean fileExist = false;
try {
fileExist = ftpclient.exists(file);
} catch (Exception e) {
// Assume file does not exist !!
}
if (log.isDebug()) {
if (fileExist) {
logDebug(BaseMessages.getString(PKG, "JobFTPPUT.Log.FileExists", file));
} else {
logDebug(BaseMessages.getString(PKG, "JobFTPPUT.Log.FileDoesNotExists", file));
}
}
if (!fileExist || !onlyPuttingNewFiles) {
if (log.isDebug()) {
logDebug(BaseMessages.getString(PKG, "JobFTPPUT.Log.PuttingFileToRemoteDirectory", file, realRemoteDirectory));
}
String localFilename = realLocalDirectory + Const.FILE_SEPARATOR + file;
ftpclient.put(localFilename, file);
filesput++;
// Delete the file if this is needed!
if (remove) {
new File(localFilename).delete();
if (log.isDetailed()) {
logDetailed(BaseMessages.getString(PKG, "JobFTPPUT.Log.DeletedFile", localFilename));
}
}
}
}
}
result.setResult(true);
if (log.isDetailed()) {
logDebug(BaseMessages.getString(PKG, "JobFTPPUT.Log.WeHavePut", "" + filesput));
}
} catch (Exception e) {
result.setNrErrors(1);
logError(BaseMessages.getString(PKG, "JobFTPPUT.Log.ErrorPuttingFiles", e.getMessage()));
logError(Const.getStackTracker(e));
} finally {
if (ftpclient != null && ftpclient.connected()) {
try {
ftpclient.quit();
} catch (Exception e) {
logError(BaseMessages.getString(PKG, "JobFTPPUT.Log.ErrorQuitingFTP", e.getMessage()));
}
}
FTPClient.clearSOCKS();
}
return result;
}
Aggregations