1 22 package org.jboss.mq.il.uil2; 23 24 import java.io.IOException ; 25 import java.lang.reflect.Method ; 26 import java.net.InetAddress ; 27 import java.net.ServerSocket ; 28 import java.net.Socket ; 29 import java.net.UnknownHostException ; 30 import java.util.Properties ; 31 import javax.naming.InitialContext ; 32 import javax.net.ServerSocketFactory; 33 34 import org.jboss.mq.il.Invoker; 35 import org.jboss.mq.il.ServerIL; 36 import org.jboss.mq.il.ServerILJMXService; 37 import org.jboss.mq.il.uil2.msgs.MsgTypes; 38 import org.jboss.mq.il.uil2.msgs.BaseMsg; 39 import org.jboss.security.SecurityDomain; 40 import org.jboss.system.server.ServerConfigUtil; 41 42 49 public class UILServerILService extends ServerILJMXService 50 implements MsgTypes, Runnable , UILServerILServiceMBean 51 { 52 final static int SO_TIMEOUT = 5000; 53 54 56 private String securityDomain; 57 59 private String clientSocketFactoryName; 60 62 private ServerSocketFactory serverSocketFactory; 63 65 private ServerSocket serverSocket; 66 private UILServerIL serverIL; 67 private boolean running; 68 69 private int serverBindPort = 0; 70 71 private InetAddress bindAddress = null; 72 73 private Thread acceptThread; 74 77 private InetAddress clientAddress; 78 81 private String connectAddress; 82 85 private int connectPort; 86 89 private boolean enableTcpNoDelay = false; 90 91 94 private int readTimeout = 0; 95 96 99 private int clientReadTimeout = 0; 100 101 104 private int bufferSize = 1; 105 106 109 private int chunkSize = 0x40000000; 110 111 114 private Properties connectionProperties; 115 116 123 public Properties getClientConnectionProperties() 124 { 125 return connectionProperties; 126 } 127 128 135 public ServerIL getServerIL() 136 { 137 return serverIL; 138 } 139 140 142 public void run() 143 { 144 boolean trace = log.isTraceEnabled(); 145 while (running) 146 { 147 Socket socket = null; 148 SocketManager socketMgr = null; 149 try 150 { 151 socket = serverSocket.accept(); 152 if( trace ) 153 log.trace("Accepted connection: "+socket); 154 socket.setSoTimeout(readTimeout); 155 socket.setTcpNoDelay(enableTcpNoDelay); 156 socketMgr = new SocketManager(socket); 157 ServerSocketManagerHandler handler = new ServerSocketManagerHandler(getJMSServer(), socketMgr); 158 socketMgr.setHandler(handler); 159 socketMgr.setBufferSize(bufferSize); 160 socketMgr.setChunkSize(chunkSize); 161 Invoker s = getJMSServer(); 162 socketMgr.start(s.getThreadGroup()); 163 } 164 catch (IOException e) 165 { 166 if (running) 167 log.warn("Failed to setup client connection", e); 168 } 169 catch(Throwable e) 170 { 171 if (running || trace) 172 log.warn("Unexpected error in setup of client connection", e); 173 } 174 } 175 } 176 177 182 public void startService() throws Exception 183 { 184 super.startService(); 185 running = true; 186 187 if (serverSocketFactory == null) 189 serverSocketFactory = ServerSocketFactory.getDefault(); 190 191 194 if (securityDomain != null) 195 { 196 try 197 { 198 InitialContext ctx = new InitialContext (); 199 Class ssfClass = serverSocketFactory.getClass(); 200 SecurityDomain domain = (SecurityDomain) ctx.lookup(securityDomain); 201 Class [] parameterTypes = {SecurityDomain.class}; 202 Method m = ssfClass.getMethod("setSecurityDomain", parameterTypes); 203 Object [] args = {domain}; 204 m.invoke(serverSocketFactory, args); 205 } 206 catch (NoSuchMethodException e) 207 { 208 log.error("Socket factory does not support setSecurityDomain(SecurityDomain)"); 209 } 210 catch (Exception e) 211 { 212 log.error("Failed to setSecurityDomain=" + securityDomain + " on socket factory"); 213 } 214 } 215 216 serverSocket = serverSocketFactory.createServerSocket(serverBindPort, 50, bindAddress); 218 219 InetAddress socketAddress = serverSocket.getInetAddress(); 220 log.info("JBossMQ UIL service available at : " + socketAddress + ":" + serverSocket.getLocalPort()); 221 acceptThread = new Thread (getJMSServer().getThreadGroup(), this, "UILServerILService Accept Thread"); 222 acceptThread.start(); 223 224 228 socketAddress = ServerConfigUtil.fixRemoteAddress(socketAddress); 229 if( clientAddress != null ) 231 socketAddress = clientAddress; 232 serverIL = new UILServerIL(socketAddress, serverSocket.getLocalPort(), 233 clientSocketFactoryName, enableTcpNoDelay, bufferSize, chunkSize, clientReadTimeout, connectAddress, connectPort); 234 235 connectionProperties = super.getClientConnectionProperties(); 237 connectionProperties.setProperty(UILServerILFactory.CLIENT_IL_SERVICE_KEY, UILClientILService.class.getName()); 238 connectionProperties.setProperty(UILServerILFactory.UIL_PORT_KEY, "" + serverSocket.getLocalPort()); 239 connectionProperties.setProperty(UILServerILFactory.UIL_ADDRESS_KEY, "" + socketAddress.getHostAddress()); 240 connectionProperties.setProperty(UILServerILFactory.UIL_TCPNODELAY_KEY, enableTcpNoDelay ? "yes" : "no"); 241 connectionProperties.setProperty(UILServerILFactory.UIL_BUFFERSIZE_KEY, "" + bufferSize); 242 connectionProperties.setProperty(UILServerILFactory.UIL_CHUNKSIZE_KEY, "" + chunkSize); 243 connectionProperties.setProperty(UILServerILFactory.UIL_RECEIVE_REPLIES_KEY, "No"); 244 connectionProperties.setProperty(UILServerILFactory.UIL_SOTIMEOUT_KEY, "" + clientReadTimeout); 245 connectionProperties.setProperty(UILServerILFactory.UIL_CONNECTADDRESS_KEY, "" + connectAddress); 246 connectionProperties.setProperty(UILServerILFactory.UIL_CONNECTPORT_KEY, "" + connectPort); 247 248 bindJNDIReferences(); 249 BaseMsg.setUseJMSServerMsgIDs(true); 250 } 251 252 255 public void stopService() 256 { 257 try 258 { 259 running = false; 260 unbindJNDIReferences(); 261 262 if (serverSocket != null) 264 { 265 serverSocket.close(); 266 } 267 } 268 catch (Exception e) 269 { 270 log.error("Exception occured when trying to stop UIL Service: ", e); 271 } 272 } 273 274 281 public int getServerBindPort() 282 { 283 return serverBindPort; 284 } 285 286 293 public void setServerBindPort(int serverBindPort) 294 { 295 this.serverBindPort = serverBindPort; 296 } 297 298 303 public String getBindAddress() 304 { 305 String addr = "0.0.0.0"; 306 if (bindAddress != null) 307 addr = bindAddress.getHostName(); 308 return addr; 309 } 310 311 316 public void setBindAddress(String host) throws UnknownHostException 317 { 318 if (host == null || host.length() == 0) 320 bindAddress = null; 321 else 322 bindAddress = InetAddress.getByName(host); 323 } 324 325 public InetAddress getClientAddress() 326 { 327 return clientAddress; 328 } 329 330 public void setClientAddress(InetAddress addr) 331 { 332 log.warn("ClientAddress has been deprecated, use ConnectAddress"); 333 clientAddress = addr; 334 } 335 336 public String getConnectAddress() 337 { 338 return connectAddress; 339 } 340 341 public void setConnectAddress(String addr) 342 { 343 connectAddress = addr; 344 } 345 346 public int getConnectPort() 347 { 348 return connectPort; 349 } 350 351 public void setConnectPort(int port) 352 { 353 connectPort = port; 354 } 355 356 362 public boolean getEnableTcpNoDelay() 363 { 364 return enableTcpNoDelay; 365 } 366 367 373 public void setEnableTcpNoDelay(boolean enableTcpNoDelay) 374 { 375 this.enableTcpNoDelay = enableTcpNoDelay; 376 } 377 378 384 public int getBufferSize() 385 { 386 return bufferSize; 387 } 388 389 395 public void setBufferSize(int size) 396 { 397 this.bufferSize = size; 398 } 399 400 406 public int getChunkSize() 407 { 408 return chunkSize; 409 } 410 411 417 public void setChunkSize(int size) 418 { 419 this.chunkSize = size; 420 } 421 422 public int getReadTimeout() 423 { 424 return readTimeout; 425 } 426 427 public void setReadTimeout(int timeout) 428 { 429 this.readTimeout = timeout; 430 } 431 432 public int getClientReadTimeout() 433 { 434 return clientReadTimeout; 435 } 436 437 public void setClientReadTimeout(int timeout) 438 { 439 this.clientReadTimeout = timeout; 440 } 441 442 446 public String getClientSocketFactory() 447 { 448 return clientSocketFactoryName; 449 } 450 451 455 public void setClientSocketFactory(String name) 456 { 457 this.clientSocketFactoryName = name; 458 } 459 460 464 public void setServerSocketFactory(String name) throws Exception 465 { 466 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 467 Class ssfClass = loader.loadClass(name); 468 serverSocketFactory = (ServerSocketFactory) ssfClass.newInstance(); 469 } 470 471 475 public String getServerSocketFactory() 476 { 477 String name = null; 478 if (serverSocketFactory != null) 479 name = serverSocketFactory.getClass().getName(); 480 return name; 481 } 482 483 486 public void setSecurityDomain(String domainName) 487 { 488 this.securityDomain = domainName; 489 } 490 491 494 public String getSecurityDomain() 495 { 496 return this.securityDomain; 497 } 498 } 499 | Popular Tags |