1 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 ; 39 import java.io.IOException ; 40 import java.util.HashMap ; 41 import java.util.logging.Level ; 42 import java.util.logging.Logger ; 43 44 51 public class ServerConnector 52 { 53 private static final Logger 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 _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 81 public Cluster getCluster() 82 { 83 return _cluster; 84 } 85 86 89 public ObjectName getObjectName() 90 { 91 return _objectName; 92 } 93 94 97 public ServerConnectorMXBean getAdmin() 98 { 99 return _admin; 100 } 101 102 105 public ClusterPort getClusterPort() 106 { 107 return _port; 108 } 109 110 113 public String getId() 114 { 115 return _server.getId(); 116 } 117 118 121 public int getIndex() 122 { 123 return _server.getIndex(); 124 } 125 126 129 public String getAddress() 130 { 131 return _port.getAddress(); 132 } 133 134 137 public int getPort() 138 { 139 return _port.getPort(); 140 } 141 142 145 public long getLoadBalanceWarmupTime() 146 { 147 return _server.getLoadBalanceWarmupTime(); 148 } 149 150 154 public long getLoadBalanceConnectTimeout() 155 { 156 return _server.getLoadBalanceConnectTimeout(); 157 } 158 159 163 public long getSocketTimeout() 164 { 165 return _server.getSocketTimeout(); 166 } 167 168 171 public long getLoadBalanceIdleTime() 172 { 173 return _server.getLoadBalanceIdleTime(); 174 } 175 176 179 public long getLoadBalanceRecoverTime() 180 { 181 return _server.getLoadBalanceRecoverTime(); 182 } 183 184 187 public int getLoadBalanceWeight() 188 { 189 return _server.getLoadBalanceWeight(); 190 } 191 192 195 public void init() 196 throws Exception 197 { 198 String address = getAddress(); 199 200 if (address == null) 201 address = "localhost"; 202 203 HashMap <String ,Object > attr = new HashMap <String ,Object >(); 204 attr.put("connect-timeout", new Long (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 name = getId(); 217 218 if (name == null) 219 name = ""; 220 221 _admin.register(); 222 } catch (Throwable e) { 223 log.log(Level.FINER, e.toString(), e); 224 } 225 } 226 227 230 public boolean isDead() 231 { 232 return ! _client.isActive(); 233 } 234 235 238 public boolean canOpenSoft() 239 { 240 return _client.canOpenSoft(); 241 } 242 243 246 public boolean isActive() 247 { 248 return _client.isEnabled(); 249 } 250 251 254 public void enable() 255 { 256 _client.start(); 257 } 258 259 262 public void disable() 263 { 264 _client.stop(); 265 } 266 267 270 public ClusterClient getClient() 271 { 272 return _client; 273 } 274 275 280 ReadWritePair openTCPPair() 281 throws IOException 282 { 283 return _tcpPath.openReadWrite(); 284 } 285 286 289 public void wake() 290 { 291 if (_client != null) 292 _client.wake(); 293 } 294 295 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 e) { 313 log.log(Level.FINER, e.toString(), e); 314 315 return false; 316 } 317 } 318 319 322 public void close() 323 { 324 if (_client != null) 325 _client.close(); 326 } 327 328 public String 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 |