source: trunk/java-client/org/nzdl/gsdl/SimpleClient.java@ 2165

Last change on this file since 2165 was 2148, checked in by say1, 23 years ago

fixed bugs and added error checking ...

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 14.3 KB
Line 
1/*
2 * SimpleClient.java
3 * Copyright (C) 2001 New Zealand Digital Library Project
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20// the package we're in
21package org.nzdl.gsdl;
22
23// long live GNU ...
24import gnu.getopt.Getopt;
25import gnu.getopt.LongOpt;
26
27// java libraries we're using
28import java.io.BufferedReader;
29import java.io.DataInputStream;
30import java.io.File;
31import java.io.FileInputStream;
32import java.io.FileOutputStream;
33import java.io.FileReader;
34import java.io.IOException;
35import java.io.InputStream;
36import java.io.InputStreamReader;
37import java.io.LineNumberReader;
38import java.io.Serializable;
39import java.net.URL;
40import java.util.Enumeration;
41import java.util.Iterator;
42import java.util.List;
43import java.util.ListIterator;
44import java.util.Map;
45import java.util.Properties;
46import java.util.Set;
47import java.util.Vector;
48
49// local libraries
50import org.nzdl.gsdl.service.NzdlCollectionInfo;
51import org.nzdl.gsdl.service.NzdlQuery;
52import org.nzdl.gsdl.service.NzdlRequest;
53import org.nzdl.gsdl.service.NzdlResponse;
54import org.nzdl.gsdl.service.NzdlResultSet;
55import org.nzdl.gsdl.service.NzdlService;
56import org.nzdl.gsdl.service.NzdlServiceClient;
57import org.nzdl.gsdl.util.NzdlIORs;
58
59/**
60 * Class SimpleClient
61 *
62 * A class to test the workings of Corba interface from the client side.
63 *
64 * @author stuart yeates ([email protected])
65 * @author Brett Sheeran ([email protected])
66 * @author Dave Nichols ([email protected])
67 * @author Gordon Paynter ([email protected])
68 * @version $Revision: 2148 $
69 * @see org.nzdl.gsdl.SimpleServer
70 * @see org.nzdl.gsdl.service.NzdlCollectionInfo
71 * @see org.nzdl.gsdl.service.NzdlQuery
72 * @see org.nzdl.gsdl.service.NzdlRequest
73 * @see org.nzdl.gsdl.service.NzdlResponse
74 * @see org.nzdl.gsdl.service.NzdlResultSet
75 * @see org.nzdl.gsdl.service.NzdlService
76 * @see org.nzdl.gsdl.service.NzdlServiceClient
77 * @see gnu.getopt.Getopt
78 * @see gnu.getopt.LongOpt
79 * @see <a href="http://www.nzdl.org/">The New Zealand Digital Library Project</a>
80 * @see <a href="http://www.nzdl.org/fast-cgi-bin/library?&a=p&p=gsdl">Greenstone Digital Library Software</a>
81 */
82
83public class SimpleClient implements Cloneable, Serializable {
84
85 /** The underlying CORBA inferface */
86 NzdlService nzdl = null;
87 /** Look for an IOR in the file first */
88 boolean useFileFirst = false;
89 /** The IOR that was specified on the commandline */
90 String IORFromCommandLine = null;
91 /** The URL that was specified on the commandline */
92 String URLFromCommandLine = null;
93 /** The name of the file we look for IOR's in */
94 static String IORFileName = "/tmp/localcorba.objid";
95 /** The collection to query */
96 String collectionNameToQuery = "";
97 /** The query to use */
98 String queryToQuery = "the";
99 /** Poll all collections */
100 boolean examineAllCollections = false;
101 /** Should we load the docs as well ?*/
102 boolean loadDocs = false;
103 /** should we query all the collections with the query or just one ? */
104 boolean queryAllCollections = false;
105 /** How verbose do we want to be ? */
106 int verbosity = 2;
107
108 /**
109 * Basic no-args constructor. Not recommended for normal use,
110 * but necessary if we want to create beans ...
111 */
112 protected SimpleClient() {
113 nzdl = NzdlIORs.findIOR(null, URLFromCommandLine,
114 IORFileName, IORFromCommandLine);
115 if (nzdl == null)
116 throw new Error("URK! unable to find an IOR...");
117 }
118
119 /**
120 * Normal constructor. the args are passed into the orb
121 * initialisation code so they can extract obscure args ...
122 */
123 SimpleClient(String [] args) {
124 parseArgs(args);
125 nzdl = NzdlIORs.findIOR(args, URLFromCommandLine,
126 IORFileName, IORFromCommandLine);
127 if (nzdl == null)
128 throw new Error("URK! unable to find an IOR...");
129 }
130
131
132 private boolean attemptToInitialise(String [] _args,
133 Properties _props,
134 String _IOR) {
135 try {
136 nzdl = new NzdlServiceClient( _args, _props, _IOR);
137 } catch (Throwable t) {
138 System.err.println ("failed to initialise using:");
139 System.err.println (_IOR);
140 nzdl = null;
141 return false;
142 }
143 return true;
144 }
145
146 void pollCollections() {
147
148 // extract some highlevel information on the collections on the server
149 // independent of the command line arguments passed in
150
151 Set collectionSet = nzdl.getCollectionSet(); // set of strings of collection titles
152 System.out.println("Number of collections on the server: " + collectionSet.size());
153
154 Iterator collectionSetIterator = collectionSet.iterator();
155 while (collectionSetIterator.hasNext() ) {
156 String collectionName = (String)collectionSetIterator.next(); // object -> String
157 NzdlCollectionInfo collectionInfo = nzdl.getCollectionInfo( collectionName );
158 // this next line should work but doesn't
159 System.out.println("Collection: " + collectionInfo.getName());
160 // so we revert to using the original string from the collectionSet
161 System.out.println("Collection: " + collectionName);
162 if (nzdl.hasCollection(collectionName) ) {
163 System.out.println("\t" + "hasCollection(" + collectionName + ")" + " is true");
164 if (nzdl.pingCollection(collectionName) ) {
165 System.out.println("\t" + "pingCollection(" + collectionName + ")" + " is true");
166 // these next statements make use of the NzdlCollection API
167 System.out.println("\t" + "Public: " + collectionInfo.isPublic());
168 System.out.println("\t" + "Beta: " + collectionInfo.isBeta());
169 int buildDate = collectionInfo.getBuildDate();
170 //process buildDate into something sensible
171 System.out.println("\t" + "Build Date: " + buildDate);
172 System.out.println("\t" + "No. of Documents: " + collectionInfo.getNumOfDocs());
173 System.out.println("\t" + "No. of Words: " + collectionInfo.getNumOfWords());
174 System.out.println("\t" + "No. of Bytes: " + collectionInfo.getNumOfBytes());
175 }
176 else {
177 System.out.println("\t" + "pingCollection(" + collectionName + ")" + " is false");
178 }
179 }
180 else
181 {
182 System.out.println("\t" + "hasCollection(" + collectionName + ")" + " is false");
183 }
184 }
185
186
187 }
188
189 void runQuery()
190 {
191 String queryTerm = queryToQuery;
192 Vector collections = new Vector();
193
194 Set collectionSet = nzdl.getCollectionSet();
195
196 if (collectionSet.size() < 1)
197 throw new Error("unable to open query collection, no collections avaliable");
198
199
200 if (queryAllCollections == false) {
201 // if we're only doing one, we taken either the first or
202 // the one we've been asked to query...
203 if (collectionNameToQuery.equals("")) {
204 Iterator collectionSetIterator = collectionSet.iterator();
205 collections.addElement(collectionSetIterator.next());
206 } else {
207 collections.addElement(collectionNameToQuery);
208 }
209 } else {
210 Iterator collectionSetIterator = collectionSet.iterator();
211 while (collectionSetIterator.hasNext() ) {
212 String collectionName = (String)collectionSetIterator.next();
213 collections.addElement(collectionName);
214 }
215 }
216
217
218 Enumeration e = collections.elements();
219 while (e.hasMoreElements()) {
220 String collName = (String) e.nextElement();
221 NzdlCollectionInfo collInfo = nzdl.getCollectionInfo( collName );
222
223 if (verbosity > 0)
224 System.out.println("Searching collection: " + collName);
225
226 int numResults = collInfo.getNumOfDocs(); // what precisely is this number ?
227
228 if (verbosity > 2)
229 System.out.println("Collection " + collName + " suggests to get results in chunks of " + numResults + " hits" );
230 if (verbosity > 2)
231 System.out.println("Query Term: " + queryTerm);
232 String metaTag = "Title";
233
234 if (verbosity > 2)
235 System.out.println("Searching in '" + metaTag + "' metadata");
236
237 NzdlQuery query = new NzdlQuery( queryTerm );
238 // return the first numResults that match
239 //query.setEndResults( 15 );
240 // "-1" means consider all the documents that match
241 query.setMaxDocs( -1 );
242
243 NzdlRequest request = new NzdlRequest( query );
244 NzdlResponse response = new NzdlResponse( );
245 if (verbosity > 2)
246 System.out.println("getting document IDs ... ");
247 nzdl.service( collName, request, response );
248
249 NzdlResultSet results = response.getResultSet();
250 List docIDs = results.getDocumentIDs();
251
252 if (verbosity > 1)
253 System.out.println("Number of documents returned: " + docIDs.size());
254 if (verbosity > 2)
255 System.out.println("getting '" + metaTag + "' meta data of documents ... ");
256
257 Map metaData = nzdl.getMetaData( collName, docIDs, metaTag );
258
259 for (ListIterator i = docIDs.listIterator(); i.hasNext(); ) {
260 String id = (String) i.next();
261 Set meta = (Set) metaData.get( id );
262 if (loadDocs) {
263 if (verbosity > 3) {
264 System.out.println(meta.toString());
265 System.out.println("getting document contents ... ");
266 }
267 String documentContents = nzdl.getDocument(collName, id);
268 if (verbosity > 3) {
269 System.out.println("got document contents. ");
270 System.out.println(" *************** START DOC " + id + " ***************");
271 System.out.println(documentContents);
272 System.out.println(" *************** END DOC " + id + " ***************");
273 }
274 } else {
275 if (verbosity > 3)
276 System.out.println(meta.toString());
277 }
278 }
279 }
280 }
281
282
283 static String getHelpText() {
284 String s = "";
285 s += "SimpleClient - a Java client to the Greenstone Digital Library";
286 s += "\n";
287 s += "Arguments -\n";
288 s += "Short form Long form Explanation\n";
289 s += "-h --help Print this help text\n";
290 s += "-u <url> --URL <url> Use <url> to connect\n";
291 s += "-I <IOR> --IOR <IOR> Use <IOR> to connect\n";
292 s += "-f <file> --file <IOR> Look for an IOR if <file>\n";
293 s += "-c <coll> --collection <coll> Examine collection <coll>\n";
294 s += "-a --all Examine all collections\n";
295 s += "-q <query> --query <query> Use <query>\n";
296 s += "-d --docs Load the documents as well\n";
297 s += "-Q --queryAllCollections Apply the <query> to all collections\n";
298 s += "-v <n> --verbose <n> Use verbosity level of <v>\n";
299 s += " 0 = silent except fro error reporting\n";
300 s += " 1 = reporting of operations\n";
301 s += " 2 = verbose reporting of operations \n";
302 s += " 3 = reporting of results of operations\n";
303 s += " 4 = showing all the documents retrieved\n";
304 s += "\n";
305 return s;
306 }
307
308 private boolean parseArgs(String [] args) {
309 System.err.println("in parseArgs");
310
311 LongOpt[] longopts = new LongOpt[10];
312 int i = 0;
313 StringBuffer sb = new StringBuffer();
314
315 longopts[i++] = new LongOpt("help", LongOpt.NO_ARGUMENT, null, 'h');
316 longopts[i++] = new LongOpt("IOR", LongOpt.REQUIRED_ARGUMENT, null, 'I');
317 longopts[i++] = new LongOpt("URL", LongOpt.REQUIRED_ARGUMENT, null, 'u');
318 longopts[i++] = new LongOpt("file", LongOpt.REQUIRED_ARGUMENT, null, 'f');
319 longopts[i++] = new LongOpt("collection", LongOpt.REQUIRED_ARGUMENT, null, 'c');
320 longopts[i++] = new LongOpt("all", LongOpt.NO_ARGUMENT, null, 'a');
321 longopts[i++] = new LongOpt("query", LongOpt.REQUIRED_ARGUMENT, null, 'q');
322 longopts[i++] = new LongOpt("docs", LongOpt.NO_ARGUMENT, null, 'd');
323 longopts[i++] = new LongOpt("verbose", LongOpt.REQUIRED_ARGUMENT, null, 'v');
324 longopts[i++] = new LongOpt("queryAllCollections", LongOpt.NO_ARGUMENT, null, 'Q');
325 Getopt g = new Getopt("org.nzdl.gsdl.SimpleClient",
326 args,
327 "hI:f:c:aq:dv:Qu:",
328 longopts);
329 int c;
330 String arg;
331 try {
332 while ((c = g.getopt()) != -1) {
333 switch(c) {
334 case 'h':
335 System.out.println(getHelpText());
336 System.exit(1);
337 return false;
338 case 'I':
339 IORFromCommandLine = g.getOptarg();
340 break;
341 case 'u':
342 URLFromCommandLine = g.getOptarg();
343 break;
344 case 'f':
345 IORFileName = g.getOptarg();
346 useFileFirst = true;
347 break;
348 case 'c':
349 collectionNameToQuery = g.getOptarg();
350 break;
351 case 'q':
352 queryToQuery = g.getOptarg();
353 break;
354 case 'a':
355 examineAllCollections = true;
356 break;
357 case 'd':
358 loadDocs = true;
359 break;
360 case 'Q':
361 queryAllCollections = true;
362 break;
363 case 'v':
364 arg = g.getOptarg();
365 verbosity = Integer.parseInt(arg);
366 break;
367 default:
368 System.err.print("getopt() returned " + c + " sb = \"" + sb +
369 "\" longopts[g.getLongind()].getName() = \"" +
370 longopts[g.getLongind()].getName() + "\"\n");
371 System.err.print(getHelpText());
372 System.exit(1);
373
374 }
375 }
376 } catch (Throwable t) {
377 System.out.println("Exception thrown while processing arguments \n");
378 t.printStackTrace(System.out);
379 return false;
380 }
381 return true;
382 }
383
384 public static final void main( String [] args ) {
385
386 SimpleClient client = new SimpleClient(args);
387
388 if (client.examineAllCollections)
389 client.pollCollections();
390
391 client.runQuery();
392
393 } //main
394
395} // class
Note: See TracBrowser for help on using the repository browser.