/*
* SimpleClient.java
* Copyright (C) 2001 New Zealand Digital Library Project
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
// the package we're in
package org.nzdl.gsdl;
// long live GNU ...
import gnu.getopt.Getopt;
import gnu.getopt.LongOpt;
// java libraries we're using
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.io.Serializable;
import java.net.URL;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.Vector;
// local libraries
import org.nzdl.gsdl.service.NzdlCollectionInfo;
import org.nzdl.gsdl.service.NzdlQuery;
import org.nzdl.gsdl.service.NzdlRequest;
import org.nzdl.gsdl.service.NzdlResponse;
import org.nzdl.gsdl.service.NzdlResultSet;
import org.nzdl.gsdl.service.NzdlService;
import org.nzdl.gsdl.service.NzdlServiceClient;
import org.nzdl.gsdl.util.NzdlServiceFactory;
/**
* Class SimpleClient
*
* A class to test the workings of Corba interface from the client side.
*
* @author stuart yeates (say1@cs.waikato.ac.nz)
* @author Brett Sheeran (bsheeran@xtra.co.nz)
* @author Dave Nichols (daven@cs.waikato.ac.nz)
* @author Gordon Paynter (paynter@cs.waikato.ac.nz)
* @version $Revision: 2220 $
* @see org.nzdl.gsdl.SimpleServer
* @see org.nzdl.gsdl.service.NzdlCollectionInfo
* @see org.nzdl.gsdl.service.NzdlQuery
* @see org.nzdl.gsdl.service.NzdlRequest
* @see org.nzdl.gsdl.service.NzdlResponse
* @see org.nzdl.gsdl.service.NzdlResultSet
* @see org.nzdl.gsdl.service.NzdlService
* @see org.nzdl.gsdl.service.NzdlServiceClient
* @see gnu.getopt.Getopt
* @see gnu.getopt.LongOpt
* @see The New Zealand Digital Library Project
* @see Greenstone Digital Library Software
*/
public class SimpleClient implements Cloneable, Serializable {
/** The underlying CORBA inferface */
NzdlService nzdl = null;
/** Look for an IOR in the file first */
boolean useFileFirst = false;
/** The IOR that was specified on the commandline */
String IORFromCommandLine = null;
/** The URL that was specified on the commandline */
String URLFromCommandLine = null;
/** The name of the file we look for IOR's in */
static String IORFileName = "/tmp/localcorba.objid";
/** The collection to query */
String collectionNameToQuery = "";
/** The query to use */
String queryToQuery = "the";
/** Poll all collections */
boolean examineAllCollections = false;
/** Should we load the docs as well ?*/
boolean loadDocs = false;
/** should we query all the collections with the query or just one ? */
boolean queryAllCollections = false;
/** How verbose do we want to be ? */
int verbosity = 2;
/**
* Basic no-args constructor. Not recommended for normal use,
* but necessary if we want to create beans ...
*/
protected SimpleClient() {
nzdl = NzdlServiceFactory.createNzdlService(null,
System.getProperties(),
URLFromCommandLine,
IORFileName,
IORFromCommandLine);
if (nzdl == null)
throw new Error("URK! unable to find an IOR...");
}
/**
* Normal constructor. the args are passed into the orb
* initialisation code so they can extract obscure args ...
*/
SimpleClient(String [] args) {
parseArgs(args);
nzdl = NzdlServiceFactory.createNzdlService(args,
System.getProperties(),
URLFromCommandLine,
IORFileName,
IORFromCommandLine);
if (nzdl == null)
throw new Error("URK! unable to find an IOR...");
}
void pollCollections() {
// extract some highlevel information on the collections on the server
// independent of the command line arguments passed in
Set collectionSet = nzdl.getCollectionSet(); // set of strings of collection titles
System.out.println("Number of collections on the server: " + collectionSet.size());
Iterator collectionSetIterator = collectionSet.iterator();
while (collectionSetIterator.hasNext() ) {
String collectionName = (String)collectionSetIterator.next(); // object -> String
NzdlCollectionInfo collectionInfo = nzdl.getCollectionInfo( collectionName );
// this next line should work but doesn't
System.out.println("Collection: " + collectionInfo.getName());
// so we revert to using the original string from the collectionSet
System.out.println("Collection: " + collectionName);
if (nzdl.hasCollection(collectionName) ) {
System.out.println("\t" + "hasCollection(" + collectionName + ")" + " is true");
if (nzdl.pingCollection(collectionName) ) {
System.out.println("\t" + "pingCollection(" + collectionName + ")" + " is true");
// these next statements make use of the NzdlCollection API
System.out.println("\t" + "Public: " + collectionInfo.isPublic());
System.out.println("\t" + "Beta: " + collectionInfo.isBeta());
long buildDate = collectionInfo.getBuildDate();
//process buildDate into something sensible
System.out.println("\t" + "Build Date: " + buildDate);
System.out.println("\t" + "No. of Documents: " + collectionInfo.getNumOfDocs());
System.out.println("\t" + "No. of Words: " + collectionInfo.getNumOfWords());
System.out.println("\t" + "No. of Bytes: " + collectionInfo.getNumOfBytes());
}
else {
System.out.println("\t" + "pingCollection(" + collectionName + ")" + " is false");
}
}
else
{
System.out.println("\t" + "hasCollection(" + collectionName + ")" + " is false");
}
}
}
void runQuery()
{
String queryTerm = queryToQuery;
Vector collections = new Vector();
Set collectionSet = nzdl.getCollectionSet();
if (collectionSet.size() < 1)
throw new Error("unable to open query collection, no collections avaliable");
if (queryAllCollections == false) {
// if we're only doing one, we taken either the first or
// the one we've been asked to query...
if (collectionNameToQuery.equals("")) {
Iterator collectionSetIterator = collectionSet.iterator();
collections.addElement(collectionSetIterator.next());
} else {
collections.addElement(collectionNameToQuery);
}
} else {
Iterator collectionSetIterator = collectionSet.iterator();
while (collectionSetIterator.hasNext() ) {
String collectionName = (String)collectionSetIterator.next();
collections.addElement(collectionName);
}
}
Enumeration e = collections.elements();
while (e.hasMoreElements()) {
String collName = (String) e.nextElement();
NzdlCollectionInfo collInfo = nzdl.getCollectionInfo( collName );
if (verbosity > 0)
System.out.println("Searching collection: " + collName);
long numResults = collInfo.getNumOfDocs(); // what precisely is this number ?
if (verbosity > 2)
System.out.println("Collection " + collName + " suggests to get results in chunks of " + numResults + " hits" );
if (verbosity > 2)
System.out.println("Query Term: " + queryTerm);
String metaTag = "Title";
if (verbosity > 2)
System.out.println("Searching in '" + metaTag + "' metadata");
NzdlQuery query = new NzdlQuery( queryTerm );
// return the first numResults that match
//query.setEndResults( 15 );
// "-1" means consider all the documents that match
query.setMaxDocs( -1 );
NzdlRequest request = new NzdlRequest( query );
NzdlResponse response = new NzdlResponse( );
if (verbosity > 2)
System.out.println("getting document IDs ... ");
nzdl.service( collName, request, response );
NzdlResultSet results = response.getResultSet();
List docIDs = results.getDocumentIDs();
if (verbosity > 1)
System.out.println("Number of documents returned: " + docIDs.size());
if (verbosity > 2)
System.out.println("getting '" + metaTag + "' meta data of documents ... ");
Map metaData = nzdl.getMetaData( collName, docIDs, metaTag );
for (ListIterator i = docIDs.listIterator(); i.hasNext(); ) {
String id = (String) i.next();
Set meta = (Set) metaData.get( id );
if (loadDocs) {
if (verbosity > 3) {
System.out.println(meta.toString());
System.out.println("getting document contents ... ");
}
String documentContents = nzdl.getDocument(collName, id);
if (verbosity > 3) {
System.out.println("got document contents. ");
System.out.println(" *************** START DOC " + id + " ***************");
System.out.println(documentContents);
System.out.println(" *************** END DOC " + id + " ***************");
}
} else {
if (verbosity > 3)
System.out.println(meta.toString());
}
}
}
}
static String getHelpText() {
String s = "";
s += "SimpleClient - a Java client to the Greenstone Digital Library";
s += "\n";
s += "Arguments -\n";
s += "Short form Long form Explanation\n";
s += "-h --help Print this help text\n";
s += "-u --URL Use to connect\n";
s += "-I --IOR Use to connect\n";
s += "-f --file Look for an IOR if \n";
s += "-c --collection Examine collection \n";
s += "-a --all Examine all collections\n";
s += "-q --query Use \n";
s += "-d --docs Load the documents as well\n";
s += "-Q --queryAllCollections Apply the to all collections\n";
s += "-v --verbose Use verbosity level of \n";
s += " 0 = silent except fro error reporting\n";
s += " 1 = reporting of operations\n";
s += " 2 = verbose reporting of operations \n";
s += " 3 = reporting of results of operations\n";
s += " 4 = showing all the documents retrieved\n";
s += "\n";
return s;
}
private boolean parseArgs(String [] args) {
System.err.println("in parseArgs");
LongOpt[] longopts = new LongOpt[10];
int i = 0;
StringBuffer sb = new StringBuffer();
longopts[i++] = new LongOpt("help", LongOpt.NO_ARGUMENT, null, 'h');
longopts[i++] = new LongOpt("IOR", LongOpt.REQUIRED_ARGUMENT, null, 'I');
longopts[i++] = new LongOpt("URL", LongOpt.REQUIRED_ARGUMENT, null, 'u');
longopts[i++] = new LongOpt("file", LongOpt.REQUIRED_ARGUMENT, null, 'f');
longopts[i++] = new LongOpt("collection", LongOpt.REQUIRED_ARGUMENT, null, 'c');
longopts[i++] = new LongOpt("all", LongOpt.NO_ARGUMENT, null, 'a');
longopts[i++] = new LongOpt("query", LongOpt.REQUIRED_ARGUMENT, null, 'q');
longopts[i++] = new LongOpt("docs", LongOpt.NO_ARGUMENT, null, 'd');
longopts[i++] = new LongOpt("verbose", LongOpt.REQUIRED_ARGUMENT, null, 'v');
longopts[i++] = new LongOpt("queryAllCollections", LongOpt.NO_ARGUMENT, null, 'Q');
Getopt g = new Getopt("org.nzdl.gsdl.SimpleClient",
args,
"hI:f:c:aq:dv:Qu:",
longopts);
int c;
String arg;
try {
while ((c = g.getopt()) != -1) {
switch(c) {
case 'h':
System.out.println(getHelpText());
System.exit(1);
return false;
case 'I':
IORFromCommandLine = g.getOptarg();
break;
case 'u':
URLFromCommandLine = g.getOptarg();
break;
case 'f':
IORFileName = g.getOptarg();
useFileFirst = true;
break;
case 'c':
collectionNameToQuery = g.getOptarg();
break;
case 'q':
queryToQuery = g.getOptarg();
break;
case 'a':
examineAllCollections = true;
break;
case 'd':
loadDocs = true;
break;
case 'Q':
queryAllCollections = true;
break;
case 'v':
arg = g.getOptarg();
verbosity = Integer.parseInt(arg);
break;
default:
System.err.print("getopt() returned " + c + " sb = \"" + sb +
"\" longopts[g.getLongind()].getName() = \"" +
longopts[g.getLongind()].getName() + "\"\n");
System.err.print(getHelpText());
System.exit(1);
}
}
} catch (Throwable t) {
System.out.println("Exception thrown while processing arguments \n");
t.printStackTrace(System.out);
return false;
}
return true;
}
public static final void main( String [] args ) {
SimpleClient client = new SimpleClient(args);
if (client.examineAllCollections)
client.pollCollections();
client.runQuery();
} //main
} // class