Index: trunk/java-client/org/nzdl/gsdl/SearchAllCollections.java
===================================================================
--- trunk/java-client/org/nzdl/gsdl/SearchAllCollections.java	(revision 2100)
+++ trunk/java-client/org/nzdl/gsdl/SearchAllCollections.java	(revision 2100)
@@ -0,0 +1,324 @@
+/*
+ *    SearchAllCollections.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.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.FileReader;
+import java.io.IOException;
+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;
+
+/**
+ * Class SearchAllCollections
+ *
+ * Send a query to each collection and report the number of results.
+ *
+ * @author Gordon Paynter (paynter@cs.waikato.ac.nz)
+ * @author stuart yeates (say1@cs.waikato.ac.nz)
+ * @author Dave Nichols (daven@cs.waikato.ac.nz)
+ * @version $Revision$
+ * @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;
+ */
+
+public class SearchAllCollections implements Cloneable, Serializable {
+
+  /** The collection of CORBA IORs we know about */
+  protected Properties knownIORs = null;
+  /** The underlying CORBA inferface */
+  NzdlService nzdl = null;
+
+  /** The name of the properties file */
+  String propertiesFileName = "knownIORs.properties";
+  /** The name of the file we look for IOR's in */
+  String IORFileName = "/tmp/localcorba.objid";
+  /** Look for an IOR in the file first */
+  boolean useFileFirst = false;
+  /** The IOR that was specified on the commandline */
+  String IORFromCommandLine = null;
+
+  /** The query to use */
+  String queryText = "the";
+
+  /** How verbose do we want to be ? */
+  int verbosity = 0;
+
+    SearchAllCollections() {
+	init(null);
+    }
+    
+    SearchAllCollections(String [] args) {
+	parseArgs(args);
+	init(args);
+    }
+
+    void init(String [] args)  {
+	
+	// Load prevously seen IORs from property file
+	knownIORs = new Properties();
+	
+	File file = new File(propertiesFileName);
+	try {
+	    if (file.exists()) {
+		knownIORs.load(new FileInputStream(file));
+	    } 
+	} catch (IOException i) {
+	    System.err.println("Error reading properties file:" + i);
+	}
+
+	// Attempt to initialise client with ORs from property file
+	Enumeration e = knownIORs.keys();
+	while (e.hasMoreElements() && nzdl == null) {
+	    String IOR = (String) e.nextElement();
+	    attemptToInitialise(args, null, IOR);
+	    
+	}
+	
+	// Attempt to initialise client with IOR from file
+	if (nzdl == null) {
+	    String IOR = getIorKey(IORFileName);
+	    attemptToInitialise(args, null, IOR);
+	    knownIORs.put(IOR,"");
+	}
+	
+    }
+    
+    public void finalize(){
+	tidy();
+    }
+
+    void tidy() {
+	try {
+	    File file = new File(propertiesFileName);
+	    knownIORs.store(new FileOutputStream(file),"These are CORBA IOR's that the SearchAllCollections client has seen and will check again...");
+	} catch (IOException i) {
+	    System.err.println("Error writing properties file:" + i);
+	}
+    }
+
+    /** 
+     * Pre:  Accept SourceOfID as either: URL or IOR string or Filename
+     * Post: extract localcorba.objid from source of ID
+     */
+    private String getIorKey(String sourceOfID) {
+	String ior = null;
+	try  {
+	    sourceOfID = sourceOfID.trim();
+	    System.err.println("Reading IOR from: " + sourceOfID);
+	    
+	    String firstChars = sourceOfID.substring(0, Math.min(4,sourceOfID.length())).toUpperCase();
+	    // if sourceOfID is a URL ---------------------------------------
+	    if (sourceOfID.length() > 4 && firstChars.equalsIgnoreCase("HTTP"))	{
+		URL myUrl = new URL(sourceOfID);
+		BufferedReader input =new
+		    BufferedReader(new InputStreamReader(myUrl.openStream()));
+		ior = input.readLine();
+	    }
+	    
+	    // if sourceOfID is an IOR --------------------------------------
+	    else if (sourceOfID.length() > 4 && firstChars.equalsIgnoreCase("IOR:"))	{
+		ior = sourceOfID;      		  	
+	    }
+	    
+	    // else assume sourceOfID is a file name ------------------------
+	    else {
+		BufferedReader input
+		    = new BufferedReader(new FileReader(sourceOfID));
+		ior = input.readLine();      		  	
+	    }
+	}  //end of try
+	catch (java.io.IOException e) {
+	    System.err.println("Error reading IOR key:\n" + e);
+	    System.err.println("Aborting service...");
+	    System.exit(1);
+	}
+	return ior;
+    }   // end of getIorKey
+    
+
+    private boolean attemptToInitialise(String [] _args, 
+					Properties _props, 
+					String _IOR) {
+	try {
+	    nzdl = new NzdlServiceClient( _args, _props, _IOR);
+	} catch (Throwable t) {
+	    System.err.println ("Failed to initialise using:");
+	    System.err.println (_IOR);
+	    nzdl = null;
+	    return false;
+	}
+	return true;
+    }
+
+  
+    static String getHelpText() {
+	String s = "";
+	s += "SearchAllCollections - a Java client to the Greenstone Digital Library";
+	s += "\n";
+	s += "Arguments -\n";
+	s += "Short form Long form                Explaination\n";
+	s += "-h         --help                   Print this help text\n";
+	s += "-I <IOR>   --IOR <IOR>              Use <IOR> to connect\n";
+	s += "-f <file>  --file <IOR>             Look for an IOR if <file>\n";
+	s += "-q <query> --query <query>          Use <query>\n";
+	s += "-v <n>     --verbose <n>            Use verbosity level of <v>\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) {
+	
+	LongOpt[] longopts = new LongOpt[9];
+	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("file",    LongOpt.REQUIRED_ARGUMENT, null, 'f');
+	longopts[i++]  = new LongOpt("query",   LongOpt.REQUIRED_ARGUMENT, null, 'q');
+	longopts[i++]  = new LongOpt("verbose", LongOpt.REQUIRED_ARGUMENT, null, 'v');
+	Getopt g = new Getopt("org.nzdl.gsdl.SearchAllCollections",
+			      args, "hI:f:c:q:v:", longopts);
+	int c;
+	String arg;
+	try {
+	    while ((c = g.getopt()) != -1) {
+		switch(c) {
+		case 'h':
+		    System.out.println(getHelpText());
+		    return false;
+		case 'I':
+		    IORFromCommandLine = g.getOptarg();
+		    break;
+		case 'f':
+		    IORFileName = g.getOptarg();
+		    useFileFirst = true;
+		    break;
+		case 'q':
+		    queryText = g.getOptarg();
+		    break;
+		case 'v':
+		    arg = g.getOptarg();
+		    verbosity = Integer.parseInt(arg);
+		    break;
+		default:
+		    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;
+    }
+  
+
+    void queryAllCollections() {
+	
+	
+	// set of collection titles
+	Set collectionSet = nzdl.getCollectionSet();
+	System.out.println("Searching for \"" + queryText 
+			   + "\" in up to " + collectionSet.size() + " collections.");
+	
+	Iterator collectionSetIterator = collectionSet.iterator();
+	while (collectionSetIterator.hasNext() ) {
+
+	    String collectionName = (String) collectionSetIterator.next(); 
+	    if (nzdl.hasCollection(collectionName) ) {
+		if (nzdl.pingCollection(collectionName) ) {
+		    queryCollection(collectionName, queryText);
+		}
+	    } 
+	}
+    }
+
+
+    void queryCollection(String collectionName, String queryText)
+    {
+	
+	NzdlCollectionInfo collInfo = nzdl.getCollectionInfo( collectionName );
+	    
+	NzdlQuery query = new NzdlQuery(queryText);
+	query.setMaxDocs( 10 );
+	
+	NzdlRequest request = new NzdlRequest( query );
+	NzdlResponse response = new NzdlResponse( );
+	
+	nzdl.service( collectionName, request, response );
+	
+	System.out.println(response.getResultSet().getNumOfDocs() + "\t" + collectionName);
+    }
+    
+    
+    public static final void main( String [] args ) {
+	
+	SearchAllCollections client = new SearchAllCollections(args);
+	
+	client.queryAllCollections();
+	client.tidy();
+
+    } //main
+    
+} // class
