org.bibalex.daf.handlers.filehandler
Class FTPFileHandler

java.lang.Object
  extended by java.util.Observable
      extended by org.bibalex.daf.handlers.filehandler.FileHandlerObservable
          extended by org.bibalex.daf.handlers.filehandler.FTPFileHandler

public class FTPFileHandler
extends FileHandlerObservable

This class is an FTP File Transfer Handler, handles all operations concerned with FTP transfer like file/folder copy, comparisons, delete It uses a multithreading technique in some of its operations This class is also the Facade for all the FTP classes in the system


Field Summary
private  java.lang.String hostName
           
private  com.enterprisedt.net.ftp.FTPClient mainFTPClient
           
private static java.util.ArrayList<java.util.Observer> observerArrLst
          Static Observers list array used to add a list of observers to the file handler before it initialization.
private  java.lang.String password
           
private  int poolSize
           
private  int port
           
private  java.util.concurrent.ExecutorService threadExecutor
          Private members
private  FTPThreadsPool threadsPool
           
private  java.lang.String userName
           
 
Constructor Summary
FTPFileHandler(java.lang.String hostName, int port, java.lang.String userName, java.lang.String password, int poolSize)
          Creates a new instacne of FTPFileHandler.
FTPFileHandler(java.lang.String hostName, java.lang.String userName, java.lang.String password, int poolSize)
          Creates a new instacne of FTPFileHandler.
 
Method Summary
static void AddObserver(java.util.Observer observer)
          Static method allowing assigning some observers before object's initialization.
private  void addPreAssignedObservers()
          Adds the pre-Assigned observers to the current object.
 int compareFolder(java.lang.String localFolderPath, java.lang.String remoteFolderPath)
          Compare 2 folders, one on the local machine and the other one is on the server, if they don't match the method returns the unmatched files count.
private  int countDelimeters(java.lang.String path)
          Counts the path depth in regard with folders and subfolders.
 boolean createFolder(java.lang.String parentPath, java.lang.String folderName, boolean overwrite)
          Creates a folder at the specified path.
 boolean createPath(java.lang.String path)
          Creates the complete path.
 void deleteFile(java.lang.String remoteFilePath)
          Deletes a file on the server
 void deleteFolder(java.lang.String remoteFolderPath)
          Deletes a folder on the server
private  void deleteSingleFile(java.lang.String remoteFilePath)
          Deletes a single file from the server, using a new FTP thread.
private  void deleteSubFolder(java.lang.String remoteFolderPath)
          Deletes a SubFolder on the server, this method is recursive; it recurses on all the subfolders.
protected  void finalize()
          Closes all FTP Threads connections still opened.
 boolean folderExists(java.lang.String folderPath)
          Checks if a folder exits on the server or not.
 void getFile(java.lang.String localFilePath, java.lang.String remoteFilePath)
          Retrieves a file from the server to the Local Machine
 java.io.File[] getFiles(java.lang.String remoteFolderPath)
          Gets the files included in a specific folder, this method is not recursive and folders are not counted as files.
 int getFilesCount(java.lang.String remoteFolderPath)
          Counts the file included in a specific folder, this method is not recursive and folders are not counted as files
 int getFilesCountRecursively(java.lang.String folderPath)
          Retreives the count of files in the given folderPath in a recursive way.
 int getFilesCountWithExtension(java.lang.String remoteFolderPath, java.lang.String fileExtension)
          Counts the file included in a specific folder with a given extension, this method is not recursive and folders are not counted as files
 long getFileSize(java.lang.String filePath)
          Gets the size of the file in bytes.
 java.lang.String[] getFilesNames(java.lang.String path)
          Gets the names of files within the given folder.
 java.lang.String[] getFilesNamesWithExt(java.lang.String path, java.lang.String extension)
          Gets the names of the file with the specified extension within the given folder.
 java.io.File[] getFilesRecursively(java.lang.String remoteFolderPath)
          Gets the files included in a specific folder, this method is recursive and folders are not counted as files.
 java.io.File[] getFilesWithExtension(java.lang.String remoteFolderPath, java.lang.String fileExtension)
          Gets the files included in a specific folder with a given extension, this method is not recursive and folders are not counted as files.
 void getFolder(java.lang.String localFolderPath, java.lang.String remoteFolderPath)
          Retrieves a folder from the server to the local Machine
 boolean getFolder(java.lang.String sourceParent, java.lang.String folderName, java.lang.String destinationParent, java.lang.String newName, boolean overwrite)
          Adds a folder from the server to the local Machine
 long getFolderSize(java.lang.String folderPath)
          Gets the size of the folder, recursively, in bytes.
