uk.org.ogsadai.activity.indexedfiles
Class FileReadingActivity

java.lang.Object
  |
  +--uk.org.ogsadai.activity.Activity
        |
        +--uk.org.ogsadai.activity.files.AbstractFileActivity
              |
              +--uk.org.ogsadai.activity.indexedfiles.FileReadingActivity

public class FileReadingActivity
extends AbstractFileActivity

An activity to read data from one or more binary or text files stored on the server. The file data is then written to an output which can be connected to another activity or delivered back to the user in the response document. See the activity user documentation for more details: OGSADAI/doc/interaction/activities/indexedfiles/readFile.html

Author:
The OGSA-DAI Project Team

Field Summary
private static int BLOCK_SIZE
          Size of blocks when retrieving bytes from random access file (1024)
private static java.lang.String COPYRIGHT_NOTICE
          Copyright statement
private static DAILogger LOG
          Logger object for logging in this class
private  boolean mBase64encode
          Is the output base64 encoded?
private  java.io.RandomAccessFile mBinaryFile
          Random access file
private  java.io.File mFile
          Text / random access file
private  java.lang.String mFilename
          Name of the text or random access file
private  BlockReader mInput
          Activity input - names of files to read
private  boolean mIsBinaryFile
          Do we have a binary/random access or a text file?
private  long mLength
          Number of bytes or lines to be read, -1 if reading to the end of the file
private  long mOffset
          Offset - skipped bytes or lines
private  BlockWriter mOutput
          Activity output - bytes/lines read
private  java.io.BufferedReader mTextFile
          Text file reader
 
Fields inherited from class uk.org.ogsadai.activity.files.AbstractFileActivity
mCredentials, mFileAccessProvider, mTopDir
 
Fields inherited from class uk.org.ogsadai.activity.Activity
mContext, mExternalInputs, mExternalOutputs, mInternalInputs, mInternalOutputs
 
Constructor Summary
FileReadingActivity(org.w3c.dom.Element element)
           
 
Method Summary
protected  void cleanUp()
          This method can be overridden by any Activity implementations that need to free up resources in the event of an error or intermediate termination of the activity processing.
private  boolean hasNextFile()
          Checks if there is another instruction waiting in the input stream and retrieves filename, offset and length.
 void initialise()
          Gets the data resource accessor for the files data resource according to whether the users credentials permit this access.
private  boolean openFile(java.lang.String filename)
          Opens a file and skips mOffset lines or bytes
private  void parseOffsetAndLength(java.lang.String namespace, org.w3c.dom.Element element)
          Extracts offset and length attributes from an element that contains either a bytes or lines subelement and stores these.
protected  void processBlock()
          Performs an iteration of the processing of an activity.
protected  void processFirst()
          This method is called during the first iteration of processing an activity.
private  int readFromBinaryFile()
          Reads a block of bytes from a file (at most mLength bytes), and puts it into the output stream
private  boolean readFromTextFile()
          Reads one line from a text file mLength is the remaining number of lines to read
 
Methods inherited from class uk.org.ogsadai.activity.Activity
connectsTo, createOutputPipe, getActivityConfiguration, getActivityName, getCause, getName, getObservableStatus, getProperties, getSession, getStatus, hasActivityConfiguration, hasProperties, process, setActivityName, setCompleted, setError
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

COPYRIGHT_NOTICE

private static final java.lang.String COPYRIGHT_NOTICE
Copyright statement

See Also:
Constant Field Values

LOG

private static final DAILogger LOG
Logger object for logging in this class


BLOCK_SIZE

private static final int BLOCK_SIZE
Size of blocks when retrieving bytes from random access file (1024)

See Also:
Constant Field Values

mFilename

private java.lang.String mFilename
Name of the text or random access file


mFile

private java.io.File mFile
Text / random access file


mBinaryFile

private java.io.RandomAccessFile mBinaryFile
Random access file


mTextFile

private java.io.BufferedReader mTextFile
Text file reader


mBase64encode

private boolean mBase64encode
Is the output base64 encoded?


mIsBinaryFile

