wjhk.jupload2.upload
Class FileUploadManagerThreadImpl

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

public class FileUploadManagerThreadImpl
extends Thread
implements FileUploadManagerThread

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)  UploadFileData fileBeingUploaded
              The file that is currently being uploaded.
    (package private)  wjhk.jupload2.gui.filepanel.FilePanel filePanel
              The current file list.
    (package private)  FilePreparationThread filePreparationThread
              The file preparatoin thread prepares each file for upload, and manage possible errors that can occurs at preparation time.
    (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 lastUpdateOfProgressBar
              Contains the last system time (in ms), when the progressBar was updated.
    (package private)  long maxChunkSize
               
    (package private)  long nbBytesUploadedForCurrentFile
              Indicated the number of bytes that have currently been sent for the current file.
    (package private)  int nbFilesPerRequest
               
    (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 nbUploadedBytes
              Current number of bytes that have been uploaded.
    (package private)  PacketConstructionThread packetConstructionThread
              This thread receives each prepared files in a queue, constructs the packets of files to be sent together, and posts them in another queue.
    (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)  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)  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)  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
     
    Fields inherited from interface wjhk.jupload2.upload.FileUploadManagerThread
    UPLOAD_STATUS_CHUNK_UPLOADED_WAITING_FOR_RESPONSE, UPLOAD_STATUS_FILE_UPLOADED_WAITING_FOR_RESPONSE, UPLOAD_STATUS_NOT_STARTED, UPLOAD_STATUS_UPLOADED, UPLOAD_STATUS_UPLOADING
     
    Constructor Summary
    FileUploadManagerThreadImpl(UploadPolicy uploadPolicy)
              Standard constructor of the class.
    FileUploadManagerThreadImpl(UploadPolicy uploadPolicy, FileUploadThread fileUploadThreadParam)
              Internal constructor.
     
    Method Summary
     void anotherFileHasBeenSent(UploadFilePacket uploadFilePacket, UploadFileData newlyUploadedFileData)
              This method is called each time a new file is sent to the server.
     void currentRequestIsFinished(UploadFilePacket packet)
              This method is called when the server response for the upload indicates a success.
     JUploadException getUploadException()
              Get the last upload exception that occurs.
     boolean isUploadFinished()
              Indicates whether the upload is finished or not.
     void nbBytesUploaded(long nbBytes, UploadFileData uploadFileData)
              The progressBar is updated each 50ms and each 10% of the target file.
     void run()
              The heart of the program.
     void setUploadException(JUploadException uploadException)
              Stores the last upload exception that occurs.
     void setUploadStatus(UploadFilePacket uploadFilePacket, UploadFileData uploadFileData, int uploadStatus)
              Indicate the current state of the upload, to allow a correct display of UPLOAD_STATUS_UPLOADED status. 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
     
    Methods inherited from interface wjhk.jupload2.upload.FileUploadManagerThread
    interrupt, isAlive, join, start
     

    Field Detail

    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.


    filePreparationThread

    FilePreparationThread filePreparationThread
    The file preparatoin thread prepares each file for upload, and manage possible errors that can occurs at preparation time.


    packetConstructionThread

    PacketConstructionThread packetConstructionThread
    This thread receives each prepared files in a queue, constructs the packets of files to be sent together, and posts them in another queue.


    fileUploadThread

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


    fileBeingUploaded

    UploadFileData fileBeingUploaded
    The file that is currently being uploaded. Allow to refresh the progress bar, with up to date information, based on a timer event.


    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.


    lastUpdateOfProgressBar

    long lastUpdateOfProgressBar
    Contains the last system time (in ms), when the progressBar was updated. It is refreshed by the #nbBytesUploaded(long) method, each 100 ms.


    maxChunkSize

    long maxChunkSize
    See Also:
    UploadPolicy.getMaxChunkSize()

    nbFilesPerRequest

    int nbFilesPerRequest
    See Also:
    UploadPolicy.getNbFilesPerRequest()

    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.


    uploadStatus

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


    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(UploadFilePacket) method)
  • An exception occurs (in the setUploadException(JUploadException) method)
  • The user stops the upload (in the stopUpload() method)

  • 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.

    Constructor Detail

    FileUploadManagerThreadImpl

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

    Parameters:
    uploadPolicy -
    Throws:
    JUploadException

    FileUploadManagerThreadImpl

    FileUploadManagerThreadImpl(UploadPolicy uploadPolicy,
                                FileUploadThread fileUploadThreadParam)
                          throws JUploadException
    Internal constructor. It is used by the JUnit test, to create a FileUploadManagerThreadImpl 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()
    Description copied from interface: FileUploadManagerThread
    The heart of the program. This method prepare the upload, then calls doUpload for each HTTP request.

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

    setUploadException

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

    Specified by:
    setUploadException in interface FileUploadManagerThread
    See Also:
    FileUploadManagerThread.setUploadException(wjhk.jupload2.exception.JUploadException)

    getUploadException

    public JUploadException getUploadException()
    Description copied from interface: FileUploadManagerThread
    Get the last upload exception that occurs.

    Specified by:
    getUploadException in interface FileUploadManagerThread
    Returns:
    The last upload exception, or null if no exception occurs.
    See Also:
    FileUploadManagerThread.getUploadException()

    isUploadFinished

    public boolean isUploadFinished()
    Description copied from interface: FileUploadManagerThread
    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 FileUploadManagerThread.run() method.

    Specified by:
    isUploadFinished in interface FileUploadManagerThread
    Returns:
    true if the upload is finished. False otherwise.
    See Also:
    FileUploadManagerThread.isUploadFinished()

    nbBytesUploaded

    public void nbBytesUploaded(long nbBytes,
                                UploadFileData uploadFileData)
                         throws JUploadException
    The progressBar is updated each 50ms and each 10% of the target file.

    Specified by:
    nbBytesUploaded in interface FileUploadManagerThread
    Parameters:
    nbBytes - Number of additional bytes that where uploaded.
    uploadFileData - The file that is currently being uploade (or null if no file is being uploaded)
    Throws:
    JUploadException
    See Also:
    FileUploadManagerThread.nbBytesUploaded(long, UploadFileData)

    setUploadStatus

    public void setUploadStatus(UploadFilePacket uploadFilePacket,
                                UploadFileData uploadFileData,
                                int uploadStatus)
                         throws JUploadException
    Description copied from interface: FileUploadManagerThread
    Indicate the current state of the upload, to allow a correct display of UPLOAD_STATUS_UPLOADED status. the upload progress bar.

    Specified by:
    setUploadStatus in interface FileUploadManagerThread
    Parameters:
    uploadFilePacket - The current packet. This parameter is mandatory only for the
    uploadFileData - The file whose upload begins, is going on or is finished.
    Throws:
    JUploadException
    See Also:
    FileUploadManagerThread.setUploadStatus(wjhk.jupload2.upload.UploadFilePacket, wjhk.jupload2.upload.UploadFileData, int)

    stopUpload

    public void stopUpload()
    Description copied from interface: FileUploadManagerThread
    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.

    Specified by:
    stopUpload in interface FileUploadManagerThread
    See Also:
    FileUploadManagerThread.stopUpload()

    anotherFileHasBeenSent

    public void anotherFileHasBeenSent(UploadFilePacket uploadFilePacket,
                                       UploadFileData newlyUploadedFileData)
                                throws JUploadException
    Description copied from interface: FileUploadManagerThread
    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.

    Specified by:
    anotherFileHasBeenSent in interface FileUploadManagerThread
    Throws:
    JUploadException
    See Also:
    FileUploadManagerThread.anotherFileHasBeenSent(wjhk.jupload2.upload.UploadFilePacket, wjhk.jupload2.upload.UploadFileData)

    currentRequestIsFinished

    public void currentRequestIsFinished(UploadFilePacket packet)
                                  throws JUploadException
    Description copied from interface: FileUploadManagerThread
    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.

    Specified by:
    currentRequestIsFinished in interface FileUploadManagerThread
    Parameters:
    packet - The packet of files that was successfully uploaded.
    Throws:
    JUploadException
    See Also:
    FileUploadManagerThread.currentRequestIsFinished(wjhk.jupload2.upload.UploadFilePacket)