private  long getFTPFileSize(com.enterprisedt.net.ftp.FTPFile ftpFile)
          This method gets a more accurate value of the file size, as it iterates on the size() method until the size value is no longer being modified
private  java.lang.String getLeafFromPath(java.lang.String fullPath)
           
private  java.lang.String getParentPath(java.lang.String childPath)
          Retreives the parent path for a given file or folder name In case it is a root, returned as it is In case it is a root first child, the root once again is returned In case it is a subfolder, or an inner file, the parent folder is returned
private  void getSingleFile(java.lang.String localFilePath, java.lang.String remoteFilePath)
          Gets a single file from the server, using a new FTP thread.
private  void getSubFolder(java.lang.String localFolderPath, java.lang.String remoteFolderPath)
          Retrieves a SubFolder from the server to the local machine, this method is recursive; it recurses on all the subfolders.
 java.lang.String[] getSubFolders(java.lang.String path)
          Gets the names of subfolders.
protected  void initializeMainThread()
           
 void putFile(java.lang.String localFilePath, java.lang.String remoteFilePath)
          Adds a file to the server
 void putFolder(java.lang.String localFolderPath, java.lang.String remoteFolderPath)
          Puts a folder from the local Machine to the server
 boolean putFolder(java.lang.String sourceParent, java.lang.String folderName, java.lang.String destinationParent, java.lang.String newName, boolean overwrite)
          Puts a folder from the local Machine to the server
private  void putSingleFile(java.lang.String localFilePath, java.lang.String remoteFilePath)
          Puts a single file on the server, using a new FTP thread.
private  void putSubFolder(java.lang.String localFolderPath, java.lang.String remoteFolderPath)
          Adds a SubFolder from the local machine to the server, this method is recursive; it recurses on all the subfolders.
static void RemoveObservers()
          Static method responsible for removing all the previously assigned observers.
 boolean rename(java.lang.String name, java.lang.String newName)
          This method is used to rename files or folders on an FTP Host.
private  void restoreFTPDir(int numberOfDelimeters)
          Restors an FTPClient to its original directory, given the delimeters.
private  java.lang.String[] tokenizePath(java.lang.String path)
          Tokenizes the given path into folder names.
 
Methods inherited from class org.bibalex.daf.handlers.filehandler.FileHandlerObservable
setChanged
 
Methods inherited from class java.util.Observable
addObserver, clearChanged, countObservers, deleteObserver, deleteObservers, hasChanged, notifyObservers, notifyObservers
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

observerArrLst

private static java.util.ArrayList<java.util.Observer> observerArrLst
Static Observers list array used to add a list of observers to the file handler before it initialization.


threadExecutor

private java.util.concurrent.ExecutorService threadExecutor
Private members


threadsPool

private FTPThreadsPool threadsPool

mainFTPClient

private com.enterprisedt.net.ftp.FTPClient mainFTPClient

poolSize

private int poolSize

hostName

private java.lang.String hostName

port

private int port

userName

private java.lang.String userName

password

private java.lang.String password
Constructor Detail

FTPFileHandler

public FTPFileHandler(java.lang.String hostName,
                      java.lang.String userName,
                      java.lang.String password,
                      int poolSize)
               throws java.lang.Exception
Creates a new instacne of FTPFileHandler. Using the default port of 21.

Parameters:
hostName - Host Network Name or IP
userName - User Name
password - Password
poolSize - The pool size, (Number of threads that could be initialized)
Throws:
java.lang.Exception - If the instacne couldn't be initialized.

FTPFileHandler

public FTPFileHandler(java.lang.String hostName,
                      int port,
                      java.lang.String userName,
                      java.lang.String password,
                      int poolSize)
               throws java.lang.Exception
Creates a new instacne of FTPFileHandler.

Parameters:
hostName - Host Network Name or IP
port - Port Number to Communicate on.
userName - User Name
password - Password
poolSize - The pool size, (Number of threads that could be initialized)
Throws:
java.lang.Exception - If the instacne couldn't be initialized.
Method Detail

AddObserver

public static void AddObserver(java.util.Observer observer)
Static method allowing assigning some observers before object's initialization.

Parameters:
observer - the observer to be (pre-)added.

RemoveObservers

