Changeset 2067 for trunk/java-client/org


Ignore:
Timestamp:
2001-02-24T19:44:51+13:00 (23 years ago)
Author:
say1
Message:

many changes to SimpleClient. a few changes to NzdlServiceImpl.

Location:
trunk/java-client/org/nzdl/gsdl
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/java-client/org/nzdl/gsdl/SimpleClient.java

    r2066 r2067  
    2121package org.nzdl;
    2222
    23 // classes and packages we're importing ...
    24 import java.util.Set;
    25 import java.util.Map;
    26 import java.util.ListIterator;
     23// long live GNU ...
     24import gnu.getopt.Getopt;
     25import gnu.getopt.LongOpt;
     26
     27// java libraries we're using
     28import java.io.File;
     29import java.io.FileInputStream;
     30import java.io.FileOutputStream;
     31import java.io.FileReader;
     32import java.io.IOException;
     33import java.io.LineNumberReader;
     34import java.io.Serializable;
     35import java.util.Vector;
     36import java.util.Enumeration;
    2737import java.util.Iterator;
    2838import java.util.List;
    29 import org.nzdl.service.NzdlCollectionInfo;
     39import java.util.ListIterator;
     40import java.util.Map;
     41import java.util.Properties;
     42import java.util.Set;
     43
     44// local libraries
     45import org.nzdl.service.NzdlCollectionInfo;
    3046import org.nzdl.service.NzdlQuery;
    3147import org.nzdl.service.NzdlRequest;
     
    3551import org.nzdl.service.NzdlServiceImpl;
    3652
    37 
    3853/**
    3954 * Class SimpleClient
     
    4156 * A class to test the workings of Corba interface from the client side.
    4257 *
    43  * @author Dave Nichol  ([email protected])
     58 * @author Dave Nichols ([email protected])
    4459 * @author Gordon Paynter ([email protected])
    4560 * @author stuart yeates ([email protected])
     
    5267 * @see org.nzdl.service.NzdlService;
    5368 * @see org.nzdl.service.NzdlServiceImpl;
     69 * @see gnu.getopt.Getopt;
     70 * @see gnu.getopt.LongOpt;
    5471 */
    5572
    56 public class SimpleClient {
    57 
    58   public static final void main( String [] args ) {
    59 
    60     NzdlService nzdl = new NzdlServiceImpl( args );
    61 
    62     if (args.length < 3) {
    63       System.err.println("WARNING: missing command line arguments. using defualts:");
    64       System.err.println("usage: java <javaoptions> SimpleClient <collect> <xxx> <query>");
    65       System.err.println("       <javaoptions> = options to the JVM ");
    66       System.err.println("       <collect>     = the greenstone collection to use   (demo)");
    67       System.err.println("       <xxx>         = a currently unused option          (xxx)");
    68       System.err.println("       <query>       = the query to use on the collection (the)");
    69       args = new  String [3];
    70       args[0] = "demo";
    71       args[1] = "xxx";
    72       args[2] = "the";
    73     }
    74    
     73public class SimpleClient implements Cloneable, Serializable{
     74
     75  /** The collection of CORBA IORs we know about */
     76  protected Properties knownIORs = null;
     77  /** The underlying CORBA inferface */
     78  NzdlService nzdl = null;
     79  /** The name of the properties file */
     80  String propertiesFileName = "knownIORs.properties";
     81  /** The name of the file we look for IOR's in */
     82  String IORFileName = "/tmp/localcorba.objid";
     83  /** Look for an IOR in the file first */
     84  boolean useFileFirst = false;
     85  /** The IOR that was specified on the commandline */
     86  String IORFromCommandLine = null;
     87  /** The collection to query */
     88  String collectionNameToQuery = "demo";
     89  /** The query to use */
     90  String queryToQuery = "the";
     91  /** Poll all collections */
     92  boolean examineAllCollections = false;
     93  /** Should we load the docs as well ?*/
     94  boolean loadDocs = false;
     95  /** should we query all the collections with the query or just one ? */
     96  boolean queryAllCollections = false;
     97  /** How verbose do we want to be ? */
     98  int verbosity = 2;
     99
     100  SimpleClient() {
     101    init(null);
     102  }
     103
     104  SimpleClient(String [] args) {
     105    parseArgs(args);
     106    init(args);
     107  }
     108
     109  void init(String [] args)  {
     110
     111    // read our prevously seed IORs ...
     112    knownIORs = new Properties();
     113   
     114    File file = new File(propertiesFileName);
     115    try {
     116      if (file.exists()) {
     117    knownIORs.load(new FileInputStream(file));
     118      }
     119    } catch (IOException i) {
     120      System.err.println("Error reading properties file:" + i);
     121    }
     122
     123    Enumeration e = knownIORs.keys();
     124    while (e.hasMoreElements() && nzdl == null) {
     125      String IOR = (String) e.nextElement();
     126      attemptToInitialise(args, null, IOR);
     127     
     128    }
     129   
     130    if (nzdl == null) {
     131      String IOR = getIorKey();
     132      System.err.println("ior = " + IOR);
     133      System.err.println("after IOR");
     134      attemptToInitialise(args, null, IOR);
     135      knownIORs.put(IOR,"");
     136    }
     137
     138  }
     139
     140  public void finalize(){
     141    tidy();
     142  }
     143
     144  void tidy() {
     145    try {
     146      File file = new File(propertiesFileName);
     147      knownIORs.store(new FileOutputStream(file),"These are CORBA IOR's that the SimpleClient has seen and will check again...");
     148    } catch (IOException i) {
     149      System.err.println("Error writing properties file:" + i);
     150    }
     151  }
     152
     153  private String getIorKey() {
     154    String ior = null;
     155    try  {
     156      LineNumberReader input
     157    = new LineNumberReader(new FileReader(IORFileName));
     158      ior = input.readLine();
     159    }
     160    catch (java.io.IOException e) {
     161      System.err.println("Error reading IOR key:\n" + e);
     162      System.err.println("Aborting service...");
     163      System.exit(1);
     164    }
     165    return ior;
     166  }
     167
     168  private boolean attemptToInitialise(String [] _args,
     169                      Properties _props,
     170                      String _IOR) {
     171    try {
     172      nzdl = new NzdlServiceImpl( _args, _props, _IOR);
     173    } catch (Throwable t) {
     174      System.err.println ("failed to initialise using:");
     175      System.err.println (_IOR);
     176      nzdl = null;
     177      return false;
     178    }
     179    return true;
     180  }
     181 
     182  void pollCollections() {
     183
    75184    // extract some highlevel information on the collections on the server
    76185    // independent of the command line arguments passed in
    77        
     186   
    78187    Set collectionSet = nzdl.getCollectionSet(); // set of strings of collection titles
    79188    System.out.println("Number of collections on the server: " + collectionSet.size());
     
    106215      }
    107216      else
    108     { System.out.println("\t" + "hasCollection(" + collectionName + ")" + " is false"); }
     217    {
     218      System.out.println("\t" + "hasCollection(" + collectionName + ")" + " is false");
     219    }
    109220    }
    110221       
    111222       
    112        
    113        
    114     String collName = args[0];
    115     NzdlCollectionInfo collInfo = nzdl.getCollectionInfo( collName );
    116    
    117     System.out.println("Searching collection: " + collName);
    118        
    119     int numResults = collInfo.getNumOfDocs(); // what precisely is this number ?
    120     System.out.println("Collection " + collName + " suggests to get results in chunks of " + numResults + " hits" );
    121     String method = args[1];
    122     System.out.println("Search method: " + method);
    123     String queryTerm = args[2];
    124     System.out.println("Query Term: " + queryTerm);
    125     String metaTag = "Title";
    126        
    127     System.out.println("Searching in '" + metaTag + "' metadata");
    128 
    129     NzdlQuery query = new NzdlQuery( queryTerm );
    130     // return the first numResults that match
    131     //query.setEndResults( 15 );
    132     // "-1" means consider all the documents that match
    133     query.setMaxDocs( -1 );
    134 
    135     NzdlRequest request = new NzdlRequest( query );
    136     NzdlResponse response = new NzdlResponse( );
    137     System.out.println("getting document IDs ... ");
    138     nzdl.service( collName, request, response );
    139 
    140     NzdlResultSet results = response.getResultSet();
    141     List docIDs = results.getDocumentIDs();
    142     System.out.println("Number of documents returned: " + docIDs.size());
    143 
    144     System.out.println("getting '" + metaTag + "' meta data of documents ... ");
    145     Map metaData = nzdl.getMetaData( collName, docIDs, metaTag );
    146 
    147     for (ListIterator i = docIDs.listIterator(); i.hasNext(); ) {
    148       String id = (String) i.next();
    149       Set meta = (Set) metaData.get( id );
    150       System.out.println(meta.toString());
    151       System.out.println("getting document contents ... ");
    152       String documentContents = nzdl.getDocument(collName, id);
    153       System.out.println("got document contents. ");
    154       System.out.println(" *************** START DOC " + id + " ***************");
    155       System.out.println(documentContents);
    156       System.out.println(" *************** END DOC " + id + " ***************");
    157     }
    158    
     223  }
     224
     225  void runQuery()
     226  {
     227    String queryTerm = queryToQuery;
     228    Vector  collections = new Vector();
     229   
     230    if (queryAllCollections == false) {
     231      collections.addElement(collectionNameToQuery);
     232    } else {
     233      Set collectionSet = nzdl.getCollectionSet();
     234      Iterator collectionSetIterator = collectionSet.iterator();
     235      while (collectionSetIterator.hasNext() ) {
     236    String collectionName = (String)collectionSetIterator.next();
     237    collections.addElement(collectionName);
     238      }
     239    }
     240   
     241   
     242    Enumeration e = collections.elements();
     243    while (e.hasMoreElements()) {
     244      String collName  = (String) e.nextElement();
     245      NzdlCollectionInfo collInfo = nzdl.getCollectionInfo( collName );
     246     
     247      if (verbosity > 0)
     248    System.out.println("Searching collection: " + collName);
     249     
     250      int numResults = collInfo.getNumOfDocs(); // what precisely is this number ?
     251
     252      if (verbosity > 2)
     253    System.out.println("Collection " + collName + " suggests to get results in chunks of " + numResults + " hits" );
     254      if (verbosity > 2)
     255    System.out.println("Query Term: " + queryTerm);
     256      String metaTag = "Title";
     257     
     258      if (verbosity > 2)
     259    System.out.println("Searching in '" + metaTag + "' metadata");
     260     
     261      NzdlQuery query = new NzdlQuery( queryTerm );
     262      // return the first numResults that match
     263      //query.setEndResults( 15 );
     264      // "-1" means consider all the documents that match
     265      query.setMaxDocs( -1 );
     266     
     267      NzdlRequest request = new NzdlRequest( query );
     268      NzdlResponse response = new NzdlResponse( );
     269      if (verbosity > 2)
     270    System.out.println("getting document IDs ... ");
     271      nzdl.service( collName, request, response );
     272     
     273      NzdlResultSet results = response.getResultSet();
     274      List docIDs = results.getDocumentIDs();
     275
     276      if (verbosity > 1)
     277    System.out.println("Number of documents returned: " + docIDs.size());
     278      if (verbosity > 2)
     279    System.out.println("getting '" + metaTag + "' meta data of documents ... ");
     280
     281      Map metaData = nzdl.getMetaData( collName, docIDs, metaTag );
     282     
     283    for (ListIterator i = docIDs.listIterator(); i.hasNext(); ) {
     284      String id = (String) i.next();
     285      Set meta = (Set) metaData.get( id );
     286      if (loadDocs) {
     287        if (verbosity > 3) {
     288          System.out.println(meta.toString());
     289          System.out.println("getting document contents ... ");
     290        }
     291        String documentContents = nzdl.getDocument(collName, id);
     292        if (verbosity > 3) {
     293          System.out.println("got document contents. ");
     294          System.out.println(" *************** START DOC " + id + " ***************");
     295          System.out.println(documentContents);
     296          System.out.println(" *************** END DOC " + id + " ***************");
     297        }
     298      } else {
     299        if (verbosity > 3)
     300          System.out.println(meta.toString());
     301      }
     302    }
     303    }
     304  }
     305       
     306   
     307  static String getHelpText() {
     308    String s = "";
     309    s += "SimpleClient - a Java client to the Greenstone Digital Library";
     310    s += "\n";
     311    s += "Arguments -\n";
     312    s += "Short form Long form                Explaination\n";
     313    s += "-h         --help                   Print this help text\n";
     314    s += "-I <IOR>   --IOR <IOR>              Use <IOR> to connect\n";
     315    s += "-f <file>  --file <IOR>             Look for an IOR if <file>\n";
     316    s += "-c <coll>  --collection <coll>      Examine collection <coll>\n";
     317    s += "-a         --all                    Examine all collections\n";
     318    s += "-q <query> --query <query>          Use <query>\n";
     319    s += "-d         --docs                   Load the documents as well\n";
     320    s += "-Q         --queryAllCollections    Apply the <query> to all collections\n";
     321    s += "-v <n>     --verbose <n>            Use verbosity level of <v>\n";
     322    s += "               0 = silent except fro error reporting\n";
     323    s += "               1 = reporting of operations\n";
     324    s += "               2 = verbose reporting of operations \n";
     325    s += "               3 = reporting of results of operations\n";
     326    s += "               4 = showing all the documents retrieved\n";
     327    s += "\n";
     328    return s;
     329  }
    159330 
     331  private boolean parseArgs(String [] args) {
     332   
     333    LongOpt[] longopts = new LongOpt[9];
     334    int i = 0;
     335    StringBuffer sb = new StringBuffer();
     336   
     337    longopts[i++]  = new LongOpt("help",                  LongOpt.NO_ARGUMENT,       null, 'h');
     338    longopts[i++]  = new LongOpt("IOR",                   LongOpt.REQUIRED_ARGUMENT, null, 'I');
     339    longopts[i++]  = new LongOpt("file",                  LongOpt.REQUIRED_ARGUMENT, null, 'f');
     340    longopts[i++]  = new LongOpt("collection",            LongOpt.REQUIRED_ARGUMENT, null, 'c');
     341    longopts[i++]  = new LongOpt("all",                   LongOpt.NO_ARGUMENT,       null, 'a');
     342    longopts[i++]  = new LongOpt("query",                 LongOpt.REQUIRED_ARGUMENT, null, 'q');
     343    longopts[i++]  = new LongOpt("docs",                  LongOpt.NO_ARGUMENT,       null, 'd');
     344    longopts[i++]  = new LongOpt("verbose",               LongOpt.REQUIRED_ARGUMENT, null, 'v');
     345    longopts[i++]  = new LongOpt("queryAllCollections",   LongOpt.NO_ARGUMENT,       null, 'Q');
     346    Getopt g = new Getopt("org.nzdl.SimpleClient",
     347                          args,
     348                          "hI:f:c:aq:dv:Q",
     349                          longopts);
     350    int c;
     351    String arg;
     352    try {
     353      while ((c = g.getopt()) != -1) {
     354        switch(c) {
     355        case 'h':
     356          System.out.println(getHelpText());
     357          return false;
     358        case 'I':
     359      IORFromCommandLine = g.getOptarg();
     360          break;
     361        case 'f':
     362      IORFileName = g.getOptarg();
     363      useFileFirst = true;
     364          break;
     365    case 'c':
     366      collectionNameToQuery = g.getOptarg();
     367          break;
     368    case 'q':
     369      queryToQuery = g.getOptarg();
     370          break;
     371    case 'a':
     372      examineAllCollections = true;
     373      break;
     374    case 'd':
     375      loadDocs = true;
     376      break;
     377    case 'Q':
     378      queryAllCollections = true;
     379      break;
     380    case 'v':
     381          arg = g.getOptarg();
     382          verbosity = Integer.parseInt(arg);
     383          break;
     384    default:
     385      System.err.print("getopt() returned " + c + " sb = \"" + sb +
     386               "\" longopts[g.getLongind()].getName() = \"" +
     387               longopts[g.getLongind()].getName() + "\"\n");
     388      System.err.print(getHelpText());
     389      System.exit(1);
     390     
     391    }
     392      }
     393    } catch (Throwable t) {
     394      System.out.println("Exception thrown while processing arguments \n");
     395      t.printStackTrace(System.out);
     396      return false;
     397    }
     398    return true;
     399  }
     400 
     401  public static final void main( String [] args ) {
     402   
     403    SimpleClient client = new SimpleClient(args);
     404   
     405    if (client.examineAllCollections)
     406      client.pollCollections();
     407   
     408    client.runQuery();
     409   
     410    client.tidy();
    160411  } //main
    161412
  • trunk/java-client/org/nzdl/gsdl/service/NzdlServiceImpl.java

    r2055 r2067  
    1414  private corbaiface m_nzdlServer = null;
    1515
    16   public NzdlServiceImpl( String [] _args ) {
     16  public NzdlServiceImpl( String [] _args, Properties _props, String _IOR) {
    1717    try {
    18     System.err.println("before ORB.init");
     18      System.err.println("before ORB.init");
     19
    1920      // getting a reference to the remote NZDL server
    20       org.omg.CORBA.ORB orb = ORB.init( _args, null );
    21       System.err.println("after ORB.init - before IOR");
    22       String ior = getIorKey();
    23       System.err.println("ior = " + ior);
    24     System.err.println("after IOR");
    25     org.omg.CORBA.Object obj = orb.string_to_object(ior) ;
     21      org.omg.CORBA.ORB orb = ORB.init( _args, _props);
     22      System.err.println("after ORB.init");
     23      System.err.println("after LIS - before IOR");
     24
     25      org.omg.CORBA.Object obj = orb.string_to_object(_IOR) ;
    2626      System.err.println("after org stringtoobject");
    27     m_nzdlServer = corbaifaceHelper.narrow(obj);
    28     System.err.println("after .narrow");
     27      m_nzdlServer = corbaifaceHelper.narrow(obj);
     28      System.err.println("after .narrow");
    2929      // basic setup
    3030      if (m_nzdlServer.initialise()) {
     
    3636    //System.exit(1);
    3737      }
     38     
    3839    } catch (Exception e) {
    3940      System.err.println("Error in service init:\n" + e) ;
     
    4142      System.exit(1);
    4243    }
     44   
    4345    System.err.println("Service init ... YES");
    4446  }
     
    128130    //return resultSet.getAllMetaData( );
    129131  }
    130 
     132 
    131133  public void service( String _name, NzdlRequest _request, NzdlResponse _response ) {   
    132134    corbatext_t c_name = NzdlCorbaFactory.toCorbaText( _name );
     
    136138      System.err.println("service() error " + c_err.value.value());
    137139  }
    138 
    139   private String getIorKey() {
    140     String ior = null;
    141     try  {
    142       LineNumberReader input
    143     = new LineNumberReader(new FileReader("/tmp/localcorba.objid"));
    144       ior = input.readLine();
    145     }
    146     catch (java.io.IOException e) {
    147       System.err.println("Error reading IOR key:\n" + e);
    148       System.err.println("Aborting service...");
    149       System.exit(1);
    150     }
    151     return ior;
    152   }
    153 
     140 
    154141}
    155142
Note: See TracChangeset for help on using the changeset viewer.