Package com.icentris.jmx

Source Code of com.icentris.jmx.MX4JRMI

package com.icentris.jmx;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.icentris.jmx.JMX;
import java.rmi.RemoteException;

import javax.management.JMException;
import javax.management.MBeanServer;
import javax.management.ObjectName;
import javax.naming.NamingException;
import mx4j.adaptor.rmi.jrmp.JRMPAdaptor;

/** An MX4J-specific class for getting the RMI server started.  I can't figure out how
* to restart the server after the first time, so this only tries the first time.
*/
public class MX4JRMI {
   /**
    * Logger for this class
    */
   private static final Log logger = LogFactory.getLog(MX4JRMI.class);

  private static boolean hasStarted = false;

  /** Cannot help you restart, keeps track if you've allready called the method and does
   * nothing after the first time! */
  public static void startRMIServer() throws JMException, RemoteException, NamingException {
    if ( hasStarted == false ) {
      // Set the JNDI name with which will be registered
      String jndiName = "jrmp";

      //Registry rmiRegistry = java.rmi.registry.LocateRegistry.getRegistry();
      //try { rmiRegistry.unbind(jndiName); } catch (java.rmi.RemoteException e) {}

      // get the MBeanServer
      MBeanServer server = JMX.getMBeanServer();

      // Create and start the naming service
      ObjectName naming = new ObjectName("Naming:type=rmiregistry");
         if (logger.isDebugEnabled()) {
            logger.debug("startRMIServer() - DEBUG: [start_MX4J_rmi] server.isRegistered(naming)=[" + server.isRegistered(naming) + "]"); //$NON-NLS-1$ //$NON-NLS-2$
         }
      if ( server.isRegistered(naming) ) {
        server.invoke(naming, "stop", null, null);
        server.unregisterMBean(naming);
      }

      // Create the JRMP adaptor
      ObjectName adaptor = new ObjectName("Adaptor:protocol=JRMP");
         if (logger.isDebugEnabled()) {
            logger.debug("startRMIServer() - DEBUG: [start_MX4J_rmi] server.isRegistered(adaptor)=[" + server.isRegistered(adaptor) + "]"); //$NON-NLS-1$ //$NON-NLS-2$
         }
      if ( server.isRegistered(adaptor) ) {
        server.unregisterMBean(adaptor);
      }
      server.createMBean("mx4j.tools.naming.NamingService", naming, null);
      //  try {
      //    java.rmi.registry.Registry registry = java.rmi.registry.LocateRegistry.getRegistry();
      //System.out.println("DEBUG: [start_MX4J_rmi] registry =[" + registry  + "]");
      //    //registry.list();
      //  } catch (java.rmi.ConnectException e) {
      //System.out.println("DEBUG: [start_MX4J_rmi] e=[" + e + "]");
      server.invoke(naming, "start", null, null);
      //  }

      JRMPAdaptor adaptorBean = new JRMPAdaptor();
      server.registerMBean(adaptorBean, adaptor);

      adaptorBean.setJNDIName(jndiName);

      // Optionally, you can specify the JNDI properties,
      // instead of having in the classpath a jndi.properties file
      adaptorBean.putJNDIProperty(javax.naming.Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.rmi.registry.RegistryContextFactory");
      adaptorBean.putJNDIProperty(javax.naming.Context.PROVIDER_URL, "rmi://localhost:1099");


      // Registers the JRMP adaptor in JNDI and starts it
      adaptorBean.start();
      hasStarted = true;
    }
  }
}
TOP

Related Classes of com.icentris.jmx.MX4JRMI

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.