User: patriot1burke
  Date: 01/09/13 20:54:25

  Added:       src/main/org/jboss/ha/jndi HANamingServiceMBean.java
                        HANamingService.java HAJNDI.java
  Log:
  clustering.  Work in Progress.   This code probably doesn't work, but it
  does compile
  
  Revision  Changes    Path
  1.1                  jbossmx/src/main/org/jboss/ha/jndi/HANamingServiceMBean.java
  
  Index: HANamingServiceMBean.java
  ===================================================================
  package org.jboss.ha.jndi;
  import java.net.UnknownHostException;
  
  public interface HANamingServiceMBean
     extends org.jboss.system.ServiceMBean
  {
     // Constants -----------------------------------------------------
     public static final String OBJECT_NAME = ":service=HANaming";
      
     // Public --------------------------------------------------------
     // Public --------------------------------------------------------
     public String getPartitionName();
     public void setPartitionName(String partitionName);
  
  
     public void setRmiPort(int p);
     public int getRmiPort();
     
     public void setPort(int p);
     public int getPort();
  
     public String getBindAddress();
     public void setBindAddress(String host) throws UnknownHostException;
  
     public int getBacklog();
     public void setBacklog(int backlog);
  
     public String getClientSocketFactory();
     public void setClientSocketFactory(String factoryClassName)
             throws ClassNotFoundException, InstantiationException, 
IllegalAccessException;
     public String getServerSocketFactory();
     public void setServerSocketFactory(String factoryClassName)
             throws ClassNotFoundException, InstantiationException, 
IllegalAccessException;
  
  }
  
  
  
  1.1                  jbossmx/src/main/org/jboss/ha/jndi/HANamingService.java
  
  Index: HANamingService.java
  ===================================================================
  /*
   * Distributable under LGPL license.
   * See terms of license at gnu.org.
   *
   * Copyright 1999 by dreamBean Software,
   * All rights reserved.
   */
  package org.jboss.ha.jndi;
  
  import java.io.FileNotFoundException;
  import java.io.InputStream;
  import java.io.IOException;
  import java.io.ObjectOutputStream;
  import java.lang.reflect.Proxy;
  import java.net.InetAddress;
  import java.net.Socket;
  import java.net.ServerSocket;
  import java.net.UnknownHostException;
  import java.net.URL;
  import java.rmi.Remote;
  import java.rmi.server.RemoteStub;
  import java.rmi.RemoteException;
  import java.rmi.MarshalledObject;
  import java.rmi.server.RMIClientSocketFactory;
  import java.rmi.server.RMIServerSocketFactory;
  import java.rmi.server.UnicastRemoteObject;
  import java.util.Properties;
  import java.util.ArrayList;
  
  import javax.naming.*;
  import javax.management.MBeanServer;
  import javax.management.ObjectName;
  import javax.management.MalformedObjectNameException;
  import javax.naming.Context;
  import javax.naming.InitialContext;
  import javax.naming.RefAddr;
  import javax.naming.Reference;
  import javax.naming.StringRefAddr;
  
  import org.apache.log4j.Category;
  import org.apache.log4j.PropertyConfigurator;
  
  import org.jnp.interfaces.Naming;
  import org.jnp.interfaces.NamingContext;
  import org.jnp.interfaces.java.javaURLContextFactory;
  
  import org.jboss.logging.Logger;
  import org.jboss.system.ServiceMBeanSupport;
  
  import org.jboss.ha.DistributedReplicantManager;
  import org.jboss.ha.HARMITarget;
  import org.jboss.ha.HAPartition;
  import org.jboss.ha.RoundRobin;
  import org.jboss.ha.LoadBalancePolicy;
  
  /** A main() entry point for running the jnp naming service implementation as
   a standalone process.
   
   @author oberg
   @author [EMAIL PROTECTED]
   @version $Revision: 1.1 $
   */
  public class HANamingService extends ServiceMBeanSupport implements Runnable, 
