View Javadoc

1   /*
2    * $Id: AxisDataService_v06.java,v 1.3 2005/03/22 12:57:37 mch Exp $
3    *
4    * (C) Copyright Astrogrid...
5    */
6   
7   package org.astrogrid.dataservice.service.soap;
8   
9   import java.io.IOException;
10  import java.io.StringWriter;
11  import java.net.MalformedURLException;
12  import java.rmi.RemoteException;
13  import javax.xml.rpc.ServiceException;
14  import javax.xml.rpc.server.ServiceLifecycle;
15  import org.apache.axis.AxisFault;
16  import org.apache.axis.MessageContext;
17  import org.astrogrid.dataservice.queriers.status.QuerierStatus;
18  import org.astrogrid.dataservice.service.AxisDataServer;
19  import org.astrogrid.query.Query;
20  import org.astrogrid.query.adql.Adql074Writer;
21  import org.astrogrid.query.adql.AdqlQueryMaker;
22  import org.astrogrid.query.sql.SqlParser;
23  import org.astrogrid.slinger.targets.TargetMaker;
24  import org.astrogrid.status.DefaultTaskStatus;
25  import org.astrogrid.status.ServiceStatus;
26  import org.astrogrid.xml.DomHelper;
27  import org.w3c.dom.Element;
28  
29  /***
30   * The implementation of the Datacenter axis web service for end of Itn06
31   * <p>
32   * When Axis receives a SOAP message from the client it is routed to this class for processing.
33   * It is a singleton; state comes from the Queriers.
34  
35   * @author M Hill
36   *
37   */
38  
39  public class AxisDataService_v06 implements ServiceLifecycle {
40     
41     
42     AxisDataServer server = new AxisDataServer();
43     
44     /*** Called by Axis when the service needs to be initialised. Not actuall of
45      * much use, as it only gets called when Axis is called, and other servlet/JSP access
46      * won't do that
47      */
48     public void init(Object context) throws ServiceException {
49        if (context instanceof javax.xml.rpc.server.ServletEndpointContext) {
50           javax.xml.rpc.server.ServletEndpointContext secontext = (javax.xml.rpc.server.ServletEndpointContext) context;
51           //erm.
52        }
53     }
54     
55     public void destroy() {
56     }
57     
58     /***
59      * Ask adql query (blocking request).  Returns results.
60      */
61     public String askAdql(Element adql, String requestedFormat) throws AxisFault {
62        try {
63           //horrible debuggy thing
64           if (MessageContext.getCurrentContext() != null) {
65              MessageContext.getCurrentContext().getMessage().writeTo(System.out);
66           }
67           StringWriter sw = new StringWriter();
68           Query query = AdqlQueryMaker.makeQuery(adql);
69           query.getResultsDef().setFormat(requestedFormat);
70           query.getResultsDef().setTarget(TargetMaker.makeTarget(sw));
71           server.askQuery(server.getUser(), query, server.getSource()+" via AxisDataService06.askAdql");
72           return sw.toString();
73        }
74        catch (Throwable e) {
75           throw server.makeFault(server.SERVERFAULT, e+", asking Query("+adql+", "+requestedFormat+")", e);
76        }
77     }
78  
79     /***
80      * Ask adql query (blocking request) sending results to given target, returning
81      * the target uri
82      */
83     public String askAdqlTo(Element adql, String requestedFormat, String target) throws AxisFault {
84        try {
85           //horrible debuggy thing
86           if (MessageContext.getCurrentContext() != null) {
87              MessageContext.getCurrentContext().getMessage().writeTo(System.out);
88           }
89           if (target == null) {
90              throw new IllegalArgumentException("No target given");
91           }
92           Query query = AdqlQueryMaker.makeQuery(adql);
93           query.getResultsDef().setFormat(requestedFormat);
94           query.getResultsDef().setTarget(TargetMaker.makeTarget(target));
95           server.askQuery(server.getUser(), query, server.getSource()+" via AxisDataService06.askAdql");
96           return target.toString();
97        }
98        catch (Throwable e) {
99           throw server.makeFault(server.SERVERFAULT, e+", asking Query("+adql+", "+requestedFormat+", ->"+target+")", e);
100       }
101    }
102 
103    /***
104     * Translation service - Converts SQL-like ADQL into XML ADQL
105     */
106    public Element adqlSql2xml(String sql) throws AxisFault {
107       try {
108          if ((sql == null) || (sql.trim().length()==0)) {
109             throw new IllegalArgumentException("No SQL given");
110          }
111          return DomHelper.newDocument(Adql074Writer.makeAdql(SqlParser.makeQuery(sql), "From SQL: "+sql)).getDocumentElement();
112       }
113       catch (Throwable e) {
114          throw server.makeFault(server.SERVERFAULT, e+", converting SQL "+sql, e);
115       }
116    }
117 
118    /*** Returns the number of matches - this ought to be part of the select statement but
119     * it will do for temporary use */
120    public long askCount(Element adql) throws AxisFault {
121       try {
122          Query query = AdqlQueryMaker.makeQuery(adql);
123          return server.askCount(server.getUser(), query, server.getSource()+" via AxisDataService06.askCount");
124       }
125       catch (Throwable e) {
126          throw server.makeFault(server.SERVERFAULT, e+", asking Query("+adql+")", e);
127       }
128    }
129 
130    /***
131     * Ask raw sql for blocking operation - returns the results
132     *
133    public String askSql(String sql, String requestedFormat) throws AxisFault {
134       try {
135          if (!ConfigFactory.getCommonConfig().getBoolean(DataServer.SQL_PASSTHROUGH_ENABLED, false)) {
136             throw new UnsupportedOperationException("This server does not support raw SQL queries - use ADQL");
137          }
138          
139          StringWriter sw = new StringWriter();
140          Query query = SqlParser.makeQuery(sql);
141          query.getResultsDef().setFormat(requestedFormat);
142          query.getResultsDef().setTarget(TargetIndicator.makeIndicator(sw));
143          server.askQuery(getUser(), query);
144          return sw.toString();
145       }
146       catch (Throwable e) {
147          throw server.makeFault(server.SERVERFAULT, "Error asking Query("+sql+", "+requestedFormat+")", e);
148       }
149    }
150 
151    /**
152     * Submit query for asynchronous operation - returns id of query
153     */
154    public String submitAdql(Element adql, String requestedFormat, String resultsTarget) throws AxisFault {
155       try {
156          Query query = AdqlQueryMaker.makeQuery(adql);
157          query.getResultsDef().setFormat(requestedFormat);
158          query.getResultsDef().setTarget(TargetMaker.makeTarget(resultsTarget));
159          return server.submitQuery(server.getUser(), query, server.getSource()+" via AxisDataService06.submitAdql");
160       }
161       catch (MalformedURLException mue) {
162          throw server.makeFault(server.CLIENTFAULT, "malformed resultsTarget", mue);
163       }
164       catch (Throwable th) {
165          throw server.makeFault(server.SERVERFAULT, th+", submitting Adql Query", th);
166       }
167    }
168    
169    
170    /***
171     * Aborts the query specified by the given id.  Returns
172     * nothing - if the server can't stop it it's a server-end problem.
173     */
174    public void abortQuery(String queryId) throws AxisFault {
175       try {
176          server.abortQuery(server.getUser(), queryId);
177       }
178       catch (IOException ioe) {
179          throw server.makeFault(server.SERVERFAULT, ioe+", aborting query "+queryId, ioe);
180       }
181    }
182    
183    /***
184     * Returns the state of the query with the given id
185     */
186    public QueryStatusSoapyBean getQueryStatus(String queryId) throws AxisFault {
187       try {
188          QueryStatusSoapyBean soapyStatus = new QueryStatusSoapyBean();
189          soapyStatus.setQueryID(queryId);
190          QuerierStatus status = server.getQueryStatus(server.getUser(), queryId);
191          soapyStatus.setState(status.getState().toString());
192          soapyStatus.setNote(status.getMessage());
193          return soapyStatus;
194       }
195       catch (IOException ioe) {
196          throw server.makeFault(server.SERVERFAULT, ioe+", getting status of query "+queryId, ioe);
197       }
198    }
199 
200    /***
201     * Returns the state of the query with the given id
202     */
203    public DefaultTaskStatus getTaskStatus(String queryId) throws AxisFault {
204       try {
205          return server.getQueryStatus(server.getUser(), queryId);
206       }
207       catch (IOException ioe) {
208          throw server.makeFault(server.SERVERFAULT, ioe+", getting status of query "+queryId, ioe);
209       }
210    }
211    
212    /***
213     * Returns the state of the service
214     */
215    public ServiceStatus getServiceStatus() throws AxisFault {
216       return server.getStatus().getServiceStatus();
217    }
218 
219    /***
220     * Returns a simple text string indicating the state of the service
221     */
222    public String getSimpleServiceStatus() throws AxisFault {
223       return server.getStatus().toString();
224    }
225    
226 
227    /*** Returns the Registry entries for this service.  Same as getMetadata */
228    public String getRegistration() throws RemoteException {
229       try {
230          return server.getMetadata();
231       }
232       catch (IOException ioe) {
233          throw server.makeFault(server.SERVERFAULT, ioe+", getting metadata", ioe);
234       }
235    }
236    
237    /*** Returns a document describing the service */
238    public Element getMetadata() throws RemoteException {
239       try {
240          return DomHelper.newDocument(server.getMetadata()).getDocumentElement();
241       }
242       catch (Exception ioe) {
243          throw server.makeFault(server.SERVERFAULT, ioe+", getting metadata", ioe);
244       }
245    }
246    
247    /***
248     * Useful mechanism for testing that clients can receive and process faults.
249     * The useful bit of stack trace will be limited to this method but ho hum
250     */
251    public void throwFault() throws AxisFault {
252       throw server.makeFault(server.CLIENTFAULT, "Client asked for this", new IOException("Client deliberately threw test fault "));
253    }
254    
255    
256    
257 }
258 
259 /*
260 $Log: AxisDataService_v06.java,v $
261 Revision 1.3  2005/03/22 12:57:37  mch
262 naughty bunch of changes
263 
264 Revision 1.2  2005/03/21 18:45:55  mch
265 Naughty big lump of changes
266 
267 Revision 1.1  2005/02/18 18:16:40  mch
268 Added
269 
270 Revision 1.13.2.10  2004/12/08 18:36:40  mch
271 Added Vizier, rationalised SqlWriters etc, separated out TableResults from QueryResults
272 
273 Revision 1.13.2.9  2004/12/07 21:21:09  mch
274 Fixes after a days integration testing
275 
276 Revision 1.13.2.8  2004/12/05 19:33:16  mch
277 changed skynode to 'raw' soap (from axis) and bug fixes
278 
279 Revision 1.13.2.7  2004/12/03 11:58:57  mch
280 various fixes while at data mining
281 
282 Revision 1.13.2.6  2004/11/30 01:04:02  mch
283 Rationalised tablewriters, reverted AxisDataService06 to string
284 
285 Revision 1.13.2.5  2004/11/29 23:33:46  mch
286 added overdone debug
287 
288 Revision 1.13.2.4  2004/11/29 21:52:18  mch
289 Fixes to skynode, log.error(), getstem, status logger, etc following tests on grendel
290 
291 Revision 1.13.2.3  2004/11/25 18:33:43  mch
292 more status (incl persisting) more tablewriting lots of fixes
293 
294 Revision 1.13.2.2  2004/11/23 11:55:06  mch
295 renamved makeTarget methods
296 
297 Revision 1.13.2.1  2004/11/22 00:57:16  mch
298 New interfaces for SIAP etc and new slinger package
299 
300 Revision 1.13  2004/11/11 20:42:50  mch
301 Fixes to Vizier plugin, introduced SkyNode, started SssImagePlugin
302 
303 Revision 1.12  2004/11/09 18:27:21  mch
304 added askCount
305 
306 Revision 1.11  2004/11/09 17:42:22  mch
307 Fixes to tests after fixes for demos, incl adding closable to targetIndicators
308 
309 Revision 1.10  2004/11/03 00:17:56  mch
310 PAL_MCH Candidate 2 merge
311 
312 Revision 1.6.8.4  2004/11/02 19:41:26  mch
313 Split TargetIndicator to indicator and maker
314 
315 Revision 1.6.8.3  2004/10/27 00:43:40  mch
316 Started adding getCount, some resource fixes, some jsps
317 
318 Revision 1.6.8.2  2004/10/21 19:10:24  mch
319 Removed deprecated translators, moved SqlMaker back to server,
320 
321 Revision 1.6.8.1  2004/10/20 19:09:21  mch
322 Added doc on init
323 
324 Revision 1.6  2004/10/12 21:33:57  mch
325 Slight change to error messages
326 
327 Revision 1.5  2004/10/08 15:16:04  mch
328 More on providing status
329 
330 Revision 1.4  2004/10/06 21:12:17  mch
331 Big Lump of changes to pass Query OM around instead of Query subclasses, and TargetIndicator mixed into Slinger
332 
333 Revision 1.3  2004/10/06 11:35:35  mch
334 A bit of tidying up around the web service interfaces.First stage SkyNode implementation
335 
336 Revision 1.2  2004/10/05 19:22:11  mch
337 Added SQL to ADQL converter method
338 
339 Revision 1.1  2004/10/05 14:56:45  mch
340 Added new web interface and partial skynode
341 
342 Revision 1.2  2004/10/01 18:04:59  mch
343 Some factoring out of status stuff, added monitor page
344 
345 Revision 1.1  2004/09/28 15:02:13  mch
346 Merged PAL and server packages
347 
348 Revision 1.6  2004/08/27 17:47:19  mch
349 Added first servlet; started making more use of ReturnSpec
350 
351 Revision 1.5  2004/08/25 23:38:34  mch
352 (Days changes) moved many query- and results- related classes, renamed packages, added tests, added CIRCLE to sql/adql parsers
353 
354 Revision 1.4  2004/08/18 22:29:39  mch
355 Take more general TargetIndicator rather than AGSL
356 
357 Revision 1.3  2004/08/17 20:19:36  mch
358 Moved TargetIndicator to client
359 
360 Revision 1.2  2004/04/05 15:59:36  mch
361 Introduced multiple services to one deployment
362 
363 Revision 1.1  2004/04/05 15:55:06  mch
364 Renamed it05 axis data service
365 
366 Revision 1.3  2004/03/18 23:42:09  mch
367 Removed dummy getMetadata
368 
369 Revision 1.2  2004/03/17 01:47:26  mch
370 Added v05 Axis web interface
371 
372 Revision 1.1  2004/03/17 00:27:21  mch
373 Added v05 AxisDataServer
374 
375  */
376 
377 
378