org.bibalex.daf.handlers.filehandler
Class FileHandlerFTPImpl

java.lang.Object
  extended by org.bibalex.daf.handlers.filehandler.FileHandler
      extended by org.bibalex.daf.handlers.filehandler.FileHandlerFTPImpl

public class FileHandlerFTPImpl
extends FileHandler

Extends FileHandler to provide FTP file handling.

Author:
Shehab.Kamal

Field Summary
private  FTPFileHandler handler
          A LocalFileHandler to create files and folders.
 
Fields inherited from class org.bibalex.daf.handlers.filehandler.FileHandler
pathSeparator
 
Constructor Summary
FileHandlerFTPImpl(java.lang.String hostname, java.lang.String username, java.lang.String password, int poolSize)
          Creates a new instance of FileHandlerFTPImp.
 
Method Summary
static void AddObserver(java.util.Observer observer)
          Static method allowing assigning some observers to the FTPFileHandler before object's initialization.
 boolean createFolder(java.lang.String parentFolder, java.lang.String folderName)
          Creates the given folder within the specified path at the server.
 boolean createPath(java.lang.String path)
          Creates the complete path.
 boolean deleteFolder(java.lang.String path, boolean recursive)
          Deletes the folder and its subfolders from the server.
 boolean downloadFile(java.lang.String sourcePath, java.lang.String fileName, java.lang.String destinationPath, java.lang.String newName)
          Downloads the file from source to destination.
 boolean downloadFiles(java.lang.String source, java.lang.String extention, java.lang.String destination)
          Downloads the files with the given extention from source, server, to destination, local file system.
 boolean downloadFolder(java.lang.String parentSource, java.lang.String folderName, java.lang.String parentDestination, java.lang.String newName)
          Downloads a folder from source, server, to destination, local file system.
 void finalize()
           
 boolean folderExists(java.lang.String folderPath)
          Determines whether the given folder exists or not.
 java.io.File[] getFiles(java.lang.String parentFolder)
          Gets the files within the given folder at the server.
 java.io.File[] getFiles(java.lang.String parentFolder, java.lang.String extention)
          Gets the files with a specific extention within the given folder at the server.
 int getFilesCount(java.lang.String parentFolder)
          Gets the count of files within the given folder at the server.
 int getFilesCount(java.lang.String parentFolder, java.lang.String extention)
          Gets the count of files with a specific extention within the given folder at the server.
 int getFilesCountRecursively(java.lang.String parentFolder)
          Gets the total number of files within the folder.
Note: The folder is visited recursively.
 long getFileSize(java.lang.String filePath)
          Gets the size of the file in bytes.
 java.lang.String[] getFilesNames(java.lang.String parentFolder)
          Gets the names of files within the given folder at the server.
 java.lang.String[] getFilesNames(java.lang.String parentFolder, java.lang.String extention)
          Gets the names of files with a specific extention within the given folder at the server.
 java.io.File[] getFilesRecursively(java.lang.String parentFolder)
          Gets the files within the given folder at the server recursively.
 long getFolderSize(java.lang.String folderPath)
          Gets the size of the folder, recursively, in bytes.
 java.lang.String[] getSubFolders(java.lang.String parentFolder)
          Gets a list of subfolders within the given folder from the server.
static void RemoveObservers()
          Static method responsible for removing all the previously assigned observers.
 boolean rename(java.lang.String oldName, java.lang.String newName)
          This method is used to rename files or folders on an FTP Host.
 boolean uploadFile(java.lang.String sourcePath, java.lang.String fileName, java.lang.String destinationPath, java.lang.String newName)
          Downloads the file from source to destination.
 boolean uploadFiles(java.lang.String source, java.lang.String extention, java.lang.String destination)
          Uploads the files with the given extention from source, local file system, to destination, server.
 boolean uploadFolder(java.lang.String parentSource, java.lang.String folderName, java.lang.String parentDestination, java.lang.String newName)
          Uploads a folder from source, local file system, to destination, server.
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

handler

private FTPFileHandler handler
A LocalFileHandler to create files and folders.

Constructor Detail

FileHandlerFTPImpl