public static void RemoveObservers()
Static method responsible for removing all the previously assigned observers.


initializeMainThread

protected void initializeMainThread()
                             throws java.lang.Exception
Throws:
java.lang.Exception

finalize

protected void finalize()
Closes all FTP Threads connections still opened.

Overrides:
finalize in class java.lang.Object

getFile

public void getFile(java.lang.String localFilePath,
                    java.lang.String remoteFilePath)
             throws java.lang.Exception
Retrieves a file from the server to the Local Machine

Parameters:
localFilePath - The destination path on the local machine
remoteFilePath - The source path on the server
Throws:
java.lang.Exception - Unable to get file

putFile

public void putFile(java.lang.String localFilePath,
                    java.lang.String remoteFilePath)
             throws java.lang.Exception
Adds a file to the server

Parameters:
localFilePath - The source file location on the local machine
remoteFilePath - The destination path on the server
Throws:
java.lang.Exception - Unable to put file

getFolder

public void getFolder(java.lang.String localFolderPath,
                      java.lang.String remoteFolderPath)
               throws java.lang.Exception
Retrieves a folder from the server to the local Machine

Parameters:
localFolderPath - The destination folder on the local Machine
remoteFolderPath - The source folder path on the server
Throws:
java.lang.Exception - Unable to get Folder

getFolder

public boolean getFolder(java.lang.String sourceParent,
                         java.lang.String folderName,
                         java.lang.String destinationParent,
                         java.lang.String newName,
                         boolean overwrite)
Adds a folder from the server to the local Machine

Parameters:
sourceParent - The source Parent Path on the Server
folderName - The folder Name to be copied
destinationParent - The destination Path on the local Machine (Non-Including the folder Name)
newName - The new Folder Name
overwrite - indicates whether to overwrite the existing folder or not, if set to false and the folder already exists then the method returns false
Returns:
if the operation succeded, return true, else return false.

putFolder

public void putFolder(java.lang.String localFolderPath,
                      java.lang.String remoteFolderPath)
               throws java.lang.Exception
Puts a folder from the local Machine to the server

Parameters:
localFolderPath - The source folder on the local Machine
remoteFolderPath - The destination folder path on the server
Throws:
java.lang.Exception - Unable to put Folder

putFolder

public boolean putFolder(java.lang.String sourceParent,
                         java.lang.String folderName,
                         java.lang.String destinationParent,
                         java.lang.String newName,
                         boolean overwrite)
Puts a folder from the local Machine to the server

Parameters:
sourceParent - The source Parent Path on the local machine
folderName - The folder Name to be copied
destinationParent - The destination Path on the server(Non-Including the folder Name)
newName - The new Folder Name
overwrite - indicates whether to overwrite the existing folder or not, if set to false and the folder already exists then the method returns false
Returns:
if the operation succeded, return true, else return false.

deleteFile

public void deleteFile(java.lang.String remoteFilePath)
                throws java.lang.Exception
Deletes a file on the server

Parameters:
remoteFilePath - The file path on the server to be deleted
Throws:
java.lang.Exception - Unable to delete File

deleteFolder

public void deleteFolder(java.lang.String remoteFolderPath)
                  throws java.lang.Exception
Deletes a folder on the server

Parameters:
remoteFolderPath - The folder path on the server to be deleted
Throws:
java.lang.Exception - Unable to delete folder

getFilesCount

public int getFilesCount(java.lang.String remoteFolderPath)
                  throws java.io.IOException,
                         com.enterprisedt.net.ftp.FTPException,
                         java.text.ParseException
Counts the file included in a specific folder, this method is not recursive and folders are not counted as files

Parameters:
remoteFolderPath - The folder path we would like to count the files within
Returns:
The files count
Throws:
java.io.IOException
com.enterprisedt.net.ftp.FTPException
java.text.ParseException

getFiles

public java.io.File[] getFiles(java.lang.String remoteFolderPath)
Gets the files included in a specific folder, this method is not recursive and folders are not counted as files.

Parameters:
remoteFolderPath - The folder path we would like to get the files within.
Returns:
A File[] holding the files within the remote folder or null.

getFilesRecursively

public java.io.File[] getFilesRecursively(java.lang.String remoteFolderPath)
Gets the files included in a specific folder, this method is recursive and folders are not counted as files.

Parameters:
remoteFolderPath - The folder path we would like to get the files within.
Returns:
A File[] holding the files within the remote folder or null.

