View Javadoc

1   /*$Id: DatacenterCommander.java,v 1.11 2004/10/12 23:09:53 mch Exp $
2    * Created on 24-Nov-2003
3    *
4    * Copyright (C) AstroGrid. All rights reserved.
5    *
6    * This software is published under the terms of the AstroGrid
7    * Software License version 1.2, a copy of which has been included
8    * with this distribution in the LICENSE.txt file.
9    *
10   **/
11  package org.astrogrid.datacenter.ui;
12  
13  import java.io.File;
14  import java.io.IOException;
15  import java.io.InputStream;
16  import java.io.OutputStream;
17  import java.net.MalformedURLException;
18  import java.net.URL;
19  import java.rmi.MarshalException;
20  import javax.xml.rpc.ServiceException;
21  import org.astrogrid.community.Account;
22  import org.astrogrid.datacenter.delegate.DatacenterDelegateFactory;
23  import org.astrogrid.datacenter.delegate.QuerySearcher;
24  import org.astrogrid.datacenter.query.Query;
25  import org.astrogrid.datacenter.query.SqlQueryMaker;
26  import org.astrogrid.datacenter.returns.ReturnTable;
27  import org.astrogrid.io.Piper;
28  
29  /*** Simple tool to fire a query at a server, and print out the response.
30   * Currently accepts queries in ADQL and SQL
31   * <h2>Examples of use</h2>
32   * For usage instructions, run with no arguments:
33   * <pre>
34   * java org.astrogrid.datacenter.tools.SimpleQuerier
35   * </pre>
36   * Submit an SQL query
37   * <pre>
38   * java org.astrogrid.datacenter.tools.SimpleQuerier http://localhost:8080/pal/services/AxisDataServer --sql "Select * from merlinCatalog where DataNo = 657038"
39   * </pre>
40   * Submit an ADQL query, stored in the file <tt>adql-query.xml</tt>
41   * <pre>
42   * java org.astrogrid.datacenter.tools.SimpleQuerier http://localhost:8080/pal/services/AxisDataServer adql-query.xml
43   * </pre>
44   * @author Noel Winstanley nw@jb.man.ac.uk 24-Nov-2003
45   *
46   */
47  public class DatacenterCommander {
48     
49     /*** Main method interprets inputs */
50     public final static void main(String[] args) throws Exception {
51        try {
52           
53           if (args.length<2) {
54              usage();
55           }
56           else {
57              String command = args[0].toLowerCase().trim();
58              String endpoint = args[1];
59              
60              if (command.equals("adql")) {
61                 doAdql(endpoint, args);
62              }
63              else if (command.equals("sql")) {
64                 if (args.length<3) {
65                    System.out.println("No SQL given");
66                    usage();
67                 }
68                 else {
69                    doSql(endpoint, args[2]);
70                 }
71              }
72              else if (command.equals("cone")) {
73                 doCone(endpoint);
74              }
75              else {
76                 System.out.println("Unknown command: "+command);
77                 usage();
78              }
79           }
80        } catch (Exception e) {
81           System.out.println("Error: " + e.getClass().getName() + " - " + e.getMessage());
82           usage();
83        }
84     }
85     
86     public DatacenterCommander() {
87     }
88     
89     
90     public static void usage() {
91        System.out.println("Usage: java "+DatacenterCommander.class+" <command> <service URL> [ parameters ]");
92        System.out.println();
93        System.out.println("commands:");
94        System.out.println("   sql: submit string given in parameters as SQL ");
95        System.out.println("   adql: submit query at url given in parameter ");
96        System.out.println("   cone: do cone search; give serviceurl as base?RA=20;DEC=30;SR=4 as normal NVO");
97     }
98     
99     public static void doAdql(String endpoint, String[] args) throws ServiceException, IOException {
100       
101       if (args.length<3) {
102          System.out.println("No ADQL file given");
103          usage();
104          return;
105       }
106       File queryFile = new File(args[2]);
107       if (queryFile == null) {
108          System.out.println("Cannot find ADQL query file "+args[2]);
109          usage();
110          return;
111       }
112       System.out.println("Connecting to server...");
113       QuerySearcher del = DatacenterDelegateFactory.makeQuerySearcher(Account.ANONYMOUS, endpoint.toString(), DatacenterDelegateFactory.ASTROGRID_WEB_SERVICE);
114       System.out.println("Reading query...");
115       System.out.println("...FAIL: need to update this bit of code");
116       /*
117       Select select = Select.unmarshalSelect(new InputStreamReader ( new FileInputStream(queryFile)));
118       Element queryBody = ADQLUtils.toQueryBody(select);
119       System.out.println("Asking query...");
120       InputStream results = del.askQuery(new AdqlQuery(queryBody), QuerySearcher.VOTABLE);
121       System.out.println("Results:");
122       Piper.bufferedPipe(results, System.out);
123        */
124    }
125    
126    
127    public static void doCone(String endpoint) throws MalformedURLException, IOException {
128       URL url = new URL(endpoint);
129       
130       System.out.println("Asking query...");
131       InputStream in = url.openStream();
132       OutputStream out = System.out;
133       
134       System.out.println("Results:");
135       Piper.bufferedPipe(in, out);
136    }
137    
138             
139 
140    
141    public static void doSql(String endpoint, String sql) throws ServiceException, IOException {
142       
143       System.out.println("Connecting to server...");
144       QuerySearcher del = DatacenterDelegateFactory.makeQuerySearcher(Account.ANONYMOUS, endpoint.toString(), DatacenterDelegateFactory.ASTROGRID_WEB_SERVICE);
145       System.out.println("Asking query...");
146       Query query = SqlQueryMaker.makeQuery(sql);
147       query.setResultsDef(new ReturnTable(null, QuerySearcher.VOTABLE));
148       InputStream results = del.askQuery(query);
149       System.out.println("Results:");
150       Piper.bufferedPipe(results, System.out);
151    }
152    
153 }
154 
155 
156 /*
157  $Log: DatacenterCommander.java,v $
158  Revision 1.11  2004/10/12 23:09:53  mch
159  Lots of changes to querying to get proxy querying to SSA, and registry stuff
160 
161  Revision 1.10  2004/10/08 15:17:54  mch
162  Some updates to try and reach SSA/etc at ROE
163 
164  Revision 1.9  2004/10/06 21:12:17  mch
165  Big Lump of changes to pass Query OM around instead of Query subclasses, and TargetIndicator mixed into Slinger
166 
167  Revision 1.8  2004/09/28 15:49:55  mch
168  Removed calls to deprecated methods
169 
170  Revision 1.7  2004/09/28 14:59:35  mch
171  Removed obsolete ADQL 0.5 object model
172 
173  Revision 1.6  2004/03/13 16:26:25  mch
174  Changed makeFullSearcher to makeQuerySearcher
175 
176  Revision 1.5  2004/03/13 16:19:38  mch
177  Tidying up after refactor
178 
179  Revision 1.4  2004/03/12 20:00:11  mch
180  It05 Refactor (Client)
181 
182  Revision 1.3  2004/03/07 21:13:52  mch
183  Changed apache XMLUtils to implementation-independent DomHelper
184 
185  Revision 1.2  2004/03/06 19:34:21  mch
186  Merged in mostly support code (eg web query form) changes
187 
188  Revision 1.1  2004/03/03 10:08:01  mch
189  Moved UI and some IO stuff into client
190 
191  Revision 1.5  2004/01/14 12:55:28  nw
192  improved documentation
193 
194  Revision 1.4  2004/01/14 12:49:49  nw
195  improved documentation
196 
197  Revision 1.3  2004/01/13 00:32:47  nw
198  Merged in branch providing
199  * sql pass-through
200  * replace Certification by User
201  * Rename _query as Query
202 
203  Revision 1.2.10.3  2004/01/12 17:04:50  nw
204  fixed bug with argument parsing
205 
206  Revision 1.2.10.2  2004/01/08 09:42:26  nw
207  tidied imports
208 
209  Revision 1.2.10.1  2004/01/08 09:10:20  nw
210  replaced adql front end with a generalized front end that accepts
211  a range of query languages (pass-thru sql at the moment)
212 
213  Revision 1.2  2003/11/26 16:31:46  nw
214  altered transport to accept any query format.
215  moved back to axis from castor
216 
217  Revision 1.1  2003/11/24 21:06:01  nw
218  added little command-line tool to query datacenter
219  
220  */