View Javadoc

1   /*
2    * Copyright (c) 2005 The University of Reading
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    * 1. Redistributions of source code must retain the above copyright
9    *    notice, this list of conditions and the following disclaimer.
10   * 2. Redistributions in binary form must reproduce the above copyright
11   *    notice, this list of conditions and the following disclaimer in the
12   *    documentation and/or other materials provided with the distribution.
13   * 3. Neither the name of the University of Reading, nor the names of the
14   *    authors or contributors may be used to endorse or promote products
15   *    derived from this software without specific prior written permission.
16   * 
17   * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18   * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20   * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21   * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23   * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24   * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26   * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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; // The port on which the server will listen
99      protected String host; // The hostname or IP address of this server
100     private String cacheLocation; // The root of all the cached files
101     protected boolean useSSL; // True if the server is to be secured with SSL
102     protected String keystore; // The location of the keystore
103     protected Vector gridServices; // Information about all the SGSs
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             // Parse the xml document using dom4j (with validation)
119             SAXReader reader = new SAXReader(true);
120             this.doc = reader.read(xmlFilename);
121             // Get the base node of the server configuration
122             Node serverNode = this.doc.selectSingleNode("/sgs/server");
123             // Get the cache location
124             this.cacheLocation = serverNode.valueOf("@cacheLocation");
125             if (this.cacheLocation == null || this.cacheLocation.trim().equals(""))
126             {
127                 // Create a default cache in the user's home directory
128                 this.cacheLocation = System.getProperty("user.home") +
129                     StyxUtils.SYSTEM_FILE_SEPARATOR + "StyxGridServices";
130             }
131             // Get the server address and port number
132             this.getServerAddressAndPort(serverNode);
133             // Get the SSL parameters
134             this.getSSLConfig(serverNode);
135             // Get the configuration for all Styx Grid Services
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                 // The hostname has not been set: attempt to find it automatically
168                 this.host = InetAddress.getLocalHost().getHostAddress();
169             }
170             catch(UnknownHostException uhe)
171             {
172                 // Shouldn't happen
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 }