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

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

fixed properties problem. saving IORs and properties in the users home directory. ChangeLog

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