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

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

renamed SimpleServer.java Proxy.java. Fixed bug in NzdlServiceFactory.java. Fixed NzdlMultiWayWrapper.java so i only found a single instance of each collection

  • Property svn:keywords set to Author Date Id Revision
File size: 17.5 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: 2269 $
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
105 /**
106 * Get the first known-good IOR
107 * @see java.lang.String
108 * @return the string containing the IOR
109 */
110 public static String getFirstValid() {
111 validate(); // implies checkLoaded();
112 try {
113 synchronized (knownIORs) {
114
115 Enumeration e = knownIORs.keys();
116 while (e.hasMoreElements()) {
117 String IOR = (String) e.nextElement();
118 if (attemptToInitialise(null, null, IOR) != null) {
119 return IOR;
120 }
121 }
122 }
123 } catch (Throwable t) {
124 System.out.println("Throwable in NzdlServiceFactory::getFirstValid()");
125 }
126 return null;
127 }
128 /**
129 * Get the all known-good IOR
130 * @see java.lang.String
131 * @return the string[] containing the IORs
132 */
133 public static String[] getAllValid() {
134 checkLoaded();
135 Vector vec = new Vector();
136
137 synchronized (knownIORs) {
138
139 Enumeration e = knownIORs.keys();
140 while (e.hasMoreElements()) {
141 String IOR = (String) e.nextElement();
142 if (attemptToInitialise(null, null, IOR) != null) {
143 vec.add(IOR);
144 }
145 }
146 }
147 String[] strings = new String[vec.size()];
148 Enumeration e = vec.elements();
149 int i = 0;
150 while (e.hasMoreElements()) {
151 strings[i++] = (String) e.nextElement();
152 }
153 return strings;
154
155 }
156
157 /**
158 * Check that the class has been correctly initialised
159 */
160 static protected void checkLoaded() {
161 // if we're already initialised we can bail now and
162 // save synchronization overhead ...
163 if (knownIORs != null)
164 return;
165
166 Properties blarg = new Properties();
167
168 // take a lock on blarg. currently it's local, but
169 // won't be soon...
170 synchronized (blarg) {
171
172 try {
173 synchronized (Class.forName("org.nzdl.gsdl.util.NzdlServiceFactory")) {
174 if (knownIORs != null)
175 return;
176 knownIORs = blarg;
177 }
178 } catch (ClassNotFoundException exception) {
179 System.err.println("Exception locking class:" + exception);
180 }
181
182 try {
183 File file = new File(propertiesFileName);
184 if (file.exists()) {
185 knownIORs.load(new FileInputStream(file));
186 }
187 } catch (IOException exception) {
188 System.err.println("Exception reading properties file:" + exception);
189 }
190 }
191 getIORsfromFile(IORFileName);
192 }
193
194 /**
195 * Save the IORs back to the underlying file
196 * @see java.io.File
197 */
198 static protected void checkSave() {
199 synchronized (knownIORs) {
200 if (knownIORs == null)
201 throw new Error("saving null property list");
202
203 File file = new File(propertiesFileName);
204 try {
205 knownIORs.store(new FileOutputStream(file),
206 "NZDL IOR's see: org.nzdl.gsdl.util.NzdlServiceFactory");
207 } catch (IOException i) {
208 System.err.println("Error reading properties file:" + i);
209 }
210 }
211 }
212
213 /**
214 * Find a valid IOR to initialise from.
215 * @param _args the command line arguments
216 * @param _URL a URL to look for an IOR in
217 * @param _filename a local filename to look for an IOR in
218 * @param _IOR a string to look for an IOR in
219 * @see org.omg.CORBA.ORB
220 */
221 public static NzdlService findIOR(String [] _args,
222 Properties _props,
223 String _URL,
224 String _filename,
225 String _IOR) {
226
227 // read our prevously seed IORs ...
228 String IOR = null;
229 NzdlService client = null;
230
231 // try a URL from the command line
232 if ( _URL != null) {
233 String[] IORs = NzdlServiceFactory.getIORsfromURL(_URL);
234 for (int i=0;( i<IORs.length && client == null );i++) {
235 client = attemptToInitialise(_args, _props,IORs[i]);
236 }
237 }
238
239 // try an IOR from the command line
240 if (_IOR != null && client == null) {
241 IOR = NzdlServiceFactory.registerIOR(_IOR);
242 client = attemptToInitialise(_args, _props, IOR);
243 }
244
245 // try an IOR from a file (the filename may have been
246 // given from the commandline
247 if (client == null) {
248 String[] IORs = NzdlServiceFactory.getIORsfromFile(_filename);
249 for (int i=0;(IORs != null && i<IORs.length && client == null );i++) {
250 client = attemptToInitialise(_args, _props,IORs[i]);
251 }
252 }
253
254 // try the compiled-in URLs
255 if (client == null && urlsCompiledIn != null) {
256 for (int i=0;( i<urlsCompiledIn.length && client == null );i++) {
257 String[] IORs = NzdlServiceFactory.getIORsfromURL(urlsCompiledIn[i]);
258 for (int j=0;(IORs != null && j<IORs.length && client == null );j++) {
259 client = attemptToInitialise(_args, _props,IORs[j]);
260 }
261 }
262 }
263 // the knownIORs is the last resort
264 if (client == null) {
265 IOR = NzdlServiceFactory.getFirstValid();
266 client = attemptToInitialise(_args, _props, IOR);
267 }
268 return client;
269
270 }
271 /**
272 * Connect for as many servers as possible
273 * @param _args the command line arguments
274 * @param _URL a URL to look for an IOR in
275 * @param _filename a local filename to look for an IOR in
276 * @param _IOR a string to look for an IOR in
277 * @see org.omg.CORBA.ORB
278 */
279 public static Hashtable findIORs(String [] _args,
280 Properties _props,
281 String _URL,
282 String _filename,
283 String _IOR) {
284 // the object we're filling ...
285 Hashtable result = new Hashtable();
286
287 // read our prevously seed IORs ...
288 String IOR = null;
289 NzdlService client = null;
290
291
292 // try a URL from the command line
293 if ( _URL != null) {
294 String[] IORs = NzdlServiceFactory.getIORsfromURL(_URL);
295 for (int i=0;( i<IORs.length && client == null );i++) {
296 client = attemptToInitialise(_args, _props,IORs[i]);
297 if (client != null)
298 result.put(IORs[i],client);
299 }
300 }
301
302 // try an IOR from the command line
303 if (_IOR != null) {
304 IOR = NzdlServiceFactory.registerIOR(_IOR);
305 client = attemptToInitialise(_args, _props, IOR);
306 if (client != null)
307 result.put(IOR,client);
308 }
309
310 // try an IOR from a file (the filename may have been
311 // given from the commandline
312 {
313 String[] IORs = NzdlServiceFactory.getIORsfromFile(_filename);
314 for (int i=0;( i<IORs.length);i++) {
315 client = attemptToInitialise(_args, _props,IORs[i]);
316 if (client != null)
317 result.put(IORs[i],client);
318 }
319 }
320
321 // try the compiled-in URLs
322 for (int i=0;(urlsCompiledIn != null && i<urlsCompiledIn.length);i++) {
323 String[] IORs = NzdlServiceFactory.getIORsfromURL(urlsCompiledIn[i]);
324 for (int j=0;( IORs != null && j<IORs.length);j++) {
325 client = attemptToInitialise(_args, _props, IORs[j]);
326 if (client != null)
327 result.put(IORs[j],client);
328 }
329 }
330
331 // the knownIORs is the last resort
332 IOR = NzdlServiceFactory.getFirstValid();
333 client = attemptToInitialise(_args, _props, IOR);
334 if (client != null)
335 result.put(IOR,client);
336 return result;
337 }
338
339 /**
340 * Connect to each of the ORB's described by the IORs and throw away
341 * any that can't be connected to.
342 * @see org.omg.CORBA.ORB
343 */
344 public static void validate() {
345 checkLoaded();
346 synchronized (knownIORs) {
347
348 Properties valid = new Properties();
349
350 if (knownIORs == null)
351 throw new Error("saving null property list");
352
353 Enumeration e = knownIORs.keys();
354 while (e.hasMoreElements()) {
355 String IOR = (String) e.nextElement();
356 if (attemptToInitialise(null, null, IOR) != null) {
357 valid.put(IOR,"");
358 }
359 }
360 try {
361 synchronized (Class.forName("org.nzdl.gsdl.util.NzdlServiceFactory")) {
362 knownIORs = valid;
363 }
364 } catch (ClassNotFoundException exception) {
365 System.err.println("Exception locking class:" + exception);
366 }
367 checkSave();
368 }
369 }
370
371 /**
372 * Register a given IOR
373 * @see java.lang.String
374 * @param str the IOR
375 * @return the str
376 */
377 public static String registerIOR(String str)
378 {
379 checkLoaded();
380 if (str != null)
381 synchronized (knownIORs) {
382 knownIORs.put(str,"");
383 }
384 checkSave();
385 return str;
386 }
387
388
389 /**
390 * Retrieve an IOR from a URL
391 * @see java.net.URL
392 * @param str the url to retrieve
393 * @return the IOR
394 */
395 public static String[] getIORsfromURL(String str)
396 {
397 checkLoaded();
398 String ior = null;
399
400 try {
401 System.err.println("Requesting IORs from: " + str);
402 URL url = new URL(str);
403 InputStream his = url.openStream();
404
405 BufferedReader rdr = new BufferedReader (new InputStreamReader(his)) ;
406 return readIORsfromReader(rdr);
407 } catch (Throwable throwable) {
408 System.err.println("NzdlHosts::getIOR() unable to construct or read URL \"" +
409 str + "\", \"" + ior + "\": " + throwable);
410 return new String[0];
411 }
412 }
413
414 public static String[] readIORsfromReader(BufferedReader rdr) {
415 Vector vec = new Vector();
416
417 try {
418 String ior = rdr.readLine();
419 while (ior != null) {
420 vec.add(ior);
421 synchronized (knownIORs) {
422 knownIORs.put(ior,"");
423 }
424 ior = rdr.readLine();
425 }
426 } catch (Throwable throwable) {
427 System.err.println("Throwable: " + throwable);
428 }
429 checkSave();
430 String[] strings = new String[vec.size()];
431 for (int i = 0; i < vec.size(); i++)
432 strings[i] = (String)vec.elementAt(i);
433 return strings;
434 }
435
436 /**
437 * Retrieve an IOR from a file
438 * @see java.io.File
439 * @param str the file to open
440 * @return the IOR
441 */
442 public static String[] getIORsfromFile(String file) {
443 checkLoaded();
444 String ior = null;
445 if (file == null || file.equals(""))
446 file = IORFileName;
447 file = file.trim();
448 try {
449 BufferedReader input
450 = new BufferedReader(new FileReader(file));
451 return readIORsfromReader(input);
452 } catch (java.io.FileNotFoundException e) {
453 // System.err.println("Error reading IOR file:\n" + e);
454 } catch (java.io.IOException e) {
455 System.err.println("Error reading IOR file:\n" + e);
456 }
457 return new String[0];
458 }
459
460 /**
461 * Write all IORs to a flat file (for reading from c++ etc).
462 *
463 * Not used for the properties files.
464 *
465 * @see java.util.Properties
466 * @param file the file to write to
467 */
468 public static void writeToFile(String file) {
469 checkLoaded();
470 if (file != null)
471 synchronized (knownIORs) {
472 String ior = null;
473 file = file.trim();
474 try {
475 BufferedWriter output
476 = new BufferedWriter(new FileWriter(file));
477 Enumeration e = knownIORs.keys();
478 while (e.hasMoreElements()) {
479 String IOR = (String) e.nextElement();
480 output.write(IOR);
481 output.newLine();
482 }
483 } catch (java.io.IOException e) {
484 System.err.println("Error reading IOR file:\n" + e);
485 }
486 }
487 } // end of getIorKey
488
489
490 /**
491 * Attempt to connect to the object pointed to by an IOR
492 * @see org.omg.CORBA.ORB
493 * @param _args the command line arguments
494 * @param _props the system properties
495 * @param _IOR the IOR string
496 * @return the newly created NzdlServiceClient
497 */
498 static public NzdlService attemptToInitialise(String [] _args,
499 Properties _props,
500 String _IOR) {
501 checkLoaded();
502 NzdlService nzdl = null;
503 if (_props == null)
504 _props = System.getProperties();
505 try {
506 nzdl = new NzdlServiceClient( _args, _props, _IOR);
507 } catch (Throwable t) {
508 System.err.println ("failed to initialise using:");
509 System.err.println (_IOR);
510 return null;
511 }
512 return nzdl;
513 }
514
515 static public NzdlService createNzdlService(String [] _args,
516 Properties _props,
517 String _URL,
518 String _filename,
519 String _IOR) {
520 if (_props == null)
521 _props = System.getProperties();
522
523 NzdlService nzdl = null;
524
525 if (NzdlPreferences.getBoolean("Connect to multiple servers"))
526 nzdl = new NzdlMultiWayWrapper(_args,_props,_URL,_filename,_IOR);
527 else
528 nzdl = findIOR(_args,_props,_URL,_filename,_IOR);
529
530 if (NzdlPreferences.getBoolean("Save documents"))
531 nzdl = new NzdlDocSaveWrapper(nzdl);
532
533 if (NzdlPreferences.getBoolean("Log at the NzdlService level"))
534 nzdl = new NzdlLogWrapper(nzdl, "log.file.after.cache", "AFTER: ");
535
536 if (NzdlPreferences.getBoolean("Cache corba requests"))
537 nzdl = new NzdlCacheWrapper(nzdl);
538
539 if (NzdlPreferences.getBoolean("Log at the NzdlService level"))
540 nzdl = new NzdlLogWrapper(nzdl, "log.file.before.cache", "BEFORE: ");
541
542 return nzdl;
543 }
544 static public NzdlService createNzdlService(String _IOR) {
545 Properties _props = System.getProperties();
546
547 NzdlService nzdl = null;
548 String IOR = null;
549
550 IOR = NzdlServiceFactory.registerIOR(_IOR);
551 nzdl = attemptToInitialise(null, _props, IOR);
552
553 if (nzdl == null) {
554 System.err.println("couldn't open the requested IOR: going for what we can ...");
555 nzdl = createNzdlService(null,null,null,null,null);
556 }
557
558
559 if (NzdlPreferences.getBoolean("Save documents"))
560 nzdl = new NzdlDocSaveWrapper(nzdl);
561
562 if (NzdlPreferences.getBoolean("Log at the NzdlService level"))
563 nzdl = new NzdlLogWrapper(nzdl, "log.file.after.cache", "AFTER: ");
564
565 if (NzdlPreferences.getBoolean("Cache corba requests"))
566 nzdl = new NzdlCacheWrapper(nzdl);
567
568 if (NzdlPreferences.getBoolean("Log at the NzdlService level"))
569 nzdl = new NzdlLogWrapper(nzdl, "log.file.before.cache", "BEFORE: ");
570
571 return nzdl;
572 }
573
574 /**
575 * Write the System properties to a file called "System.properties" in
576 * the current directory.
577 */
578 public static final void main( String [] args ) {
579
580 try {
581 File file = new File("System.properties");
582
583 System.getProperties().store(new FileOutputStream(file),"");
584 } catch (Exception exception) {
585 System.err.println("Error locking class:" + exception);
586 }
587 } //main
588} // class
Note: See TracBrowser for help on using the repository browser.