1 package fr.in2p3.jsaga.impl.task;
2
3 import org.ogf.saga.error.*;
4 import org.ogf.saga.task.*;
5
6
7
8
9
10
11
12
13
14
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
22 public AbstractThreadedTask(TaskMode mode) throws NotImplementedException {
23 super(null, null, true);
24
25
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
66
67 public void doSubmit() throws NotImplementedException, IncorrectStateException, TimeoutException, NoSuccessException {
68
69 m_thread.start();
70 }
71
72 protected void doCancel() {
73
74 try {
75 m_thread.interrupt();
76 this.setState(State.CANCELED);
77 } catch(SecurityException e) {
78
79 }
80 }
81
82 protected State queryState() {
83 return null;
84 }
85
86 public boolean startListening() {
87 return true;
88 }
89
90 public void stopListening() {
91
92 }
93 }