FileDocCategorySizeDatePackage
DetachedHANamingService.javaAPI DocJBoss 4.2.123707Fri Jul 13 20:52:36 BST 2007org.jboss.ha.jndi

DetachedHANamingService

public class DetachedHANamingService extends org.jboss.system.ServiceMBeanSupport implements DetachedHANamingServiceMBean
Management Bean for the protocol independent HA-JNDI service. This allows the naming service transport layer to be provided by a detached invoker service like JRMPInvokerHA + ProxyFactoryHA.
author
Bill Burke
author
Sacha Labourey
author
Scott.Stark@jboss.org
version
$Revision: 57188 $

Fields Summary
protected ServerSocket
bootstrapSocket
The jnp server socket through which the HAJNDI stub is vended
protected HAJNDI
theServer
The Naming interface server implementation
protected Map
marshalledInvocationMapping
The mapping from the long method hash to the Naming Method
protected org.jnp.interfaces.Naming
stub
The protocol stub returned to clients by the bootstrap lookup
protected org.jboss.ha.framework.interfaces.HAPartition
partition
The HAPartition used for the state transfer service
protected org.jboss.ha.framework.server.ClusterPartitionMBean
clusterPartition
The ClusterPartition with which we are associated.
protected String
partitionName
The partition name used to lookup the HAPartition binding
private ObjectName
proxyFactory
The proxy factory service that generates the Naming stub
protected InetAddress
bindAddress
The interface to bind to. This is useful for multi-homed hosts that want control over which interfaces accept connections.
protected int
backlog
The bootstrapSocket listen queue depth
protected int
port
The jnp protocol listening port. The default is 1100, the same as the RMI registry default port.
protected String
adGroupAddress
The autodiscovery multicast group
protected int
adGroupPort
The autodiscovery port
protected InetAddress
discoveryBindAddress
The interface to bind the Multicast socket for autodiscovery to
protected AutomaticDiscovery
autoDiscovery
The runable task for discovery request packets
protected boolean
discoveryDisabled
A flag indicating if autodiscovery should be disabled
protected int
autoDiscoveryTTL
The autodiscovery Multicast reply TTL
protected ServerSocketFactory
jnpServerSocketFactory
An optional custom server socket factory for the bootstrap lookup
protected String
jnpServerSocketFactoryName
The class name of the optional custom JNP server socket factory
protected org.jboss.util.threadpool.ThreadPool
lookupPool
The thread pool used to handle jnp stub lookup requests
Constructors Summary
public DetachedHANamingService()


   // Public --------------------------------------------------------

    
   
      // for JMX
   
Methods Summary
protected voidcreateService()

      boolean debug = log.isDebugEnabled();

      if (this.clusterPartition == null)
      {
         partition = findHAPartitionWithName(partitionName);
      }
      else 
      {
         partition = clusterPartition.getHAPartition();
         partitionName = partition.getPartitionName();
      }
      
      if (partition == null)
         throw new IllegalStateException("Cannot find partition '" + partitionName + "'");

      if (debug)
         log.debug("Initializing HAJNDI server on partition: " + partitionName);
      
      // Start HAJNDI service
      theServer = new HAJNDI(partition);
      log.debug("initialize HAJNDI");
      theServer.init();

      // Build the Naming interface method map
      HashMap tmpMap = new HashMap(13);
      Method[] methods = Naming.class.getMethods();
      for (int m = 0; m < methods.length; m++)
      {
         Method method = methods[m];
         Long hash = new Long(MarshalledInvocation.calculateHash(method));
         tmpMap.put(hash, method);
      }
      marshalledInvocationMapping = Collections.unmodifiableMap(tmpMap);
      
      // share instance for in-vm discovery
      NamingContext.setHANamingServerForPartition(partitionName, theServer);
   
protected voiddestroyService()

      log.debug("Destroying the HAJNDI service");
      theServer.destroy();
   
protected org.jboss.ha.framework.interfaces.HAPartitionfindHAPartitionWithName(java.lang.String name)

      HAPartition result = null;
      QueryExp exp = Query.and(Query.eq(Query.classattr(),
         Query.value(ClusterPartition.class.getName())),
         Query.match(Query.attr("PartitionName"),
            Query.value(name)));

      Set mbeans = this.getServer().queryMBeans(null, exp);
      if (mbeans != null && mbeans.size() > 0)
      {
         ObjectInstance inst = (ObjectInstance) (mbeans.iterator().next());
         ClusterPartitionMBean cp = (ClusterPartitionMBean) MBeanProxyExt.create(ClusterPartitionMBean.class,
            inst.getObjectName(),
            this.getServer());
         result = cp.getHAPartition();
      }

      return result;
   
public java.lang.StringgetAutoDiscoveryAddress()

      return this.adGroupAddress;
   
public java.lang.StringgetAutoDiscoveryBindAddress()

      String address = null;
      if (discoveryBindAddress != null)
         address = discoveryBindAddress.getHostAddress();
      return address;      
   
public intgetAutoDiscoveryGroup()

      return this.adGroupPort;
   
public intgetAutoDiscoveryTTL()

      return autoDiscoveryTTL;
   
public intgetBacklog()

      return backlog;
   
public java.lang.StringgetBindAddress()

      String address = null;
      if (bindAddress != null)
         address = bindAddress.getHostAddress();
      return address;
   
public org.jboss.ha.framework.server.ClusterPartitionMBeangetClusterPartition()

      return clusterPartition;
   
public booleangetDiscoveryDisabled()

      return this.discoveryDisabled;
   
public java.util.MapgetMethodMap()
Expose the Naming service interface mapping as a read-only attribute

return
A Map of the Naming interface
jmx:managed-attribute

      return marshalledInvocationMapping;
   
