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