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

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

moved init stuff to NzdlIORs. comments

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 13.9 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: 2140 $
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 if (queryAllCollections == false) {
195 collections.addElement(collectionNameToQuery);
196 } else {
197 Set collectionSet = nzdl.getCollectionSet();
198 Iterator collectionSetIterator = collectionSet.iterator();
199 while (collectionSetIterator.hasNext() ) {
200 String collectionName = (String)collectionSetIterator.next();
201 collections.addElement(collectionName);
202 }
203 }
204
205
206 Enumeration e = collections.elements();
207 while (e.hasMoreElements()) {
208 String collName = (String) e.nextElement();
209 NzdlCollectionInfo collInfo = nzdl.getCollectionInfo( collName );
210
211 if (verbosity > 0)
212 System.out.println("Searching collection: " + collName);
213
214 int numResults = collInfo.getNumOfDocs(); // what precisely is this number ?
215
216 if (verbosity > 2)
217 System.out.println("Collection " + collName + " suggests to get results in chunks of " + numResults + " hits" );
218 if (verbosity > 2)
219 System.out.println("Query Term: " + queryTerm);
220 String metaTag = "Title";
221
222 if (verbosity > 2)
223 System.out.println("Searching in '" + metaTag + "' metadata");
224
225 NzdlQuery query = new NzdlQuery( queryTerm );
226 // return the first numResults that match
227 //query.setEndResults( 15 );
228 // "-1" means consider all the documents that match
229 query.setMaxDocs( -1 );
230
231 NzdlRequest request = new NzdlRequest( query );
232 NzdlResponse response = new NzdlResponse( );
233 if (verbosity > 2)
234 System.out.println("getting document IDs ... ");
235 nzdl.service( collName, request, response );
236
237 NzdlResultSet results = response.getResultSet();
238 List docIDs = results.getDocumentIDs();
239
240 if (verbosity > 1)
241 System.out.println("Number of documents returned: " + docIDs.size());
242 if (verbosity > 2)
243 System.out.println("getting '" + metaTag + "' meta data of documents ... ");
244
245 Map metaData = nzdl.getMetaData( collName, docIDs, metaTag );
246
247 for (ListIterator i = docIDs.listIterator(); i.hasNext(); ) {
248 String id = (String) i.next();
249 Set meta = (Set) metaData.get( id );
250 if (loadDocs) {
251 if (verbosity > 3) {
252 System.out.println(meta.toString());
253 System.out.println("getting document contents ... ");
254 }
255 String documentContents = nzdl.getDocument(collName, id);
256 if (verbosity > 3) {
257 System.out.println("got document contents. ");
258 System.out.println(" *************** START DOC " + id + " ***************");
259 System.out.println(documentContents);
260 System.out.println(" *************** END DOC " + id + " ***************");
261 }
262 } else {
263 if (verbosity > 3)
264 System.out.println(meta.toString());
265 }
266 }
267 }
268 }
269
270
271 static String getHelpText() {
272 String s = "";
273 s += "SimpleClient - a Java client to the Greenstone Digital Library";
274 s += "\n";
275 s += "Arguments -\n";
276 s += "Short form Long form Explanation\n";
277 s += "-h --help Print this help text\n";
278 s += "-u <url> --URL <url> Use <url> to connect\n";
279 s += "-I <IOR> --IOR <IOR> Use <IOR> to connect\n";
280 s += "-f <file> --file <IOR> Look for an IOR if <file>\n";
281 s += "-c <coll> --collection <coll> Examine collection <coll>\n";
282 s += "-a --all Examine all collections\n";
283 s += "-q <query> --query <query> Use <query>\n";
284 s += "-d --docs Load the documents as well\n";
285 s += "-Q --queryAllCollections Apply the <query> to all collections\n";
286 s += "-v <n> --verbose <n> Use verbosity level of <v>\n";
287 s += " 0 = silent except fro error reporting\n";
288 s += " 1 = reporting of operations\n";
289 s += " 2 = verbose reporting of operations \n";
290 s += " 3 = reporting of results of operations\n";
291 s += " 4 = showing all the documents retrieved\n";
292 s += "\n";
293 return s;
294 }
295
296 private boolean parseArgs(String [] args) {
297 System.err.println("in parseArgs");
298
299 LongOpt[] longopts = new LongOpt[10];
300 int i = 0;
301 StringBuffer sb = new StringBuffer();
302
303 longopts[i++] = new LongOpt("help", LongOpt.NO_ARGUMENT, null, 'h');
304 longopts[i++] = new LongOpt("IOR", LongOpt.REQUIRED_ARGUMENT, null, 'I');
305 longopts[i++] = new LongOpt("URL", LongOpt.REQUIRED_ARGUMENT, null, 'u');
306 longopts[i++] = new LongOpt("file", LongOpt.REQUIRED_ARGUMENT, null, 'f');
307 longopts[i++] = new LongOpt("collection", LongOpt.REQUIRED_ARGUMENT, null, 'c');
308 longopts[i++] = new LongOpt("all", LongOpt.NO_ARGUMENT, null, 'a');
309 longopts[i++] = new LongOpt("query", LongOpt.REQUIRED_ARGUMENT, null, 'q');
310 longopts[i++] = new LongOpt("docs", LongOpt.NO_ARGUMENT, null, 'd');
311 longopts[i++] = new LongOpt("verbose", LongOpt.REQUIRED_ARGUMENT, null, 'v');
312 longopts[i++] = new LongOpt("queryAllCollections", LongOpt.NO_ARGUMENT, null, 'Q');
313 Getopt g = new Getopt("org.nzdl.gsdl.SimpleClient",
314 args,
315 "hI:f:c:aq:dv:Qu:",
316 longopts);
317 int c;
318 String arg;
319 try {
320 while ((c = g.getopt()) != -1) {
321 switch(c) {
322 case 'h':
323 System.out.println(getHelpText());
324 System.exit(1);
325 return false;
326 case 'I':
327 IORFromCommandLine = g.getOptarg();
328 break;
329 case 'u':
330 URLFromCommandLine = g.getOptarg();
331 break;
332 case 'f':
333 IORFileName = g.getOptarg();
334 useFileFirst = true;
335 break;
336 case 'c':
337 collectionNameToQuery = g.getOptarg();
338 break;
339 case 'q':
340 queryToQuery = g.getOptarg();
341 break;
342 case 'a':
343 examineAllCollections = true;
344 break;
345 case 'd':
346 loadDocs = true;
347 break;
348 case 'Q':
349 queryAllCollections = true;
350 break;
351 case 'v':
352 arg = g.getOptarg();
353 verbosity = Integer.parseInt(arg);
354 break;
355 default:
356 System.err.print("getopt() returned " + c + " sb = \"" + sb +
357 "\" longopts[g.getLongind()].getName() = \"" +
358 longopts[g.getLongind()].getName() + "\"\n");
359 System.err.print(getHelpText());
360 System.exit(1);
361
362 }
363 }
364 } catch (Throwable t) {
365 System.out.println("Exception thrown while processing arguments \n");
366 t.printStackTrace(System.out);
367 return false;
368 }
369 return true;
370 }
371
372 public static final void main( String [] args ) {
373
374 SimpleClient client = new SimpleClient(args);
375
376 if (client.examineAllCollections)
377 client.pollCollections();
378
379 client.runQuery();
380
381 } //main
382
383} // class
Note: See TracBrowser for help on using the repository browser.