1
2
3
4
5
6
7
8
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
118
119
120
121
122
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
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
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