protected org.jnp.interfaces.NaminggetNamingProxy()
Get the Naming proxy for the transport. This version looks up the proxyFactory service Proxy attribute. Subclasses can override this to set the proxy another way.

return
The Naming proxy for the protocol used with the HAJNDI service

      Naming proxy = (Naming) server.getAttribute(proxyFactory, "Proxy");
      return proxy;
   
public java.lang.StringgetPartitionName()

      return partitionName;
   
public intgetPort()

      return port;
   
public javax.management.ObjectNamegetProxyFactoryObjectName()

      return proxyFactory;
   
protected voidinitBootstrapListener()
Bring up the bootstrap lookup port for obtaining the naming service proxy

      // Start listener
      try
      {
         // Get the default ServerSocketFactory is one was not specified
         if (jnpServerSocketFactory == null)
            jnpServerSocketFactory = ServerSocketFactory.getDefault();
         bootstrapSocket = jnpServerSocketFactory.createServerSocket(port, backlog, bindAddress);
         // If an anonymous port was specified get the actual port used
         if (port == 0)
            port = bootstrapSocket.getLocalPort();
         String msg = "Started ha-jndi bootstrap jnpPort=" + port
            + ", backlog=" + backlog + ", bindAddress=" + bindAddress;
         log.info(msg);
      }
      catch (IOException e)
      {
         log.error("Could not start on port " + port, e);
      }

      if (lookupPool == null)
         lookupPool = new BasicThreadPool("HANamingBootstrap Pool");
      AcceptHandler handler = new AcceptHandler();
      lookupPool.run(handler);
   
public java.lang.Objectinvoke(org.jboss.invocation.Invocation invocation)
Expose the Naming service via JMX to invokers.

param
invocation A pointer to the invocation object
return
Return value of method invocation.
throws
Exception Failed to invoke method.
jmx:managed-operation

      // Set the method hash to Method mapping
      if (invocation instanceof MarshalledInvocation)
      {
         MarshalledInvocation mi = (MarshalledInvocation) invocation;
         mi.setMethodMap(marshalledInvocationMapping);
      }
      // Invoke the Naming method via reflection
      Method method = invocation.getMethod();
      Object[] args = invocation.getArguments();
      Object value = null;
      try
      {
         value = method.invoke(theServer, args);
      }
      catch (InvocationTargetException e)
      {
         Throwable t = e.getTargetException();
         if (t instanceof Exception)
            throw (Exception) t;
         else
            throw new UndeclaredThrowableException(t, method.toString());
      }

      return value;
   
public voidsetAutoDiscoveryAddress(java.lang.String adAddress)

      this.adGroupAddress = adAddress;
   
public voidsetAutoDiscoveryBindAddress(java.lang.String address)

      discoveryBindAddress = InetAddress.getByName(address);
   
public voidsetAutoDiscoveryGroup(int adGroup)

      this.adGroupPort = adGroup;
   
public voidsetAutoDiscoveryTTL(int ttl)

      autoDiscoveryTTL = ttl;
   
public voidsetBacklog(int backlog)

      if (backlog <= 0)
         backlog = 50;
      this.backlog = backlog;
   
public voidsetBindAddress(java.lang.String host)

      bindAddress = InetAddress.getByName(host);
   
public voidsetClusterPartition(org.jboss.ha.framework.server.ClusterPartitionMBean clusterPartition)

      this.clusterPartition = clusterPartition;
   
public voidsetDiscoveryDisabled(boolean disable)

      this.discoveryDisabled = disable;
   
public voidsetJNPServerSocketFactory(java.lang.String factoryClassName)

      this.jnpServerSocketFactoryName = factoryClassName;
      ClassLoader loader = Thread.currentThread().getContextClassLoader();
      Class clazz = loader.loadClass(jnpServerSocketFactoryName);
      jnpServerSocketFactory = (ServerSocketFactory) clazz.newInstance();
   
public voidsetLookupPool(org.jboss.util.threadpool.BasicThreadPoolMBean poolMBean)

      lookupPool = poolMBean.getInstance();
   
public voidsetPartitionName(java.lang.String partitionName)

      this.partitionName = partitionName;
   
public voidsetPort(int p)

      port = p;
   
public voidsetProxyFactoryObjectName(javax.management.ObjectName proxyFactory)

      this.proxyFactory = proxyFactory;
   
public voidstartService(org.jboss.ha.framework.interfaces.HAPartition haPartition)

      this.partition = haPartition;
      this.startService();
   
protected voidstartService()

      log.debug("Obtaining the transport proxy");
      stub = this.getNamingProxy();
      this.theServer.setHAStub(stub);
      if (port >= 0)
      {
         log.debug("Starting HAJNDI bootstrap listener");
         initBootstrapListener();
      }

      // Automatic Discovery for unconfigured clients
      if (adGroupAddress != null && discoveryDisabled == false)
      {
         try
         {
            autoDiscovery = new AutomaticDiscovery();
            autoDiscovery.start();
            lookupPool.run(autoDiscovery);
         }
         catch (Exception e)
         {
            log.warn("Failed to start AutomaticDiscovery", e);
         }
      }
   
protected voidstopService()

      // un-share instance for in-vm discovery
      NamingContext.removeHANamingServerForPartition(partitionName);

      // Stop listener
      ServerSocket s = bootstrapSocket;
      bootstrapSocket = null;
      if (s != null)
      {
         log.debug("Closing the bootstrap listener");
         s.close();
      }

      // Stop HAJNDI service
      log.debug("Stopping the HAJNDI service");
      theServer.stop();

      log.debug("Stopping AutomaticDiscovery");
      if (autoDiscovery != null && discoveryDisabled == false)
         autoDiscovery.stop();