List of usage examples for org.apache.commons.vfs FileObject close
public void close() throws FileSystemException;
From source file:org.pentaho.di.trans.steps.scriptvalues_mod.ScriptValuesAddedFunctions.java
public static double getFileSize(Context actualContext, Scriptable actualObject, Object[] ArgList, Function FunctionContext) { try {//w w w . ja v a2 s . c om if (ArgList.length == 1 && !isNull(ArgList[0]) && !isUndefined(ArgList[0])) { if (ArgList[0].equals(null)) { return 0; } FileObject file = null; try { // Source file file = KettleVFS.getFileObject(Context.toString(ArgList[0])); long filesize = 0; if (file.exists()) { if (file.getType().equals(FileType.FILE)) { filesize = file.getContent().getSize(); } else { Context.reportRuntimeError("[" + Context.toString(ArgList[0]) + "] is not a file!"); } } else { Context.reportRuntimeError("file [" + Context.toString(ArgList[0]) + "] can not be found!"); } return filesize; } catch (IOException e) { throw Context .reportRuntimeError("The function call getFileSize throw an error : " + e.toString()); } finally { if (file != null) { try { file.close(); } catch (Exception e) { // Ignore close errors } } } } else { throw Context.reportRuntimeError("The function call getFileSize is not valid."); } } catch (Exception e) { throw Context.reportRuntimeError(e.toString()); } }
From source file:org.pentaho.di.trans.steps.scriptvalues_mod.ScriptValuesAddedFunctions.java
public static boolean isFile(Context actualContext, Scriptable actualObject, Object[] ArgList, Function FunctionContext) { try {/*from ww w . j a v a 2s. 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) { // Ignore errors } } } } else { throw Context.reportRuntimeError("The function call isFile is not valid."); } } catch (Exception e) { throw Context.reportRuntimeError(e.toString()); } }
From source file:org.pentaho.di.trans.steps.scriptvalues_mod.ScriptValuesAddedFunctions.java
public static boolean isFolder(Context actualContext, Scriptable actualObject, Object[] ArgList, Function FunctionContext) { try {/* w w w . java2s . 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) { // Ignore errors } } } } else { throw Context.reportRuntimeError("The function call isFolder is not valid."); } } catch (Exception e) { throw Context.reportRuntimeError(e.toString()); } }
From source file:org.pentaho.di.trans.steps.scriptvalues_mod.ScriptValuesAddedFunctions.java
public static String getShortFilename(Context actualContext, Scriptable actualObject, Object[] ArgList, Function FunctionContext) { try {/*from www. j av a2s . c o m*/ 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) { // Ignore errors } } } } else { throw Context.reportRuntimeError("The function call getShortFilename is not valid."); } } catch (Exception e) { throw Context.reportRuntimeError(e.toString()); } }
From source file:org.pentaho.di.trans.steps.scriptvalues_mod.ScriptValuesAddedFunctions.java
public static String getFileExtension(Context actualContext, Scriptable actualObject, Object[] ArgList, Function FunctionContext) { try {//from w ww .j a va 2 s.c o m 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) { // Ignore errors } } } } else { throw Context.reportRuntimeError("The function call getFileExtension is not valid."); } } catch (Exception e) { throw Context.reportRuntimeError(e.toString()); } }
From source file:org.pentaho.di.trans.steps.scriptvalues_mod.ScriptValuesAddedFunctions.java
public static String getParentFoldername(Context actualContext, Scriptable actualObject, Object[] ArgList, Function FunctionContext) { try {// w w w.j a v a2 s . c o m 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) { // Ignore errors } } } } else { throw Context.reportRuntimeError("The function call getParentFoldername is not valid."); } } catch (Exception e) { throw Context.reportRuntimeError(e.toString()); } }
From source file:org.pentaho.di.trans.steps.scriptvalues_mod.ScriptValuesAddedFunctions.java
public static String getLastModifiedTime(Context actualContext, Scriptable actualObject, Object[] ArgList, Function FunctionContext) { try {/* w w w .j a v a2 s . co m*/ if (ArgList.length == 2 && !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 dateformat = Context.toString(ArgList[1]); if (isNull(dateformat)) { dateformat = "yyyy-MM-dd"; } String lastmodifiedtime = null; if (file.exists()) { java.util.Date lastmodifiedtimedate = new java.util.Date( file.getContent().getLastModifiedTime()); java.text.DateFormat dateFormat = new SimpleDateFormat(dateformat); lastmodifiedtime = dateFormat.format(lastmodifiedtimedate); } else { Context.reportRuntimeError("file [" + Context.toString(ArgList[0]) + "] can not be found!"); } return lastmodifiedtime; } catch (IOException e) { throw Context.reportRuntimeError( "The function call getLastModifiedTime throw an error : " + e.toString()); } finally { if (file != null) { try { file.close(); } catch (Exception e) { // Ignore errors } } } } else { throw Context.reportRuntimeError("The function call getLastModifiedTime is not valid."); } } catch (Exception e) { throw Context.reportRuntimeError(e.toString()); } }
From source file:org.pentaho.di.trans.steps.scriptvalues_mod.ScriptValuesAddedFunctions.java
public static void moveFile(Context actualContext, Scriptable actualObject, Object[] ArgList, Function FunctionContext) { try {/*from w ww . j a v a 2 s. c om*/ 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 move fileSource = KettleVFS.getFileObject(Context.toString(ArgList[0])); // Destination filename fileDestination = KettleVFS.getFileObject(Context.toString(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 = Context.toBoolean(ArgList[2]); } boolean destinationExists = fileDestination.exists(); // Let's move the file... if ((destinationExists && overwrite) || !destinationExists) { fileSource.moveTo(fileDestination); } } } else { Context.reportRuntimeError( "file to move [" + Context.toString(ArgList[0]) + "] can not be found!"); } } catch (IOException e) { throw Context.reportRuntimeError("The function call moveFile throw an error : " + e.toString()); } finally { if (fileSource != null) { try { fileSource.close(); } catch (Exception e) { // Ignore errors } } if (fileDestination != null) { try { fileDestination.close(); } catch (Exception e) { // Ignore errors } } } } else { throw Context.reportRuntimeError("The function call copyFile is not valid."); } } catch (Exception e) { throw Context.reportRuntimeError(e.toString()); } }
From source file:org.pentaho.di.trans.steps.sftpput.SFTPPut.java
public boolean processRow(StepMetaInterface smi, StepDataInterface sdi) throws KettleException { meta = (SFTPPutMeta) smi;//from w w w .ja v a 2 s .co m data = (SFTPPutData) sdi; boolean sendToErrorRow = false; String errorMessage = null; Object[] r = getRow(); // get row, set busy! if (r == null) { // no more input to be expected... setOutputDone(); return false; } if (first) { // Go there only for the first row received first = false; try { // String substitution.. String realServerName = environmentSubstitute(meta.getServerName()); String realServerPort = environmentSubstitute(meta.getServerPort()); String realUsername = environmentSubstitute(meta.getUserName()); String realPassword = Encr .decryptPasswordOptionallyEncrypted(environmentSubstitute(meta.getPassword())); String realKeyFilename = null; String realPassPhrase = null; if (meta.isUseKeyFile()) { // We must have here a private keyfilename realKeyFilename = environmentSubstitute(meta.getKeyFilename()); if (Const.isEmpty(realKeyFilename)) { // Error..Missing keyfile logError(BaseMessages.getString(PKG, "SFTPPut.Error.KeyFileMissing")); return false; } if (!KettleVFS.fileExists(realKeyFilename)) { // Error.. can not reach keyfile logError(BaseMessages.getString(PKG, "SFTPPut.Error.KeyFileNotFound", realKeyFilename)); return false; } realPassPhrase = environmentSubstitute(meta.getKeyPassPhrase()); } // Let's try to establish SFTP connection.... // Create sftp client to host ... data.sftpclient = new SFTPClient(InetAddress.getByName(realServerName), Const.toInt(realServerPort, 22), realUsername, realKeyFilename, realPassPhrase); // connection successfully established if (isDetailed()) { logDetailed(BaseMessages.getString(PKG, "SFTPPUT.Log.OpenedConnection", realServerName, realServerPort, realUsername)); } // Set compression data.sftpclient.setCompression(meta.getCompression()); // Set proxy? String realProxyHost = environmentSubstitute(meta.getProxyHost()); if (!Const.isEmpty(realProxyHost)) { // Set proxy data.sftpclient.setProxy(realProxyHost, environmentSubstitute(meta.getProxyPort()), environmentSubstitute(meta.getProxyUsername()), environmentSubstitute(meta.getProxyPassword()), meta.getProxyType()); } // login to ftp host ... data.sftpclient.login(realPassword); } catch (Exception e) { throw new KettleException(BaseMessages.getString(PKG, "SFTPPUT.Error.Connection"), e); } // Let's perform some checks // Sourcefilename field String sourceFilenameFieldName = environmentSubstitute(meta.getSourceFileFieldName()); if (Const.isEmpty(sourceFilenameFieldName)) { // source filename field is missing throw new KettleStepException( BaseMessages.getString(PKG, "SFTPPut.Error.SourceFileNameFieldMissing")); } data.indexOfSourceFileFieldName = getInputRowMeta().indexOfValue(sourceFilenameFieldName); if (data.indexOfSourceFileFieldName < -1) { // source filename field is missing throw new KettleStepException( BaseMessages.getString(PKG, "SFTPPut.Error.CanNotFindField", sourceFilenameFieldName)); } // Remote folder fieldname String remoteFoldernameFieldName = environmentSubstitute(meta.getRemoteDirectoryFieldName()); if (Const.isEmpty(remoteFoldernameFieldName)) { // remote folder field is missing throw new KettleStepException( BaseMessages.getString(PKG, "SFTPPut.Error.RemoteFolderNameFieldMissing")); } data.indexOfRemoteDirectory = getInputRowMeta().indexOfValue(remoteFoldernameFieldName); if (data.indexOfRemoteDirectory < -1) { // remote foldername field is missing throw new KettleStepException( BaseMessages.getString(PKG, "SFTPPut.Error.CanNotFindField", remoteFoldernameFieldName)); } // Move to folder if (meta.getAfterFTPS() == JobEntrySFTPPUT.AFTER_FTPSPUT_MOVE) { String realDestinationFoldernameFieldName = environmentSubstitute( meta.getDestinationFolderFieldName()); if (Const.isEmpty(realDestinationFoldernameFieldName)) { throw new KettleStepException( BaseMessages.getString(PKG, "SFTPPut.Log.DestinatFolderNameFieldNameMissing")); } data.indexOfMoveToFolderFieldName = getInputRowMeta() .indexOfValue(realDestinationFoldernameFieldName); if (data.indexOfMoveToFolderFieldName < -1) { // move to folder field is missing throw new KettleStepException(BaseMessages.getString(PKG, "SFTPPut.Error.CanNotFindField", realDestinationFoldernameFieldName)); } } } // Read data top upload String sourceData = getInputRowMeta().getString(r, data.indexOfSourceFileFieldName); InputStream inputStream = null; FileObject destinationFolder = null; FileObject file = null; try { if (Const.isEmpty(sourceData)) { // Source data is empty throw new KettleStepException(BaseMessages.getString(PKG, "SFTPPut.Error.SourceDataEmpty")); } if (meta.isInputStream()) { // Source data is a stream inputStream = new ByteArrayInputStream(sourceData.getBytes()); } else { // source data is a file // let's check file file = KettleVFS.getFileObject(sourceData); if (!file.exists()) { // We can not find file throw new KettleStepException( BaseMessages.getString(PKG, "SFTPPut.Error.CanNotFindField", sourceData)); } // get stream from file inputStream = KettleVFS.getInputStream(file); } if (file != null) { if (meta.getAfterFTPS() == JobEntrySFTPPUT.AFTER_FTPSPUT_MOVE) { String realDestationFolder = getInputRowMeta().getString(r, data.indexOfMoveToFolderFieldName); if (Const.isEmpty(realDestationFolder)) { // Move to destination folder is empty throw new KettleStepException( BaseMessages.getString(PKG, "SFTPPut.Error.MoveToDestinationFolderIsEmpty")); } destinationFolder = KettleVFS.getFileObject(realDestationFolder); if (!destinationFolder.exists()) { // We can not find folder throw new KettleStepException( BaseMessages.getString(PKG, "SFTPPut.Error.CanNotFindFolder", realDestationFolder)); } } } // move to spool dir ... setSFTPDirectory(getInputRowMeta().getString(r, data.indexOfRemoteDirectory)); // Destination filename String destinationFilename = file.getName().getBaseName(); // Upload a stream data.sftpclient.put(inputStream, destinationFilename); if (file != null) { // We successfully uploaded the file // what's next ... finishTheJob(file, sourceData, destinationFolder); } putRow(getInputRowMeta(), r); // copy row to possible alternate rowset(s). if (checkFeedback(getLinesRead())) { if (isDetailed()) { logDetailed(BaseMessages.getString(PKG, "SFTPPut.Log.LineNumber") + getLinesRead()); } } } catch (Exception e) { if (getStepMeta().isDoingErrorHandling()) { sendToErrorRow = true; errorMessage = e.toString(); } else { logError(BaseMessages.getString(PKG, "SFTPPut.Log.ErrorInStep"), e); setErrors(1); stopAll(); setOutputDone(); // signal end to receiver(s) return false; } if (sendToErrorRow) { // Simply add this row to the error row putError(getInputRowMeta(), r, 1, errorMessage, null, "SFTPPUT001"); } } finally { try { if (destinationFolder != null) { destinationFolder.close(); } if (file != null) { file.close(); } if (inputStream != null) { inputStream.close(); } } catch (Exception e) { // ignore this } } return true; }
From source file:org.pentaho.di.trans.steps.sftpput.SFTPPut.java
protected void finishTheJob(FileObject file, String sourceData, FileObject destinationFolder) throws KettleException { try {//www.j a v a2 s . c om switch (meta.getAfterFTPS()) { case JobEntrySFTPPUT.AFTER_FTPSPUT_DELETE: // Delete source file if (!file.exists()) { file.delete(); if (isDetailed()) { logDetailed(BaseMessages.getString(PKG, "SFTPPut.Log.DeletedFile", sourceData)); } } break; case JobEntrySFTPPUT.AFTER_FTPSPUT_MOVE: // Move source file FileObject destination = null; try { destination = KettleVFS.getFileObject(destinationFolder.getName().getBaseName() + Const.FILE_SEPARATOR + file.getName().getBaseName(), this); file.moveTo(destination); if (isDetailed()) { logDetailed(BaseMessages.getString(PKG, "SFTPPut.Log.FileMoved", file, destination)); } } finally { if (destination != null) { destination.close(); } } break; default: if (meta.isAddFilenameResut()) { // Add this to the result file names... ResultFile resultFile = new ResultFile(ResultFile.FILE_TYPE_GENERAL, file, getTransMeta().getName(), getStepname()); resultFile .setComment(BaseMessages.getString(PKG, "SFTPPut.Log.FilenameAddedToResultFilenames")); addResultFile(resultFile); if (isDetailed()) { logDetailed(BaseMessages.getString(PKG, "SFTPPut.Log.FilenameAddedToResultFilenames", sourceData)); } } break; } } catch (Exception e) { throw new KettleException(e); } }