View Javadoc

1   package org.xvsm.internal.tasks;
2   
3   import java.net.URI;
4   import java.util.ArrayList;
5   import java.util.List;
6   
7   import org.xvsm.core.AtomicEntry;
8   import org.xvsm.core.ContainerRef;
9   import org.xvsm.core.Entry;
10  import org.xvsm.core.ExceptionEntry;
11  import org.xvsm.core.VoidEntry;
12  import org.xvsm.internal.exceptions.FatalException;
13  import org.xvsm.internal.exceptions.XCoreRemoteException;
14  import org.xvsm.remote.TransportHandler;
15  import org.xvsm.remote.interfaces.ITransportSender;
16  import org.xvsm.selectors.Selector;
17  
18  /***
19   * 
20   * @author Christian Schreiber, Michael Proestler
21   * 
22   */
23  public class ReplyTask extends Task {
24  
25  	/***
26  	 * The SerialVersionUID of this Class.
27  	 */
28  	private static final long serialVersionUID = 1566846387170832107L;
29  
30  	/***
31  	 * Default Constructor taking a task. Creates a new ReplyTask with the
32  	 * result of the given task.
33  	 * 
34  	 * @param task
35  	 *            the task that has been fulfilled.
36  	 * 
37  	 */
38  	public ReplyTask(Task task) {
39  		super(task.getTaskID());
40  		this.setResult(task.readResult());
41  		this.setAnswerToContainer(task.getAnswerToContainer());
42  	}
43  
44  	/***
45  	 * Creates a new ReplyTask.
46  	 * 
47  	 * @param answer
48  	 *            the answer which has to be send.
49  	 * @param answerContainer
50  	 *            the container in which the answer is expected.
51  	 */
52  	public ReplyTask(Object answer, URI answerContainer, long taskID) {
53  		super(taskID);
54  		this.setResult(answer);
55  		this.setAnswerToContainer(answerContainer);
56  	}
57  
58  	/***
59  	 * {@inheritDoc}
60  	 */
61  	// @Override
62  	@SuppressWarnings("unchecked")
63  	public void run() {
64  		Thread.currentThread().setName(this.getClass().getName());
65  		Object o = this.readResult();
66  		List<Entry> result;
67  		// Read and take return a List of entries ... these results should not
68  		// be encapsulated in an AtomicEntry
69  		if (o instanceof List) {
70  			result = (List<Entry>) o;
71  			List<Entry> entries = new ArrayList(result.size());
72  			for (int i = 0; i < result.size(); i++) {
73  				if (result.get(i).getEntryType().equals(Entry.EntryTypes.VOID)) {
74  					// result.remove(i);
75  				} else {
76  					// create a copy of the entry and remove all the
77  					// selectors.
78  					Entry e = result.get(i).getFlatCopy();
79  					e.setSelectors(null);
80  					entries.add(e);
81  				}
82  			}
83  			if (entries.size() <= 0) {
84  				// happens when CNT_ALL is used and no entry was available.
85  				entries.add(new VoidEntry());
86  			}
87  			result = entries;
88  		} else if (o instanceof VoidEntry) {
89  			result = new ArrayList<Entry>();
90  			result.add((VoidEntry) o);
91  		} else if (o instanceof Throwable) {
92  			result = new ArrayList<Entry>();
93  			result.add(new ExceptionEntry(o.getClass().getSimpleName(),
94  					((Throwable) o).getMessage()));
95  		} else {
96  			// we write the result as Entry containing the result.
97  			// TODO: I think this can not be mapped to xml.
98  			result = new ArrayList<Entry>();
99  			result.add(new AtomicEntry<Object>(o));
100 		}
101 
102 		ContainerRef cref = new ContainerRef(this.getAnswerToContainer());
103 		@SuppressWarnings("unchecked")
104 		ITransportSender sender = TransportHandler.getInstance().getSender(
105 				cref.getSite());
106 		try {
107 			sender.sendResponse(cref.getSite(), new OperationTask(
108 					OperationTaskType.WRITE, getTaskID(), result, cref, null,
109 					new ArrayList<Selector>(), 0));
110 		} catch (XCoreRemoteException e) {
111 			throw new FatalException(e);
112 		}
113 	}
114 }