org.openadaptor.adaptor.ftp
Class ApacheFTP

java.lang.Object
  extended by org.openadaptor.adaptor.ftp.ApacheFTP
All Implemented Interfaces:
FTP

public class ApacheFTP
extends java.lang.Object
implements FTP

This component will provide basic File Transfer Protocol (FTP) connunication to allow the adaptor to GET a file from or PUT a file onto a remote machine.

The methods return FTP input/output streams to the caller which can then be used to either read a file from or write a file to the remote server.

NB: while this component will happily write file in any format, the other componets (eg. FileSink, FileSource, ...) will only handle files that correspond to DOXML standards (eg. DOXML, csv, fixed width, etc. )

Uses the org.apache.commons.net.ftp classes to perform the actual file transfer.

Now allows you to set the file encoding for the i/o streams via the TextEncoding property

Properties:

  UseReaderThread     if true then a separate reader thread is used
                      to perform the data transfers. On some platforms
                      this is known to cause deadlocks!
                      Default is false
 

Author:
Pablo Bawdekar, Russ Fennell

Field Summary
(package private) static org.apache.log4j.Logger log
           
 
Fields inherited from interface org.openadaptor.adaptor.ftp.FTP
_DEFAULT_DIR, _DEFAULT_FTP_PORT, _DEFAULT_SFTP_PORT, _SUN
 
Constructor Summary
ApacheFTP()
          basic constructor for those that want it
 