HANamingServiceMBean, DistributedReplicantManager.ReplicantListener
  {
     // Constants -----------------------------------------------------
     
     // Attributes ----------------------------------------------------
     /** The Naming interface server implementation */
     protected HAJNDI theServer;
     /** The jnp server socket through which the HAJNDI stub is vended */
     protected ServerSocket serverSocket;
     /** An optional custom client socket factory */
     protected RMIClientSocketFactory clientSocketFactory;
     /** An optional custom server socket factory */
     protected RMIServerSocketFactory serverSocketFactory;
     /** The class name of the optional custom client socket factory */
     protected String clientSocketFactoryName;
     /** The class name of the optional custom server socket factory */
     protected String serverSocketFactoryName;
     /** The interface to bind to. This is useful for multi-homed hosts
      that want control over which interfaces accept connections.
      */
     protected InetAddress bindAddress;
     /** The serverSocket listen queue depth */
     protected int backlog = 50;
     /** The jnp protocol listening port. The default is 1100, the same as
      the RMI registry default port. */
     protected int port = 1100;
     /** The RMI port on which the Naming implementation will be exported. The
      default is 0 which means use any available port. */
     protected int rmiPort = 0;
     protected Category log;
  
     protected HARMITarget hatarget;
     protected Naming hastub;
     protected RemoteStub stub;
     protected HAPartition partition;
     protected DistributedReplicantManager replicantManager;
     protected String partitionName = "DefaultPartition";
  
     // Public --------------------------------------------------------
     
     public String getName()
     {
        return "HA-JNDI";
     }
  
     public ObjectName getObjectName(MBeanServer server, ObjectName name)
        throws javax.management.MalformedObjectNameException
     {
        return new ObjectName(OBJECT_NAME);
     }
     
     public String getPartitionName()
     {
        return partitionName;
     }
  
     public void setPartitionName(String partitionName)
     {
        this.partitionName = partitionName;
     }
  
     public void setRmiPort(int p)
     {
        rmiPort = p;
     }
     public int getRmiPort()
     {
        return rmiPort;
     }
  
     public void setPort(int p)
     {
        port = p;
     }
     public int getPort()
     {
        return port;
     }
  
     public String getBindAddress()
     {
        String address = null;
        if( bindAddress != null )
           address = bindAddress.getHostAddress();
        return address;
     }
     public void setBindAddress(String host) throws UnknownHostException
     {
        bindAddress = InetAddress.getByName(host);
     }
  
     public int getBacklog()
     {
        return backlog;
     }
     public void setBacklog(int backlog)
     {
        if( backlog <= 0 )
           backlog = 50;
        this.backlog = backlog;
     }
  
     public String getClientSocketFactory()
     {
        return serverSocketFactoryName;
     }
     public void setClientSocketFactory(String factoryClassName)
        throws ClassNotFoundException, InstantiationException, IllegalAccessException
     {
        this.clientSocketFactoryName = factoryClassName;
        ClassLoader loader = Thread.currentThread().getContextClassLoader();
        Class clazz = loader.loadClass(clientSocketFactoryName);
        clientSocketFactory = (RMIClientSocketFactory) clazz.newInstance();
     }
     
     public String getServerSocketFactory()
     {
        return serverSocketFactoryName;
     }
     public void setServerSocketFactory(String factoryClassName)
        throws ClassNotFoundException, InstantiationException, IllegalAccessException
     {
        this.serverSocketFactoryName = factoryClassName;
        ClassLoader loader = Thread.currentThread().getContextClassLoader();
        Class clazz = loader.loadClass(serverSocketFactoryName);
        serverSocketFactory = (RMIServerSocketFactory) clazz.newInstance();
     }
     
     public void replicantsChanged(String key, ArrayList newReplicants)
     {
        if (key.equals("HA-JNDI"))
        {
           synchronized(this.hatarget)
           {
              hatarget.setTargets(newReplicants.toArray());
           }
        }
        else if (key.equals("DistributedReplicantManager"))
        {
           synchronized(this.hatarget)
           {
              hatarget.setManagers(newReplicants);
           }
        }
     }
  
     public void initService() throws Exception
     {
        partition = (HAPartition)new InitialContext().lookup("/HAPartition/" + 
partitionName);
        replicantManager = partition.getDistributedReplicantManager();
     }
     
     public void startService()
        throws Exception
     {
        log.info("Starting HA-JNDI server");
  
        // Create remote object
        theServer = new HAJNDI(partition);
        ((HAJNDI)theServer).init();
  
        // Export RMI server
        stub = (RemoteStub)UnicastRemoteObject.exportObject(theServer, rmiPort, 
clientSocketFactory, serverSocketFactory);
  
        // Bind replicant to DistributedReplicantManager
        replicantManager.add("HA-JNDI", stub);
  
        // Get all replicants for DistributedReplicantManager and HA-JNDI
        ArrayList managers = 
this.replicantManager.lookupReplicants("DistributedReplicantManager");
        ArrayList replicants = this.replicantManager.lookupReplicants("HA-JNDI");
        
        // Create HA-JNDI RMI Proxy 
        this.hatarget = new HARMITarget("HA-JNDI", 100000, 500000, managers, 
replicants.toArray(), new RoundRobin());
        this.hastub = (Naming)Proxy.newProxyInstance(
           Naming.class.getClassLoader(),
           new Class[] { Naming.class },
           this.hatarget);
  
        // Register with replicantManager as listener for HA-JNDI replicants
        replicantManager.registerListener("HA-JNDI", this);
  
        // Start listener
        try
        {
           serverSocket = null;
           serverSocket = new ServerSocket(port, backlog, bindAddress);
           // If an anonymous port was specified get the actual port used
           if( port == 0 )
              port = serverSocket.getLocalPort();
           String msg = "Started hajndiPort=" + port +", rmiPort=" + rmiPort
           + ", backlog="+backlog+", bindAddress="+bindAddress
           + ", Client SocketFactory="+clientSocketFactory+", Server 
SocketFactory="+serverSocketFactory;
           log.info(msg);
           listen();
        } catch (IOException e)
        {
           log.error("Could not start on port " + port, e);
        }
     }
     
     public void stopService()
     {
        try
        {
           log.info("Stopping");
           
           // Unexport server
           UnicastRemoteObject.unexportObject(theServer, false);
           
           // Stop listener
           ServerSocket s = serverSocket;
           serverSocket = null;
           s.close();
           log.info("Stopped");
        } catch (Exception e)
        {
           log.error("Exception during shutdown", e);
        }
     }
     
     // Runnable implementation ---------------------------------------
     public void run()
     {
        Socket socket = null;
        
        // Accept a connection
        try
        {
           socket = serverSocket.accept();
        } catch (IOException e)
        {
           if (serverSocket == null) return; // Stopped by normal means
           log.error("Naming stopped", e);
           log.info("Restarting naming");
           try
           {
              start();
           } catch (Exception ex)
           {
              log.error("Restart failed", ex);
              return;
           }
        }
  
        // Create a new thread to accept the next connection
        listen();
        
        // Return the naming server stub
        try
        {
           ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
           synchronized(hatarget)
           {
              out.writeObject(new MarshalledObject(hastub));
           }
        }
        catch (IOException ex)
        {
           log.error("Error writing response", ex);
        }
        finally
        {
           try
           {
              socket.close();
           } catch (IOException e)
           {
           }
        }
     }
     
     // Protected -----------------------------------------------------
     
     protected void listen()
     {
        Thread t = new Thread(this, "HA-JNDI Server");
        t.start();
     }
     
     // Private -------------------------------------------------------
     
     // Inner classes -------------------------------------------------
  }
  
  
  
  1.1                  jbossmx/src/main/org/jboss/ha/jndi/HAJNDI.java
  
  Index: HAJNDI.java
  ===================================================================
  /*
   * Distributable under LGPL license.
   * See terms of license at gnu.org.
   *
   * Copyright 1999 by dreamBean Software,
   * All rights reserved.
   */
  package org.jboss.ha.jndi;
  
  import java.io.Serializable;
  import java.io.ObjectStreamException;
  import java.io.NotSerializableException;
  import java.rmi.RemoteException;
  import java.rmi.server.UnicastRemoteObject;
  import java.rmi.server.RemoteObject;
  import java.util.Enumeration;
  import java.util.Collection;
  import java.util.Iterator;
  import java.util.Hashtable;
  import java.util.Vector;
  import java.util.ArrayList;
  
  import javax.naming.Context;
  import javax.naming.Name;
  import javax.naming.NameClassPair;
  import javax.naming.Binding;
  import javax.naming.Reference;
  import javax.naming.NamingEnumeration;
  import javax.naming.InvalidNameException;
  import javax.naming.NamingException;
  import javax.naming.InvalidNameException;
  import javax.naming.NameNotFoundException;
  import javax.naming.NotContextException;
  import javax.naming.NameAlreadyBoundException;
  import javax.naming.CannotProceedException;
  import javax.naming.spi.ResolveResult;
  import javax.naming.InitialContext;
  
  import org.jnp.interfaces.*;
  import org.jnp.server.NamingServer;
  import org.jboss.ha.HAPartition;
  import org.jboss.ha.HARMITarget;
  /**
   *   <description> 
   *      
   *   @see <related>
   *   @author $Author: patriot1burke $
   *   @version $Revision: 1.1 $
   */
  public class HAJNDI extends NamingServer
     implements Naming, java.io.Serializable, HAPartition.HAPartitionStateTransfer
  {
     private HAPartition partition;
     public HAJNDI(HAPartition partition)
        throws NamingException
     {
        super();
        this.partition = partition;
     }
     
     // Public --------------------------------------------------------
  
     public void init()
     {
        partition.registerRPCHandler("HA-JNDI", this);
        partition.subscribeToStateTransferEvents("HA-JNDI", this);
     }
  
     public Serializable getCurrentState()
     {
        return table;
     }
  
     public void setCurrentState(Serializable newState)
     {
        table.clear();
        table.putAll((Hashtable)newState);
     }
  
     // Naming implementation -----------------------------------------
     
  
     public synchronized void bind(Name name, Object obj, String className)
        throws NamingException
     {
        super.bind(name, obj, className);
        
        // if we get here, this means we can do it on every node.
        Object[] args = new Object[3];
        args[0] = name;
        args[1] = obj;
        args[2] = className;
        try
        {
           partition.callMethodOnCluster("HA-JNDI", "bind", args, true);
        }
        catch (Exception ignored) {
           // FIXME is this right?
        }
     }
  
     public synchronized void rebind(Name name, Object obj, String className)
        throws NamingException
     {
        super.rebind(name, obj, className);
        
        // if we get here, this means we can do it on every node.
        Object[] args = new Object[3];
        args[0] = name;
        args[1] = obj;
        args[2] = className;
        try
        {
           partition.callMethodOnCluster("HA-JNDI", "rebind", args, true);
        }
        catch (Exception ignored)
        {
           // FIXME is this right?
        }
     }
     
     public synchronized void unbind(Name name)
        throws NamingException
     {
        super.unbind(name);
        
        // if we get here, this means we can do it on every node.
        Object[] args = new Object[1];
        args[0] = name;
        try
        {
           partition.callMethodOnCluster("HA-JNDI", "unbind", args, true);
        }
        catch (Exception ignored)
        {
           // FIXME is this right?
        }
     }
  
     public Object lookup(Name name)
        throws NamingException
     {
        Object result = null;
        try
        {
           result = super.lookup(name);
        }
        catch (NameNotFoundException ex)
        {
           try
           {
              // not found in global jndi, look in local.
              result = lookupLocally(name);
           }
           catch (NameNotFoundException nnfe)
           {
              // if we get here, this means we can do it on every node.
              Object[] args = new Object[1];
              args[0] = name;
              ArrayList rsp = null;
              try
              {
                 rsp = partition.callMethodOnCluster("HA-JNDI", "lookupLocally", args, 
true);
              }
              catch (Exception ignored) {}
              if (rsp == null || rsp.size() == 0) throw new NameNotFoundException();
              result = rsp.get(0);
           }
              
        }
        return result;
     }
  
     public Object lookupLocally(Name name) throws NamingException
     {
        return new InitialContext().lookup(name);
     }
  
     protected ArrayList enum2list(NamingEnumeration en)
     {
        ArrayList rtn = new ArrayList();
        try
        {
           while (en.hasMore())
           {
              rtn.add(en.next());
           }
           en.close();
        }
        catch (NamingException ignored) {}
        return rtn;
     }
     
     public Collection list(Name name)
        throws NamingException
     {
        Collection result = null;
        try
        {
           result = super.list(name);
        }
        catch (NameNotFoundException ex)
        {
           // not found in global jndi, look in local.
           result =  enum2list(new InitialContext().list(name));
        }
        return result;
     }
      
     public Collection listBindings(Name name)
        throws NamingException
     {
        Collection result = null;
        try
        {
           result = super.listBindings(name);
        }
        catch (NameNotFoundException ex)
        {
           // not found in global jndi, look in local.
           result =  enum2list(new InitialContext().listBindings(name));
        }
        return result;
     }
     
  }
  
  
  

_______________________________________________
Jboss-development mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/jboss-development

Reply via email to