source: trunk/java-client/org/nzdl/gsdl/util/NzdlIORs.java@ 2140

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

moved init stuff to NzdlIORs. comments

  • Property svn:keywords set to Author Date Id Revision
File size: 11.3 KB
Line 
1/*
2 * NzdlIORs.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// long live GNU ...
24import gnu.getopt.Getopt;
25import gnu.getopt.LongOpt;
26
27// java libraries we're using
28import java.io.BufferedWriter;
29import java.io.BufferedReader;
30import java.io.DataInputStream;
31import java.io.File;
32import java.io.FileInputStream;
33import java.io.FileOutputStream;
34import java.io.FileReader;
35import java.io.FileWriter;
36import java.io.IOException;
37import java.io.InputStream;
38import java.io.InputStreamReader;
39import java.io.LineNumberReader;
40import java.io.Serializable;
41import java.net.URL;
42import java.util.Enumeration;
43import java.util.Iterator;
44import java.util.List;
45import java.util.ListIterator;
46import java.util.Map;
47import java.util.Properties;
48import java.util.Set;
49import java.util.Vector;
50
51// local libraries
52import org.nzdl.gsdl.service.NzdlCollectionInfo;
53import org.nzdl.gsdl.service.NzdlQuery;
54import org.nzdl.gsdl.service.NzdlRequest;
55import org.nzdl.gsdl.service.NzdlResponse;
56import org.nzdl.gsdl.service.NzdlResultSet;
57import org.nzdl.gsdl.service.NzdlService;
58import org.nzdl.gsdl.service.NzdlServiceClient;
59
60/**
61 * Class NzdlIORs
62 *
63 * A repositry for IORs of various NZDL servers we've seen.
64 *
65 * @author stuart yeates ([email protected])
66 * @author Brett Sheeran ([email protected])
67 * @author Dave Nichols ([email protected])
68 * @author Gordon Paynter ([email protected])
69 * @version $Revision: 2140 $
70 * @see org.nzdl.gsdl.SimpleServer
71 * @see org.nzdl.gsdl.service.NzdlCollectionInfo
72 * @see org.nzdl.gsdl.service.NzdlQuery
73 * @see org.nzdl.gsdl.service.NzdlRequest
74 * @see org.nzdl.gsdl.service.NzdlResponse
75 * @see org.nzdl.gsdl.service.NzdlResultSet
76 * @see org.nzdl.gsdl.service.NzdlService
77 * @see org.nzdl.gsdl.service.NzdlServiceClient
78 * @see gnu.getopt.Getopt
79 * @see gnu.getopt.LongOpt
80 * @see <a href="http://www.nzdl.org/">The New Zealand Digital Library Project</a>
81 * @see <a href="http://www.nzdl.org/fast-cgi-bin/library?&a=p&p=gsdl">Greenstone Digital Library Software</a>
82 */
83
84public class NzdlIORs implements Cloneable, Serializable {
85
86 /** The collection of CORBA IORs we know about */
87 protected static Properties knownIORs = null;
88 /** The name of the properties file */
89 static String propertiesFileName = "knownIORs.properties";
90 /** The name of the file we look for IOR's in */
91 public static String IORFileName = "/tmp/localcorba.objid";
92
93 /** default places wit look for IORs ... */
94 static String[] urlsCompiledIn =
95 {
96 "http://nikau.cs.waikato.ac.nz/~say1/gsdl/cgi-bin/getior",
97 "http://www.nzdl.org/cgi-bin/getior"
98 };
99
100
101
102 /**
103 * Get the first known-good IOR
104 * @see java.lang.String
105 * @return the string containing the IOR
106 */
107 public static String getFirstValid() {
108 checkLoaded();
109 synchronized (knownIORs) {
110
111 Enumeration e = knownIORs.keys();
112 while (e.hasMoreElements()) {
113 String IOR = (String) e.nextElement();
114 if (attemptToInitialise(null, null, IOR) != null) {
115 return IOR;
116 }
117 }
118 return null;
119 }
120 }
121
122 /**
123 * Check that the class has been correctly initialised
124 */
125 static protected void checkLoaded() {
126 // if we're already initialised we can bail now and
127 // save synchronization overhead ...
128 if (knownIORs != null)
129 return;
130
131 Properties blarg = new Properties();
132
133 // take a lock on blarg. currently it's local, but
134 // won't be soon...
135 synchronized (blarg) {
136
137 try {
138 synchronized (Class.forName("org.nzdl.gsdl.util.NzdlIORs")) {
139 if (knownIORs != null)
140 return;
141 knownIORs = blarg;
142 }
143 } catch (ClassNotFoundException exception) {
144 System.err.println("Error locking class:" + exception);
145 }
146
147
148 File file = new File(propertiesFileName);
149 try {
150 if (file.exists()) {
151 knownIORs.load(new FileInputStream(file));
152 }
153 } catch (IOException exception) {
154 System.err.println("Error reading properties file:" + exception);
155 }
156 }
157 getIORfromFile(IORFileName);
158 }
159
160 /**
161 * Save the IORs back to the underlying file
162 * @see java.io.File
163 */
164 static protected void checkSave() {
165 synchronized (knownIORs) {
166 if (knownIORs == null)
167 throw new Error("saving null property list");
168
169 File file = new File(propertiesFileName);
170 try {
171 knownIORs.store(new FileOutputStream(file),
172 "NZDL IOR's see: org.nzdl.gsdl.util.NzdlIORs");
173 } catch (IOException i) {
174 System.err.println("Error reading properties file:" + i);
175 }
176 }
177 }
178 /**
179 * Find a valid IOR to initialise from.
180 * @param _args the command line arguments
181 * @param _URL a URL to look for an IOR in
182 * @param _filename a local filename to look for an IOR in
183 * @param _IOR a string to look for an IOR in
184 * @see org.omg.CORBA.ORB
185 */
186 public static NzdlServiceClient findIOR(String [] _args,
187 String _URL,
188 String _filename,
189 String _IOR) {
190
191 // read our prevously seed IORs ...
192 String IOR = null;
193 NzdlServiceClient client = null;
194
195 // try a URL from the command line
196 if ( _URL != null) {
197 IOR = NzdlIORs.getIORfromURL(_URL);
198 client = attemptToInitialise(_args, null, IOR);
199 }
200
201 // try an IOR from the command line
202 if (_IOR != null && client == null) {
203 IOR = NzdlIORs.registerIOR(_IOR);
204 client = attemptToInitialise(_args, null, IOR);
205 }
206
207 // try an IOR from a file (the filename may have been
208 // given from teh commandline
209 if (client == null) {
210 IOR = NzdlIORs.getIORfromFile(_filename);
211 client = attemptToInitialise(_args, null, IOR);
212 }
213
214 // try the compiled-in URLs
215 if (client == null) {
216 for (int i=0;( i<urlsCompiledIn.length && client == null );i++) {
217 IOR = NzdlIORs.getIORfromURL(urlsCompiledIn[i]);
218 client = attemptToInitialise(_args, null,IOR);
219 }
220 }
221
222 // the knownIORs is the last resort
223 if (client == null) {
224 IOR = NzdlIORs.getFirstValid();
225 client = attemptToInitialise(_args, null, IOR);
226 }
227 return client;
228 }
229
230 /**
231 * Connect to each of the ORB's described by the IORs and throw away
232 * any that can't be connected to.
233 * @see org.omg.CORBA.ORB
234 */
235 public static void validate() {
236 checkLoaded();
237 synchronized (knownIORs) {
238
239 Properties valid = new Properties();
240
241 if (knownIORs != null)
242 throw new Error("saving null property list");
243
244 Enumeration e = knownIORs.keys();
245 while (e.hasMoreElements()) {
246 String IOR = (String) e.nextElement();
247 if (attemptToInitialise(null, null, IOR) != null) {
248 valid.put(IOR,"");
249 }
250 }
251 try {
252 synchronized (Class.forName("org.nzdl.gsdl.util.NzdlIORs")) {
253 knownIORs = valid;
254 }
255 } catch (ClassNotFoundException exception) {
256 System.err.println("Error locking class:" + exception);
257 }
258 checkSave();
259 }
260 }
261
262 /**
263 * Register a given IOR
264 * @see java.lang.String
265 * @param str the IOR
266 * @return the str
267 */
268 public static String registerIOR(String str)
269 {
270 checkLoaded();
271 synchronized (knownIORs) {
272 knownIORs.put(str,"");
273 }
274 checkSave();
275 return str;
276 }
277
278
279 /**
280 * Retrieve an IOR from a URL
281 * @see java.net.URL
282 * @param str the url to retrieve
283 * @return the IOR
284 */
285 public static String getIORfromURL(String str)
286 {
287 checkLoaded();
288 String ior = null;
289
290 try {
291 System.err.println("Looking for a URL at:" + str);
292 URL url = new URL(str);
293 InputStream his = url.openStream();
294 DataInputStream dhis = new DataInputStream(his) ;
295 ior = dhis.readLine();
296
297 synchronized (knownIORs) {
298 knownIORs.put(ior,"");
299 }
300 checkSave();
301 return ior;
302 } catch (Throwable throwable) {
303 System.err.println("NzdlHosts::getIOR() unable to construct or read URL \"" +
304 str + "\", \"" + ior + "\": " + throwable);
305 return "";
306 }
307 }
308
309 /**
310 * Retrieve an IOR from a file
311 * @see java.io.File
312 * @param str the file to open
313 * @return the IOR
314 */
315 public static String getIORfromFile(String file) {
316 checkLoaded();
317 String ior = null;
318 if (file == null || file.equals(""))
319 return "";
320 file = file.trim();
321 try {
322 BufferedReader input
323 = new BufferedReader(new FileReader(file));
324 ior = input.readLine();
325 } catch (java.io.IOException e) {
326 System.err.println("Error reading IOR file:\n" + e);
327 }
328 synchronized (knownIORs) {
329 knownIORs.put(ior,"");
330 }
331 checkSave();
332 return ior;
333 } // end of getIorKey
334
335 /**
336 * Write all IORs to a flat file (for reading from c++ etc).
337 *
338 * Not used for the properties files.
339 *
340 * @see java.util.Properties
341 * @param file the file to write to
342 */
343 public static void writeToFile(String file) {
344 checkLoaded();
345 synchronized (knownIORs) {
346 String ior = null;
347 file = file.trim();
348 try {
349 BufferedWriter output
350 = new BufferedWriter(new FileWriter(file));
351 Enumeration e = knownIORs.keys();
352 while (e.hasMoreElements()) {
353 String IOR = (String) e.nextElement();
354 output.write(IOR);
355 output.newLine();
356 }
357 } catch (java.io.IOException e) {
358 System.err.println("Error reading IOR file:\n" + e);
359 }
360 }
361 } // end of getIorKey
362
363
364 /**
365 * Attempt to connect to the object pointed to by an IOR
366 * @see org.omg.CORBA.ORB
367 * @param _args the command line arguments
368 * @param _props the system properties
369 * @param _IOR the IOR string
370 * @return the newly created NzdlServiceClient
371 */
372 static public NzdlServiceClient attemptToInitialise(String [] _args,
373 Properties _props,
374 String _IOR) {
375 checkLoaded();
376 NzdlServiceClient nzdl = null;
377 try {
378 nzdl = new NzdlServiceClient( _args, _props, _IOR);
379 } catch (Throwable t) {
380 System.err.println ("failed to initialise using:");
381 System.err.println (_IOR);
382 return null;
383 }
384 return nzdl;
385 }
386
387 /**
388 * Write the System properties to a file called "System.properties" in
389 * the current directory.
390 */
391 public static final void main( String [] args ) {
392
393 try {
394 File file = new File("System.properties");
395
396 System.getProperties().store(new FileOutputStream(file),"");
397 } catch (Exception exception) {
398 System.err.println("Error locking class:" + exception);
399 }
400 } //main
401} // class
Note: See TracBrowser for help on using the repository browser.