Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*     */ package org.jnp.server;
  /*     */ 
  /*     */ import java.io.IOException;
  /*     */ import java.io.InputStream;
  /*     */ import java.io.ObjectOutputStream;
  /*     */ import java.io.OutputStream;
  /*     */ import java.lang.reflect.Method;
  /*     */ import java.net.InetAddress;
  /*     */ import java.net.ServerSocket;
 /*     */ import java.net.Socket;
 /*     */ import java.net.UnknownHostException;
 /*     */ import java.rmi.MarshalledObject;
 /*     */ import java.rmi.Remote;
 /*     */ import java.rmi.server.RMIClientSocketFactory;
 /*     */ import java.rmi.server.RMIServerSocketFactory;
 /*     */ import java.rmi.server.UnicastRemoteObject;
 /*     */ import java.util.Properties;
 /*     */ import javax.net.ServerSocketFactory;
 /*     */ import org.jboss.logging.Logger;
 /*     */ import org.jboss.util.threadpool.ThreadPool;
 /*     */ import org.jnp.interfaces.Naming;
 /*     */ 
 /*     */ public class Main
 /*     */   implements MainMBean
 /*     */ {
 /*     */   protected NamingBean theServer;
 /*     */   protected MarshalledObject serverStub;
 /*     */   protected boolean isStubExported;
 /*     */   protected ServerSocket serverSocket;
 /*     */   protected RMIClientSocketFactory clientSocketFactory;
 /*     */   protected RMIServerSocketFactory serverSocketFactory;
 /*     */   protected ServerSocketFactory jnpServerSocketFactory;
 /*     */   protected String clientSocketFactoryName;
 /*     */   protected String serverSocketFactoryName;
 /*     */   protected String jnpServerSocketFactoryName;
 /*     */   protected InetAddress bindAddress;
 /*     */   protected InetAddress rmiBindAddress;
 /*  85 */   protected int backlog = 50;
 /*     */ 
 /*  88 */   protected int port = 1099;
 /*     */ 
 /*  91 */   protected int rmiPort = 0;
 /*     */ 
 /*  93 */   protected boolean InstallGlobalService = true;
 /*     */ 
 /*  95 */   protected boolean UseGlobalService = true;
 /*     */   protected Logger log;
 /*     */   protected ThreadPool lookupPool;
 /*     */ 
 /*     */   public static void main(String[] args)
 /*     */     throws Exception
 /*     */   {
 /* 104 */     new Main().start();
 /*     */   }
 /*     */ 
 /*     */   public Main()
 /*     */   {
 /* 110 */     this("org.jboss.naming.Naming");
 /*     */   }
 /*     */ 
 /*     */   public Main(String categoryName)
 /*     */   {
 /*     */     try
 /*     */     {
 /* 117 */       ClassLoader loader = getClass().getClassLoader();
 /* 118 */       InputStream is = loader.getResourceAsStream("jnp.properties");
 /* 119 */       System.getProperties().load(is);
 /*     */     }
 /*     */     catch (Exception e)
 /*     */     {
 /*     */     }
 /*     */ 
 /* 127 */     setPort(Integer.getInteger("jnp.port"getPort()).intValue());
 /* 128 */     setRmiPort(Integer.getInteger("jnp.rmiPort"getRmiPort()).intValue());
 /* 129 */     this. = Logger.getLogger(categoryName);
 /*     */   }
 /*     */ 
 /*     */   public NamingBean getNamingInfo()
 /*     */   {
 /* 135 */     return this.;
 /*     */   }
 /*     */ 
 /*     */   public void setNamingInfo(NamingBean info) {
 /* 139 */     this. = info;
 /*     */   }
 /*     */ 
 /*     */   public ThreadPool getLookupPool()
 /*     */   {
 /* 144 */     return this.;
 /*     */   }
 /*     */ 
 /*     */   public void setLookupPool(ThreadPool lookupPool) {
 /* 148 */     this. = lookupPool;
 /*     */   }
 /*     */ 
 /*     */   public void setNamingProxy(Object proxy)
 /*     */     throws IOException
/*     */   {
/* 154 */     this. = new MarshalledObject(proxy);
/*     */   }
/*     */ 
/*     */   public void setRmiPort(int p)
/*     */   {
/* 159 */     this. = p;
/*     */   }
/*     */ 
/*     */   public int getRmiPort() {
/* 163 */     return this.;
/*     */   }
/*     */ 
/*     */   public void setPort(int p)
/*     */   {
/* 168 */     this. = p;
/*     */   }
/*     */ 
/*     */   public int getPort() {
/* 172 */     return this.;
/*     */   }
/*     */ 
/*     */   public String getBindAddress()
/*     */   {
/* 177 */     String address = null;
/* 178 */     if (this. != null)
/* 179 */       address = this..getHostAddress();
/* 180 */     return address;
/*     */   }
/*     */ 
/*     */   public void setBindAddress(String hostthrows UnknownHostException {
/* 184 */     if ((host == null) || (host.length() == 0))
/* 185 */       this. = null;
/*     */     else
/* 187 */       this. = InetAddress.getByName(host);
/*     */   }
/*     */ 
/*     */   public String getRmiBindAddress()
/*     */   {
/* 192 */     String address = null;
/* 193 */     if (this. != null)
/* 194 */       address = this..getHostAddress();
/* 195 */     return address;
/*     */   }
/*     */ 
/*     */   public void setRmiBindAddress(String hostthrows UnknownHostException {
/* 199 */     if ((host == null) || (host.length() == 0))
/* 200 */       this. = null;
/*     */     else
/* 202 */       this. = InetAddress.getByName(host);
/*     */   }
/*     */ 
/*     */   public int getBacklog()
/*     */   {
/* 207 */     return this.;
/*     */   }
/*     */ 
/*     */   public void setBacklog(int backlog) {
/* 211 */     if (backlog <= 0)
/* 212 */       backlog = 50;
/* 213 */     this. = backlog;
/*     */   }
/*     */ 
/*     */   public boolean getInstallGlobalService()
/*     */   {
/* 218 */     return this.;
/*     */   }
/*     */ 
/*     */   public void setInstallGlobalService(boolean flag) {
/* 222 */     this. = flag;
/*     */   }
/*     */ 
/*     */   public boolean getUseGlobalService() {
/* 226 */     return this.;
/*     */   }
/*     */ 
/*     */   public void setUseGlobalService(boolean flag) {
/* 230 */     this. = flag;
/*     */   }
/*     */ 
/*     */   public String getClientSocketFactory()
/*     */   {
/* 235 */     return this.;
/*     */   }
/*     */ 
/*     */   public void setClientSocketFactory(String factoryClassNamethrows ClassNotFoundExceptionInstantiationExceptionIllegalAccessException
/*     */   {
/* 240 */     this. = factoryClassName;
/* 241 */     ClassLoader loader = Thread.currentThread().getContextClassLoader();
/* 242 */     Class clazz = loader.loadClass(this.);
/* 243 */     this. = ((RMIClientSocketFactory)clazz.newInstance());
/*     */   }
/*     */ 
/*     */   {
/* 248 */     return this.;
/*     */   }
/*     */ 
/*     */   public void setClientSocketFactoryBean(RMIClientSocketFactory factory) {
/* 252 */     this. = factory;
/*     */   }
/*     */ 
/*     */   public String getServerSocketFactory()
/*     */   {
/* 257 */     return this.;
/*     */   }
/*     */ 
/*     */   public void setServerSocketFactory(String factoryClassNamethrows ClassNotFoundExceptionInstantiationExceptionIllegalAccessException
/*     */   {
/* 262 */     this. = factoryClassName;
/* 263 */     ClassLoader loader = Thread.currentThread().getContextClassLoader();
/* 264 */     Class clazz = loader.loadClass(this.);
/* 265 */     this. = ((RMIServerSocketFactory)clazz.newInstance());
/*     */   }
/*     */ 
/*     */   {
/* 270 */     return this.;
/*     */   }
/*     */ 
/*     */   public void setServerSocketFactoryBean(RMIServerSocketFactory factory) {
/* 274 */     this. = factory;
/*     */   }
/*     */ 
/*     */   public String getJNPServerSocketFactory()
/*     */   {
/* 279 */     return this.;
/*     */   }
/*     */ 
/*     */   public void setJNPServerSocketFactory(String factoryClassNamethrows ClassNotFoundExceptionInstantiationExceptionIllegalAccessException
/*     */   {
/* 284 */     this. = factoryClassName;
/* 285 */     ClassLoader loader = Thread.currentThread().getContextClassLoader();
/* 286 */     Class clazz = loader.loadClass(this.);
/* 287 */     this. = ((ServerSocketFactory)clazz.newInstance());
/*     */   }
/*     */ 
/*     */   {
/* 292 */     return this.;
/*     */   }
/*     */ 
/*     */   public void setJNPServerSocketFactoryBean(ServerSocketFactory factory) {
/* 296 */     this. = factory;
/*     */   }
/*     */ 
/*     */   public Naming getNamingInstance()
/*     */   {
/* 301 */     return this..getNamingInstance();
/*     */   }
/*     */ 
/*     */   public void start()
/*     */     throws Exception
/*     */   {
/* 309 */     initCustomSocketFactories();
/*     */ 
/* 314 */     if ((this. == null) && (this. >= 0))
/*     */     {
/* 316 */       initJnpInvoker();
/*     */     }
/*     */ 
/* 319 */     if (this. != null)
/*     */     {
/* 321 */       initBootstrapListener();
/*     */     }
/*     */   }
/*     */ 
/*     */   public void stop()
/*     */   {
/*     */     try
/*     */     {
/* 330 */       if (this. != null)
/*     */       {
/* 332 */         ServerSocket s = this.;
/* 333 */         this. = null;
/* 334 */         s.close();
/*     */       }
/* 336 */       if (this. == true)
/* 337 */         UnicastRemoteObject.unexportObject(this..getNamingInstance(), false);
/*     */     }
/*     */     catch (Exception e)
/*     */     {
/* 341 */       this..error("Exception during shutdown"e);
/*     */     }
/*     */   }
/*     */ 
/*     */   protected void initJnpInvoker()
/*     */     throws IOException
/*     */   {
/* 350 */     this..debug("Creating NamingServer stub, theServer=" + this. + ",rmiPort=" + this. + ",clientSocketFactory=" + this. + ",serverSocketFactory=" + this.);
/*     */ 
/* 354 */     Remote stub = UnicastRemoteObject.exportObject(getNamingInstance(), this.this.this.);
/*     */ 
/* 356 */     this..debug("NamingServer stub: " + stub);
/* 357 */     this. = new MarshalledObject(stub);
/*     */   }
/*     */ 
/*     */   protected void initBootstrapListener()
/*     */   {
/*     */     try
/*     */     {
/* 369 */       if (this. == null)
/* 370 */         this. = ServerSocketFactory.getDefault();
/* 371 */       this. = this..createServerSocket(this.this.this.);
/*     */ 
/* 373 */       if (this. == 0)
/* 374 */         this. = this..getLocalPort();
/* 375 */       String msg = "JNDI bootstrap JNP=" + this. + ":" + this. + ", RMI=" + this. + ":" + this. + ", backlog=" + this.;
/*     */ 
/* 379 */       if (this. == null)
/* 380 */         msg = msg + ", no client SocketFactory";
/*     */       else {
/* 382 */         msg = msg + ", Client SocketFactory=" + this..toString();
/*     */       }
/* 384 */       if (this. == null)
/* 385 */         msg = msg + ", no server SocketFactory";
/*     */       else {
/* 387 */         msg = msg + ", Server SocketFactory=" + this..toString();
/*     */       }
/* 389 */       this..debug(msg);
/*     */     }
/*     */     catch (IOException e)
/*     */     {
/* 393 */       this..error("Could not start on port " + this.e);
/*     */     }
/*     */ 
/* 396 */     if (this. == null)
/* 397 */       this. = new BasicThreadPool("NamingBootstrap Pool");
/* 398 */     AcceptHandler handler = new AcceptHandler(null);
/* 399 */     this..run(handler);
/*     */   }
/*     */ 
/*     */   protected void initCustomSocketFactories()
/*     */   {
/* 408 */     InetAddress addr = this. != null ? this. : this.;
/*     */ 
/* 410 */     if ((this. != null) && (addr != null))
/*     */     {
/*     */       try
/*     */       {
/* 415 */         Class csfClass = this..getClass();
/* 416 */         Class[] parameterTypes = { String.class };
/* 417 */         Method m = csfClass.getMethod("setBindAddress"parameterTypes);
/* 418 */         Object[] args = { addr.getHostAddress() };
/* 419 */         m.invoke(this.args);
/*     */       }
/*     */       catch (NoSuchMethodException e)
/*     */       {
/* 423 */         this..warn("Socket factory does not support setBindAddress(String)");
/*     */       }
/*     */       catch (Exception e)
/*     */       {
/* 428 */         this..warn("Failed to setBindAddress=" + addr + " on socket factory"e);
/*     */       }
/*     */ 
/*     */     }
/*     */ 
/*     */     try
/*     */     {
/* 435 */       if (this. == null) {
/* 436 */         this. = new DefaultSocketFactory(addr);
/*     */       }
/* 439 */       else if (addr != null)
/*     */       {
/*     */         try
/*     */         {
/* 444 */           Class ssfClass = this..getClass();
/* 445 */           Class[] parameterTypes = { String.class };
/* 446 */           Method m = ssfClass.getMethod("setBindAddress"parameterTypes);
/* 447 */           Object[] args = { addr.getHostAddress() };
/* 448 */           m.invoke(this.args);
/*     */         }
/*     */         catch (NoSuchMethodException e)
/*     */         {
/* 452 */           this..warn("Socket factory does not support setBindAddress(String)");
/*     */         }
/*     */         catch (Exception e)
/*     */         {
/* 457 */           this..warn("Failed to setBindAddress=" + addr + " on socket factory"e);
/*     */         }
/*     */ 
/*     */       }
/*     */ 
/*     */     }
/*     */     catch (Exception e)
/*     */     {
/* 465 */       this..error("operation failed"e);
/* 466 */       this. = null;
/*     */     }
/*     */   }
/*     */ 
/*     */   private class BootstrapRequestHandler
/*     */     implements Runnable
/*     */   {
/*     */     private Socket socket;
/*     */ 
/*     */     BootstrapRequestHandler(Socket socket)
/*     */     {
/* 507 */       this. = socket;
/*     */     }
/*     */ 
/*     */     public void run()
/*     */     {
/*     */       try
/*     */       {
/* 514 */         OutputStream os = this..getOutputStream();
/* 515 */         ObjectOutputStream out = new ObjectOutputStream(os);
/* 516 */         out.writeObject(Main.this.);
/* 517 */         out.close();
/*     */       }
/*     */       catch (IOException e)
/*     */       {
/* 521 */         Main.this..debug("Error writing response to " + this..getInetAddress(), ex);
/*     */       }
/*     */       finally
/*     */       {
/*     */         try
/*     */         {
/* 527 */           this..close();
/*     */         }
/*     */         catch (IOException e)
/*     */         {
/*     */         }
/*     */       }
/*     */     }
/*     */   }
/*     */ 
/*     */   private class AcceptHandler
/*     */     implements Runnable
/*     */   {
/*     */     private AcceptHandler()
/*     */     {
/*     */     }
/*     */ 
/*     */     public void run()
/*     */     {
/* 474 */       boolean trace = Main.this..isTraceEnabled();
/* 475 */       while (Main.this. != null)
/*     */       {
/* 477 */         Socket socket = null;
/*     */         try
/*     */         {
/* 481 */           socket = Main.this..accept();
/* 482 */           if (trace)
/* 483 */             Main.this..trace("Accepted bootstrap client: " + socket);
/* 484 */           Main.BootstrapRequestHandler handler = new Main.BootstrapRequestHandler(Main.thissocket);
/* 485 */           Main.this..run(handler);
/*     */         }
/*     */         catch (IOException e)
/*     */         {
/* 490 */           if (Main.this. == null)
/* 491 */             return;
/* 492 */           Main.this..error("Naming accept handler stopping"e);
/*     */         }
/*     */         catch (Throwable e)
/*     */         {
/* 496 */           Main.this..error("Unexpected exception during accept"e);
/*     */         }
/*     */       }
/*     */     }
/*     */   }
/*     */ }
/* Location:           /home/mnovotny/projects/EMBEDDED_JBOSS_BETA3_COMMUNITY/embedded/output/lib/embedded-jboss/lib/jboss-embedded-all.jar
 * Qualified Name:     org.jnp.server.Main
 * JD-Core Version:    0.6.0
 */
New to GrepCode? Check out our FAQ X