getFilesCountWithExtension

public int getFilesCountWithExtension(java.lang.String remoteFolderPath,
                                      java.lang.String fileExtension)
                               throws java.io.IOException,
                                      com.enterprisedt.net.ftp.FTPException,
                                      java.text.ParseException
Counts the file included in a specific folder with a given extension, this method is not recursive and folders are not counted as files

Parameters:
remoteFolderPath - The folder path we would like to count the files within
fileExtension - The extension used to filter the files count, the extension is added without the ".", ex: tif, frf
Returns:
The files count
Throws:
java.io.IOException
com.enterprisedt.net.ftp.FTPException
java.text.ParseException

getFilesWithExtension

public java.io.File[] getFilesWithExtension(java.lang.String remoteFolderPath,
                                            java.lang.String fileExtension)
Gets the files included in a specific folder with a given extension, this method is not recursive and folders are not counted as files.

Parameters:
remoteFolderPath - The folder path we would like to get the files within.
fileExtension - The extension used to filter the files count, the extension is added without the ".", ex: tif, frf
Returns:
A File[] holding the files within the remote folder or null.

compareFolder

public int compareFolder(java.lang.String localFolderPath,
                         java.lang.String remoteFolderPath)
                  throws java.lang.Exception
Compare 2 folders, one on the local machine and the other one is on the server, if they don't match the method returns the unmatched files count.

Parameters:
localFolderPath - The folder on the local machine.
remoteFolderPath - The folder on the server.
Returns:
0 if matching, otherwise returns the unmatched files count.
Throws:
java.lang.Exception - Unable to compare folders

getFilesNames

public java.lang.String[] getFilesNames(java.lang.String path)
Gets the names of files within the given folder.

Parameters:
path - The path of the folder to get its files.
Returns:
A String[] holding the names of sub-files. If there is no subfiles an empty array is returned. If the given path doesn't represent an existing folder, null is returned.

getFilesNamesWithExt

public java.lang.String[] getFilesNamesWithExt(java.lang.String path,
                                               java.lang.String extension)
Gets the names of the file with the specified extension within the given folder.

Parameters:
path - The path fo the foldders to get the names of the files within.
extension - The extension of the files to get their names. The extension is the last 3 letters of the file name without the dot '.' .
Returns:
A String[] holding the names of sub-files. If there is no subfiles an empty array is returned. If the given path doesn't represent an existing folder, null is returned.

folderExists

public boolean folderExists(java.lang.String folderPath)
Checks if a folder exits on the server or not.

Parameters:
folderPath - The folder path that we would like to check for its existance.
Returns:
true if the folder exists, false if it doesn't.

createFolder

public boolean createFolder(java.lang.String parentPath,
                            java.lang.String folderName,
                            boolean overwrite)
Creates a folder at the specified path.

Parameters:
parentPath - The path of the folder at which the folder will be created.
folderName - The name of the folder to create.
overwrite - Whether to overwrite a folder with the same name at the parent or not.
Returns:
true if the folder has been crated, otherwise false.

getSubFolders

public java.lang.String[] getSubFolders(java.lang.String path)
Gets the names of subfolders.

Parameters:
path - The path of the folder to get its sub-folders
Returns:
A String[] holding the names of sub-folders. If there is no subfolders an empty array is returned. If the given path doesn't represent an existing folder, null is returned.

createPath

public boolean createPath(java.lang.String path)
Creates the complete path. This method creates the necessary folders to create the whole path. For example: If the given path is "./Collections/Collection Name/Job" and only "./Collection/" exists, The folders "Collection Name/Job" are created.

Parameters:
path - The path to create
Returns:
True if the whole path has been created otherwise false.

getFolderSize

public long getFolderSize(java.lang.String folderPath)
Gets the size of the folder, recursively, in bytes.

Parameters:
folderPath - The path of the folder.
Returns:
The size of the folder in bytes. If the folder doesn't exists or the size couldn't be obtained -1 is returned.

getFileSize

public long getFileSize(java.lang.String filePath)
Gets the size of the file in bytes.

Parameters:
filePath - The path of the file.
Returns:
The size of the file in bytes. If the file doesn't exists or the size couldn't be obtained -1 is returned.

getFilesCountRecursively

public int getFilesCountRecursively(java.lang.String folderPath)
Retreives the count of files in the given folderPath in a recursive way. Only files are counted, folders are used to fetch sub-folders.

