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

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

implemented multiway wrapper to allow a client to connect to multiple servers seamlessly

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