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
|
---|
22 | package org.nzdl.gsdl;
|
---|
23 |
|
---|
24 | // long live GNU ...
|
---|
25 | import gnu.getopt.Getopt;
|
---|
26 | import gnu.getopt.LongOpt;
|
---|
27 |
|
---|
28 | // java libraries we're using
|
---|
29 | import java.io.BufferedReader;
|
---|
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;
|
---|
35 | import java.io.InputStreamReader;
|
---|
36 | import java.io.LineNumberReader;
|
---|
37 | import java.io.Serializable;
|
---|
38 | import java.net.URL;
|
---|
39 | import java.util.Enumeration;
|
---|
40 | import java.util.Iterator;
|
---|
41 | import java.util.List;
|
---|
42 | import java.util.ListIterator;
|
---|
43 | import java.util.Map;
|
---|
44 | import java.util.Properties;
|
---|
45 | import java.util.Set;
|
---|
46 | import java.util.Vector;
|
---|
47 |
|
---|
48 | // local libraries
|
---|
49 | import org.nzdl.gsdl.service.NzdlCollectionInfo;
|
---|
50 | import org.nzdl.gsdl.service.NzdlQuery;
|
---|
51 | import org.nzdl.gsdl.service.NzdlRequest;
|
---|
52 | import org.nzdl.gsdl.service.NzdlResponse;
|
---|
53 | import org.nzdl.gsdl.service.NzdlResultSet;
|
---|
54 | import org.nzdl.gsdl.service.NzdlService;
|
---|
55 | import 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: 2119 $
|
---|
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 |
|
---|
77 | public 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 | queryCollection(collectionName, queryText);
|
---|
292 | }
|
---|
293 | }
|
---|
294 | }
|
---|
295 | }
|
---|
296 |
|
---|
297 |
|
---|
298 | void queryCollection(String collectionName, String queryText)
|
---|
299 | {
|
---|
300 | NzdlQuery query = new NzdlQuery(queryText);
|
---|
301 | query.setMaxDocs( 10 );
|
---|
302 |
|
---|
303 | NzdlRequest request = new NzdlRequest( query );
|
---|
304 | NzdlResponse response = new NzdlResponse( );
|
---|
305 |
|
---|
306 | nzdl.service( collectionName, request, response );
|
---|
307 |
|
---|
308 | int frequency = response.getResultSet().getNumOfDocs();
|
---|
309 | if (frequency > 0) {
|
---|
310 | System.out.println("------------------------------");
|
---|
311 | System.out.println(frequency + "\t" + collectionName);
|
---|
312 | describeCollection(collectionName);
|
---|
313 | }
|
---|
314 | }
|
---|
315 |
|
---|
316 | void describeCollection(String collectionName) {
|
---|
317 |
|
---|
318 | String document = nzdl.getDocument(collectionName, "collection");
|
---|
319 | System.out.println(document);
|
---|
320 |
|
---|
321 | Set s = nzdl.getMetaData(collectionName, "document", "collectionextra");
|
---|
322 | /*
|
---|
323 | Iterator setIterator = s.iterator();
|
---|
324 | while (setIterator.hasNext() ) {
|
---|
325 | String value = (String) setIterator.next();
|
---|
326 | System.out.println("collectionextra: " + value + "\n---\n");
|
---|
327 | }
|
---|
328 | */
|
---|
329 |
|
---|
330 | }
|
---|
331 |
|
---|
332 |
|
---|
333 | public static final void main( String [] args ) {
|
---|
334 |
|
---|
335 | SearchAllCollections client = new SearchAllCollections(args);
|
---|
336 |
|
---|
337 | client.queryAllCollections();
|
---|
338 | client.tidy();
|
---|
339 |
|
---|
340 | } //main
|
---|
341 |
|
---|
342 | } // class
|
---|