org.bibalex.daf.reflectioncalls
Class BackupPhase

java.lang.Object
  extended by org.bibalex.daf.reflectioncalls.BackupPhase

public class BackupPhase
extends java.lang.Object

Copyright (c) 2006-2007 by Bibliotheca Alexandrina This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. This class represents the backup phase. The backup phase responsibility, among other things, is to create a version of the job located at the production server to the backup server.
For a complete list of the operations performed by this phase, see aplyPhaseActions() method.

Author:
Shehab.Kamal

Field Summary
private static int BACKUP_PHASE_ID
          Holds the ID of the backup phase.
private static int backupServerFTPPoolSize
           
private static java.lang.String backupServerIP
           
private static java.lang.String backupServerPassword
           
private static java.lang.String backupServerPath
           
private static java.lang.String backupServerUser
           
private static TransitionInfo info
           
private static Job job
           
private static java.lang.String jobPath
           
private static boolean localFolderCreated
          Indicates whether the local job or job_version folder has been created in the working directory or not.
private static int QUICK_BACKUP_PHASE_ID
          Holds the ID of the quick backup phase.
private static boolean remoteFolderCreated
          Indicates whether the local job or job_version folder has been created in the backup server or not.
private static java.lang.String statusData
          Holds the status data obtained from the phase before the backup phase.
private static User user
           
private static WorkStation workstation
           
 
Constructor Summary
BackupPhase()
           
 
Method Summary
private static int addVersionSuffixToJobFolder()
          Adds the version suffix to the job folder, that is; it renames the job folder.
This method logs any errors that might happen.
This method sets the localPath to the job_version folder at working directory.
static void applyBackupActions(int jobID)
          Applies the actions associated with the backup phase.
The backup phase responsibility is to do the following:
1) Copy the entire job folder form the production server to the working directory.
2) Create metadata files for the job.
3) Zip each folder and calculate the size of the job.
4) Calculate the version of the job = number of backup entries in the TransactionLog table.
5) Copy the job to the backup server.
static void applyQuickBackupActions(int jobID)
          Applies the actions associated with the backup phase.
The backup phase responsibility is to do the following:
1) Copy the entire job folder form the production server to the working directory.
2) Create metadata files for the job.
3) Zip each folder and calculate the size of the job.
4) Calculate the version of the job = number of backup entries in the TransactionLog table.
5) Copy the job to the backup server.
private static long calculateJobSize()
          Calculates the size of the job.
This method logs any errors that might happen.
private static boolean compressJobSubFolder(java.util.ArrayList<java.lang.String> compressedFolders)
          Compresses the subfolders within the job folder.
private static StatusDef convertToStatusDef(java.lang.String status)
          Converts the gievn string into a StatusDef instance.
private static boolean copyJobFromProductionServerToWorkingDirectory()
          Copies the job form the production server to working directory.
This method logs any errors that might happen.
Note:
1) If an exception occurs, the downloaded folder from the production server is not deleted.
2) If the job folder is copied to the working directory, the localFolderCreated is set to true.
private static boolean copyJobFromWorkingDirectoryToBackupServer()
          Copies the job form the working directory to the backup server.
This method logs any errors that might happen.
Note:
1) If an exception occurs, neither the folder at the working directory or backup server is not deleted.
2) If the job folder is copied to the backup server, the remoteFolderCreated is set to true.
private static boolean createMD5File(java.lang.String filePath)
          Calculates the MD5 digest for the given file.
The calculated MD5 is stored in a file named the same as the gievn file but with the extension md5.
Note: The md5 file is written as 32 hex characters.
If an exception is encuntered, a warning is logged.
private static boolean deleteBackupFolder(java.lang.String folderPath)
          Attempts to delete the given remote folder.
private static boolean deleteLocalFolder(java.lang.String folderPath)
          Attempts to delete the given local folder.
If the job couldn't be deleted a warning is logged.
private static int getAssignPhaseID()
          Gets the phase ID to which the job will be assigned after it finishes backup.
The Phase ID equals the ID of the phase after the last finished phase in sequence.
This method logs any errors that might happen.
private static void getBackupServerInformation()
          Get the necessary information to connect to the backup phase.
The information are read from the rescource file.
This method logs any errors that might occur.
private static boolean getLastFinishedPhase()
           
private static boolean isEmpty(java.lang.String path)
          Determines whether the given folder is empty or not
private static boolean makeDatabaseModification(int version, java.util.ArrayList<java.lang.String> compressedFolders, long size)
          Makes the necessary database modifications.
1) Add the TransactionLog entries for the job.
The method inserts 1 entry indicating the finish of the backup phase .
2) update the job record to point ot the last inserted database record.
Note:
1) The method executes in a transaction.
2) If an exception occurs, the database transaction is rolled back.
private static boolean makeDatabaseModification(int version, java.util.ArrayList<java.lang.String> compressedFolders, long size, int assignPhaseID)
          Makes the necessary database modifications.