public FileHandlerFTPImpl(java.lang.String hostname,
                          java.lang.String username,
                          java.lang.String password,
                          int poolSize)
                   throws java.lang.Exception
Creates a new instance of FileHandlerFTPImp.

Parameters:
hostname - The host name, i.e. the ftp server name.
username - The user name to log on to the server.
password - The password.
poolSize - The number of threads used to transfer files.
Throws:
java.lang.Exception - If the underlying FTPFileHandler couldn't be instantiated.
Method Detail

AddObserver

public static void AddObserver(java.util.Observer observer)
Static method allowing assigning some observers to the FTPFileHandler before object's initialization. This method is used because the FTPFileHandler is an package access class.

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

RemoveObservers

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


createFolder

public boolean createFolder(java.lang.String parentFolder,
                            java.lang.String folderName)
Creates the given folder within the specified path at the server.

Specified by:
createFolder in class FileHandler
Parameters:
parentFolder - The parnet folder at which the folder will be created.
folderName - the name of the folder.
Returns:
True if the folder has been created otherwise false.

downloadFolder

public boolean downloadFolder(java.lang.String parentSource,
                              java.lang.String folderName,
                              java.lang.String parentDestination,
                              java.lang.String newName)
Downloads a folder from source, server, to destination, local file system.

Specified by:
downloadFolder in class FileHandler
Parameters:
parentSource - The source parent folder.
folderName - The name of the folder to copy.
parentDestination - The destination parent folder.
newName - The new name of the folder after copying.
Returns:
True if the folder has been copied, otherwise false.

uploadFolder

public boolean uploadFolder(java.lang.String parentSource,
                            java.lang.String folderName,
                            java.lang.String parentDestination,
                            java.lang.String newName)
Uploads a folder from source, local file system, to destination, server.

Specified by:
uploadFolder in class FileHandler
Parameters:
parentSource - The source parent folder.
folderName - The name of the folder to copy.
parentDestination - The destination parent folder.
newName - The new name of the folder after copying.
Returns:
True if the folder has been copied, otherwise false.

downloadFiles

public boolean downloadFiles(java.lang.String source,
                             java.lang.String extention,
                             java.lang.String destination)
Downloads the files with the given extention from source, server, to destination, local file system.

Specified by:
downloadFiles in class FileHandler
Parameters:
source - The source folder that contain the files.
extention - The extension of the files to be copied.;
destination - The destination folder to which the files will be copied.
Returns:
True if the files have been copied successfully, otherwise false.

downloadFile

public boolean downloadFile(java.lang.String sourcePath,
                            java.lang.String fileName,
                            java.lang.String destinationPath,
                            java.lang.String newName)
Downloads the file from source to destination.

Specified by:
downloadFile in class FileHandler
Parameters:
sourcePath - The source folder taht contain the file.
fileName - The name of the file to download.
destinationPath - The destination folder to which the files will be copied.
newName - The new name of the file.
Returns:
True if the file has been downloaded successfully, otherwise false.

uploadFiles

public boolean uploadFiles(java.lang.String source,
                           java.lang.String extention,
                           java.lang.String destination)
Uploads the files with the given extention from source, local file system, to destination, server. Since there is no server and client in local implementation, the upload is merely a copy operation.

Specified by:
uploadFiles in class FileHandler
Parameters:
source - The source folder that contain the files.
extention - The extension of the files to be copied.;
destination - The destination folder to which the files will be copied.
Returns:
True if the files have been copied successfully, otherwise false.

uploadFile

public boolean uploadFile(java.lang.String sourcePath,
                          java.lang.String fileName,
                          java.lang.String destinationPath,
                          java.lang.String newName)
Downloads the file from source to destination.

Specified by:
uploadFile in class FileHandler
Parameters:
sourcePath - The source folder taht contain the file.
fileName - The name of the file to download.
destinationPath - The destination folder to which the files will be copied.
newName - The new name of the file.
Returns:
True if the file has been uploaded successfully, otherwise false.

deleteFolder

public boolean deleteFolder(java.lang.String path,
                            boolean recursive)
Deletes the folder and its subfolders from the server.

