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

Last change on this file since 2254 was 2220, checked in by say1, 23 years ago

change int-> long in a number of places, new createNzdlService method taking only an IOR, added the start of a hli (high level interface

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 14.1 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.NzdlServiceFactory;
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: 2220 $
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 = NzdlServiceFactory.createNzdlService(null,
114 System.getProperties(),
115 URLFromCommandLine,
116 IORFileName,
117 IORFromCommandLine);
118 if (nzdl == null)
119 throw new Error("URK! unable to find an IOR...");
120 }
121
122 /**
123 * Normal constructor. the args are passed into the orb
124 * initialisation code so they can extract obscure args ...
125 */
126 SimpleClient(String [] args) {
127 parseArgs(args);
128 nzdl = NzdlServiceFactory.createNzdlService(args,
129 System.getProperties(),
130 URLFromCommandLine,
131 IORFileName,
132 IORFromCommandLine);
133 if (nzdl == null)
134 throw new Error("URK! unable to find an IOR...");
135 }
136
137
138 void pollCollections() {
139
140 // extract some highlevel information on the collections on the server
141 // independent of the command line arguments passed in
142
143 Set collectionSet = nzdl.getCollectionSet(); // set of strings of collection titles
144 System.out.println("Number of collections on the server: " + collectionSet.size());
145
146 Iterator collectionSetIterator = collectionSet.iterator();
147 while (collectionSetIterator.hasNext() ) {
148 String collectionName = (String)collectionSetIterator.next(); // object -> String
149 NzdlCollectionInfo collectionInfo = nzdl.getCollectionInfo( collectionName );
150 // this next line should work but doesn't
151 System.out.println("Collection: " + collectionInfo.getName());
152 // so we revert to using the original string from the collectionSet
153 System.out.println("Collection: " + collectionName);
154 if (nzdl.hasCollection(collectionName) ) {
155 System.out.println("\t" + "hasCollection(" + collectionName + ")" + " is true");
156 if (nzdl.pingCollection(collectionName) ) {
157 System.out.println("\t" + "pingCollection(" + collectionName + ")" + " is true");
158 // these next statements make use of the NzdlCollection API
159 System.out.println("\t" + "Public: " + collectionInfo.isPublic());
160 System.out.println("\t" + "Beta: " + collectionInfo.isBeta());
161 long buildDate = collectionInfo.getBuildDate();
162 //process buildDate into something sensible
163 System.out.println("\t" + "Build Date: " + buildDate);
164 System.out.println("\t" + "No. of Documents: " + collectionInfo.getNumOfDocs());
165 System.out.println("\t" + "No. of Words: " + collectionInfo.getNumOfWords());
166 System.out.println("\t" + "No. of Bytes: " + collectionInfo.getNumOfBytes());
167 }
168 else {
169 System.out.println("\t" + "pingCollection(" + collectionName + ")" + " is false");
170 }
171 }
172 else
173 {
174 System.out.println("\t" + "hasCollection(" + collectionName + ")" + " is false");
175 }
176 }
177
178
179 }
180
181 void runQuery()
182 {
183 String queryTerm = queryToQuery;
184 Vector collections = new Vector();
185
186 Set collectionSet = nzdl.getCollectionSet();
187
188 if (collectionSet.size() < 1)
189 throw new Error("unable to open query collection, no collections avaliable");
190
191
192 if (queryAllCollections == false) {
193 // if we're only doing one, we taken either the first or
194 // the one we've been asked to query...
195 if (collectionNameToQuery.equals("")) {
196 Iterator collectionSetIterator = collectionSet.iterator();
197 collections.addElement(collectionSetIterator.next());
198 } else {
199 collections.addElement(collectionNameToQuery);
200 }
201 } else {
202 Iterator collectionSetIterator = collectionSet.iterator();
203 while (collectionSetIterator.hasNext() ) {
204 String collectionName = (String)collectionSetIterator.next();
205 collections.addElement(collectionName);
206 }
207 }
208
209
210 Enumeration e = collections.elements();
211 while (e.hasMoreElements()) {
212 String collName = (String) e.nextElement();
213 NzdlCollectionInfo collInfo = nzdl.getCollectionInfo( collName );
214
215 if (verbosity > 0)
216 System.out.println("Searching collection: " + collName);
217
218 long numResults = collInfo.getNumOfDocs(); // what precisely is this number ?
219
220 if (verbosity > 2)
221 System.out.println("Collection " + collName + " suggests to get results in chunks of " + numResults + " hits" );
222 if (verbosity > 2)
223 System.out.println("Query Term: " + queryTerm);
224 String metaTag = "Title";
225
226 if (verbosity > 2)
227 System.out.println("Searching in '" + metaTag + "' metadata");
228
229 NzdlQuery query = new NzdlQuery( queryTerm );
230 // return the first numResults that match
231 //query.setEndResults( 15 );
232 // "-1" means consider all the documents that match
233 query.setMaxDocs( -1 );
234
235 NzdlRequest request = new NzdlRequest( query );
236 NzdlResponse response = new NzdlResponse( );
237 if (verbosity > 2)
238 System.out.println("getting document IDs ... ");
239 nzdl.service( collName, request, response );
240
241 NzdlResultSet results = response.getResultSet();
242 List docIDs = results.getDocumentIDs();
243
244 if (verbosity > 1)
245 System.out.println("Number of documents returned: " + docIDs.size());
246 if (verbosity > 2)
247 System.out.println("getting '" + metaTag + "' meta data of documents ... ");
248
249 Map metaData = nzdl.getMetaData( collName, docIDs, metaTag );
250
251 for (ListIterator i = docIDs.listIterator(); i.hasNext(); ) {
252 String id = (String) i.next();
253 Set meta = (Set) metaData.get( id );
254 if (loadDocs) {
255 if (verbosity > 3) {
256 System.out.println(meta.toString());
257 System.out.println("getting document contents ... ");
258 }
259 String documentContents = nzdl.getDocument(collName, id);
260 if (verbosity > 3) {
261 System.out.println("got document contents. ");
262 System.out.println(" *************** START DOC " + id + " ***************");
263 System.out.println(documentContents);
264 System.out.println(" *************** END DOC " + id + " ***************");
265 }
266 } else {
267 if (verbosity > 3)
268 System.out.println(meta.toString());
269 }
270 }
271 }
272 }
273
274
275 static String getHelpText() {
276 String s = "";
277 s += "SimpleClient - a Java client to the Greenstone Digital Library";
278 s += "\n";
279 s += "Arguments -\n";
280 s += "Short form Long form Explanation\n";
281 s += "-h --help Print this help text\n";
282 s += "-u <url> --URL <url> Use <url> to connect\n";
283 s += "-I <IOR> --IOR <IOR> Use <IOR> to connect\n";
284 s += "-f <file> --file <IOR> Look for an IOR if <file>\n";
285 s += "-c <coll> --collection <coll> Examine collection <coll>\n";
286 s += "-a --all Examine all collections\n";
287 s += "-q <query> --query <query> Use <query>\n";
288 s += "-d --docs Load the documents as well\n";
289 s += "-Q --queryAllCollections Apply the <query> to all collections\n";
290 s += "-v <n> --verbose <n> Use verbosity level of <v>\n";
291 s += " 0 = silent except fro error reporting\n";
292 s += " 1 = reporting of operations\n";
293 s += " 2 = verbose reporting of operations \n";
294 s += " 3 = reporting of results of operations\n";
295 s += " 4 = showing all the documents retrieved\n";
296 s += "\n";
297 return s;
298 }
299
300 private boolean parseArgs(String [] args) {
301 System.err.println("in parseArgs");
302
303 LongOpt[] longopts = new LongOpt[10];
304 int i = 0;
305 StringBuffer sb = new StringBuffer();
306
307 longopts[i++] = new LongOpt("help", LongOpt.NO_ARGUMENT, null, 'h');
308 longopts[i++] = new LongOpt("IOR", LongOpt.REQUIRED_ARGUMENT, null, 'I');
309 longopts[i++] = new LongOpt("URL", LongOpt.REQUIRED_ARGUMENT, null, 'u');
310 longopts[i++] = new LongOpt("file", LongOpt.REQUIRED_ARGUMENT, null, 'f');
311 longopts[i++] = new LongOpt("collection", LongOpt.REQUIRED_ARGUMENT, null, 'c');
312 longopts[i++] = new LongOpt("all", LongOpt.NO_ARGUMENT, null, 'a');
313 longopts[i++] = new LongOpt("query", LongOpt.REQUIRED_ARGUMENT, null, 'q');
314 longopts[i++] = new LongOpt("docs", LongOpt.NO_ARGUMENT, null, 'd');
315 longopts[i++] = new LongOpt("verbose", LongOpt.REQUIRED_ARGUMENT, null, 'v');
316 longopts[i++] = new LongOpt("queryAllCollections", LongOpt.NO_ARGUMENT, null, 'Q');
317 Getopt g = new Getopt("org.nzdl.gsdl.SimpleClient",
318 args,
319 "hI:f:c:aq:dv:Qu:",
320 longopts);
321 int c;
322 String arg;
323 try {
324 while ((c = g.getopt()) != -1) {
325 switch(c) {
326 case 'h':
327 System.out.println(getHelpText());
328 System.exit(1);
329 return false;
330 case 'I':
331 IORFromCommandLine = g.getOptarg();
332 break;
333 case 'u':
334 URLFromCommandLine = g.getOptarg();
335 break;
336 case 'f':
337 IORFileName = g.getOptarg();
338 useFileFirst = true;
339 break;
340 case 'c':
341 collectionNameToQuery = g.getOptarg();
342 break;
343 case 'q':
344 queryToQuery = g.getOptarg();
345 break;
346 case 'a':
347 examineAllCollections = true;
348 break;
349 case 'd':
350 loadDocs = true;
351 break;
352 case 'Q':
353 queryAllCollections = true;
354 break;
355 case 'v':
356 arg = g.getOptarg();
357 verbosity = Integer.parseInt(arg);
358 break;
359 default:
360 System.err.print("getopt() returned " + c + " sb = \"" + sb +
361 "\" longopts[g.getLongind()].getName() = \"" +
362 longopts[g.getLongind()].getName() + "\"\n");
363 System.err.print(getHelpText());
364 System.exit(1);
365
366 }
367 }
368 } catch (Throwable t) {
369 System.out.println("Exception thrown while processing arguments \n");
370 t.printStackTrace(System.out);
371 return false;
372 }
373 return true;
374 }
375
376 public static final void main( String [] args ) {
377
378 SimpleClient client = new SimpleClient(args);
379
380 if (client.examineAllCollections)
381 client.pollCollections();
382
383 client.runQuery();
384
385 } //main
386
387} // class
Note: See TracBrowser for help on using the repository browser.