[2066] | 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 | */
|
---|
[2055] | 19 |
|
---|
[2066] | 20 | // the package we're in
|
---|
[2098] | 21 | package org.nzdl.gsdl;
|
---|
[2055] | 22 |
|
---|
[2067] | 23 | // long live GNU ...
|
---|
| 24 | import gnu.getopt.Getopt;
|
---|
| 25 | import gnu.getopt.LongOpt;
|
---|
| 26 |
|
---|
| 27 | // java libraries we're using
|
---|
[2070] | 28 | import java.io.BufferedReader;
|
---|
[2131] | 29 | import java.io.DataInputStream;
|
---|
[2067] | 30 | import java.io.File;
|
---|
| 31 | import java.io.FileInputStream;
|
---|
| 32 | import java.io.FileOutputStream;
|
---|
| 33 | import java.io.FileReader;
|
---|
| 34 | import java.io.IOException;
|
---|
[2131] | 35 | import java.io.InputStream;
|
---|
[2070] | 36 | import java.io.InputStreamReader;
|
---|
[2067] | 37 | import java.io.LineNumberReader;
|
---|
| 38 | import java.io.Serializable;
|
---|
[2070] | 39 | import java.net.URL;
|
---|
[2067] | 40 | import java.util.Enumeration;
|
---|
[2066] | 41 | import java.util.Iterator;
|
---|
| 42 | import java.util.List;
|
---|
[2067] | 43 | import java.util.ListIterator;
|
---|
| 44 | import java.util.Map;
|
---|
| 45 | import java.util.Properties;
|
---|
| 46 | import java.util.Set;
|
---|
[2070] | 47 | import java.util.Vector;
|
---|
[2067] | 48 |
|
---|
| 49 | // local libraries
|
---|
[2098] | 50 | import org.nzdl.gsdl.service.NzdlCollectionInfo;
|
---|
| 51 | import org.nzdl.gsdl.service.NzdlQuery;
|
---|
| 52 | import org.nzdl.gsdl.service.NzdlRequest;
|
---|
| 53 | import org.nzdl.gsdl.service.NzdlResponse;
|
---|
| 54 | import org.nzdl.gsdl.service.NzdlResultSet;
|
---|
| 55 | import org.nzdl.gsdl.service.NzdlService;
|
---|
| 56 | import org.nzdl.gsdl.service.NzdlServiceClient;
|
---|
[2135] | 57 | import org.nzdl.gsdl.util.NzdlIORs;
|
---|
[2055] | 58 |
|
---|
[2066] | 59 | /**
|
---|
| 60 | * Class SimpleClient
|
---|
| 61 | *
|
---|
| 62 | * A class to test the workings of Corba interface from the client side.
|
---|
| 63 | *
|
---|
[2070] | 64 | * @author stuart yeates ([email protected])
|
---|
| 65 | * @author Brett Sheeran ([email protected])
|
---|
[2067] | 66 | * @author Dave Nichols ([email protected])
|
---|
[2066] | 67 | * @author Gordon Paynter ([email protected])
|
---|
| 68 | * @version $Revision: 2148 $
|
---|
[2119] | 69 | * @see org.nzdl.gsdl.SimpleServer
|
---|
[2098] | 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
|
---|
[2119] | 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>
|
---|
[2066] | 81 | */
|
---|
| 82 |
|
---|
[2070] | 83 | public class SimpleClient implements Cloneable, Serializable {
|
---|
[2055] | 84 |
|
---|
[2067] | 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;
|
---|
[2131] | 91 | /** The URL that was specified on the commandline */
|
---|
| 92 | String URLFromCommandLine = null;
|
---|
[2135] | 93 | /** The name of the file we look for IOR's in */
|
---|
| 94 | static String IORFileName = "/tmp/localcorba.objid";
|
---|
[2067] | 95 | /** The collection to query */
|
---|
[2130] | 96 | String collectionNameToQuery = "";
|
---|
[2067] | 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;
|
---|
[2055] | 107 |
|
---|
[2119] | 108 | /**
|
---|
| 109 | * Basic no-args constructor. Not recommended for normal use,
|
---|
| 110 | * but necessary if we want to create beans ...
|
---|
| 111 | */
|
---|
| 112 | protected SimpleClient() {
|
---|
[2140] | 113 | nzdl = NzdlIORs.findIOR(null, URLFromCommandLine,
|
---|
| 114 | IORFileName, IORFromCommandLine);
|
---|
| 115 | if (nzdl == null)
|
---|
| 116 | throw new Error("URK! unable to find an IOR...");
|
---|
[2067] | 117 | }
|
---|
[2119] | 118 |
|
---|
| 119 | /**
|
---|
| 120 | * Normal constructor. the args are passed into the orb
|
---|
| 121 | * initialisation code so they can extract obscure args ...
|
---|
| 122 | */
|
---|
[2067] | 123 | SimpleClient(String [] args) {
|
---|
| 124 | parseArgs(args);
|
---|
[2140] | 125 | nzdl = NzdlIORs.findIOR(args, URLFromCommandLine,
|
---|
| 126 | IORFileName, IORFromCommandLine);
|
---|
| 127 | if (nzdl == null)
|
---|
| 128 | throw new Error("URK! unable to find an IOR...");
|
---|
[2067] | 129 | }
|
---|
| 130 |
|
---|
| 131 |
|
---|
| 132 | private boolean attemptToInitialise(String [] _args,
|
---|
| 133 | Properties _props,
|
---|
| 134 | String _IOR) {
|
---|
| 135 | try {
|
---|
[2088] | 136 | nzdl = new NzdlServiceClient( _args, _props, _IOR);
|
---|
[2067] | 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 |
|
---|
[2055] | 148 | // extract some highlevel information on the collections on the server
|
---|
| 149 | // independent of the command line arguments passed in
|
---|
[2067] | 150 |
|
---|
[2055] | 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
|
---|
[2067] | 181 | {
|
---|
| 182 | System.out.println("\t" + "hasCollection(" + collectionName + ")" + " is false");
|
---|
| 183 | }
|
---|
[2055] | 184 | }
|
---|
| 185 |
|
---|
| 186 |
|
---|
[2067] | 187 | }
|
---|
[2055] | 188 |
|
---|
[2067] | 189 | void runQuery()
|
---|
| 190 | {
|
---|
| 191 | String queryTerm = queryToQuery;
|
---|
| 192 | Vector collections = new Vector();
|
---|
[2148] | 193 |
|
---|
| 194 | Set collectionSet = nzdl.getCollectionSet();
|
---|
[2067] | 195 |
|
---|
[2148] | 196 | if (collectionSet.size() < 1)
|
---|
| 197 | throw new Error("unable to open query collection, no collections avaliable");
|
---|
| 198 |
|
---|
| 199 |
|
---|
[2067] | 200 | if (queryAllCollections == false) {
|
---|
[2148] | 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 | }
|
---|
[2067] | 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 ?
|
---|
[2055] | 227 |
|
---|
[2067] | 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();
|
---|
[2055] | 251 |
|
---|
[2067] | 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 ... ");
|
---|
[2055] | 256 |
|
---|
[2067] | 257 | Map metaData = nzdl.getMetaData( collName, docIDs, metaTag );
|
---|
| 258 |
|
---|
[2070] | 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 ... ");
|
---|
[2067] | 266 | }
|
---|
[2070] | 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());
|
---|
[2067] | 277 | }
|
---|
[2070] | 278 | }
|
---|
[2055] | 279 | }
|
---|
[2067] | 280 | }
|
---|
| 281 |
|
---|
[2055] | 282 |
|
---|
[2067] | 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";
|
---|
[2116] | 288 | s += "Short form Long form Explanation\n";
|
---|
[2067] | 289 | s += "-h --help Print this help text\n";
|
---|
[2131] | 290 | s += "-u <url> --URL <url> Use <url> to connect\n";
|
---|
[2067] | 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 | }
|
---|
[2055] | 307 |
|
---|
[2067] | 308 | private boolean parseArgs(String [] args) {
|
---|
[2128] | 309 | System.err.println("in parseArgs");
|
---|
| 310 |
|
---|
[2131] | 311 | LongOpt[] longopts = new LongOpt[10];
|
---|
[2067] | 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');
|
---|
[2131] | 317 | longopts[i++] = new LongOpt("URL", LongOpt.REQUIRED_ARGUMENT, null, 'u');
|
---|
[2067] | 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');
|
---|
[2098] | 325 | Getopt g = new Getopt("org.nzdl.gsdl.SimpleClient",
|
---|
[2067] | 326 | args,
|
---|
[2131] | 327 | "hI:f:c:aq:dv:Qu:",
|
---|
[2067] | 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());
|
---|
[2128] | 336 | System.exit(1);
|
---|
[2067] | 337 | return false;
|
---|
| 338 | case 'I':
|
---|
| 339 | IORFromCommandLine = g.getOptarg();
|
---|
| 340 | break;
|
---|
[2131] | 341 | case 'u':
|
---|
| 342 | URLFromCommandLine = g.getOptarg();
|
---|
| 343 | break;
|
---|
[2067] | 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 |
|
---|
[2055] | 393 | } //main
|
---|
| 394 |
|
---|
| 395 | } // class
|
---|