wjhk.jupload2.upload
Class FileUploadManagerThread

java.lang.Object
  extended by java.lang.Thread
      extended by wjhk.jupload2.upload.FileUploadManagerThread
All Implemented Interfaces:
ActionListener, Runnable, EventListener

public class FileUploadManagerThread
extends Thread
implements ActionListener

This class is responsible for managing the upload. At the end of the upload, the JUploadPanel.updateButtonState() is called, to refresh the button state. Its job is to:

  • Prepare upload for the file (calls to FileData.beforeUpload() for each file in the file list.
  • Create the thread to send a packet of files.
  • Prepare the packets, that will be red by the upload thread.
  • Manage the end of upload: trigger the call to JUploadPanel.updateButtonState() and the call to UploadPolicy.afterUpload(Exception, String).
  • Manage the 'stop' button reaction.
  • This class is created by JUploadPanel, when the user clicks on the upload button.

    Author:
    etienne_sf

    Nested Class Summary
     
    Nested classes/interfaces inherited from class java.lang.Thread
    Thread.State, Thread.UncaughtExceptionHandler
     
    Field Summary
    (package private)  long currentRequestStartTime
              Contains the date/time (as a long) of the start of the current upload.
    (package private)  wjhk.jupload2.gui.filepanel.FilePanel filePanel
              The current file list.
    (package private)  FileUploadThread fileUploadThread
              The upload thread, that will wait for the next file packet to be ready, then send it.
    (package private)  long globalStartTime
              Contains the system time of the start of the global upload.
    (package private)  long maxChunkSize
               
    (package private)  long nbBytesReadyForUpload
              Contains the sum of the files, ready for upload, and not uploaded yet
    (package private)  long nbBytesUploadedForCurrentFile
              Indicated the number of bytes that have currently been sent for the current file.
    (package private)  int nbFilesBeingUploaded
              Number of files that has been read by the FileUploadThread thread.
    (package private)  int nbFilesPerRequest
               
    (package private)  int nbPreparedFiles
              Number of files that are prepared for upload.
    (package private)  int nbSentFiles
              Number of files that have already been sent.
    (package private)  int nbSuccessfullyUploadedFiles
              Number of files that have been successfully uploaded. already been sent.
    (package private)  long nbTotalNumberOfPreparedBytes
              Sum of the length for all prepared files.
    (package private)  long nbUploadedBytes
              Current number of bytes that have been uploaded.
    (package private)  UploadFileData[] nextPacket
              Contains the next packet to upload.
    (package private)  int numOfFileInCurrentRequest
              During the upload, when uploading several files in one packet, this attribute indicates which file is currently being uploaded.
    (package private)  JProgressBar preparationProgressBar
              The JUploadPanel progress bar, to follow the file preparation progress.
    (package private)  boolean stop
              If set to 'true', the thread will stop the current upload.
    (package private)  Timer timerProgressBar
              Used to update the progress: 50ms is nice, as it is fast enough, and doesn't make CPU rise to 100%.
    (package private)  Timer timerStatusBar
              Used to update the status bar (upload speed, ETA...): 300ms make it accurate, and avoid an always changing value.
    static int UPLOAD_STATUS_CHUNK_UPLOADED_WAITING_FOR_RESPONSE
              A chunk (a part) of the file identified by numOfFileInCurrentRequest has been sent.
    static int UPLOAD_STATUS_FILE_UPLOADED_WAITING_FOR_RESPONSE
              All data for the file identified by numOfFileInCurrentRequest has been sent.
    static int UPLOAD_STATUS_NOT_STARTED
              Indicates that nothings has begun
    static int UPLOAD_STATUS_UPLOADED
              The upload for the file identified by numOfFileInCurrentRequest is finished
    static int UPLOAD_STATUS_UPLOADING
              We're sending data to the server, for the file identified by numOfFileInCurrentRequest.
    (package private)  long uploadDuration
              Contains the sum of the upload duration for all requests.
    (package private)  JUploadException uploadException
              Thread Exception, if any occurred during upload.
    (package private)  UploadFileData[] uploadFileDataArray
              The list of files to upload
    (package private)  boolean uploadFinished
              Indicates whether the upload is finished or not.
    (package private)  JUploadPanel uploadPanel
              A shortcut to the upload panel
    (package private)  UploadPolicy uploadPolicy
              The current upload policy.
    (package private)  JProgressBar uploadProgressBar
              The JUploadPanel progress bar, to follow the upload of the prepared files to the server.
    (package private)  long uploadStartTime
              Contains the time of the actual start of upload.
    (package private)  int uploadStatus
              Indicates what is the current file being uploaded, and its upload status.
     
    Fields inherited from class java.lang.Thread
    MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
     
    Constructor Summary
    FileUploadManagerThread(UploadPolicy uploadPolicy)
              Standard constructor of the class.
    FileUploadManagerThread(UploadPolicy uploadPolicy, FileUploadThread fileUploadThreadParam)
              Internal constructor.
     
    Method Summary
     void actionPerformed(ActionEvent e)
               
     void anotherFileHasBeenSent(FileData newlyUploadedFileData)
              This method is called each time a new file is sent to the server.
     void currentRequestIsFinished(UploadFileData[] currentPacket)
              This method is called when the server response for the upload indicates a success.
     int getNbUploadedFiles()
              Get the total number of files which have been successfully uploaded.
     UploadFileData[] getNextPacket()
              Returns the next packet of files, for upload, according to the current upload policy.
     JUploadException getUploadException()
              Get the last upload exception that occurs.
     long getUploadStartTime()
              Retrieve the start time of the upload.
     boolean isUploadFinished()
              Indicates whether the upload is finished or not.
     void nbBytesUploaded(long nbBytes)
              Used by the UploadFileData#uploadFile(java.io.OutputStream, long) for each uploaded buffer
     void run()
              The heart of the program.
     void setUploadException(JUploadException uploadException)
              Stores the last upload exception that occurs.
     void setUploadStatus(int numOfFileInCurrentRequest, int uploadStatus)
              Indicate the current state of the upload, to allow a correct display of the upload progress bar.
     void stopUpload()
              Reaction to the user click on the 'Stop' button, or any action from the user asking to stop the upload.
     
    Methods inherited from class java.lang.Thread
    activeCount, checkAccess, countStackFrames, currentThread, destroy, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, start, stop, stop, suspend, toString, yield
     
    Methods inherited from class java.lang.Object
    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
     

    Field Detail

    UPLOAD_STATUS_NOT_STARTED

    public static final int UPLOAD_STATUS_NOT_STARTED
    Indicates that nothings has begun

    See Also:
    Constant Field Values

    UPLOAD_STATUS_UPLOADING

    public static final int UPLOAD_STATUS_UPLOADING
    We're sending data to the server, for the file identified by numOfFileInCurrentRequest.

    See Also:
    Constant Field Values

    UPLOAD_STATUS_CHUNK_UPLOADED_WAITING_FOR_RESPONSE

    public static final int UPLOAD_STATUS_CHUNK_UPLOADED_WAITING_FOR_RESPONSE
    A chunk (a part) of the file identified by numOfFileInCurrentRequest has been sent. But the server response has not been received yet.

    See Also:
    Constant Field Values

    UPLOAD_STATUS_FILE_UPLOADED_WAITING_FOR_RESPONSE

    public static final int UPLOAD_STATUS_FILE_UPLOADED_WAITING_FOR_RESPONSE
    All data for the file identified by numOfFileInCurrentRequest has been sent. But the server response has not been received yet.

    See Also:
    Constant Field Values

    UPLOAD_STATUS_UPLOADED

    public static final int UPLOAD_STATUS_UPLOADED
    The upload for the file identified by numOfFileInCurrentRequest is finished

    See Also:
    Constant Field Values

    currentRequestStartTime

    long currentRequestStartTime
    Contains the date/time (as a long) of the start of the current upload. This allows to sum the time of the actual upload, and ignore the time the applet is waiting for the server's response. Once the request is finished, and the applet waits for the server's response, the duration of the sending to the server is added to currentRequestStartTime, and currentRequestStartTime is reseted to 0. It's then ready for the next upload request.


    filePanel

    wjhk.jupload2.gui.filepanel.FilePanel filePanel
    The current file list.


    fileUploadThread

    FileUploadThread fileUploadThread
    The upload thread, that will wait for the next file packet to be ready, then send it.


    globalStartTime

    long globalStartTime
    Contains the system time of the start of the global upload. This is used to calculate the ETA, and display it to the user, on the status bar.


    maxChunkSize

    long maxChunkSize
    See Also:
    UploadPolicy.getMaxChunkSize()

    nbBytesReadyForUpload

    long nbBytesReadyForUpload
    Contains the sum of the files, ready for upload, and not uploaded yet


    nbFilesBeingUploaded

    int nbFilesBeingUploaded
    Number of files that has been read by the FileUploadThread thread. These files have been read by the getNextPacket() method.


    nbFilesPerRequest

    int nbFilesPerRequest
    See Also:
    UploadPolicy.getNbFilesPerRequest()

    nbPreparedFiles

    int nbPreparedFiles
    Number of files that are prepared for upload. A file is prepared for upload, if the FileData.beforeUpload() has been called.


    nbSentFiles

    int nbSentFiles
    Number of files that have already been sent. The control on the upload success may be done or not. It's used to properly display the progress bar.


    nbSuccessfullyUploadedFiles

    int nbSuccessfullyUploadedFiles
    Number of files that have been successfully uploaded. already been sent. The control on the upload success may be done or not. It's used to properly display the progress bar.


    nbBytesUploadedForCurrentFile

    long nbBytesUploadedForCurrentFile
    Indicated the number of bytes that have currently been sent for the current file. This allows the management of the progress bar.


    nbTotalNumberOfPreparedBytes

    long nbTotalNumberOfPreparedBytes
    Sum of the length for all prepared files. This allow the calculation of the estimatedTotalLength.

    See Also:
    anotherFileHasBeenSent(FileData)

    numOfFileInCurrentRequest

    int numOfFileInCurrentRequest
    During the upload, when uploading several files in one packet, this attribute indicates which file is currently being uploaded.


    uploadStatus

    int uploadStatus
    Indicates what is the current file being uploaded, and its upload status.


    nextPacket

    UploadFileData[] nextPacket
    Contains the next packet to upload.

    See Also:
    getNextPacket()

    preparationProgressBar

    JProgressBar preparationProgressBar
    The JUploadPanel progress bar, to follow the file preparation progress.


    uploadProgressBar

    JProgressBar uploadProgressBar
    The JUploadPanel progress bar, to follow the upload of the prepared files to the server.


    uploadFinished

    boolean uploadFinished
    Indicates whether the upload is finished or not. Passed to true as soon as one of these conditions becomes true:
  • All files are uploaded (in the currentRequestIsFinished(UploadFileData[]) method)
  • An exception occurs (in the setUploadException(JUploadException) method)
  • The user stops the upload (in the stopUpload() method)

  • uploadStartTime

    long uploadStartTime
    Contains the time of the actual start of upload. Doesn't take into account the time for preparing files.


    stop

    boolean stop
    If set to 'true', the thread will stop the current upload.

    See Also:
    UploadFileData.uploadFile(java.io.OutputStream, long)

    uploadException

    JUploadException uploadException
    Thread Exception, if any occurred during upload.


    uploadDuration

    long uploadDuration
    Contains the sum of the upload duration for all requests. For instance, if sending in 10 chunks one big file, the uploadDuration contains the sum of the sending of these 10 request to the server. This allows to calculate the true upload speed, and ignore the time we'll wait for the server's response.


    nbUploadedBytes

    long nbUploadedBytes
    Current number of bytes that have been uploaded.


    uploadPanel

    JUploadPanel uploadPanel
    A shortcut to the upload panel


    uploadPolicy

    UploadPolicy uploadPolicy
    The current upload policy.


    uploadFileDataArray

    UploadFileData[] uploadFileDataArray
    The list of files to upload


    timerStatusBar

    Timer timerStatusBar
    Used to update the status bar (upload speed, ETA...): 300ms make it accurate, and avoid an always changing value.


    timerProgressBar

    Timer timerProgressBar
    Used to update the progress: 50ms is nice, as it is fast enough, and doesn't make CPU rise to 100%.

    Constructor Detail

    FileUploadManagerThread

    public FileUploadManagerThread(UploadPolicy uploadPolicy)
                            throws JUploadException
    Standard constructor of the class.

    Parameters:
    uploadPolicy -
    Throws:
    JUploadException

    FileUploadManagerThread

    public FileUploadManagerThread(UploadPolicy uploadPolicy,
                                   FileUploadThread fileUploadThreadParam)
                            throws JUploadException
    Internal constructor. It is used by the JUnit test, to create a FileUploadManagerThread instance, based on a non-active FileUploadThread.

    Parameters:
    uploadPolicy - The current uploadPolicy
    fileUploadThreadParam - The instance of FileUploadThread that should be used. Allows execution of unit tests, based on a specific FileUploadThread, that does ... nothing.
    Throws:
    JUploadException
    Method Detail

    run

    public final void run()
    The heart of the program. This method prepare the upload, then calls doUpload for each HTTP request.

    Specified by:
    run in interface Runnable
    Overrides:
    run in class Thread
    See Also:
    Thread.run()

    getNbUploadedFiles

    public int getNbUploadedFiles()
    Get the total number of files which have been successfully uploaded.

    Returns:
    Total number of uploaded files.

    getUploadStartTime

    public final long getUploadStartTime()
    Retrieve the start time of the upload. That is: when the first upload starts. It can be some delay after this thread creation, as it first need to prepare files to upload.

    Returns:
    The time this thread was started in ms.

    setUploadException

    public void setUploadException(JUploadException uploadException)
    Stores the last upload exception that occurs. This method won't write to the log file.

    Parameters:
    uploadException -

    getUploadException

    public JUploadException getUploadException()
    Get the last upload exception that occurs.

    Returns:
    The last upload exception, or null if no exception occurs.

    isUploadFinished

    public boolean isUploadFinished()
    Indicates whether the upload is finished or not. As several conditions can make the upload being finished (all files uploaded, an error occured, the user stops the upload), a specific boolean is built. It's managed by the run() method.

    Returns:
    true if the upload is finished. False otherwise.

    nbBytesUploaded

    public void nbBytesUploaded(long nbBytes)
                         throws JUploadException
    Used by the UploadFileData#uploadFile(java.io.OutputStream, long) for each uploaded buffer

    Parameters:
    nbBytes - Number of additional bytes that where uploaded.
    Throws:
    JUploadException

    setUploadStatus

    public void setUploadStatus(int numOfFileInCurrentRequest,
                                int uploadStatus)
                         throws JUploadException
    Indicate the current state of the upload, to allow a correct display of the upload progress bar.

    Parameters:
    numOfFileInCurrentRequest -
    uploadStatus -
    Throws:
    JUploadException

    stopUpload

    public void stopUpload()
    Reaction to the user click on the 'Stop' button, or any action from the user asking to stop the upload. The upload should go on for the current file, and stop before starting the next upload request to the server, to avoid strange problems on the server.


    actionPerformed

    public void actionPerformed(ActionEvent e)
    Specified by:
    actionPerformed in interface ActionListener
    Parameters:
    e -

    anotherFileHasBeenSent

    public void anotherFileHasBeenSent(FileData newlyUploadedFileData)
                                throws JUploadException
    This method is called each time a new file is sent to the server. It's main aim is to allow a proper display of the progress bar. It is public, as upload is done in another thread, whose class maybe in another package.

    Parameters:
    newlyUploadedFileData -
    Throws:
    JUploadException

    currentRequestIsFinished

    public void currentRequestIsFinished(UploadFileData[] currentPacket)
                                  throws JUploadException
    This method is called when the server response for the upload indicates a success. It is public, as upload is done in another thread, whose class maybe in another package.

    Parameters:
    currentPacket - The packet of files that was successfully uploaded.
    Throws:
    JUploadException

    getNextPacket

    public UploadFileData[] getNextPacket()
                                   throws JUploadException
    Returns the next packet of files, for upload, according to the current upload policy.

    Returns:
    The array of files to upload.
    Throws:
    JUploadException