org.jdesktop.ws
Class BaseService

java.lang.Object
  extended by org.jdesktop.beans.AbstractBean
      extended by org.jdesktop.ws.BaseService
Direct Known Subclasses:
AbstractHttpService

public abstract class BaseService
extends org.jdesktop.beans.AbstractBean

An abstact class useful as a base for asynchronous data service classes. For example, HttpService is a useful subclass for executing REST like webservice calls at a low level. WebService is a useful class which embodies one or more Operations (which are instances of BaseService) and which is useful for executing SOAP requests.

One could also imagine the creation of FtpService, SshService, SftpService, DatabaseService, and other such non http-based services.

BaseService serves as a base class for creating asynchronous Swing-friendly web service APIs. All events fired from BaseService are fired on the EDT, and it is assumed that all methods invoked on BaseService are likewise invoked on the Event Dispatching Thread (EDT).


Constructor Summary
protected BaseService()
          Creates a new BaseService.
 
Method Summary
 void abort()
          Aborts the current request.
 void addAbortListener(AbortListener listener)
          Adds an AbortListener
 void addDoneListener(DoneListener listener)
          Adds a DoneListener.
 void addFailureListener(FailureListener listener)
          Adds a FailureListener
protected abstract  void doAbort()
          Invoked by this AbstractHttpService, this method performs the actual chores behind aborting the asynchronous process.
protected  void done()
          Called by the subclass when the background process completes successfully.
protected abstract  void doSend()
          Invoked by this AbstractHttpService, this method performs the actual send operation.
 void fail(Throwable th)
          Invoke this method from subclasses to force the connection to fail.
protected  void fireOnEDT(String prop, Object oldv, Object newv)
          Fires the given property change event on the EDT in a manner that avoids calling invokeLater for every property change event.
 float getProgress()
          Property indicating the upload or download progress.
 State getState()
          Property indicating the state of the service.
 void removeAbortListener(AbortListener listener)
          Removes an AbortListener
 void removeDoneListener(DoneListener listener)
          Removes a DoneListener
 void removeFailureListener(FailureListener listener)
          Removes a FailureListener
 void send()
          Initiates the request/response cycle by sending a request to the remote service.
protected  void setProgress(float p)
          Sets the progress.
protected  void setState(State s)
          Sets the local state property, firing a property change event on the EventDispatchThread if necessary.
 
Methods inherited from class org.jdesktop.beans.AbstractBean
addPropertyChangeListener, addPropertyChangeListener, addVetoableChangeListener, addVetoableChangeListener, clone, fireIndexedPropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, fireVetoableChange, getPropertyChangeListeners, getPropertyChangeListeners, getVetoableChangeListeners, getVetoableChangeListeners, hasPropertyChangeListeners, hasVetoableChangeListeners, removePropertyChangeListener, removePropertyChangeListener, removeVetoableChangeListener, removeVetoableChangeListener
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

BaseService

protected BaseService()
Creates a new BaseService.

Method Detail

getState

public final State getState()
Property indicating the state of the service. PropertyChange events will be fired whenever this state changes


setState

protected void setState(State s)
Sets the local state property, firing a property change event on the EventDispatchThread if necessary. The value of the progress property is set according to the rules outlined in the field javadocs for progress.

Parameters:
s -

getProgress

public final float getProgress()
Property indicating the upload or download progress. This is updated automatically based on the state of the service. A value of -1 indicates that progress is not trackable, either because it is indeterminate or because the State is not Sending or Receiving. All states other than Sending or Receiving will always indicate a value of -1, except for SENT and DONE. This is a float value between 0-1 when it is valid.


setProgress

protected void setProgress(float p)
Sets the progress. If values are < -1 or > 1, then they are clamped to -1 and 1 respectively. A property change event is fired on the EDT.

Parameters:
p -

send

public final void send()
Initiates the request/response cycle by sending a request to the remote service. This call returns immediately. Either use a property change listener for the state property or add one or more of DoneListener, FailureListener, AbortListener.


abort

public final void abort()
Aborts the current request. If called when in a state that is not abortable (such as Ready or Done) then nothing happens. The final state may not be set to ABORTED, but the aborted function of any AbortListeners will be invoked.


fail

public void fail(Throwable th)
Invoke this method from subclasses to force the connection to fail. Note that this only works if the current state is CONNECTING, SENDING, SENT, or RECEIVING. In all other states invoking this method has no effect. TODO This method should be protected, but the FX-Script class needs access to it. Not sure what the right choice is here. Do not invoke this method directly. In particular, it doesn't abort first, so if the task hasn't already completed then don't even bother calling this method until it does because there will be problems.

Parameters:
th -

doSend

protected abstract void doSend()
                        throws Exception
Invoked by this AbstractHttpService, this method performs the actual send operation. This method must not block.

Throws:
Exception

doAbort

protected abstract void doAbort()
Invoked by this AbstractHttpService, this method performs the actual chores behind aborting the asynchronous process. Do not bother setting the state, just abort the process.


done

protected final void done()
Called by the subclass when the background process completes successfully.


fireOnEDT

protected final void fireOnEDT(String prop,
                               Object oldv,
                               Object newv)
Fires the given property change event on the EDT in a manner that avoids calling invokeLater for every property change event. This can greatly improve performance in cases where many events happen very quickly.

Parameters:
prop -
oldv -
newv -

addDoneListener

public final void addDoneListener(DoneListener listener)
Adds a DoneListener.

Parameters:
listener -

removeDoneListener

public final void removeDoneListener(DoneListener listener)
Removes a DoneListener

Parameters:
listener -

addAbortListener

public void addAbortListener(AbortListener listener)
Adds an AbortListener

Parameters:
listener -

removeAbortListener

public void removeAbortListener(AbortListener listener)
Removes an AbortListener

Parameters:
listener -

addFailureListener

public void addFailureListener(FailureListener listener)
Adds a FailureListener

Parameters:
listener -

removeFailureListener

public void removeFailureListener(FailureListener listener)
Removes a FailureListener

Parameters:
listener -