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
|
---|
21 | package org.nzdl.gsdl.util;
|
---|
22 |
|
---|
23 | // long live GNU ...
|
---|
24 | import gnu.getopt.Getopt;
|
---|
25 | import gnu.getopt.LongOpt;
|
---|
26 |
|
---|
27 | // java libraries we're using
|
---|
28 | import java.io.BufferedWriter;
|
---|
29 | import java.io.BufferedReader;
|
---|
30 | import java.io.DataInputStream;
|
---|
31 | import java.io.File;
|
---|
32 | import java.io.FileInputStream;
|
---|
33 | import java.io.FileOutputStream;
|
---|
34 | import java.io.FileReader;
|
---|
35 | import java.io.FileWriter;
|
---|
36 | import java.io.IOException;
|
---|
37 | import java.io.InputStream;
|
---|
38 | import java.io.InputStreamReader;
|
---|
39 | import java.io.LineNumberReader;
|
---|
40 | import java.io.Serializable;
|
---|
41 | import java.net.URL;
|
---|
42 | import java.util.Enumeration;
|
---|
43 | import java.util.Iterator;
|
---|
44 | import java.util.List;
|
---|
45 | import java.util.ListIterator;
|
---|
46 | import java.util.Map;
|
---|
47 | import java.util.Properties;
|
---|
48 | import java.util.Set;
|
---|
49 | import java.util.Vector;
|
---|
50 |
|
---|
51 | // local libraries
|
---|
52 | import org.nzdl.gsdl.service.NzdlCollectionInfo;
|
---|
53 | import org.nzdl.gsdl.service.NzdlQuery;
|
---|
54 | import org.nzdl.gsdl.service.NzdlRequest;
|
---|
55 | import org.nzdl.gsdl.service.NzdlResponse;
|
---|
56 | import org.nzdl.gsdl.service.NzdlResultSet;
|
---|
57 | import org.nzdl.gsdl.service.NzdlService;
|
---|
58 | import 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: 2141 $
|
---|
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 |
|
---|
84 | public 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("Exception locking class:" + exception);
|
---|
145 | }
|
---|
146 |
|
---|
147 |
|
---|
148 | try {
|
---|
149 | File file = new File(propertiesFileName);
|
---|
150 | if (file.exists()) {
|
---|
151 | knownIORs.load(new FileInputStream(file));
|
---|
152 | }
|
---|
153 | } catch (IOException exception) {
|
---|
154 | System.err.println("Exception 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("Exception 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 | if (str != null)
|
---|
272 | synchronized (knownIORs) {
|
---|
273 | knownIORs.put(str,"");
|
---|
274 | }
|
---|
275 | checkSave();
|
---|
276 | return str;
|
---|
277 | }
|
---|
278 |
|
---|
279 |
|
---|
280 | /**
|
---|
281 | * Retrieve an IOR from a URL
|
---|
282 | * @see java.net.URL
|
---|
283 | * @param str the url to retrieve
|
---|
284 | * @return the IOR
|
---|
285 | */
|
---|
286 | public static String getIORfromURL(String str)
|
---|
287 | {
|
---|
288 | checkLoaded();
|
---|
289 | String ior = null;
|
---|
290 |
|
---|
291 | try {
|
---|
292 | System.err.println("Looking for a URL at:" + str);
|
---|
293 | URL url = new URL(str);
|
---|
294 | InputStream his = url.openStream();
|
---|
295 | DataInputStream dhis = new DataInputStream(his) ;
|
---|
296 | ior = dhis.readLine();
|
---|
297 |
|
---|
298 | if (ior != null)
|
---|
299 | synchronized (knownIORs) {
|
---|
300 | knownIORs.put(ior,"");
|
---|
301 | }
|
---|
302 | checkSave();
|
---|
303 | return ior;
|
---|
304 | } catch (Throwable throwable) {
|
---|
305 | System.err.println("NzdlHosts::getIOR() unable to construct or read URL \"" +
|
---|
306 | str + "\", \"" + ior + "\": " + throwable);
|
---|
307 | return "";
|
---|
308 | }
|
---|
309 | }
|
---|
310 |
|
---|
311 | /**
|
---|
312 | * Retrieve an IOR from a file
|
---|
313 | * @see java.io.File
|
---|
314 | * @param str the file to open
|
---|
315 | * @return the IOR
|
---|
316 | */
|
---|
317 | public static String getIORfromFile(String file) {
|
---|
318 | checkLoaded();
|
---|
319 | String ior = null;
|
---|
320 | if (file == null || file.equals(""))
|
---|
321 | return "";
|
---|
322 | file = file.trim();
|
---|
323 | try {
|
---|
324 | BufferedReader input
|
---|
325 | = new BufferedReader(new FileReader(file));
|
---|
326 | ior = input.readLine();
|
---|
327 | } catch (java.io.IOException e) {
|
---|
328 | System.err.println("Error reading IOR file:\n" + e);
|
---|
329 | }
|
---|
330 | if (ior != null)
|
---|
331 | synchronized (knownIORs) {
|
---|
332 | knownIORs.put(ior,"");
|
---|
333 | }
|
---|
334 | checkSave();
|
---|
335 | return ior;
|
---|
336 | } // end of getIorKey
|
---|
337 |
|
---|
338 | /**
|
---|
339 | * Write all IORs to a flat file (for reading from c++ etc).
|
---|
340 | *
|
---|
341 | * Not used for the properties files.
|
---|
342 | *
|
---|
343 | * @see java.util.Properties
|
---|
344 | * @param file the file to write to
|
---|
345 | */
|
---|
346 | public static void writeToFile(String file) {
|
---|
347 | checkLoaded();
|
---|
348 | if (file != null)
|
---|
349 | synchronized (knownIORs) {
|
---|
350 | String ior = null;
|
---|
351 | file = file.trim();
|
---|
352 | try {
|
---|
353 | BufferedWriter output
|
---|
354 | = new BufferedWriter(new FileWriter(file));
|
---|
355 | Enumeration e = knownIORs.keys();
|
---|
356 | while (e.hasMoreElements()) {
|
---|
357 | String IOR = (String) e.nextElement();
|
---|
358 | output.write(IOR);
|
---|
359 | output.newLine();
|
---|
360 | }
|
---|
361 | } catch (java.io.IOException e) {
|
---|
362 | System.err.println("Error reading IOR file:\n" + e);
|
---|
363 | }
|
---|
364 | }
|
---|
365 | } // end of getIorKey
|
---|
366 |
|
---|
367 |
|
---|
368 | /**
|
---|
369 | * Attempt to connect to the object pointed to by an IOR
|
---|
370 | * @see org.omg.CORBA.ORB
|
---|
371 | * @param _args the command line arguments
|
---|
372 | * @param _props the system properties
|
---|
373 | * @param _IOR the IOR string
|
---|
374 | * @return the newly created NzdlServiceClient
|
---|
375 | */
|
---|
376 | static public NzdlServiceClient attemptToInitialise(String [] _args,
|
---|
377 | Properties _props,
|
---|
378 | String _IOR) {
|
---|
379 | checkLoaded();
|
---|
380 | NzdlServiceClient nzdl = null;
|
---|
381 | try {
|
---|
382 | nzdl = new NzdlServiceClient( _args, _props, _IOR);
|
---|
383 | } catch (Throwable t) {
|
---|
384 | System.err.println ("failed to initialise using:");
|
---|
385 | System.err.println (_IOR);
|
---|
386 | return null;
|
---|
387 | }
|
---|
388 | return nzdl;
|
---|
389 | }
|
---|
390 |
|
---|
391 | /**
|
---|
392 | * Write the System properties to a file called "System.properties" in
|
---|
393 | * the current directory.
|
---|
394 | */
|
---|
395 | public static final void main( String [] args ) {
|
---|
396 |
|
---|
397 | try {
|
---|
398 | File file = new File("System.properties");
|
---|
399 |
|
---|
400 | System.getProperties().store(new FileOutputStream(file),"");
|
---|
401 | } catch (Exception exception) {
|
---|
402 | System.err.println("Error locking class:" + exception);
|
---|
403 | }
|
---|
404 | } //main
|
---|
405 | } // class
|
---|