1
2
3
4
5
6
7
8
9
10
11 package org.astrogrid.applications.manager;
12
13 import org.astrogrid.applications.Application;
14 import org.astrogrid.applications.CeaException;
15 import org.astrogrid.applications.beans.v1.cea.castor.ExecutionSummaryType;
16 import org.astrogrid.applications.beans.v1.cea.castor.MessageType;
17 import org.astrogrid.applications.beans.v1.cea.castor.ResultListType;
18 import org.astrogrid.applications.beans.v1.cea.castor.types.ExecutionPhase;
19 import org.astrogrid.applications.beans.v1.cea.castor.types.LogLevel;
20 import org.astrogrid.applications.manager.observer.RemoteProgressListener;
21 import org.astrogrid.applications.manager.observer.RemoteResultsListener;
22 import org.astrogrid.applications.manager.persist.ExecutionHistory;
23 import org.astrogrid.applications.manager.persist.SummaryHelper;
24 import org.astrogrid.component.descriptor.ComponentDescriptor;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28
29 import java.net.MalformedURLException;
30 import java.net.URI;
31 import java.util.Date;
32 import java.util.Observer;
33
34 import javax.xml.rpc.ServiceException;
35
36 import junit.framework.Test;
37
38 /*** Default implementation of the {@link org.astrogrid.applications.manager.QueryService}
39 * @author Noel Winstanley nw@jb.man.ac.uk 16-Jun-2004
40 *
41 */
42 public class DefaultQueryService implements QueryService, ComponentDescriptor {
43 /***
44 * Commons Logger for this class
45 */
46 private static final Log logger = LogFactory.getLog(DefaultQueryService.class);
47
48 /*** Construct a new DefaultQueryService
49 * @param eh the store to use to service queries
50 */
51 public DefaultQueryService(ExecutionHistory eh) {
52 super();
53 this.executionHistory = eh;
54 }
55 protected final ExecutionHistory executionHistory;
56
57
58 public MessageType queryExecutionStatus(String executionId) throws CeaException {
59 logger.debug("Getting execution status for " +executionId);
60 MessageType retval = null;
61 if (executionHistory.isApplicationInCurrentSet(executionId)) {
62 Application app = executionHistory.getApplicationFromCurrentSet(executionId);
63 retval = app.createTemplateMessage();
64 retval.setContent(app.getStatus().toString());
65 retval.setLevel(LogLevel.INFO);
66 retval.setPhase(app.getStatus().toExecutionPhase());
67 }
68 else
69 {
70 ExecutionSummaryType summary = executionHistory.getApplicationFromArchive(executionId);
71 retval = new MessageType();
72 retval.setContent("The application is no longer running" + summary.getStatus().toString());
73 retval.setLevel(LogLevel.INFO);
74 retval.setPhase(ExecutionPhase.COMPLETED);
75 retval.setSource(summary.getApplicationName() + "\nid" + summary.getExecutionId());
76 retval.setTimestamp(new Date());
77 }
78 return retval;
79
80 }
81
82 public ResultListType getResults(String executionId) throws CeaException {
83 logger.debug("Getting results for " + executionId);
84 if (executionHistory.isApplicationInCurrentSet(executionId)) {
85 return executionHistory.getApplicationFromCurrentSet(executionId).getResult();
86 } else {
87 return executionHistory.getApplicationFromArchive(executionId).getResultList();
88 }
89
90 }
91
92 public ExecutionSummaryType getSummary(String executionId) throws CeaException {
93 logger.debug("Getting summary for " + executionId);
94 if (executionHistory.isApplicationInCurrentSet(executionId)) {
95 Application app = executionHistory.getApplicationFromCurrentSet(executionId);
96 return SummaryHelper.summarize(executionId,app);
97 } else {
98 return executionHistory.getApplicationFromArchive(executionId);
99 }
100 }
101
102 public boolean registerProgressListener(String executionId, URI endpoint) throws CeaException {
103 logger.debug("Registering progress listener for " + executionId + " at " + endpoint);
104 if (! executionHistory.isApplicationInCurrentSet(executionId)) {
105 logger.warn("applicaiton not in current set - no point registering listener, already finished");
106 return false;
107 }
108 Application app = executionHistory.getApplicationFromCurrentSet(executionId);
109 Observer obs = new RemoteProgressListener(endpoint);
110 app.addObserver(obs);
111 return true;
112 }
113
114 public boolean registerResultsListener(String executionId, URI endpoint) throws CeaException {
115 logger.debug("Registering results listener for " + executionId + " at " + endpoint);
116 if (! executionHistory.isApplicationInCurrentSet(executionId)) {
117 logger.warn("application not in current set - no point registering listener, its finished already");
118 return false;
119 }
120 Application app = executionHistory.getApplicationFromCurrentSet(executionId);
121 Observer obs;
122 try {
123 obs = new RemoteResultsListener(endpoint);
124 }
125 catch (MalformedURLException e) {
126 throw new CeaException("could not connect create client for remote service",e);
127 }
128 catch (ServiceException e) {
129 throw new CeaException("Could not create client for remote service",e);
130
131 }
132 app.addObserver(obs);
133 return true;
134 }
135 /***
136 * @see org.astrogrid.component.descriptor.ComponentDescriptor#getName()
137 */
138 public String getName() {
139 return "DefaultQueryService";
140 }
141 /***
142 * @see org.astrogrid.component.descriptor.ComponentDescriptor#getDescription()
143 */
144 public String getDescription() {
145 return "Component that queries status of appliations, and manages callbacks";
146 }
147 /***
148 * @see org.astrogrid.component.descriptor.ComponentDescriptor#getInstallationTest()
149 */
150 public Test getInstallationTest() {
151 return null;
152 }
153
154 }
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183