source: trunk/java-client/org/nzdl/gsdl/util/NzdlHosts.java@ 2165

Last change on this file since 2165 was 2163, checked in by bas6, 23 years ago

Tidied up some comments & added package.html to SimpleGraphical Client

  • Property svn:keywords set to Author Date Id Revision
File size: 5.2 KB
Line 
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
21package org.nzdl.gsdl.util;
22
23// java standard library classes used
24import java.io.IOException;
25import java.io.Serializable;
26import java.lang.Cloneable;
27import java.util.Properties;
28import java.util.Vector;
29import java.util.Enumeration;
30import java.io.File;
31import java.io.FileInputStream;
32import java.io.FileOutputStream;
33import java.io.DataInputStream;
34import java.io.InputStream;
35import org.omg.CORBA.Object;
36import org.omg.CORBA.ORB;
37import java.net.URL;
38
39import 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: 2163 $
47 * @see org.nzdl.gsdl.SimpleClient
48 * @see org.nzdl.gsdl.SimpleServer
49 *
50 */
51
52public 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 getUrl(String code)
125 {
126 return "http://" + getHost(code) + "/cgi-bin/getior";
127 }
128
129 static public String getIOR(String code)
130 {
131 if (singleton == null)
132 singleton = new NzdlHosts();
133
134 String str = getUrl(code);
135 String ior = null;
136
137 try {
138 URL url = new URL(str);
139 InputStream his = url.openStream();
140 DataInputStream dhis = new DataInputStream(his) ;
141 ior = dhis.readLine();
142 return ior;
143 } catch (Throwable throwable) {
144 System.err.println("NzdlHosts::getIOR() unable to construct or read URL \"" +
145 str + "\", \"" + ior + "\": " + throwable);
146 return "";
147 }
148 }
149
150 static public NzdlService getHost(String code) {
151 NzdlService nzdl = null;
152
153 try {
154 nzdl = new NzdlServiceClient(null,null,getIOR(code));
155 } catch (Throwable throwable) {
156 System.err.println("NzdlHosts::getHost() constructing NzdlServiceClient(" +
157 code + ") " + throwable);
158 }
159 return nzdl;
160 }
161
162 static public Vector getAllHosts() {
163 if (singleton == null)
164 singleton = new NzdlHosts();
165
166 Vector results = new Vector();
167
168 Enumeration e = singleton.table.keys();
169 while (e.hasMoreElements()) {
170 String code = (String) e.nextElement();
171 try {
172 NzdlService nzdl = new NzdlServiceClient(null,null,getIOR(code));
173 results.addElement(nzdl);
174 } catch (Throwable throwable) {
175 System.err.println("NzdlHosts::getHost() constructing NzdlServiceClient(" +
176 code + "): " + throwable);
177 }
178 }
179 return results;
180 }
181
182 static public NzdlService hostFromCommandlineArgs(String [] _args, Properties _props) {
183 NzdlService nzdl = null;
184
185 try {
186 nzdl = new NzdlServiceClient( _args, _props, null);
187 } catch (Throwable throwable) {
188 System.err.println("NzdlHosts::hostFromCommandlineArgs() constructing " +
189 "NzdlServiceClient() " + throwable);
190
191 }
192 return nzdl;
193 }
194}
195
196
197
Note: See TracBrowser for help on using the repository browser.