Specified by:
deleteFolder in class FileHandler
Parameters:
path - The path of the folder.
recursive - Whether to delete subfiles and subfolders or not. This parameter is not used.
Returns:
True if the folder has been deleted, otherwise false.

getSubFolders

public java.lang.String[] getSubFolders(java.lang.String parentFolder)
Gets a list of subfolders within the given folder from the server.

Specified by:
getSubFolders in class FileHandler
Parameters:
parentFolder - The folder to get its subfolders.
Returns:
A list of folder within the given folder. If there is not any folders an empty array is returned. If any exception occurs or the given path doesn't designate an existing folder, null is returned.

getFiles

public java.io.File[] getFiles(java.lang.String parentFolder)
Gets the files within the given folder at the server.

Specified by:
getFiles in class FileHandler
Parameters:
parentFolder - The folder to get the files within it.
Returns:
The files within the folder or null if an exception occurs.

getFilesRecursively

public java.io.File[] getFilesRecursively(java.lang.String parentFolder)
Gets the files within the given folder at the server recursively.

Specified by:
getFilesRecursively in class FileHandler
Parameters:
parentFolder - The folder to get the files within it.
Returns:
The files within the folder or null if an exception occurs.

getFiles

public java.io.File[] getFiles(java.lang.String parentFolder,
                               java.lang.String extention)
Gets the files with a specific extention within the given folder at the server.

Specified by:
getFiles in class FileHandler
Parameters:
parentFolder - The folder to get the files within it.
extention - The extention of the files to get.
Returns:
The number of files within the folder or null if an exception occurs.

getFilesNames

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

Specified by:
getFilesNames in class FileHandler
Parameters:
parentFolder - The folder to get the names of files within it.
Returns:
The names of files within the folder or null if an exception occurs.

getFilesNames

public java.lang.String[] getFilesNames(java.lang.String parentFolder,
                                        java.lang.String extention)
Gets the names of files with a specific extention within the given folder at the server.

Specified by:
getFilesNames in class FileHandler
Parameters:
parentFolder - The folder to get the names of files within it.
extention - The extention of the files to get.
Returns:
The names of files within the folder or null if an exception occurs.

getFilesCount

public int getFilesCount(java.lang.String parentFolder)
Gets the count of files within the given folder at the server.

Specified by:
getFilesCount in class FileHandler
Parameters:
parentFolder - The folder to get the count of files within it.
Returns:
The number of files within the folder or -1 if an exception occurs.

getFilesCount

public int getFilesCount(java.lang.String parentFolder,
                         java.lang.String extention)
Gets the count of files with a specific extention within the given folder at the server.

Specified by:
getFilesCount in class FileHandler
Parameters:
parentFolder - The folder to get the count of files within it.
extention - The extention of the files to count.
Returns:
The number of files within the folder or -1 if an exception occurs.

getFilesCountRecursively

public int getFilesCountRecursively(java.lang.String parentFolder)
Gets the total number of files within the folder.
Note: The folder is visited recursively.

Specified by:
getFilesCountRecursively in class FileHandler
Parameters:
parentFolder - The path of the folder to count its files.
Returns:
The number of files within the folder or -1 if an exception occurs.

getFolderSize

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

Specified by:
getFolderSize in class FileHandler
Parameters:
folderPath - The full path of the folder; that is folder path + folder name.
Returns:
The size of the folder in bytes. If the folder doesn't exist -1 is returned.

getFileSize

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

Specified by:
getFileSize in class FileHandler
Parameters:
filePath - The full path of the file; that is folder path + file name + extension.
Returns:
The size of the file in bytes. If the file doesn't exist -1 is returned.

folderExists

public boolean folderExists(java.lang.String folderPath)
Determines whether the given folder exists or not.

Specified by:
folderExists in class FileHandler
Parameters:
folderPath - The path to the folder.
Returns:
True if the folder exists, otherwise false.

createPath

public boolean createPath(java.lang.String path)
Creates the complete path.

Specified by:
createPath in class FileHandler
Parameters:
path - The path to create.
Returns:
True if the path has been created or already exists otherwise false.

finalize

public void finalize()
Overrides:
finalize in class java.lang.Object
See Also:
Object.finalize()

rename

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

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