View Javadoc

1   package fr.in2p3.jsaga.impl.task;
2   
3   import org.ogf.saga.error.*;
4   import org.ogf.saga.task.*;
5   
6   /* ***************************************************
7    * *** Centre de Calcul de l'IN2P3 - Lyon (France) ***
8    * ***             http://cc.in2p3.fr/             ***
9    * ***************************************************
10   * File:   AbstractThreadedTask
11   * Author: Sylvain Reynaud (sreynaud@in2p3.fr)
12   * Date:   29 mai 2009
13   * ***************************************************
14   * Description:                                      */
15  /**
16   *
17   */
18  public abstract class AbstractThreadedTask<T,E> extends AbstractTaskImpl<T,E> implements Task<T,E> {
19      private Thread m_thread;
20  
21      /** constructor */
22      public AbstractThreadedTask(TaskMode mode) throws NotImplementedException {
23          super(null, null, true);
24  
25          // set thread
26          m_thread = new Thread(new Runnable(){
27              public void run() {
28                  AbstractThreadedTask.super.setState(State.RUNNING);
29                  try {
30                      E result = AbstractThreadedTask.this.invoke();
31                      AbstractThreadedTask.super.setResult(result);
32                      AbstractThreadedTask.super.setState(State.DONE);
33                  } catch (SagaException e) {
34                      AbstractThreadedTask.super.setException(e);
35                      AbstractThreadedTask.super.setState(State.FAILED);
36                  }
37              }
38          });
39          try {
40              switch(mode) {
41                  case TASK:
42                      break;
43                  case ASYNC:
44                      this.run();
45                      break;
46                  case SYNC:
47                      this.run();
48                      this.waitFor();
49                      break;
50                  default:
51                      throw new NotImplementedException("INTERNAL ERROR: unexpected exception");
52              }
53          } catch (NotImplementedException e) {
54              throw e;
55          } catch (SagaException e) {
56              throw new NotImplementedException(e);
57          }
58      }
59  
60      public abstract E invoke() throws NotImplementedException, IncorrectURLException,
61              AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException,
62              BadParameterException, IncorrectStateException, AlreadyExistsException, DoesNotExistException,
63              TimeoutException, NoSuccessException;
64  
65      ////////////////////////////////////// implementation of AbstractTaskImpl //////////////////////////////////////
66  
67      public void doSubmit() throws NotImplementedException, IncorrectStateException, TimeoutException, NoSuccessException {
68          // start thread
69          m_thread.start();
70      }
71  
72      protected void doCancel() {
73          // cancel thread
74          try {
75              m_thread.interrupt();
76              this.setState(State.CANCELED);
77          } catch(SecurityException e) {
78              // do nothing (failed to cancel task)
79          }
80      }
81  
82      protected State queryState() {
83          return null;    // GenericThreadedTask does not support queryState
84      }
85  
86      public boolean startListening() {
87          return true;    // GenericThreadedTask is always listening anyway...
88      }
89  
90      public void stopListening() {
91          // do nothing
92      }
93  }