source: trunk/java-client/org/nzdl/gsdl/util/NzdlServiceFactory.java@ 2183

Last change on this file since 2183 was 2183, checked in by say1, 23 years ago

fixed code after previous commit ...

  • Property svn:keywords set to Author Date Id Revision
File size: 16.7 KB
Line 
1/*
2 * NzdlServiceFactory.java
3 * Copyright (C) 2001 New Zealand Digital Library Project
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20// the package we're in
21package org.nzdl.gsdl.util;
22
23// java libraries we're using
24import java.io.BufferedWriter;
25import java.io.BufferedReader;
26import java.io.DataInputStream;
27import java.io.File;
28import java.io.FileInputStream;
29import java.io.FileOutputStream;
30import java.io.FileReader;
31import java.io.FileWriter;
32import java.io.IOException;
33import java.io.InputStream;
34import java.io.InputStreamReader;
35import java.io.LineNumberReader;
36import java.io.Serializable;
37import java.net.URL;
38import java.util.Enumeration;
39import java.util.Iterator;
40import java.util.List;
41import java.util.ListIterator;
42import java.util.Map;
43import java.util.Properties;
44import java.util.Set;
45import java.util.Vector;
46import java.util.Hashtable;
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;
56import org.nzdl.gsdl.service.NzdlCacheWrapper;
57import org.nzdl.gsdl.service.NzdlLogWrapper;
58import org.nzdl.gsdl.service.NzdlDocSaveWrapper;
59import org.nzdl.gsdl.service.NzdlMultiWayWrapper;
60
61/**
62 * Class NzdlServiceFactory
63 *
64 * A repositry for IORs of various NZDL servers we've seen.
65 * Finds IORs from sources such as: URL, file, string, or server based
66 * list of known IORs.
67 *
68 * @author stuart yeates ([email protected])
69 * @author Brett Sheeran ([email protected])
70 * @author Dave Nichols ([email protected])
71 * @author Gordon Paynter ([email protected])
72 * @version $Revision: 2183 $
73 * @see org.nzdl.gsdl.SimpleServer
74 * @see org.nzdl.gsdl.service.NzdlCollectionInfo
75 * @see org.nzdl.gsdl.service.NzdlQuery
76 * @see org.nzdl.gsdl.service.NzdlRequest
77 * @see org.nzdl.gsdl.service.NzdlResponse
78 * @see org.nzdl.gsdl.service.NzdlResultSet
79 * @see org.nzdl.gsdl.service.NzdlService
80 * @see org.nzdl.gsdl.service.NzdlServiceClient
81 * @see gnu.getopt.Getopt
82 * @see gnu.getopt.LongOpt
83 * @see <a href="http://www.nzdl.org/">The New Zealand Digital Library Project</a>
84 * @see <a href="http://www.nzdl.org/fast-cgi-bin/library?&a=p&p=gsdl">Greenstone Digital Library Software</a>
85 */
86
87
88public class NzdlServiceFactory implements Cloneable, Serializable {
89
90 /** The collection of CORBA IORs we know about */
91 protected static Properties knownIORs = null;
92 /** The name of the properties file */
93 static String propertiesFileName = "knownIORs.properties";
94 /** The name of the file we look for IOR's in */
95 public static String IORFileName = "/tmp/localcorba.objid";
96
97 /** default places wit look for IORs ... */
98 static String[] urlsCompiledIn =
99 {
100 "http://nikau.cs.waikato.ac.nz/~say1/gsdl/cgi-bin/getior",
101 "http://www.nzdl.org/cgi-bin/getior"
102 };
103
104 /** Are we using a caching NzdlService ? */
105 static public boolean caching = true;
106 /** Are we logging before the cache ? */
107 static public boolean logBefore = true;
108 /** Are we logging after the cache ? */
109 static public boolean logAfter = true;
110 /** Are we saving documents ? */
111 static public boolean saveDocs = false;
112 /** Are we trying for multiple servers ? */
113 static public boolean doMultiple = false;
114 /** Are we logging after individual servers ? */
115 static public boolean logMultiple = false;
116
117 /**
118 * Get the first known-good IOR
119 * @see java.lang.String
120 * @return the string containing the IOR
121 */
122 public static String getFirstValid() {
123 validate(); // implies checkLoaded();
124 synchronized (knownIORs) {
125
126 Enumeration e = knownIORs.keys();
127 while (e.hasMoreElements()) {
128 String IOR = (String) e.nextElement();
129 if (attemptToInitialise(null, null, IOR) != null) {
130 return IOR;
131 }
132 }
133 return null;
134 }
135 }
136 /**
137 * Get the all known-good IOR
138 * @see java.lang.String
139 * @return the string[] containing the IORs
140 */
141 public static String[] getAllValid() {
142 checkLoaded();
143 Vector vec = new Vector();
144
145 synchronized (knownIORs) {
146
147 Enumeration e = knownIORs.keys();
148 while (e.hasMoreElements()) {
149 String IOR = (String) e.nextElement();
150 if (attemptToInitialise(null, null, IOR) != null) {
151 vec.add(IOR);
152 }
153 }
154 }
155 String[] strings = new String[vec.size()];
156 Enumeration e = vec.elements();
157 int i = 0;
158 while (e.hasMoreElements()) {
159 strings[i++] = (String) e.nextElement();
160 }
161 return strings;
162
163 }
164
165 /**
166 * Check that the class has been correctly initialised
167 */
168 static protected void checkLoaded() {
169 // if we're already initialised we can bail now and
170 // save synchronization overhead ...
171 if (knownIORs != null)
172 return;
173
174 Properties blarg = new Properties();
175
176 // take a lock on blarg. currently it's local, but
177 // won't be soon...
178 synchronized (blarg) {
179
180 try {
181 synchronized (Class.forName("org.nzdl.gsdl.util.NzdlServiceFactory")) {
182 if (knownIORs != null)
183 return;
184 knownIORs = blarg;
185 }
186 } catch (ClassNotFoundException exception) {
187 System.err.println("Exception locking class:" + exception);
188 }
189
190 try {
191 File file = new File(propertiesFileName);
192 if (file.exists()) {
193 knownIORs.load(new FileInputStream(file));
194 }
195 } catch (IOException exception) {
196 System.err.println("Exception reading properties file:" + exception);
197 }
198 }
199 getIORsfromFile(IORFileName);
200 }
201
202 /**
203 * Save the IORs back to the underlying file
204 * @see java.io.File
205 */
206 static protected void checkSave() {
207 synchronized (knownIORs) {
208 if (knownIORs == null)
209 throw new Error("saving null property list");
210
211 File file = new File(propertiesFileName);
212 try {
213 knownIORs.store(new FileOutputStream(file),
214 "NZDL IOR's see: org.nzdl.gsdl.util.NzdlServiceFactory");
215 } catch (IOException i) {
216 System.err.println("Error reading properties file:" + i);
217 }
218 }
219 }
220
221 /**
222 * Find a valid IOR to initialise from.
223 * @param _args the command line arguments
224 * @param _URL a URL to look for an IOR in
225 * @param _filename a local filename to look for an IOR in
226 * @param _IOR a string to look for an IOR in
227 * @see org.omg.CORBA.ORB
228 */
229 public static NzdlService findIOR(String [] _args,
230 Properties _props,
231 String _URL,
232 String _filename,
233 String _IOR) {
234
235 // read our prevously seed IORs ...
236 String IOR = null;
237 NzdlService client = null;
238
239 // try a URL from the command line
240 if ( _URL != null) {
241 String[] IORs = NzdlServiceFactory.getIORsfromURL(_URL);
242 for (int i=0;( i<IORs.length && client == null );i++) {
243 client = attemptToInitialise(_args, _props,IORs[i]);
244 }
245 }
246
247 // try an IOR from the command line
248 if (_IOR != null && client == null) {
249 IOR = NzdlServiceFactory.registerIOR(_IOR);
250 client = attemptToInitialise(_args, _props, IOR);
251 }
252
253 // try an IOR from a file (the filename may have been
254 // given from the commandline
255 if (client == null) {
256 String[] IORs = NzdlServiceFactory.getIORsfromFile(_filename);
257 for (int i=0;( i<IORs.length && client == null );i++) {
258 client = attemptToInitialise(_args, _props,IORs[i]);
259 }
260 }
261
262 // try the compiled-in URLs
263 if (client == null) {
264 for (int i=0;( i<urlsCompiledIn.length && client == null );i++) {
265 String[] IORs = NzdlServiceFactory.getIORsfromURL(urlsCompiledIn[i]);
266 for (int j=0;( j<IORs.length && client == null );j++) {
267 client = attemptToInitialise(_args, _props,IORs[j]);
268 }
269 }
270 }
271 // the knownIORs is the last resort
272 if (client == null) {
273 IOR = NzdlServiceFactory.getFirstValid();
274 client = attemptToInitialise(_args, _props, IOR);
275 }
276 return client;
277
278 }
279 /**
280 * Connect for as many servers as possible
281 * @param _args the command line arguments
282 * @param _URL a URL to look for an IOR in
283 * @param _filename a local filename to look for an IOR in
284 * @param _IOR a string to look for an IOR in
285 * @see org.omg.CORBA.ORB
286 */
287 public static Hashtable findIORs(String [] _args,
288 Properties _props,
289 String _URL,
290 String _filename,
291 String _IOR) {
292 // the object we're filling ...
293 Hashtable result = new Hashtable();
294
295 // read our prevously seed IORs ...
296 String IOR = null;
297 NzdlService client = null;
298
299
300 // try a URL from the command line
301 if ( _URL != null) {
302 String[] IORs = NzdlServiceFactory.getIORsfromURL(_URL);
303 for (int i=0;( i<IORs.length && client == null );i++) {
304 client = attemptToInitialise(_args, _props,IORs[i]);
305 if (client != null)
306 result.put(IORs[i],client);
307 }
308 }
309
310 System.err.println("1");
311 // try an IOR from the command line
312 if (_IOR != null) {
313 IOR = NzdlServiceFactory.registerIOR(_IOR);
314 client = attemptToInitialise(_args, _props, IOR);
315 if (client != null)
316 result.put(IOR,client);
317 }
318
319 System.err.println("2");
320 // try an IOR from a file (the filename may have been
321 // given from the commandline
322 {
323 String[] IORs = NzdlServiceFactory.getIORsfromFile(_filename);
324 for (int i=0;( i<IORs.length);i++) {
325 client = attemptToInitialise(_args, _props,IORs[i]);
326 if (client != null)
327 result.put(IORs[i],client);
328 }
329 }
330
331 System.err.println("3");
332 // try the compiled-in URLs
333 for (int i=0;(urlsCompiledIn != null && i<urlsCompiledIn.length);i++) {
334 String[] IORs = NzdlServiceFactory.getIORsfromURL(urlsCompiledIn[i]);
335 for (int j=0;( IORs != null && j<IORs.length);j++) {
336 client = attemptToInitialise(_args, _props, IORs[j]);
337 if (client != null)
338 result.put(IORs[j],client);
339 }
340 }
341
342 System.err.println("4");
343 // the knownIORs is the last resort
344 IOR = NzdlServiceFactory.getFirstValid();
345 client = attemptToInitialise(_args, _props, IOR);
346 if (client != null)
347 result.put(IOR,client);
348 return result;
349 }
350
351 /**
352 * Connect to each of the ORB's described by the IORs and throw away
353 * any that can't be connected to.
354 * @see org.omg.CORBA.ORB
355 */
356 public static void validate() {
357 checkLoaded();
358 synchronized (knownIORs) {
359
360 Properties valid = new Properties();
361
362 if (knownIORs != null)
363 throw new Error("saving null property list");
364
365 Enumeration e = knownIORs.keys();
366 while (e.hasMoreElements()) {
367 String IOR = (String) e.nextElement();
368 if (attemptToInitialise(null, null, IOR) != null) {
369 valid.put(IOR,"");
370 }
371 }
372 try {
373 synchronized (Class.forName("org.nzdl.gsdl.util.NzdlServiceFactory")) {
374 knownIORs = valid;
375 }
376 } catch (ClassNotFoundException exception) {
377 System.err.println("Exception locking class:" + exception);
378 }
379 checkSave();
380 }
381 }
382
383 /**
384 * Register a given IOR
385 * @see java.lang.String
386 * @param str the IOR
387 * @return the str
388 */
389 public static String registerIOR(String str)
390 {
391 checkLoaded();
392 if (str != null)
393 synchronized (knownIORs) {
394 knownIORs.put(str,"");
395 }
396 checkSave();
397 return str;
398 }
399
400
401 /**
402 * Retrieve an IOR from a URL
403 * @see java.net.URL
404 * @param str the url to retrieve
405 * @return the IOR
406 */
407 public static String[] getIORsfromURL(String str)
408 {
409 checkLoaded();
410 String ior = null;
411
412 try {
413 System.err.println("Looking for a URL at:" + str);
414 URL url = new URL(str);
415 InputStream his = url.openStream();
416
417 BufferedReader rdr = new BufferedReader (new InputStreamReader(his)) ;
418 return readIORsfromReader(rdr);
419 } catch (Throwable throwable) {
420 System.err.println("NzdlHosts::getIOR() unable to construct or read URL \"" +
421 str + "\", \"" + ior + "\": " + throwable);
422 return null;
423 }
424 }
425
426 public static String[] readIORsfromReader(BufferedReader rdr) {
427 Vector vec = new Vector();
428
429 try {
430 String ior = rdr.readLine();
431 while (ior != null) {
432 vec.add(ior);
433 synchronized (knownIORs) {
434 knownIORs.put(ior,"");
435 }
436 ior = rdr.readLine();
437 }
438 } catch (Throwable throwable) {
439 System.err.println("Throwable: " + throwable);
440 }
441 checkSave();
442 String[] strings = new String[vec.size()];
443 for (int i = 0; i < vec.size(); i++)
444 strings[i] = (String)vec.elementAt(i);
445 return strings;
446 }
447
448 /**
449 * Retrieve an IOR from a file
450 * @see java.io.File
451 * @param str the file to open
452 * @return the IOR
453 */
454 public static String[] getIORsfromFile(String file) {
455 checkLoaded();
456 String ior = null;
457 if (file == null || file.equals(""))
458 file = IORFileName;
459 file = file.trim();
460 try {
461 BufferedReader input
462 = new BufferedReader(new FileReader(file));
463 return readIORsfromReader(input);
464 } catch (java.io.IOException e) {
465 System.err.println("Error reading IOR file:\n" + e);
466 }
467 return null;
468 } // end of getIorKey
469
470 /**
471 * Write all IORs to a flat file (for reading from c++ etc).
472 *
473 * Not used for the properties files.
474 *
475 * @see java.util.Properties
476 * @param file the file to write to
477 */
478 public static void writeToFile(String file) {
479 checkLoaded();
480 if (file != null)
481 synchronized (knownIORs) {
482 String ior = null;
483 file = file.trim();
484 try {
485 BufferedWriter output
486 = new BufferedWriter(new FileWriter(file));
487 Enumeration e = knownIORs.keys();
488 while (e.hasMoreElements()) {
489 String IOR = (String) e.nextElement();
490 output.write(IOR);
491 output.newLine();
492 }
493 } catch (java.io.IOException e) {
494 System.err.println("Error reading IOR file:\n" + e);
495 }
496 }
497 } // end of getIorKey
498
499
500 /**
501 * Attempt to connect to the object pointed to by an IOR
502 * @see org.omg.CORBA.ORB
503 * @param _args the command line arguments
504 * @param _props the system properties
505 * @param _IOR the IOR string
506 * @return the newly created NzdlServiceClient
507 */
508 static public NzdlService attemptToInitialise(String [] _args,
509 Properties _props,
510 String _IOR) {
511 checkLoaded();
512 NzdlService nzdl = null;
513 if (_props == null)
514 _props = System.getProperties();
515 try {
516 nzdl = new NzdlServiceClient( _args, _props, _IOR);
517 } catch (Throwable t) {
518 System.err.println ("failed to initialise using:");
519 System.err.println (_IOR);
520 return null;
521 }
522 return nzdl;
523 }
524
525 static public NzdlService createNzdlService(String [] _args,
526 Properties _props,
527 String _URL,
528 String _filename,
529 String _IOR) {
530 if (_props == null)
531 _props = System.getProperties();
532
533 NzdlService nzdl = null;
534
535 if (doMultiple)
536 nzdl = new NzdlMultiWayWrapper(_args,_props,_URL,_filename,_IOR);
537 else
538 nzdl = findIOR(_args,_props,_URL,_filename,_IOR);
539
540 if (saveDocs)
541 nzdl = new NzdlDocSaveWrapper(nzdl);
542
543 if (logAfter)
544 nzdl = new NzdlLogWrapper(nzdl, "log.file.after.cache", "AFTER: ");
545
546 if (caching)
547 nzdl = new NzdlCacheWrapper(nzdl);
548
549 if (logBefore)
550 nzdl = new NzdlLogWrapper(nzdl, "log.file.before.cache", "BEFORE: ");
551
552 return nzdl;
553 }
554
555 /**
556 * Write the System properties to a file called "System.properties" in
557 * the current directory.
558 */
559 public static final void main( String [] args ) {
560
561 try {
562 File file = new File("System.properties");
563
564 System.getProperties().store(new FileOutputStream(file),"");
565 } catch (Exception exception) {
566 System.err.println("Error locking class:" + exception);
567 }
568 } //main
569} // class
Note: See TracBrowser for help on using the repository browser.