1 | /*
|
---|
2 | * TODO.java
|
---|
3 | * Copyright (C) 2000 Stuart Yeates
|
---|
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 | // java standard library classes used
|
---|
24 | import java.io.IOException;
|
---|
25 | import java.io.Serializable;
|
---|
26 | import java.lang.Cloneable;
|
---|
27 | import java.util.Properties;
|
---|
28 | import java.util.Vector;
|
---|
29 | import java.util.Enumeration;
|
---|
30 | import java.io.File;
|
---|
31 | import java.io.FileInputStream;
|
---|
32 | import java.io.FileOutputStream;
|
---|
33 | import java.io.DataInputStream;
|
---|
34 | import java.io.InputStream;
|
---|
35 | import org.omg.CORBA.Object;
|
---|
36 | import org.omg.CORBA.ORB;
|
---|
37 | import java.net.URL;
|
---|
38 |
|
---|
39 | import org.nzdl.gsdl.service.*;
|
---|
40 |
|
---|
41 | /**
|
---|
42 | * Used to get host information including: name, IOR and URL.
|
---|
43 | *
|
---|
44 | * @author stuart yeates ([email protected])
|
---|
45 | * @author George Buchanan ([email protected])
|
---|
46 | * @version $Revision: 2924 $
|
---|
47 | * @see org.nzdl.gsdl.SimpleClient
|
---|
48 | * @see org.nzdl.gsdl.SimpleServer
|
---|
49 | *
|
---|
50 | */
|
---|
51 |
|
---|
52 | public class NzdlHosts implements Cloneable, Serializable {
|
---|
53 |
|
---|
54 | static String propertiesFileName = "NZDLServers.properties";
|
---|
55 |
|
---|
56 | static String defaultHostName = "nzdl2.cs.waikato.ac.nz";
|
---|
57 |
|
---|
58 | protected Properties table;
|
---|
59 |
|
---|
60 | private static NzdlHosts singleton = new NzdlHosts();
|
---|
61 |
|
---|
62 | private NzdlHosts()
|
---|
63 | {
|
---|
64 | table = new Properties();
|
---|
65 |
|
---|
66 | try {
|
---|
67 | File file = new File(propertiesFileName);
|
---|
68 | if (file.exists() && file.canRead())
|
---|
69 | table.load(new FileInputStream(file));
|
---|
70 | else {
|
---|
71 | table.put("mdxlite", "ickenham.mdx.ac.uk");
|
---|
72 | table.put("nzdl2", "nzdl2.cs.waikato.ac.nz");
|
---|
73 | table.put("csdl1", "csdl1.mdx.ac.uk");
|
---|
74 | }
|
---|
75 | } catch (IOException exception) {
|
---|
76 | System.err.println("NzdlHosts::NzdlHosts() error reading properties file: " +
|
---|
77 | exception);
|
---|
78 | }
|
---|
79 | }
|
---|
80 |
|
---|
81 | public void finalize() {
|
---|
82 | tidy();
|
---|
83 | }
|
---|
84 |
|
---|
85 | static public void tidy() {
|
---|
86 | if (singleton == null)
|
---|
87 | singleton = new NzdlHosts();
|
---|
88 | try {
|
---|
89 | File file = new File(propertiesFileName);
|
---|
90 | if (file.canWrite() || !file.exists())
|
---|
91 | singleton.table.store(new FileOutputStream(file),"NZDL hosts we know about");
|
---|
92 | else {
|
---|
93 | System.err.println("NzdlHosts::tidy() unable ");
|
---|
94 | }
|
---|
95 | } catch (IOException exception) {
|
---|
96 | System.err.println("NzdlHosts::NzdlHosts() error reading properties file: " +
|
---|
97 | exception);
|
---|
98 | }
|
---|
99 | }
|
---|
100 |
|
---|
101 |
|
---|
102 | static public void addHost(String code, String url)
|
---|
103 | {
|
---|
104 | if (singleton == null)
|
---|
105 | singleton = new NzdlHosts();
|
---|
106 | singleton.table.put(code, url);
|
---|
107 | }
|
---|
108 |
|
---|
109 |
|
---|
110 | static public String getFullName(String code)
|
---|
111 | {
|
---|
112 | if (singleton == null)
|
---|
113 | singleton = new NzdlHosts();
|
---|
114 | String reply;
|
---|
115 | reply = (String) singleton.table.get(code);
|
---|
116 | System.out.println("Read " + reply);
|
---|
117 | if (reply == null)
|
---|
118 | {
|
---|
119 | return defaultHostName;
|
---|
120 | }
|
---|
121 | return reply;
|
---|
122 | }
|
---|
123 |
|
---|
124 | static public String getUrlFromSite(String sitename)
|
---|
125 | {
|
---|
126 | return "http://" + sitename + "/cgi-bin/getior";
|
---|
127 | }
|
---|
128 |
|
---|
129 | static public String getUrlFromCode(String code)
|
---|
130 | {
|
---|
131 | return "http://" + getFullName(code) + "/cgi-bin/getior";
|
---|
132 | }
|
---|
133 |
|
---|
134 | static String getIOR(URL url)
|
---|
135 | {
|
---|
136 | try {
|
---|
137 | InputStream his = url.openStream();
|
---|
138 | DataInputStream dhis = new DataInputStream(his) ;
|
---|
139 | String ior = dhis.readLine();
|
---|
140 | System.out.println(ior);
|
---|
141 | return ior;
|
---|
142 | }
|
---|
143 | catch (Throwable throwable) {
|
---|
144 | System.err.println("NzdlHosts:getIOR() unable to communicate with URL \"" + url + "\"");
|
---|
145 | }
|
---|
146 | return "";
|
---|
147 | }
|
---|
148 |
|
---|
149 | static public String getIOR(String code)
|
---|
150 | {
|
---|
151 | if (singleton == null)
|
---|
152 | singleton = new NzdlHosts();
|
---|
153 |
|
---|
154 | String str = getUrlFromCode(code);
|
---|
155 | String ior = null;
|
---|
156 | URL url;
|
---|
157 |
|
---|
158 | try {
|
---|
159 | url = new URL(str);
|
---|
160 | }
|
---|
161 | catch (Throwable malEx) {
|
---|
162 | System.err.println("NzdlHosts::getIOR() unable to construct or read URL \"" +
|
---|
163 | str + "\", \"" + ior + "\": " + malEx);
|
---|
164 | return "";
|
---|
165 | }
|
---|
166 |
|
---|
167 | return getIOR(url);
|
---|
168 | }
|
---|
169 |
|
---|
170 | static public NzdlService getHost(String code) {
|
---|
171 | NzdlService nzdl = null;
|
---|
172 |
|
---|
173 | try {
|
---|
174 | nzdl = new NzdlServiceClient(null,null,getIOR(code));
|
---|
175 | } catch (Throwable throwable) {
|
---|
176 | System.err.println("NzdlHosts::getHost() constructing NzdlServiceClient(" +
|
---|
177 | code + ") " + throwable);
|
---|
178 | }
|
---|
179 | return nzdl;
|
---|
180 | }
|
---|
181 |
|
---|
182 | static public NzdlService getSite(String sitename) {
|
---|
183 | NzdlService nzdl = null;
|
---|
184 |
|
---|
185 | try {
|
---|
186 | String siteUrl = getUrlFromSite(sitename);
|
---|
187 | URL url = new URL(siteUrl);
|
---|
188 | nzdl = new NzdlServiceClient(null,null,getIOR(url));
|
---|
189 | } catch (Throwable throwable) {
|
---|
190 | System.err.println("NzdlHosts::getHost() constructing NzdlServiceClient(" +
|
---|
191 | sitename + ") " + throwable);
|
---|
192 | }
|
---|
193 | return nzdl;
|
---|
194 | }
|
---|
195 |
|
---|
196 | static public Vector getAllHosts() {
|
---|
197 | if (singleton == null)
|
---|
198 | singleton = new NzdlHosts();
|
---|
199 |
|
---|
200 | Vector results = new Vector();
|
---|
201 |
|
---|
202 | Enumeration e = singleton.table.keys();
|
---|
203 | while (e.hasMoreElements()) {
|
---|
204 | String code = (String) e.nextElement();
|
---|
205 | try {
|
---|
206 | NzdlService nzdl = new NzdlServiceClient(null,null,getIOR(code));
|
---|
207 | results.addElement(nzdl);
|
---|
208 | } catch (Throwable throwable) {
|
---|
209 | System.err.println("NzdlHosts::getHost() constructing NzdlServiceClient(" +
|
---|
210 | code + "): " + throwable);
|
---|
211 | }
|
---|
212 | }
|
---|
213 | return results;
|
---|
214 | }
|
---|
215 |
|
---|
216 | static public NzdlService hostFromCommandlineArgs(String [] _args, Properties _props) {
|
---|
217 | NzdlService nzdl = null;
|
---|
218 |
|
---|
219 | try {
|
---|
220 | nzdl = new NzdlServiceClient( _args, _props, null);
|
---|
221 | } catch (Throwable throwable) {
|
---|
222 | System.err.println("NzdlHosts::hostFromCommandlineArgs() constructing " +
|
---|
223 | "NzdlServiceClient() " + throwable);
|
---|
224 |
|
---|
225 | }
|
---|
226 | return nzdl;
|
---|
227 | }
|
---|
228 | }
|
---|
229 |
|
---|
230 |
|
---|
231 |
|
---|