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.client;
30  
31  import java.util.Hashtable;
32  import java.net.InetAddress;
33  import java.net.UnknownHostException;
34  
35  import uk.ac.rdg.resc.jstyx.client.StyxConnection;
36  import uk.ac.rdg.resc.jstyx.client.CStyxFile;
37  import uk.ac.rdg.resc.jstyx.StyxException;
38  
39  /***
40   * A client of an SGS server.  Use this class to find the SGSs that are available
41   * on a server.  To create an instance of this class, use the <code>getServerClient()</code>
42   * static factory method.
43   *
44   * @author Jon Blower
45   * $Revision: 588 $
46   * $Date: 2006-02-20 17:34:27 +0000 (Mon, 20 Feb 2006) $
47   * $Log$
48   * Revision 1.5  2006/02/20 17:34:27  jonblower
49   * Added getConnection() method
50   *
51   * Revision 1.4  2006/01/05 16:06:34  jonblower
52   * SGS clients now deal with possibility that client could be created on a different server
53   *
54   * Revision 1.3  2005/12/01 08:36:02  jonblower
55   * Changed constructor to accept hostname and port instead of CStyxFile
56   *
57   * Revision 1.2  2005/10/14 18:07:06  jonblower
58   * Added getSGSClient()
59   *
60   * Revision 1.1  2005/08/12 08:08:39  jonblower
61   * Developments to support web interface
62   *
63   */
64  public class SGSServerClient
65  {
66      // Contains the server clients that have already been created
67      private static Hashtable serverClients = new Hashtable();
68      
69      // The underlying connection
70      private StyxConnection conn;
71      
72      // Contains the SGS clients that have been created
73      private Hashtable sgsClients;
74      
75      private CStyxFile serverRoot; // File representing the root of the server
76      
77      /***
78       * Connects to the SGS server and sets up the client.  Blocks until the connection
79       * to the server is made.
80       * @param hostname The hostname or IP address of the SGS server
81       * @param port The port of the SGS server
82       * @throws StyxException if there was an error connecting to the server
83       */
84      private SGSServerClient(String hostname, int port) throws StyxException
85      {
86          this.sgsClients = new Hashtable();
87          // Connect to the server
88          this.conn = new StyxConnection(hostname, port);
89          this.conn.connect();
90          this.serverRoot = conn.getRootDirectory();
91      }
92      
93      /***
94       * @return the underlying connection to the server
95       */
96      public StyxConnection getConnection()
97      {
98          return this.conn;
99      }
100     
101     /***
102      * Static factory method for creating an SGSServerClient.  If a client
103      * already exists for the given server (and port number) it will be returned.
104      * If not, a new one will be created.
105      * @param hostname The hostname or IP address of the SGS server
106      * @param port The port of the SGS server
107      * @throws StyxException if there was an error connecting to the server.
108      * @throws UnknownHostException if the host could not be found
109      */
110     public static SGSServerClient getServerClient(String hostname, int port)
111         throws StyxException, UnknownHostException
112     {
113         // Look up the IP address of the server
114         InetAddress serverAddr = InetAddress.getByName(hostname);
115         // See if we already have a client for this server
116         String key = serverAddr.getHostAddress() + ":" + port;
117         SGSServerClient serverClient = (SGSServerClient)serverClients.get(key);
118         if (serverClient == null)
119         {
120             serverClient = new SGSServerClient(hostname, port);
121             serverClients.put(key, serverClient);
122         }
123         return serverClient;
124     }
125     
126     /***
127      * Gets an SGSClient object for a given Styx Grid Service.  This method
128      * blocks until the SGS is found and proven to exist on the server.  If a
129      * client for the given SGS has already been created, it is simply returned.
130      * @param serviceName The name of the Styx Grid Service
131      * @return an SGSClient object for the requested Styx Grid Service
132      * @throws StyxException if there is no SGS with the given name on the server
133      */
134     public synchronized SGSClient getSGSClient(String serviceName) throws StyxException
135     {
136         SGSClient sgsClient = (SGSClient)this.sgsClients.get(serviceName);
137         if (sgsClient == null)
138         {
139             // We need to create a new client
140             CStyxFile sgsRoot = this.serverRoot.getFile(serviceName);
141             if (sgsRoot.exists())
142             {
143                 sgsClient = new SGSClient(sgsRoot);
144             }
145             else
146             {
147                 throw new StyxException("There is no Styx Grid Service called " +
148                     serviceName + " on the server");
149             }
150         }
151         return sgsClient;
152     }
153     
154 }