1 22 package org.jnp.server; 23 24 import java.io.IOException ; 25 import java.io.InputStream ; 26 import java.io.ObjectOutputStream ; 27 import java.io.OutputStream ; 28 import java.lang.reflect.Method ; 29 import java.net.InetAddress ; 30 import java.net.ServerSocket ; 31 import java.net.Socket ; 32 import java.net.UnknownHostException ; 33 import java.rmi.MarshalledObject ; 34 import java.rmi.Remote ; 35 import java.rmi.server.RMIClientSocketFactory ; 36 import java.rmi.server.RMIServerSocketFactory ; 37 import java.rmi.server.UnicastRemoteObject ; 38 39 import javax.net.ServerSocketFactory; 40 41 import org.jboss.logging.Logger; 42 import org.jboss.net.sockets.DefaultSocketFactory; 43 import org.jboss.util.threadpool.BasicThreadPool; 44 import org.jboss.util.threadpool.ThreadPool; 45 import org.jnp.interfaces.Naming; 46 import org.jnp.interfaces.NamingContext; 47 48 56 public class Main implements MainMBean 57 { 58 60 62 protected NamingServer theServer; 63 protected MarshalledObject serverStub; 64 protected boolean isStubExported; 65 66 protected ServerSocket serverSocket; 67 68 protected RMIClientSocketFactory clientSocketFactory; 69 70 protected RMIServerSocketFactory serverSocketFactory; 71 72 protected ServerSocketFactory jnpServerSocketFactory; 73 74 protected String clientSocketFactoryName; 75 76 protected String serverSocketFactoryName; 77 78 protected String jnpServerSocketFactoryName; 79 82 protected InetAddress bindAddress; 83 84 protected InetAddress rmiBindAddress; 85 86 protected int backlog = 50; 87 89 protected int port = 1099; 90 92 protected int rmiPort = 0; 93 94 protected boolean InstallGlobalService = true; 95 protected Logger log; 96 97 protected ThreadPool lookupPool; 98 99 public static void main(String [] args) 101 throws Exception 102 { 103 new Main().start(); 104 } 105 106 public Main() 108 { 109 this("org.jboss.naming.Naming"); 110 } 111 public Main(String categoryName) 112 { 113 try 115 { 116 ClassLoader loader = getClass().getClassLoader(); 117 InputStream is = loader.getResourceAsStream("jnp.properties"); 118 System.getProperties().load(is); 119 } 120 catch (Exception e) 121 { 122 } 124 125 setPort(Integer.getInteger("jnp.port",getPort()).intValue()); 127 setRmiPort(Integer.getInteger("jnp.rmiPort",getRmiPort()).intValue()); 128 log = Logger.getLogger(categoryName); 129 } 130 131 public Naming getServer() 133 { 134 return theServer; 135 } 136 137 public ThreadPool getLookupPool() 138 { 139 return lookupPool; 140 } 141 public void setLookupPool(ThreadPool lookupPool) 142 { 143 this.lookupPool = lookupPool; 144 } 145 146 public void setNamingProxy(Object proxy) 147 throws IOException 148 { 149 serverStub = new MarshalledObject (proxy); 150 } 151 152 public void setRmiPort(int p) 153 { 154 rmiPort = p; 155 } 156 public int getRmiPort() 157 { 158 return rmiPort; 159 } 160 161 public void setPort(int p) 162 { 163 port = p; 164 } 165 public int getPort() 166 { 167 return port; 168 } 169 170 public String getBindAddress() 171 { 172 String address = null; 173 if( bindAddress != null ) 174 address = bindAddress.getHostAddress(); 175 return address; 176 } 177 public void setBindAddress(String host) throws UnknownHostException 178 { 179 if( host == null || host.length() == 0 ) 180 bindAddress = null; 181 else 182 bindAddress = InetAddress.getByName(host); 183 } 184 185 public String getRmiBindAddress() 186 { 187 String address = null; 188 if( rmiBindAddress != null ) 189 address = rmiBindAddress.getHostAddress(); 190 return address; 191 } 192 public void setRmiBindAddress(String host) throws UnknownHostException 193 { 194 if( host == null || host.length() == 0 ) 195 rmiBindAddress = null; 196 else 197 rmiBindAddress = InetAddress.getByName(host); 198 } 199 200 public int getBacklog() 201 { 202 return backlog; 203 } 204 public void setBacklog(int backlog) 205 { 206 if( backlog <= 0 ) 207 backlog = 50; 208 this.backlog = backlog; 209 } 210 211 public boolean getInstallGlobalService() 212 { 213 return InstallGlobalService; 214 } 215 public void setInstallGlobalService(boolean flag) 216 { 217 this.InstallGlobalService = flag; 218 } 219 220 public String getClientSocketFactory() 221 { 222 return clientSocketFactoryName; 223 } 224 public void setClientSocketFactory(String factoryClassName) 225 throws ClassNotFoundException , InstantiationException , IllegalAccessException 226 { 227 this.clientSocketFactoryName = factoryClassName; 228 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 229 Class clazz = loader.loadClass(clientSocketFactoryName); 230 clientSocketFactory = (RMIClientSocketFactory ) clazz.newInstance(); 231 } 232 233 public String getServerSocketFactory() 234 { 235 return serverSocketFactoryName; 236 } 237 public void setServerSocketFactory(String factoryClassName) 238 throws ClassNotFoundException , InstantiationException , IllegalAccessException 239 { 240 this.serverSocketFactoryName = factoryClassName; 241 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 242 Class clazz = loader.loadClass(serverSocketFactoryName); 243 serverSocketFactory = (RMIServerSocketFactory ) clazz.newInstance(); 244 } 245 246 public void setJNPServerSocketFactory(String factoryClassName) 247 throws ClassNotFoundException , InstantiationException , IllegalAccessException 248 { 249 this.jnpServerSocketFactoryName = factoryClassName; 250 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 251 Class clazz = loader.loadClass(jnpServerSocketFactoryName); 252 jnpServerSocketFactory = (ServerSocketFactory ) clazz.newInstance(); 253 } 254 255 public void start() 256 throws Exception 257 { 258 if( theServer == null ) 260 { 261 theServer = new NamingServer(); 262 if( InstallGlobalService == true ) 263 { 264 NamingContext.setLocal(theServer); 266 } 267 } 268 269 initCustomSocketFactories(); 271 275 if( this.serverStub == null && this.port >= 0 ) 276 { 277 initJnpInvoker(); 278 } 279 if( this.serverStub != null ) 281 { 282 initBootstrapListener(); 283 } 284 } 285 286 public void stop() 287 { 288 try 289 { 290 if( serverSocket != null ) 292 { 293 ServerSocket s = serverSocket; 294 serverSocket = null; 295 s.close(); 296 } 297 if( isStubExported == true ) 298 UnicastRemoteObject.unexportObject(theServer, false); 299 } 300 catch (Exception e) 301 { 302 log.error("Exception during shutdown", e); 303 } 304 } 305 306 309 protected void initJnpInvoker() throws IOException 310 { 311 log.debug("Creating NamingServer stub, theServer="+theServer 312 +",rmiPort="+rmiPort+",clientSocketFactory="+clientSocketFactory 313 +",serverSocketFactory="+serverSocketFactory); 314 Remote stub = UnicastRemoteObject.exportObject(theServer, rmiPort, 315 clientSocketFactory, serverSocketFactory); 316 log.debug("NamingServer stub: "+stub); 317 serverStub = new MarshalledObject (stub); 318 } 319 320 323 protected void initBootstrapListener() 324 { 325 try 327 { 328 if( jnpServerSocketFactory == null ) 330 jnpServerSocketFactory = ServerSocketFactory.getDefault(); 331 serverSocket = jnpServerSocketFactory.createServerSocket(port, backlog, bindAddress); 332 if( port == 0 ) 334 port = serverSocket.getLocalPort(); 335 String msg = "JNDI bootstrap JNP=" + bindAddress + ":" + port 336 + ", RMI=" + bindAddress + ":" + rmiPort 337 + ", backlog="+backlog; 338 339 if (clientSocketFactory == null) 340 msg+= ", no client SocketFactory"; 341 else 342 msg+= ", Client SocketFactory="+clientSocketFactory.getClass().toString(); 343 344 if (serverSocketFactory == null) 345 msg+= ", no server SocketFactory"; 346 else 347 msg+= ", Server SocketFactory="+serverSocketFactory.getClass().toString(); 348 349 log.info(msg); 350 } 351 catch (IOException e) 352 { 353 log.error("Could not start on port " + port, e); 354 } 355 356 if( lookupPool == null ) 357 lookupPool = new BasicThreadPool("NamingBootstrap Pool"); 358 AcceptHandler handler = new AcceptHandler(); 359 lookupPool.run(handler); 360 } 361 362 365 protected void initCustomSocketFactories() 366 { 367 InetAddress addr = rmiBindAddress != null ? rmiBindAddress : bindAddress; 369 370 if (clientSocketFactory != null) 371 { 372 try 374 { 375 Class csfClass = clientSocketFactory.getClass(); 376 Class [] parameterTypes = {String .class}; 377 Method m = csfClass.getMethod("setBindAddress", parameterTypes); 378 Object [] args = {addr.getHostAddress()}; 379 m.invoke(clientSocketFactory, args); 380 } 381 catch (NoSuchMethodException e) 382 { 383 log.warn("Socket factory does not support setBindAddress(String)"); 384 } 386 catch (Exception e) 387 { 388 log.warn("Failed to setBindAddress="+addr+" on socket factory", e); 389 } 391 } 392 393 try 394 { 395 if (serverSocketFactory == null) 396 serverSocketFactory = new DefaultSocketFactory(addr); 397 else 398 { 399 if (addr != null) 400 { 401 try 403 { 404 Class ssfClass = serverSocketFactory.getClass(); 405 Class [] parameterTypes = {String .class}; 406 Method m = ssfClass.getMethod("setBindAddress", parameterTypes); 407 Object [] args = {addr.getHostAddress()}; 408 m.invoke(serverSocketFactory, args); 409 } 410 catch (NoSuchMethodException e) 411 { 412 log.warn("Socket factory does not support setBindAddress(String)"); 413 } 415 catch (Exception e) 416 { 417 log.warn("Failed to setBindAddress="+addr+" on socket factory", e); 418 } 420 } 421 } 422 } 423 catch (Exception e) 424 { 425 log.error("operation failed", e); 426 serverSocketFactory = null; 427 } 428 } 429 430 private class AcceptHandler implements Runnable 431 { 432 public void run() 433 { 434 boolean trace = log.isTraceEnabled(); 435 while( serverSocket != null ) 436 { 437 Socket socket = null; 438 try 440 { 441 socket = serverSocket.accept(); 442 if( trace ) 443 log.trace("Accepted bootstrap client: "+socket); 444 BootstrapRequestHandler handler = new BootstrapRequestHandler(socket); 445 lookupPool.run(handler); 446 } 447 catch (IOException e) 448 { 449 if (serverSocket == null) 451 return; 452 log.error("Naming accept handler stopping", e); 453 } 454 catch(Throwable e) 455 { 456 log.error("Unexpected exception during accept", e); 457 } 458 } 459 } 460 } 461 462 private class BootstrapRequestHandler implements Runnable 463 { 464 private Socket socket; 465 BootstrapRequestHandler(Socket socket) 466 { 467 this.socket = socket; 468 } 469 public void run() 470 { 471 try 473 { 474 OutputStream os = socket.getOutputStream(); 475 ObjectOutputStream out = new ObjectOutputStream (os); 476 out.writeObject(serverStub); 477 out.close(); 478 } 479 catch (IOException ex) 480 { 481 log.debug("Error writing response to " + socket.getInetAddress(), ex); 482 } 483 finally 484 { 485 try 486 { 487 socket.close(); 488 } catch (IOException e) 489 { 490 } 491 } 492 } 493 } 494 } 495 | Popular Tags |