Method Summary
 java.io.OutputStreamWriter append(java.lang.String fileName)
          Transfers a file to the remote server but appends the output stream the remote file rather than overwriting it.
 void close()
          close the connection to the remote server
 void connect(java.lang.String hostName, int port)
          takes the supplied hostname and port of the target machine and attempts to connect to it.
 void delete(java.lang.String fileName)
          deletes supplied file from the remote server
 boolean directoryExists(java.lang.String dirName)
          check for directory on remote server.
 java.lang.String[] fileList(java.lang.String filePattern)
          retrieves a list of file names on the remote server that match the supplied pattern.
 java.io.InputStreamReader get(java.lang.String fileName)
          This method creates a FTP input stream in the shape of an InputStreamReader that the caller can use to perform the GET function.
 java.lang.String getCurrentWorkingDirectory()
          returns the current working directory or null if there was an error
 int getDefaultPort()
          returns the default FTP communications port to use
 void init(java.util.Properties props, java.lang.String prefix)
          allows for library specific initialisation (eg.
 void initSession(java.lang.String workDir, boolean binaryTransfer)
          sets up the FTP session.
 boolean isConnected()
          returns flag to indicate if the FTPClient object has successfully connected to the remote server
 boolean isLoggedIn()
          returns flag to indicate if the FTPClient object has successfully logged into the remote server
 void logon(java.lang.String userName, java.lang.String password)
          attempt to log into the remote server using the supplied credentials.
 java.io.OutputStreamWriter put(java.lang.String fileName)
          This method creates a FTP output stream in the shape of an OutputStreamWriter that the caller can use to perform the PUT function.
 void setTextEncoding(java.lang.String enc)
          sets the file encoding to use when reading from the input stream
 boolean verifyFileTransfer()
          There are a few FTPClient methods that do not complete the entire sequence of FTP commands to complete a transaction.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

log

static org.apache.log4j.Logger log
Constructor Detail

ApacheFTP

public ApacheFTP()
basic constructor for those that want it

Method Detail

init

public void init(java.util.Properties props,
                 java.lang.String prefix)
          throws IbafException
allows for library specific initialisation (eg. special properties)

Specified by:
init in interface FTP
Parameters:
props - - reference to the Java properties file
prefix - - the component prefix in the properties file (eg. A.C1)
Throws:
IbafException - - if mandatory properties are not found or the properties cannot be converted into the right format

initSession

public void initSession(java.lang.String workDir,
                        boolean binaryTransfer)
                 throws IbafException
sets up the FTP session. Sets the working directory and the transfer mode.

Specified by:
initSession in interface FTP
Parameters:
workDir - - the working directory for the session
binaryTransfer - - true if binary transfer mode is to be used
Throws:
IbafException

connect

public void connect(java.lang.String hostName,
                    int port)
             throws IbafException
takes the supplied hostname and port of the target machine and attempts to connect to it. If successful the isConnected() flag is set.

Specified by:
connect in interface FTP
Parameters:
hostName - - The name of the server you wish to connect to
port - - The FTP control port to connect on
Throws:
IbafException

isConnected

public boolean isConnected()
returns flag to indicate if the FTPClient object has successfully connected to the remote server

Specified by:
isConnected in interface FTP
Returns:
boolean

isLoggedIn

public boolean isLoggedIn()
returns flag to indicate if the FTPClient object has successfully logged into the remote server

Specified by:
isLoggedIn in interface FTP
Returns:
boolean

logon

public void logon(java.lang.String userName,
                  java.lang.String password)
           throws IbafException
attempt to log into the remote server using the supplied credentials. This method checks to make saure that the client has been successfully connected to the remote server before attempting to log in.

Specified by:
logon in interface FTP
Parameters:
userName - - The username to use to authenticate the connection
password - - The password to use to authenticate the connection
Throws:
IbafException

get

public java.io.InputStreamReader get(java.lang.String fileName)
                              throws IbafException
This method creates a FTP input stream in the shape of an InputStreamReader that the caller can use to perform the GET function. Assumes that the FTP client has already been successfully connected to and authenticated. Although, you really shouldn't be able to get this far without this being the case anyway! Due to the way that the Apache libraries work we have to download the file, use the completePendingCommand() to finish the transfer process. We then read the file back in and pass it as an InputSreamReader to the caller.

Specified by:
get in interface FTP
Parameters:
fileName - - The name of the file to retrieve
Returns:
An InputStreamReader object containing the file contents
Throws:
IbafException

put

public java.io.OutputStreamWriter put(java.lang.String fileName)
                               throws IbafException
This method creates a FTP output stream in the shape of an OutputStreamWriter that the caller can use to perform the PUT function. Assumes that the FTP client has already been successfully connected to and authenticated. Although, you really shouldn't be able to get this far without this being the case anyway! If the createDirectory flag is set then the upload directory will be created if it is not present on the remote server

Specified by:
put in interface FTP
Parameters:
fileName - - The name of the file to transfer
Returns:
OutputStreamWriter that the caller can use to write the file
Throws:
IbafException - - if the client is not conected and logged into the remote server or the FTP output stream cannot be created (eg. does not have permission)

append

public java.io.OutputStreamWriter append(java.lang.String fileName)
                                  throws IbafException
Transfers a file to the remote server but appends the output stream the remote file rather than overwriting it. If the createDirectory flag is set then the upload directory will be created if it is not present on the remote server

Specified by:
append in interface FTP
Parameters:
fileName - - The name of the file to transfer
Returns:
OutputStreamWriter that the caller can use to write the file
Throws:
IbafException - - if the client is not conected and logged into the remote server or the FTP output stream cannot be created (eg. does not have permission)

close

public void close()
           throws IbafException
close the connection to the remote server

Specified by:
close in interface FTP
Throws:
IbafException

directoryExists

public boolean directoryExists(java.lang.String dirName)
                        throws IbafException
check for directory on remote server. Assumes that the client is already connected and logged into the remote server

Specified by:
directoryExists in interface FTP
Parameters:
dirName - - the directory to check for
Returns:
boolean to indicate the presence of the directory
Throws:
IbafException - - if the client is not connected and logged into the remote server

delete

public void delete(java.lang.String fileName)
            throws IbafException
deletes supplied file from the remote server

Specified by:
delete in interface FTP
Parameters:
fileName - - the file to delete
Throws:
IbafException - - if the client is not logged into the remote server or there was a problem with the deletion

fileList

public java.lang.String[] fileList(java.lang.String filePattern)
                            throws IbafException
retrieves a list of file names on the remote server that match the supplied pattern.

Specified by:
fileList in interface FTP
Parameters:
filePattern - - the pattern the match file names against
Returns:
- array of the file names or null if none found
Throws:
IbafException - - if there was an communications error

getDefaultPort

public int getDefaultPort()
returns the default FTP communications port to use

Specified by:
getDefaultPort in interface FTP

getCurrentWorkingDirectory

public java.lang.String getCurrentWorkingDirectory()
returns the current working directory or null if there was an error

Specified by:
getCurrentWorkingDirectory in interface FTP

verifyFileTransfer

public boolean verifyFileTransfer()
                           throws IbafException
There are a few FTPClient methods that do not complete the entire sequence of FTP commands to complete a transaction. These commands require some action by the programmer after the reception of a positive intermediate command. After the programmer's code completes its actions, it must call this method to receive the completion reply from the server and verify the success of the entire transaction.

Specified by:
verifyFileTransfer in interface FTP
Returns:
true if the transfer has been successful
Throws:
IbafException

setTextEncoding

public void setTextEncoding(java.lang.String enc)
sets the file encoding to use when reading from the input stream

Specified by:
setTextEncoding in interface FTP
Parameters:
enc - - String representing the file encoding to use (eg. ISO-8859-1)