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 | import org.nzdl.gsdl.util.NzdlServiceFactory;
|
---|
57 |
|
---|
58 | /**
|
---|
59 | * Class SearchAllCollections
|
---|
60 | *
|
---|
61 | * Send a query to each collection and report the number of results.
|
---|
62 | *
|
---|
63 | * @author Gordon Paynter ([email protected])
|
---|
64 | * @author Stuart Yeates ([email protected])
|
---|
65 | * @author Dave Nichols ([email protected])
|
---|
66 | * @version $Revision: 2232 $
|
---|
67 | * @see org.nzdl.gsdl.service.NzdlCollectionInfo
|
---|
68 | * @see org.nzdl.gsdl.service.NzdlQuery
|
---|
69 | * @see org.nzdl.gsdl.service.NzdlRequest
|
---|
70 | * @see org.nzdl.gsdl.service.NzdlResponse
|
---|
71 | * @see org.nzdl.gsdl.service.NzdlResultSet
|
---|
72 | * @see org.nzdl.gsdl.service.NzdlService
|
---|
73 | * @see org.nzdl.gsdl.service.NzdlServiceClient
|
---|
74 | * @see org.nzdl.gsdl.util.NzdlServiceFactory
|
---|
75 | * @see gnu.getopt.Getopt
|
---|
76 | * @see gnu.getopt.LongOpt
|
---|
77 | */
|
---|
78 |
|
---|
79 | public class SearchAllCollections implements Cloneable, Serializable {
|
---|
80 |
|
---|
81 | /** The underlying CORBA inferface */
|
---|
82 | NzdlService nzdl = null;
|
---|
83 |
|
---|
84 | /** Look for an IOR in the file first */
|
---|
85 | boolean useFileFirst = false;
|
---|
86 | /** The IOR that was specified on the commandline */
|
---|
87 | String IORFromCommandLine = null;
|
---|
88 | /** The URL that was specified on the commandline */
|
---|
89 | String URLFromCommandLine = null;
|
---|
90 | /** The name of the file we look for IOR's in */
|
---|
91 | static String IORFileName = "/tmp/localcorba.objid";
|
---|
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 | nzdl = NzdlServiceFactory.createNzdlService(null,
|
---|
101 | System.getProperties(),
|
---|
102 | URLFromCommandLine,
|
---|
103 | IORFileName,
|
---|
104 | IORFromCommandLine);
|
---|
105 | if (nzdl == null)
|
---|
106 | throw new Error("URK! unable to find an IOR...");
|
---|
107 | }
|
---|
108 |
|
---|
109 |
|
---|
110 | /**
|
---|
111 | * Normal constructor. the args are passed into the orb
|
---|
112 | * initialisation code so they can extract obscure args ...
|
---|
113 | */
|
---|
114 | SearchAllCollections(String [] args) {
|
---|
115 | // parseArgs(args);
|
---|
116 | nzdl = NzdlServiceFactory.createNzdlService(args,
|
---|
117 | System.getProperties(),
|
---|
118 | URLFromCommandLine,
|
---|
119 | IORFileName,
|
---|
120 | IORFromCommandLine);
|
---|
121 | if (nzdl == null)
|
---|
122 | throw new Error("Unable to find an IOR.");
|
---|
123 | }
|
---|
124 |
|
---|
125 | public void finalize(){
|
---|
126 |
|
---|
127 | }
|
---|
128 |
|
---|
129 | /**
|
---|
130 | * Pre: Accept SourceOfID as either: URL or IOR string or Filename
|
---|
131 | * Post: extract localcorba.objid from source of ID
|
---|
132 | */
|
---|
133 | private String getIorKey(String sourceOfID) {
|
---|
134 | String ior = null;
|
---|
135 | try {
|
---|
136 | sourceOfID = sourceOfID.trim();
|
---|
137 | System.err.println("Reading IOR from: " + sourceOfID);
|
---|
138 |
|
---|
139 | String firstChars = sourceOfID.substring(0, Math.min(4,sourceOfID.length())).toUpperCase();
|
---|
140 | // if sourceOfID is a URL ---------------------------------------
|
---|
141 | if (sourceOfID.length() > 4 && firstChars.equalsIgnoreCase("HTTP")) {
|
---|
142 | URL myUrl = new URL(sourceOfID);
|
---|
143 | BufferedReader input =new
|
---|
144 | BufferedReader(new InputStreamReader(myUrl.openStream()));
|
---|
145 | ior = input.readLine();
|
---|
146 | }
|
---|
147 |
|
---|
148 | // if sourceOfID is an IOR --------------------------------------
|
---|
149 | else if (sourceOfID.length() > 4 && firstChars.equalsIgnoreCase("IOR:")) {
|
---|
150 | ior = sourceOfID;
|
---|
151 | }
|
---|
152 |
|
---|
153 | // else assume sourceOfID is a file name ------------------------
|
---|
154 | else {
|
---|
155 | BufferedReader input
|
---|
156 | = new BufferedReader(new FileReader(sourceOfID));
|
---|
157 | ior = input.readLine();
|
---|
158 | }
|
---|
159 | } //end of try
|
---|
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 | } // end of getIorKey
|
---|
167 |
|
---|
168 |
|
---|
169 | static String getHelpText() {
|
---|
170 | String s = "";
|
---|
171 | s += "SearchAllCollections - a Java client to the Greenstone Digital Library";
|
---|
172 | s += "\n";
|
---|
173 | s += "Arguments -\n";
|
---|
174 | s += "Short form Long form Explaination\n";
|
---|
175 | s += "-h --help Print this help text\n";
|
---|
176 | s += "-I <IOR> --IOR <IOR> Use <IOR> to connect\n";
|
---|
177 | s += "-f <file> --file <IOR> Look for an IOR if <file>\n";
|
---|
178 | s += "-q <query> --query <query> Use <query>\n";
|
---|
179 | s += "-v <n> --verbose <n> Use verbosity level of <v>\n";
|
---|
180 | s += " 0 = silent except fro error reporting\n";
|
---|
181 | s += " 1 = reporting of operations\n";
|
---|
182 | s += " 2 = verbose reporting of operations \n";
|
---|
183 | s += " 3 = reporting of results of operations\n";
|
---|
184 | s += " 4 = showing all the documents retrieved\n";
|
---|
185 | s += "\n";
|
---|
186 | return s;
|
---|
187 | }
|
---|
188 |
|
---|
189 | private boolean parseArgs(String [] args) {
|
---|
190 |
|
---|
191 | LongOpt[] longopts = new LongOpt[9];
|
---|
192 | int i = 0;
|
---|
193 | StringBuffer sb = new StringBuffer();
|
---|
194 |
|
---|
195 | longopts[i++] = new LongOpt("help", LongOpt.NO_ARGUMENT, null, 'h');
|
---|
196 | longopts[i++] = new LongOpt("IOR", LongOpt.REQUIRED_ARGUMENT, null, 'I');
|
---|
197 | longopts[i++] = new LongOpt("file", LongOpt.REQUIRED_ARGUMENT, null, 'f');
|
---|
198 | longopts[i++] = new LongOpt("query", LongOpt.REQUIRED_ARGUMENT, null, 'q');
|
---|
199 | longopts[i++] = new LongOpt("verbose", LongOpt.REQUIRED_ARGUMENT, null, 'v');
|
---|
200 | Getopt g = new Getopt("org.nzdl.gsdl.SearchAllCollections",
|
---|
201 | args, "hI:f:c:q:v:", longopts);
|
---|
202 | int c;
|
---|
203 | String arg;
|
---|
204 | try {
|
---|
205 | while ((c = g.getopt()) != -1) {
|
---|
206 | switch(c) {
|
---|
207 | case 'h':
|
---|
208 | System.out.println(getHelpText());
|
---|
209 | return false;
|
---|
210 | case 'I':
|
---|
211 | IORFromCommandLine = g.getOptarg();
|
---|
212 | break;
|
---|
213 | case 'f':
|
---|
214 | IORFileName = g.getOptarg();
|
---|
215 | useFileFirst = true;
|
---|
216 | break;
|
---|
217 | case 'q':
|
---|
218 | queryText = g.getOptarg();
|
---|
219 | break;
|
---|
220 | case 'v':
|
---|
221 | arg = g.getOptarg();
|
---|
222 | verbosity = Integer.parseInt(arg);
|
---|
223 | break;
|
---|
224 | default:
|
---|
225 | System.err.print(getHelpText());
|
---|
226 | System.exit(1);
|
---|
227 |
|
---|
228 | }
|
---|
229 | }
|
---|
230 | } catch (Throwable t) {
|
---|
231 | System.out.println("Exception thrown while processing arguments \n");
|
---|
232 | t.printStackTrace(System.out);
|
---|
233 | return false;
|
---|
234 | }
|
---|
235 | return true;
|
---|
236 | }
|
---|
237 |
|
---|
238 |
|
---|
239 | void queryAllCollections() {
|
---|
240 |
|
---|
241 |
|
---|
242 | // set of collection titles
|
---|
243 | Set collectionSet = nzdl.getCollectionSet();
|
---|
244 | System.out.println("Searching for \"" + queryText
|
---|
245 | + "\" in up to " + collectionSet.size() + " collections.");
|
---|
246 |
|
---|
247 | Iterator collectionSetIterator = collectionSet.iterator();
|
---|
248 | while (collectionSetIterator.hasNext() ) {
|
---|
249 |
|
---|
250 | String collectionName = (String) collectionSetIterator.next();
|
---|
251 | if (nzdl.hasCollection(collectionName) ) {
|
---|
252 | // if (nzdl.pingCollection(collectionName) ) {
|
---|
253 |
|
---|
254 | // ping is broken, so use builddate to decide whether this
|
---|
255 | // collection is valid
|
---|
256 | NzdlCollectionInfo info = nzdl.getCollectionInfo(collectionName);
|
---|
257 | if (info != null && info.getBuildDate() != 0) {
|
---|
258 | queryCollection(collectionName, queryText);
|
---|
259 | }
|
---|
260 | // }
|
---|
261 | }
|
---|
262 | }
|
---|
263 | }
|
---|
264 |
|
---|
265 | void queryCollection(String collectionName, String queryText)
|
---|
266 | {
|
---|
267 | NzdlQuery query = new NzdlQuery(queryText);
|
---|
268 | query.setMaxDocs( 10 );
|
---|
269 |
|
---|
270 | NzdlRequest request = new NzdlRequest( query );
|
---|
271 | NzdlResponse response = new NzdlResponse( );
|
---|
272 |
|
---|
273 | nzdl.service( collectionName, request, response );
|
---|
274 |
|
---|
275 | long frequency = response.getResultSet().getNumOfDocs();
|
---|
276 | if (frequency > 0) {
|
---|
277 | System.out.println("\n------------------------------");
|
---|
278 | System.out.println(frequency + "\t" + collectionName);
|
---|
279 | describeCollection(collectionName);
|
---|
280 | }
|
---|
281 | }
|
---|
282 |
|
---|
283 | void describeCollection(String collectionName) {
|
---|
284 |
|
---|
285 | // collection name
|
---|
286 | Set n = nzdl.getMetaData(collectionName, "collection", "collectionname");
|
---|
287 | if (n.size() == 1) {
|
---|
288 | System.out.println("\nName: " + n.toArray()[0]);
|
---|
289 | }
|
---|
290 |
|
---|
291 | // collection description
|
---|
292 | Set d = nzdl.getMetaData(collectionName, "collection", "collectionextra");
|
---|
293 | if (d.size() == 1) {
|
---|
294 | String value = (String) d.toArray()[0];
|
---|
295 | if (value.length() > 0) {
|
---|
296 | System.out.println("Description: " + value);
|
---|
297 | }
|
---|
298 | }
|
---|
299 |
|
---|
300 | // basic collection information
|
---|
301 | NzdlCollectionInfo info = nzdl.getCollectionInfo(collectionName);
|
---|
302 | System.out.println("Info: " + info);
|
---|
303 | }
|
---|
304 |
|
---|
305 |
|
---|
306 | public static final void main( String [] args ) {
|
---|
307 |
|
---|
308 | SearchAllCollections client = new SearchAllCollections(args);
|
---|
309 | client.queryAllCollections();
|
---|
310 |
|
---|
311 | } //main
|
---|
312 |
|
---|
313 | } // class
|
---|