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.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
67 private static Hashtable serverClients = new Hashtable();
68
69
70 private StyxConnection conn;
71
72
73 private Hashtable sgsClients;
74
75 private CStyxFile serverRoot;
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
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
114 InetAddress serverAddr = InetAddress.getByName(hostname);
115
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
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 }