KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > server > cluster > ServerConnector


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.server.cluster;
31
32 import com.caucho.management.server.ServerConnectorMXBean;
33 import com.caucho.util.L10N;
34 import com.caucho.vfs.Path;
35 import com.caucho.vfs.ReadWritePair;
36 import com.caucho.vfs.Vfs;
37
38 import javax.management.ObjectName JavaDoc;
39 import java.io.IOException JavaDoc;
40 import java.util.HashMap JavaDoc;
41 import java.util.logging.Level JavaDoc;
42 import java.util.logging.Logger JavaDoc;
43
44 /**
45  * Defines a member of the cluster.
46  *
47  * A {@link ClusterClient} obtained with {@link #getClient} is used to actually
48  * communicate with this ClusterServer when it is active in another instance of
49  * Resin .
50  */

51 public class ServerConnector
52 {
53   private static final Logger JavaDoc log
54     = Logger.getLogger(ServerConnector.class.getName());
55   private static final L10N L = new L10N(ServerConnector.class);
56
57   private ClusterServer _server;
58   private ClusterPort _port;
59
60   private ObjectName JavaDoc _objectName;
61
62   private Cluster _cluster;
63
64   private int _srunIndex;
65   private Path _tcpPath;
66
67   private ClusterClient _client;
68
69   private ServerConnectorAdmin _admin;
70
71   public ServerConnector(ClusterServer server)
72   {
73     _server = server;
74     _cluster = _server.getCluster();
75     _port = server.getClusterPort();
76   }
77
78   /**
79    * Gets the owning cluster.
80    */

81   public Cluster getCluster()
82   {
83     return _cluster;
84   }
85
86   /**
87    * Returns the object name.
88    */

89   public ObjectName JavaDoc getObjectName()
90   {
91     return _objectName;
92   }
93
94   /**
95    * Returns the admin.
96    */

97   public ServerConnectorMXBean getAdmin()
98   {
99     return _admin;
100   }
101
102   /**
103    * Gets the cluster port.
104    */

105   public ClusterPort getClusterPort()
106   {
107     return _port;
108   }
109
110   /**
111    * Returns the user-readable id of the target server.
112    */

113   public String JavaDoc getId()
114   {
115     return _server.getId();
116   }
117
118   /**
119    * Returns the index of this connection in the connection group.
120    */

121   public int getIndex()
122   {
123     return _server.getIndex();
124   }
125
126   /**
127    * Returns the hostname of the target server.
128    */

129   public String JavaDoc getAddress()
130   {
131     return _port.getAddress();
132   }
133
134   /**
135    * Gets the port of the target server.
136    */

137   public int getPort()
138   {
139     return _port.getPort();
140   }
141
142   /**
143    * Returns the time in milliseconds for the slow start throttling.
144    */

145   public long getLoadBalanceWarmupTime()
146   {
147     return _server.getLoadBalanceWarmupTime();
148   }
149
150   /**
151    * Returns the socket timeout when connecting to the
152    * target server.
153    */

154   public long getLoadBalanceConnectTimeout()
155   {
156     return _server.getLoadBalanceConnectTimeout();
157   }
158
159   /**
160    * Returns the socket timeout when reading from the
161    * target server.
162    */

163   public long getSocketTimeout()
164   {
165     return _server.getSocketTimeout();
166   }
167
168   /**
169    * Returns how long the connection can be cached in the free pool.
170    */

171   public long getLoadBalanceIdleTime()
172   {
173     return _server.getLoadBalanceIdleTime();
174   }
175
176   /**
177    * Returns how long the connection will be treated as dead.
178    */

179   public long getLoadBalanceRecoverTime()
180   {
181     return _server.getLoadBalanceRecoverTime();
182   }
183
184   /**
185    * Returns the load balance weight.
186    */

187   public int getLoadBalanceWeight()
188   {
189     return _server.getLoadBalanceWeight();
190   }
191
192   /**
193    * Initialize
194    */

195   public void init()
196     throws Exception JavaDoc
197   {
198     String JavaDoc address = getAddress();
199
200     if (address == null)
201       address = "localhost";
202
203     HashMap JavaDoc<String JavaDoc,Object JavaDoc> attr = new HashMap JavaDoc<String JavaDoc,Object JavaDoc>();
204     attr.put("connect-timeout", new Long JavaDoc(getLoadBalanceConnectTimeout()));
205
206     if (_port.isSSL())
207       _tcpPath = Vfs.lookup("tcps://" + address + ":" + getPort(), attr);
208     else
209       _tcpPath = Vfs.lookup("tcp://" + address + ":" + getPort(), attr);
210
211     _client = new ClusterClient(this);
212
213     _admin = new ServerConnectorAdmin(_client);
214
215     try {
216       String JavaDoc name = getId();
217
218       if (name == null)
219         name = "";
220
221       _admin.register();
222     } catch (Throwable JavaDoc e) {
223       log.log(Level.FINER, e.toString(), e);
224     }
225   }
226   
227   /**
228    * Returns true if the server is dead.
229    */

230   public boolean isDead()
231   {
232     return ! _client.isActive();
233   }
234   
235   /**
236    * Returns true if the server is dead.
237    */

238   public boolean canOpenSoft()
239   {
240     return _client.canOpenSoft();
241   }
242
243   /**
244    * Returns true if active.
245    */

246   public boolean isActive()
247   {
248     return _client.isEnabled();
249   }
250
251   /**
252    * Enable the client
253    */

254   public void enable()
255   {
256     _client.start();
257   }
258
259   /**
260    * Disable the client
261    */

262   public void disable()
263   {
264     _client.stop();
265   }
266
267   /**
268    * Returns the client.
269    */

270   public ClusterClient getClient()
271   {
272     return _client;
273   }
274
275   /**
276    * Open a read/write pair to the target srun connection.
277    *
278    * @return the socket's read/write pair.
279    */

280   ReadWritePair openTCPPair()
281     throws IOException JavaDoc
282   {
283     return _tcpPath.openReadWrite();
284   }
285
286   /**
287    * We now know that the server is live.
288    */

289   public void wake()
290   {
291     if (_client != null)
292       _client.wake();
293   }
294
295   /**
296    * Returns true if can connect to the client.
297    */

298   public boolean canConnect()
299   {
300     try {
301       wake();
302
303       ClusterStream stream = _client.open();
304
305       if (stream != null) {
306         stream.free();
307
308         return true;
309       }
310
311       return false;
312     } catch (Throwable JavaDoc e) {
313       log.log(Level.FINER, e.toString(), e);
314
315       return false;
316     }
317   }
318
319   /**
320    * Close any clients.
321    */

322   public void close()
323   {
324     if (_client != null)
325       _client.close();
326   }
327
328   public String JavaDoc toString()
329   {
330     return ("ServerConnector[id=" + getId() +
331             " index=" + _port.getIndex() +
332             " address=" + _port.getAddress() + ":" + _port.getPort() +
333             " cluster=" + _cluster.getId() + "]");
334   }
335
336 }
337
Popular Tags