Package org.apache.catalina.ha.deploy
Class FileMessageFactory
- java.lang.Object
- 
- org.apache.catalina.ha.deploy.FileMessageFactory
 
- 
 public class FileMessageFactory extends java.lang.ObjectThis factory is used to read files and write files by splitting them up into smaller messages. So that entire files don't have to be read into memory.
 The factory can be used as a reader or writer but not both at the same time. When done reading or writing the factory will close the input or output streams and mark the factory as closed. It is not possible to use it after that.
 To force a cleanup, call cleanup() from the calling object.
 This class is not thread safe.
- 
- 
Field SummaryFields Modifier and Type Field Description protected booleanclosedOnce the factory is used, it cannot be reused.protected longcreationTimeThe time this instance was created.protected byte[]dataThe bytes that we hold the data in, not thread safe.protected java.io.FilefileThe file that we are reading/writingprotected java.io.FileInputStreaminWhen openForWrite=false, the input stream is held by this variableprotected booleanisWritingFlag that indicates if a thread is writing messages to disk.protected java.util.concurrent.atomic.AtomicLonglastMessageProcessedThe number of the last message processed.protected intmaxValidTimeThe maximum valid time(in seconds) from creationTime.protected java.util.Map<java.lang.Long,FileMessage>msgBufferMessages received out of order are held in the buffer until required.protected intnrOfMessagesProcessedThe number of messages we have writtenprotected booleanopenForWriteTrue means that we are writing with this factory.protected java.io.FileOutputStreamoutWhen openForWrite=true, the output stream is held by this variablestatic intREAD_SIZEThe number of bytes that we read from fileprotected longsizeThe total size of the fileprotected longtotalNrOfMessagesThe total number of packets that we split this file into
 - 
Method SummaryAll Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected voidcheckState(boolean openForWrite)Check to make sure the factory is able to perform the function it is asked to do.voidcleanup()Closes the factory, its streams and sets all its references to nulljava.io.FilegetFile()static FileMessageFactorygetInstance(java.io.File f, boolean openForWrite)Creates a factory to read or write from a file.intgetMaxValidTime()booleanisValid()static voidmain(java.lang.String[] args)Example usage.FileMessagereadMessage(FileMessage f)Reads file data into the file message and sets the size, totalLength, totalNrOfMsgs and the message number
 If EOF is reached, the factory returns null, and closes itself, otherwise the same message is returned as was passed in.voidsetMaxValidTime(int maxValidTime)booleanwriteMessage(FileMessage msg)Writes a message to file.
 
- 
- 
- 
Field Detail- 
READ_SIZEpublic static final int READ_SIZE The number of bytes that we read from file- See Also:
- Constant Field Values
 
 - 
fileprotected final java.io.File file The file that we are reading/writing
 - 
openForWriteprotected final boolean openForWrite True means that we are writing with this factory. False means that we are reading with this factory
 - 
closedprotected boolean closed Once the factory is used, it cannot be reused.
 - 
inprotected java.io.FileInputStream in When openForWrite=false, the input stream is held by this variable
 - 
outprotected java.io.FileOutputStream out When openForWrite=true, the output stream is held by this variable
 - 
nrOfMessagesProcessedprotected int nrOfMessagesProcessed The number of messages we have written
 - 
sizeprotected long size The total size of the file
 - 
totalNrOfMessagesprotected long totalNrOfMessages The total number of packets that we split this file into
 - 
lastMessageProcessedprotected java.util.concurrent.atomic.AtomicLong lastMessageProcessed The number of the last message processed. Message IDs are 1 based.
 - 
msgBufferprotected final java.util.Map<java.lang.Long,FileMessage> msgBuffer Messages received out of order are held in the buffer until required. If everything is worked as expected, messages will spend very little time in the buffer.
 - 
dataprotected byte[] data The bytes that we hold the data in, not thread safe.
 - 
isWritingprotected boolean isWriting Flag that indicates if a thread is writing messages to disk. Access to this flag must be synchronised.
 - 
creationTimeprotected long creationTime The time this instance was created. (in milliseconds)
 - 
maxValidTimeprotected int maxValidTime The maximum valid time(in seconds) from creationTime.
 
- 
 - 
Method Detail- 
getInstancepublic static FileMessageFactory getInstance(java.io.File f, boolean openForWrite) throws java.io.FileNotFoundException, java.io.IOException Creates a factory to read or write from a file. When opening for read, the readMessage can be invoked, and when opening for write the writeMessage can be invoked.- Parameters:
- f- File - the file to be read or written
- openForWrite- boolean - true, means we are writing to the file, false means we are reading from it
- Returns:
- FileMessageFactory
- Throws:
- java.io.FileNotFoundException- - if the file to be read doesn't exist
- java.io.IOException- - if it fails to create the file that is to be written
 
 - 
readMessagepublic FileMessage readMessage(FileMessage f) throws java.lang.IllegalArgumentException, java.io.IOException Reads file data into the file message and sets the size, totalLength, totalNrOfMsgs and the message number
 If EOF is reached, the factory returns null, and closes itself, otherwise the same message is returned as was passed in. This makes sure that not more memory is ever used. To remember, neither the file message or the factory are thread safe. Don't hand off the message to one thread and read the same with another.- Parameters:
- f- FileMessage - the message to be populated with file data
- Returns:
- FileMessage - returns the same message passed in as a parameter, or null if EOF
- Throws:
- java.lang.IllegalArgumentException- - if the factory is for writing or is closed
- java.io.IOException- - if a file read exception occurs
 
 - 
writeMessagepublic boolean writeMessage(FileMessage msg) throws java.lang.IllegalArgumentException, java.io.IOException Writes a message to file. If (msg.getMessageNumber() == msg.getTotalNrOfMsgs()) the output stream will be closed after writing.- Parameters:
- msg- FileMessage - message containing data to be written
- Returns:
- returns true if the file is complete and outputstream is closed, false otherwise.
- Throws:
- java.lang.IllegalArgumentException- - if the factory is opened for read or closed
- java.io.IOException- - if a file write error occurs
 
 - 
cleanuppublic void cleanup() Closes the factory, its streams and sets all its references to null
 - 
checkStateprotected void checkState(boolean openForWrite) throws java.lang.IllegalArgumentExceptionCheck to make sure the factory is able to perform the function it is asked to do. Invoked by readMessage/writeMessage before those methods proceed.- Parameters:
- openForWrite- The value to check
- Throws:
- java.lang.IllegalArgumentException- if the state is not the expected one
 
 - 
mainpublic static void main(java.lang.String[] args) throws java.lang.ExceptionExample usage.- Parameters:
- args- String[], args[0] - read from filename, args[1] write to filename
- Throws:
- java.lang.Exception- An error occurred
 
 - 
getFilepublic java.io.File getFile() 
 - 
isValidpublic boolean isValid() 
 - 
getMaxValidTimepublic int getMaxValidTime() 
 - 
setMaxValidTimepublic void setMaxValidTime(int maxValidTime) 
 
- 
 
-