1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29 package uk.ac.rdg.resc.jstyx.gridservice.server;
30
31 import java.io.File;
32 import java.io.IOException;
33
34 import java.util.Vector;
35 import java.util.Iterator;
36 import java.util.List;
37
38 import java.net.InetAddress;
39 import java.net.UnknownHostException;
40
41 import org.apache.log4j.Logger;
42
43 import org.dom4j.io.SAXReader;
44 import org.dom4j.Document;
45 import org.dom4j.DocumentException;
46 import org.dom4j.Node;
47
48 import uk.ac.rdg.resc.jstyx.gridservice.config.SGSConfig;
49 import uk.ac.rdg.resc.jstyx.gridservice.config.SGSConfigException;
50 import uk.ac.rdg.resc.jstyx.StyxUtils;
51
52 /***
53 * Configuration of a Styx Grid Service server
54 *
55 * @author Jon Blower
56 * $Revision: 568 $
57 * $Date: 2006-01-05 12:09:15 +0000 (Thu, 05 Jan 2006) $
58 * $Log$
59 * Revision 1.10 2006/01/05 12:09:15 jonblower
60 * Restructured configuration to give default values for server settings
61 *
62 * Revision 1.9 2005/11/28 17:21:17 jonblower
63 * Allowed for <ssl> tag not existing in config file
64 *
65 * Revision 1.8 2005/11/07 21:04:48 jonblower
66 * Moved SGS config classes to new package
67 *
68 * Revision 1.7 2005/05/16 11:00:53 jonblower
69 * Changed SGS config XML file structure: separated input and output streams and changed some tag names
70 *
71 * Revision 1.6 2005/05/11 13:45:19 jonblower
72 * Converted SGS config code to use dom4j and Jaxen for XML parsing
73 *
74 * Revision 1.5 2005/03/26 14:27:53 jonblower
75 * Modified to use SGSConfigException
76 *
77 * Revision 1.4 2005/03/24 17:33:51 jonblower
78 * Improved reading of service parameters from config file
79 *
80 * Revision 1.3 2005/03/24 07:57:41 jonblower
81 * Improved code for reading SSL info from SGSconfig file and included parameter information for the Grid Services in the config file
82 *
83 * Revision 1.2 2005/03/22 17:45:25 jonblower
84 * Now reads SSL switch from config file
85 *
86 * Revision 1.1 2005/03/16 22:16:44 jonblower
87 * Added Styx Grid Service classes to core module
88 *
89 * Revision 1.1 2005/02/16 19:22:32 jonblower
90 * Commit adding of SGS files to CVS
91 *
92 */
93 public class SGSServerConfig
94 {
95
96 private static final Logger log = Logger.getLogger(SGSServerConfig.class);
97
98 protected int port;
99 protected String host;
100 private String cacheLocation;
101 protected boolean useSSL;
102 protected String keystore;
103 protected Vector gridServices;
104
105 private Document doc;
106
107 /***
108 * Creates a new server configuration from the given XML document
109 * @throws Exception i
110 */
111 public SGSServerConfig(String xmlFilename) throws SGSConfigException
112 {
113 this.gridServices = new Vector();
114 this.useSSL = false;
115
116 try
117 {
118
119 SAXReader reader = new SAXReader(true);
120 this.doc = reader.read(xmlFilename);
121
122 Node serverNode = this.doc.selectSingleNode("/sgs/server");
123
124 this.cacheLocation = serverNode.valueOf("@cacheLocation");
125 if (this.cacheLocation == null || this.cacheLocation.trim().equals(""))
126 {
127
128 this.cacheLocation = System.getProperty("user.home") +
129 StyxUtils.SYSTEM_FILE_SEPARATOR + "StyxGridServices";
130 }
131
132 this.getServerAddressAndPort(serverNode);
133
134 this.getSSLConfig(serverNode);
135
136 this.getSGSConfig();
137 }
138 catch(DocumentException de)
139 {
140 if (log.isDebugEnabled())
141 {
142 de.printStackTrace();
143 }
144 throw new SGSConfigException(de.getMessage());
145 }
146 }
147
148 /***
149 * Get the address and port of the server
150 */
151 private void getServerAddressAndPort(Node serverNode) throws SGSConfigException
152 {
153 String portStr = serverNode.valueOf("@port");
154 try
155 {
156 this.port = Integer.parseInt(portStr);
157 }
158 catch(NumberFormatException nfe)
159 {
160 throw new SGSConfigException("Invalid port number: " + portStr);
161 }
162 this.host = serverNode.valueOf("@address");
163 if (this.host == null || this.host.trim().equals(""))
164 {
165 try
166 {
167
168 this.host = InetAddress.getLocalHost().getHostAddress();
169 }
170 catch(UnknownHostException uhe)
171 {
172
173 throw new SGSConfigException("Cannot get the address of this server");
174 }
175 }
176 System.out.println("Host address = " + this.host);
177 }
178
179 /***
180 * Gets the SSL-related parameters
181 */
182 private void getSSLConfig(Node serverNode) throws SGSConfigException
183 {
184 Node sslNode = serverNode.selectSingleNode("ssl");
185 if (sslNode != null)
186 {
187 if (sslNode.valueOf("@activated").equalsIgnoreCase("yes"))
188 {
189 this.useSSL = true;
190 }
191 Node keystoreNode = sslNode.selectSingleNode("keystore");
192 this.keystore = keystoreNode.valueOf("@location");
193 log.debug("SSL keystore location: " + this.keystore);
194 }
195 }
196
197 /***
198 * Get the configuration of each Styx Grid Service
199 */
200 private void getSGSConfig() throws SGSConfigException
201 {
202 Node gridServicesNode = this.doc.selectSingleNode("/sgs/gridservices");
203 List gridServicesList = gridServicesNode.selectNodes("gridservice");
204 Iterator it = gridServicesList.iterator();
205 while(it.hasNext())
206 {
207 this.gridServices.add(new SGSConfig((Node)it.next(), this));
208 }
209 }
210
211 /***
212 * @return The number of the port on which the server will listen
213 */
214 public int getPort()
215 {
216 return this.port;
217 }
218
219 /***
220 * @return The host address (hostname or IP address) of this server. This
221 * is the address of the server from the point of view of clients (i.e. the
222 * public address)
223 */
224 public String getHostAddress()
225 {
226 return this.host;
227 }
228
229 /***
230 * @return true if the server is to use SSL, false otherwise
231 */
232 public boolean getUseSSL()
233 {
234 return this.useSSL;
235 }
236
237 /***
238 * @return the location of the keystore file, or null if the server is not
239 * using SSL
240 */
241 public String getKeystoreLocation()
242 {
243 return this.keystore;
244 }
245
246 /***
247 * @return the location of the cache of files that will be created by
248 * the SGSs
249 */
250 public String getCacheLocation()
251 {
252 return this.cacheLocation;
253 }
254
255 /***
256 * @return iterator of all the SGSConfig objects (one per SGS)
257 */
258 public Iterator getSGSConfigInfo()
259 {
260 return this.gridServices.iterator();
261 }
262 }