Parameters:
folderPath - the root folder path for the count.
Returns:
an int displaying the count of files within this fodler recusively.

rename

public boolean rename(java.lang.String name,
                      java.lang.String newName)
This method is used to rename files or folders on an FTP Host.

Parameters:
name - the old name to replace.
newName - the new Name to use.
Returns:
true if the rename was successfull, false otherwise.

getSingleFile

private void getSingleFile(java.lang.String localFilePath,
                           java.lang.String remoteFilePath)
                    throws java.lang.Exception
Gets a single file from the server, using a new FTP thread.

Parameters:
localFilePath - The destination path on the local machine.
remoteFilePath - The source file path on the server
Throws:
java.lang.Exception - Unable to get the file.

putSingleFile

private void putSingleFile(java.lang.String localFilePath,
                           java.lang.String remoteFilePath)
                    throws java.lang.Exception
Puts a single file on the server, using a new FTP thread.

Parameters:
localFilePath - The source path on the local machine.
remoteFilePath - The destination path on the server.
Throws:
java.lang.Exception - Unable to put file.

deleteSingleFile

private void deleteSingleFile(java.lang.String remoteFilePath)
                       throws java.lang.Exception
Deletes a single file from the server, using a new FTP thread.

Parameters:
remoteFilePath - The file path on the server.
Throws:
java.lang.Exception - Unable to delete the file (might be because it doesn't originally exists).

getSubFolder

private void getSubFolder(java.lang.String localFolderPath,
                          java.lang.String remoteFolderPath)
                   throws java.lang.Exception
Retrieves a SubFolder from the server to the local machine, this method is recursive; it recurses on all the subfolders.

Parameters:
localFolderPath - The destination SubFolder path on the local machine.
remoteFolderPath - The source Subfolder path on the server.
Throws:
java.lang.Exception - Unable to get the SubFolder

putSubFolder

private void putSubFolder(java.lang.String localFolderPath,
                          java.lang.String remoteFolderPath)
                   throws java.lang.Exception
Adds a SubFolder from the local machine to the server, this method is recursive; it recurses on all the subfolders.

Parameters:
localFolderPath - The source SubFolder path on the local machine.
remoteFolderPath - The destination Subfolder path on the server.
Throws:
java.lang.Exception - Unable to put the SubFolder

deleteSubFolder

private void deleteSubFolder(java.lang.String remoteFolderPath)
                      throws java.lang.Exception
Deletes a SubFolder on the server, this method is recursive; it recurses on all the subfolders.

Parameters:
remoteFolderPath - The Subfolder path on the server to be deleted.
Throws:
java.lang.Exception - Unable to delete the SubFolder

countDelimeters

private int countDelimeters(java.lang.String path)
Counts the path depth in regard with folders and subfolders. Used in restoring FTPClient to Original Directory after a ChangeDir operation. This operation is done through string manipulation functions.

Parameters:
path - The Path to count the delimiters for.
Returns:
The delimeters count.

restoreFTPDir

private void restoreFTPDir(int numberOfDelimeters)
Restors an FTPClient to its original directory, given the delimeters. It is a one way method, it only restores to a parent directory and not a child one as it doesn't maintain the actual path instead it maintains the number of jumps.

Parameters:
numberOfDelimeters - the number of Jumps to go Up.

tokenizePath

private java.lang.String[] tokenizePath(java.lang.String path)
Tokenizes the given path into folder names.

Parameters:
path - the full path String to tokenize.
Returns:
Object[] representing the tokens of the path, the root at the top and the leaf at the bottom.

addPreAssignedObservers

private void addPreAssignedObservers()
Adds the pre-Assigned observers to the current object.


getParentPath

private java.lang.String getParentPath(java.lang.String childPath)
Retreives the parent path for a given file or folder name In case it is a root, returned as it is In case it is a root first child, the root once again is returned In case it is a subfolder, or an inner file, the parent folder is returned

Parameters:
childPath - the child path, either file or folder to be fetched for the parent.
Returns:
a string representing the parent path according to the above mentioned rules.

getLeafFromPath

private java.lang.String getLeafFromPath(java.lang.String fullPath)

getFTPFileSize

private long getFTPFileSize(com.enterprisedt.net.ftp.FTPFile ftpFile)
This method gets a more accurate value of the file size, as it iterates on the size() method until the size value is no longer being modified

Parameters:
ftpFile - The ftp File to get the size for.
Returns:
the FTP File size.
See Also:
FTPFile.size()