org.openadaptor.adaptor.ftp
Class SunFTP

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

public class SunFTP
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 standard sun.net.ftp.FtpClient and sun.net.TelnetInputStream
 classes to perform the actual file transfer.

 NB: the Sun libraries are subject to change between JDK releases. A
 case in point is - if you specify a wildcard in the filename. Under
 JDK 1.4 the source will retrieve each file that matches the file
 pattern on subsequent polls pretty much as you would expect. However,
 if you use JDK 1.3 then it will only retrieve the first file that
 matches and then empty files for the rest.

 Upshot, only use JDK 1.3 if you are getting single files!

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

 

Author:
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
SunFTP()
          basic constructor for those that want it
 
Method Summary
 java.io.OutputStreamWriter append(java.lang.String fileName)
          the append() functionality requires JDK 1.4 and therefore we don't implement it here.
 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 SunFTP 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 is 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 SunFTP 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 is no way to check that all is ok so we return true anyway!!
 
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

SunFTP

public SunFTP()
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 SunFTP 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 SunFTP input stream in the shape of an InputStreamReader that the caller can use to perform the GET function. Assumes that the SunFTP 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!

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 SunFTP output stream in the shape of an OutputStreamWriter that the caller can use to perform the PUT function. Assumes that the SunFTP 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 SunFTP output stream cannot be created (eg. does not have permission)

append

public java.io.OutputStreamWriter append(java.lang.String fileName)
                                  throws IbafException
the append() functionality requires JDK 1.4 and therefore we don't implement it here. The method will throw an IbafException when it is called!!

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 SunFTP 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 is an error

Specified by:
getCurrentWorkingDirectory in interface FTP

verifyFileTransfer

public boolean verifyFileTransfer()
                           throws IbafException
there is no way to check that all is ok so we return true anyway!!

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)