private boolean mIsBinaryFile
Do we have a binary/random access or a text file?


mOffset

private long mOffset
Offset - skipped bytes or lines


mLength

private long mLength
Number of bytes or lines to be read, -1 if reading to the end of the file


mOutput

private BlockWriter mOutput
Activity output - bytes/lines read


mInput

private BlockReader mInput
Activity input - names of files to read

Constructor Detail

FileReadingActivity

public FileReadingActivity(org.w3c.dom.Element element)
                    throws ActivityCreationException,
                           ActivitySpecificationException
Method Detail

initialise

public void initialise()
                throws ActivitySpecificationException,
                       ActivityExecutionException
Description copied from class: AbstractFileActivity
Gets the data resource accessor for the files data resource according to whether the users credentials permit this access.

Overrides:
initialise in class AbstractFileActivity
Throws:
ActivitySpecificationException - If the user is not permitted to access the resource.
ActivityExecutionException - If some internal problem occurs.
See Also:
Activity.initialise()

processFirst

protected void processFirst()
Description copied from class: Activity
This method is called during the first iteration of processing an activity. It can be overridden by subclasses to define some processing that should be done only once during the first iteration of the activity processing, for example evaluating a database query.

Overrides:
processFirst in class Activity

processBlock

protected void processBlock()
Description copied from class: Activity
Performs an iteration of the processing of an activity. This may involve reading a block of input data and writing a block of output data, or may involve some other kind of processing. When an activity is processed by the OGSA-DAI engine, this method will be invoked repeatedly until the activity either completes or stops due to an error or termination call. An implementation of this method should invoke the setCompleted method to indicate that processing is complete, or the setError method if an error occurs that will prevent the processing from completing.

Specified by:
processBlock in class Activity

cleanUp

protected void cleanUp()
Description copied from class: Activity
This method can be overridden by any Activity implementations that need to free up resources in the event of an error or intermediate termination of the activity processing.

Overrides:
cleanUp in class Activity

readFromTextFile

private boolean readFromTextFile()
                          throws DAIFileReadIOException
Reads one line from a text file mLength is the remaining number of lines to read

Throws:
DAIFileReadIOException - If there is a problem.

readFromBinaryFile

private int readFromBinaryFile()
                        throws DAIFileReadIOException
Reads a block of bytes from a file (at most mLength bytes), and puts it into the output stream

Throws:
DAIFileReadIOException - If there is a problem.

hasNextFile

private boolean hasNextFile()
                     throws ActivitySpecificationException,
                            ActivityExecutionException
Checks if there is another instruction waiting in the input stream and retrieves filename, offset and length.

The instruction is expected to be of form:

   <readFile>
     <file>FILE</file>
     <bytes offset="M" length="M"/>
   </readFile>
 
or
   <readFile>
     <file>FILE</file>
     <lines offset="M" length="M"/>
   </readFile>
 

Returns:
true if there is another instruction to process, false if there are no more instructions or the input stream is null.
Throws:
ActivitySpecificationException - If the instruction format is not as expected, the file name is missing, the offset and length are not numbers or are < 0.
ActivityExecutionException - If an internal parsing-related problem occurs.

parseOffsetAndLength

private void parseOffsetAndLength(java.lang.String namespace,
                                  org.w3c.dom.Element element)
                           throws ActivitySpecificationException
Extracts offset and length attributes from an element that contains either a bytes or lines subelement and stores these.

Parameters:
namespace - Namespace of elements in element.
element - Element to parse.
Throws:
ActivitySpecificationException - If the offset and length are not numbers or are < 0.

openFile

private boolean openFile(java.lang.String filename)
                  throws ActivitySpecificationException,
                         ActivityExecutionException
Opens a file and skips mOffset lines or bytes

Parameters:
filename - Name of file to open.
Returns:
true if the file has been openend and the cursor is positioned after the offset, false if the file is not open (e.g. because the offset is larger than the length of the file
Throws:
ActivitySpecificationException - If the file does not exist or is outside the root directory
ActivityExecutionException - If some internal problem arises.