1) Add the TransactionLog entries for the job.
The method inserts 1 entry indicating the finish of the backup phase and another entry indicating assigning to the next phase if it is different from the backup phase ID.
2) pdate the job record to point ot the last inserted database record.
Note:
1) The method executes in a transaction.
2) If an exception occurs, the database transaction is rolled back.
private static void removesExtraFilesFromWorkingDirectory()
          Clean the working directory by deleting extra files in the root folder.
private static boolean writeMetadataFile()
          Writes the metadata files of the job.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

QUICK_BACKUP_PHASE_ID

private static int QUICK_BACKUP_PHASE_ID
Holds the ID of the quick backup phase.


BACKUP_PHASE_ID

private static int BACKUP_PHASE_ID
Holds the ID of the backup phase.


localFolderCreated

private static boolean localFolderCreated
Indicates whether the local job or job_version folder has been created in the working directory or not.


remoteFolderCreated

private static boolean remoteFolderCreated
Indicates whether the local job or job_version folder has been created in the backup server or not.


statusData

private static java.lang.String statusData
Holds the status data obtained from the phase before the backup phase.


job

private static Job job

jobPath

private static java.lang.String jobPath

user

private static User user

workstation

private static WorkStation workstation

info

private static TransitionInfo info

backupServerIP

private static java.lang.String backupServerIP

backupServerUser

private static java.lang.String backupServerUser

backupServerPassword

private static java.lang.String backupServerPassword

backupServerPath

private static java.lang.String backupServerPath

backupServerFTPPoolSize

private static int backupServerFTPPoolSize
Constructor Detail

BackupPhase

public BackupPhase()
Method Detail

applyQuickBackupActions

public static void applyQuickBackupActions(int jobID)
                                    throws PhaseActionException
Applies the actions associated with the backup phase.
The backup phase responsibility is to do the following:
1) Copy the entire job folder form the production server to the working directory.
2) Create metadata files for the job.
3) Zip each folder and calculate the size of the job.
4) Calculate the version of the job = number of backup entries in the TransactionLog table.
5) Copy the job to the backup server. The job folder will be named after the version of the job. 6) Add the finish entry to the TransactionLog table. The folders that have been zipped and the size of the job will be stored in the StatusData column.
7) Assign the job to the next phase right before the backup phase (e.g. process -> backup -> ocr). However if the backup phase was the last phase then no entry will be added.

Parameters:
jobID - The ID of the job to apply backup actions to.
Throws:
PhaseActionException - If the backup phase actions couldn't be applied.

applyBackupActions

public static void applyBackupActions(int jobID)
                               throws PhaseActionException
Applies the actions associated with the backup phase.
The backup phase responsibility is to do the following:
1) Copy the entire job folder form the production server to the working directory.
2) Create metadata files for the job.
3) Zip each folder and calculate the size of the job.
4) Calculate the version of the job = number of backup entries in the TransactionLog table.
5) Copy the job to the backup server. The job folder will be named after the version of the job. 6) Add the finish entry to the TransactionLog table. The folders that have been zipped and the size of the job will be stored in the StatusData column.

Parameters:
jobID - The ID of the job to apply backup actions to.
Throws:
PhaseActionException - If the backup phase actions couldn't be applied.

getBackupServerInformation

private static void getBackupServerInformation()
                                        throws java.lang.Exception
Get the necessary information to connect to the backup phase.
The information are read from the rescource file.
This method logs any errors that might occur.

Throws:
java.lang.Exception - If the backup server information couldn't be obtained.

copyJobFromProductionServerToWorkingDirectory

private static boolean copyJobFromProductionServerToWorkingDirectory()
Copies the job form the production server to working directory.
This method logs any errors that might happen.
Note:
1) If an exception occurs, the downloaded folder from the production server is not deleted.
2) If the job folder is copied to the working directory, the localFolderCreated is set to true.

Returns:
True if the job has been copied successfully, otherwise false.

removesExtraFilesFromWorkingDirectory

private static void removesExtraFilesFromWorkingDirectory()
Clean the working directory by deleting extra files in the root folder.

Throws:
java.lang.Exception

compressJobSubFolder

private static boolean compressJobSubFolder(java.util.ArrayList<java.lang.String> compressedFolders)
Compresses the subfolders within the job folder. MD5 files are generated for the zip files.
This method logs any errors that might happen.
Note: If an exception occurs, the original and zipped folders are not deleted.

Parameters:
compressedFolders - The folders that have been compressed.
Returns:
True if all the folders within the job folder has been compressed, otherwise false.

isEmpty

private static boolean isEmpty(java.lang.String path)
Determines whether the given folder is empty or not

Parameters:
path - The path of the folder
Returns:
True if the folder is empty, otherwise false.

createMD5File

private static boolean createMD5File(java.lang.String filePath)
Calculates the MD5 digest for the given file.
The calculated MD5 is stored in a file named the same as the gievn file but with the extension md5.
Note: The md5 file is written as 32 hex characters.
If an exception is encuntered, a warning is logged.

