source: trunk/java-client/org/nzdl/gsdl/SearchAllCollections.java@ 2203

Last change on this file since 2203 was 2203, checked in by paynter, 23 years ago

Get collection name and description.

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 10.8 KB
Line 
1/*
2 * SearchAllCollections.java
3 *
4 * Copyright (C) 2001 New Zealand Digital Library Project
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21// the package we're in
22package org.nzdl.gsdl;
23
24// long live GNU ...
25import gnu.getopt.Getopt;
26import gnu.getopt.LongOpt;
27
28// java libraries we're using
29import java.io.BufferedReader;
30import java.io.File;
31import java.io.FileInputStream;
32import java.io.FileOutputStream;
33import java.io.FileReader;
34import java.io.IOException;
35import java.io.InputStreamReader;
36import java.io.LineNumberReader;
37import java.io.Serializable;
38import java.net.URL;
39import java.util.Enumeration;
40import java.util.Iterator;
41import java.util.List;
42import java.util.ListIterator;
43import java.util.Map;
44import java.util.Properties;
45import java.util.Set;
46import java.util.Vector;
47
48// local libraries
49import org.nzdl.gsdl.service.NzdlCollectionInfo;
50import org.nzdl.gsdl.service.NzdlQuery;
51import org.nzdl.gsdl.service.NzdlRequest;
52import org.nzdl.gsdl.service.NzdlResponse;
53import org.nzdl.gsdl.service.NzdlResultSet;
54import org.nzdl.gsdl.service.NzdlService;
55import org.nzdl.gsdl.service.NzdlServiceClient;
56
57/**
58 * Class SearchAllCollections
59 *
60 * Send a query to each collection and report the number of results.
61 *
62 * @author Gordon Paynter ([email protected])
63 * @author Stuart Yeates ([email protected])
64 * @author Dave Nichols ([email protected])
65 * @version $Revision: 2203 $
66 * @see org.nzdl.gsdl.service.NzdlCollectionInfo
67 * @see org.nzdl.gsdl.service.NzdlQuery
68 * @see org.nzdl.gsdl.service.NzdlRequest
69 * @see org.nzdl.gsdl.service.NzdlResponse
70 * @see org.nzdl.gsdl.service.NzdlResultSet
71 * @see org.nzdl.gsdl.service.NzdlService
72 * @see org.nzdl.gsdl.service.NzdlServiceClient
73 * @see gnu.getopt.Getopt
74 * @see gnu.getopt.LongOpt
75 */
76
77public class SearchAllCollections implements Cloneable, Serializable {
78
79 /** The collection of CORBA IORs we know about */
80 protected Properties knownIORs = null;
81 /** The underlying CORBA inferface */
82 NzdlService nzdl = null;
83
84 /** The name of the properties file */
85 String propertiesFileName = "knownIORs.properties";
86 /** The name of the file we look for IOR's in */
87 String IORFileName = "/tmp/localcorba.objid";
88 /** Look for an IOR in the file first */
89 boolean useFileFirst = false;
90 /** The IOR that was specified on the commandline */
91 String IORFromCommandLine = null;
92
93 /** The query to use */
94 String queryText = "the";
95
96 /** How verbose do we want to be ? */
97 int verbosity = 0;
98
99 SearchAllCollections() {
100 init(null);
101 }
102
103 SearchAllCollections(String [] args) {
104 parseArgs(args);
105 init(args);
106 }
107
108 void init(String [] args) {
109
110 // Load prevously seen IORs from property file
111 knownIORs = new Properties();
112
113 File file = new File(propertiesFileName);
114 try {
115 if (file.exists()) {
116 knownIORs.load(new FileInputStream(file));
117 }
118 } catch (IOException i) {
119 System.err.println("Error reading properties file:" + i);
120 }
121
122 // Attempt to initialise client with ORs from property file
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 // Attempt to initialise client with IOR from file
131 if (nzdl == null) {
132 String IOR = getIorKey(IORFileName);
133 attemptToInitialise(args, null, IOR);
134 knownIORs.put(IOR,"");
135 }
136
137 }
138
139 public void finalize(){
140 tidy();
141 }
142
143 void tidy() {
144 try {
145 File file = new File(propertiesFileName);
146 knownIORs.store(new FileOutputStream(file),"These are CORBA IOR's that the SearchAllCollections client has seen and will check again...");
147 } catch (IOException i) {
148 System.err.println("Error writing properties file:" + i);
149 }
150 }
151
152 /**
153 * Pre: Accept SourceOfID as either: URL or IOR string or Filename
154 * Post: extract localcorba.objid from source of ID
155 */
156 private String getIorKey(String sourceOfID) {
157 String ior = null;
158 try {
159 sourceOfID = sourceOfID.trim();
160 System.err.println("Reading IOR from: " + sourceOfID);
161
162 String firstChars = sourceOfID.substring(0, Math.min(4,sourceOfID.length())).toUpperCase();
163 // if sourceOfID is a URL ---------------------------------------
164 if (sourceOfID.length() > 4 && firstChars.equalsIgnoreCase("HTTP")) {
165 URL myUrl = new URL(sourceOfID);
166 BufferedReader input =new
167 BufferedReader(new InputStreamReader(myUrl.openStream()));
168 ior = input.readLine();
169 }
170
171 // if sourceOfID is an IOR --------------------------------------
172 else if (sourceOfID.length() > 4 && firstChars.equalsIgnoreCase("IOR:")) {
173 ior = sourceOfID;
174 }
175
176 // else assume sourceOfID is a file name ------------------------
177 else {
178 BufferedReader input
179 = new BufferedReader(new FileReader(sourceOfID));
180 ior = input.readLine();
181 }
182 } //end of try
183 catch (java.io.IOException e) {
184 System.err.println("Error reading IOR key:\n" + e);
185 System.err.println("Aborting service...");
186 System.exit(1);
187 }
188 return ior;
189 } // end of getIorKey
190
191
192 private boolean attemptToInitialise(String [] _args,
193 Properties _props,
194 String _IOR) {
195 try {
196 nzdl = new NzdlServiceClient( _args, _props, _IOR);
197 } catch (Throwable t) {
198 System.err.println ("Failed to initialise using:");
199 System.err.println (_IOR);
200 nzdl = null;
201 return false;
202 }
203 return true;
204 }
205
206
207 static String getHelpText() {
208 String s = "";
209 s += "SearchAllCollections - a Java client to the Greenstone Digital Library";
210 s += "\n";
211 s += "Arguments -\n";
212 s += "Short form Long form Explaination\n";
213 s += "-h --help Print this help text\n";
214 s += "-I <IOR> --IOR <IOR> Use <IOR> to connect\n";
215 s += "-f <file> --file <IOR> Look for an IOR if <file>\n";
216 s += "-q <query> --query <query> Use <query>\n";
217 s += "-v <n> --verbose <n> Use verbosity level of <v>\n";
218 s += " 0 = silent except fro error reporting\n";
219 s += " 1 = reporting of operations\n";
220 s += " 2 = verbose reporting of operations \n";
221 s += " 3 = reporting of results of operations\n";
222 s += " 4 = showing all the documents retrieved\n";
223 s += "\n";
224 return s;
225 }
226
227 private boolean parseArgs(String [] args) {
228
229 LongOpt[] longopts = new LongOpt[9];
230 int i = 0;
231 StringBuffer sb = new StringBuffer();
232
233 longopts[i++] = new LongOpt("help", LongOpt.NO_ARGUMENT, null, 'h');
234 longopts[i++] = new LongOpt("IOR", LongOpt.REQUIRED_ARGUMENT, null, 'I');
235 longopts[i++] = new LongOpt("file", LongOpt.REQUIRED_ARGUMENT, null, 'f');
236 longopts[i++] = new LongOpt("query", LongOpt.REQUIRED_ARGUMENT, null, 'q');
237 longopts[i++] = new LongOpt("verbose", LongOpt.REQUIRED_ARGUMENT, null, 'v');
238 Getopt g = new Getopt("org.nzdl.gsdl.SearchAllCollections",
239 args, "hI:f:c:q:v:", longopts);
240 int c;
241 String arg;
242 try {
243 while ((c = g.getopt()) != -1) {
244 switch(c) {
245 case 'h':
246 System.out.println(getHelpText());
247 return false;
248 case 'I':
249 IORFromCommandLine = g.getOptarg();
250 break;
251 case 'f':
252 IORFileName = g.getOptarg();
253 useFileFirst = true;
254 break;
255 case 'q':
256 queryText = g.getOptarg();
257 break;
258 case 'v':
259 arg = g.getOptarg();
260 verbosity = Integer.parseInt(arg);
261 break;
262 default:
263 System.err.print(getHelpText());
264 System.exit(1);
265
266 }
267 }
268 } catch (Throwable t) {
269 System.out.println("Exception thrown while processing arguments \n");
270 t.printStackTrace(System.out);
271 return false;
272 }
273 return true;
274 }
275
276
277 void queryAllCollections() {
278
279
280 // set of collection titles
281 Set collectionSet = nzdl.getCollectionSet();
282 System.out.println("Searching for \"" + queryText
283 + "\" in up to " + collectionSet.size() + " collections.");
284
285 Iterator collectionSetIterator = collectionSet.iterator();
286 while (collectionSetIterator.hasNext() ) {
287
288 String collectionName = (String) collectionSetIterator.next();
289 if (nzdl.hasCollection(collectionName) ) {
290 // if (nzdl.pingCollection(collectionName) ) {
291
292 // ping is broken, so use builddate to decide whether this
293 // collection is valid
294 NzdlCollectionInfo info = nzdl.getCollectionInfo(collectionName);
295 if (info != null && info.getBuildDate() != 0) {
296 queryCollection(collectionName, queryText);
297 }
298 // }
299 }
300 }
301 }
302
303 void queryCollection(String collectionName, String queryText)
304 {
305 NzdlQuery query = new NzdlQuery(queryText);
306 query.setMaxDocs( 10 );
307
308 NzdlRequest request = new NzdlRequest( query );
309 NzdlResponse response = new NzdlResponse( );
310
311 nzdl.service( collectionName, request, response );
312
313 int frequency = response.getResultSet().getNumOfDocs();
314 if (frequency > 0) {
315 System.out.println("\n------------------------------");
316 System.out.println(frequency + "\t" + collectionName);
317 describeCollection(collectionName);
318 }
319 }
320
321 void describeCollection(String collectionName) {
322
323 // collection name
324 Set n = nzdl.getMetaData(collectionName, "collection", "collectionname");
325 if (n.size() == 1) {
326 System.out.println("\nName: " + n.toArray()[0]);
327 }
328
329 // collection description
330 Set d = nzdl.getMetaData(collectionName, "collection", "collectionextra");
331 if (d.size() == 1) {
332 String value = (String) d.toArray()[0];
333 if (value.length() > 0) {
334 System.out.println("Description: " + value);
335 }
336 }
337
338 // basic collection information
339 NzdlCollectionInfo info = nzdl.getCollectionInfo(collectionName);
340 System.out.println("Info: " + info);
341 }
342
343
344 public static final void main( String [] args ) {
345
346 SearchAllCollections client = new SearchAllCollections(args);
347
348 client.queryAllCollections();
349 client.tidy();
350
351 } //main
352
353} // class
Note: See TracBrowser for help on using the repository browser.