Parameters:
filePath - The complete path of the file to generated MD5 for.
Returns:
True if the a file containing the MD5 of the given file has been generated otherwise false.

writeMetadataFile

private static boolean writeMetadataFile()
Writes the metadata files of the job. Each job will have the following:
1) REC file, which will contain the LOB of the job. The file name is jobID_meta.rec
2) XML file, which will conatin job information and transaction log information. The file name is jobID_meta.xml
This method logs any errors that might happen.

Returns:
True if all the folders within the job folder has been compressed, otherwise false.

addVersionSuffixToJobFolder

private static int addVersionSuffixToJobFolder()
Adds the version suffix to the job folder, that is; it renames the job folder.
This method logs any errors that might happen.
This method sets the localPath to the job_version folder at working directory. The method does the following:
1) Calculates the version of the job from the database.
2) Rename the job folder by adding the version suffix to the folder name.
3) Sets the path of the job to the new name so that further references to job.getPath() will yield the correct result. The new path is not persisted to the database.
Note: If an exception occurs, the original or renamed folder is not deleted.

Returns:
The version of the job or -1 if the version couldn't be obtained.

calculateJobSize

private static long calculateJobSize()
Calculates the size of the job.
This method logs any errors that might happen.

Returns:
The size of the job, or -1 if the size couldn't be calculated due to exception.

copyJobFromWorkingDirectoryToBackupServer

private static boolean copyJobFromWorkingDirectoryToBackupServer()
Copies the job form the working directory to the backup server.
This method logs any errors that might happen.
Note:
1) If an exception occurs, neither the folder at the working directory or backup server is not deleted.
2) If the job folder is copied to the backup server, the remoteFolderCreated is set to true.

Returns:
True if the job has been copied successfully, otherwise false.

getAssignPhaseID

private static int getAssignPhaseID()
Gets the phase ID to which the job will be assigned after it finishes backup.
The Phase ID equals the ID of the phase after the last finished phase in sequence.
This method logs any errors that might happen.

Returns:
The ID of the pahse to which the job will be assigned.
If the next phase is the backup phase the ID of the backup pahse is returned. If the phase ID couldn't be obtained due to an exception -1 is returned.

getLastFinishedPhase

private static boolean getLastFinishedPhase()

makeDatabaseModification

private static boolean makeDatabaseModification(int version,
                                                java.util.ArrayList<java.lang.String> compressedFolders,
                                                long size,
                                                int assignPhaseID)
Makes the necessary database modifications.
1) Add the TransactionLog entries for the job.
The method inserts 1 entry indicating the finish of the backup phase and another entry indicating assigning to the next phase if it is different from the backup phase ID.
2) pdate the job record to point ot the last inserted database record.
Note:
1) The method executes in a transaction.
2) If an exception occurs, the database transaction is rolled back.

Parameters:
version - The version of the backup, it is used to write the status data of the backup finish entry.
compressedFolders - An ArrayList holding the names of the folders that has been compressed, it is used to write the status data of the backup finish entry.
size - The size of the entire job folder.
assignPhaseID - The ID of the phase to add an assign entry to. If the phase ID equals backupPhaseID no assign entry is added.
Returns:
True if all the TransactionLog entries has been added, otherwise false.

makeDatabaseModification

private static boolean makeDatabaseModification(int version,
                                                java.util.ArrayList<java.lang.String> compressedFolders,
                                                long size)
Makes the necessary database modifications.
1) Add the TransactionLog entries for the job.
The method inserts 1 entry indicating the finish of the backup phase .
2) update the job record to point ot the last inserted database record.
Note:
1) The method executes in a transaction.
2) If an exception occurs, the database transaction is rolled back.

Parameters:
version - The version of the backup, it is used to write the status data of the backup finish entry.
compressedFolders - An ArrayList holding the names of the folders that has been compressed, it is used to write the status data of the backup finish entry.
size - The size of the entire job folder.
Returns:
True if all the TransactionLog entries has been added, otherwise false.

convertToStatusDef

private static StatusDef convertToStatusDef(java.lang.String status)
Converts the gievn string into a StatusDef instance.

Parameters:
status - The string to parse.
Returns:
The StatusDef of the given String with comment, backup and database omitted.
Null if the statsu data cannot be parsed.

deleteLocalFolder

private static boolean deleteLocalFolder(java.lang.String folderPath)
Attempts to delete the given local folder.
If the job couldn't be deleted a warning is logged.

Parameters:
folderPath - The complete path of the folder.
Returns:
True if ther folder has been deleted, otherwise false.

deleteBackupFolder

private static boolean deleteBackupFolder(java.lang.String folderPath)
Attempts to delete the given remote folder.

Parameters:
folderPath - The complete path of the folder.
Returns:
True if ther folder has been deleted, otherwise false.