Example usage for java.rmi.registry LocateRegistry createRegistry

List of usage examples for java.rmi.registry LocateRegistry createRegistry

Introduction

In this page you can find the example usage for java.rmi.registry LocateRegistry createRegistry.

Prototype

public static Registry createRegistry(int port) throws RemoteException 

Source Link

Document

Creates and exports a Registry instance on the local host that accepts requests on the specified port.

Usage

From source file:com.l2jfree.gameserver.elayne.RemoteAdministrationImpl.java

License:asdf

public void startServer() {
    if (Config.ALLOW_RMI_SERVER && _port != 0) {
        try {/*from ww  w .  j  a  va 2  s .c  o m*/
            _lReg = LocateRegistry.createRegistry(_port);
            _obj = new RemoteAdministrationImpl();

            if (_pass.isEmpty()) {
                _log.info("No password defined for RMI Server");
                _pass = generateRandomPassword(10);
                _log.info("A password has been automatically generated: " + _pass);
            }

            Naming.rebind("//localhost:" + _port + "/Elayne", _obj);
            _log.info("RMI Server started on port: " + _port + ", Password: " + _pass + ".");
        } catch (Exception e) {
            _log.error("RemoteAdministrationImpl error: ", e);
            e.printStackTrace();
        }
    } else
        _log.info("RMI Server is currently disabled.");
}

From source file:org.wso2.carbon.core.init.JMXServerManager.java

/**
 * The method to start JMX service./* w w w.  j  av  a2 s.co m*/
 *
 * @throws ServerException If an error occurs while starting the RMI server
 */
public void startJMXService() throws ServerException {

    //File path for the jmx config file.
    String filePath = CarbonUtils.getEtcCarbonConfigDirPath() + File.separator + "jmx.xml";
    boolean startJMXServer = false;

    File jmxConfigFile = new File(filePath);

    //Check whether jmx.xml file exists
    if (jmxConfigFile.exists()) {
        //Read jmx.xml file.
        parseJMXConfigXML(filePath);
        startJMXServer = jmxProperties.isStartServer();
        if (!startJMXServer) {
            return;
        }
    }

    int rmiRegistryPort = jmxProperties.getRmiRegistryPort();
    if (rmiRegistryPort == -1) {
        throw new RuntimeException(
                "RMIRegistry port has not been properly defined in the " + "jmx.xml or carbon.xml files");
    }
    MBeanServer mbs = ManagementFactory.getMBeanServer();
    String jmxURL;
    try {
        try {
            rmiRegistry = LocateRegistry.createRegistry(rmiRegistryPort);
        } catch (Throwable ignored) {
            log.error("Could not create the RMI local registry", ignored);
        }

        String hostName;
        //If 'startRMIServer' element in jmx.xml file set to true and 'HostName' element
        // value that file is not null.
        if (startJMXServer && jmxProperties.getHostName() != null) {
            hostName = jmxProperties.getHostName();//Set hostname value from jmx.xml file.
        } else { //Else
            hostName = NetworkUtils.getLocalHostname();
        }
        // Create an RMI connector and start it
        int rmiServerPort = jmxProperties.getRmiServerPort();
        if (rmiServerPort != -1) {
            jmxURL = "service:jmx:rmi://" + hostName + ":" + rmiServerPort + "/jndi/rmi://" + hostName + ":"
                    + rmiRegistryPort + "/jmxrmi";

        } else {
            jmxURL = "service:jmx:rmi:///jndi/rmi://" + hostName + ":" + rmiRegistryPort + "/jmxrmi";
        }
        JMXServiceURL url = new JMXServiceURL(jmxURL);

        // Security credentials are included in the env Map
        HashMap<String, CarbonJMXAuthenticator> env = new HashMap<String, CarbonJMXAuthenticator>();
        env.put(JMXConnectorServer.AUTHENTICATOR, new CarbonJMXAuthenticator());
        jmxConnectorServer = JMXConnectorServerFactory.newJMXConnectorServer(url, env, mbs);
        jmxConnectorServer.start();
        log.info("JMX Service URL  : " + jmxURL);
    } catch (Exception e) {
        String msg = "Could not initialize RMI server";
        log.error(msg, e);
    }
}

From source file:control.ServerControl.java

public ServerControl(ServerFrame serverFrame) throws RemoteException {
    initComponents(serverFrame);
    registry = LocateRegistry.createRegistry(PORT);
}

From source file:de.tudarmstadt.lt.lm.app.StartRMI.java

@Override
public void run() {

    try {//from  w w  w.j  a v  a 2  s .  c  o m
        // create own registry, alternative: use 'rmiregistry' command in class output folder
        _registry = LocateRegistry.createRegistry(_port);
    } catch (Exception e) {
        LOG.error("Could not create regisry. ", e);
        return;
    }

    _stdin_scan = new Scanner(System.in);
    for (String input_line = null; (input_line = readInput(String.format(
            "+++%nRMI Registry running. What do you want to do?%n" + "Type ':l' to list connected services, %n"
                    + "Type ':q' to quit program. %n" + "$> "))) != null;) {

        if (input_line.isEmpty())
            continue;
        if (":q".equalsIgnoreCase(input_line))
            exit();
        if (":l".equalsIgnoreCase(input_line))
            try {
                System.out.format("%s%n", Arrays.toString(_registry.list()));
            } catch (Exception e) {
                /* */}
    }
}

From source file:org.chorusbdd.chorus.tools.webagent.JmxManagementServerExporter.java

public void startServer() throws Exception {

    // Ensure cryptographically strong random number generator used
    // to choose the object number - see java.rmi.server.ObjID
    ///*from  w w w  .j  av  a  2 s.  co  m*/
    //System.setProperty("java.rmi.server.randomIDs", "true");

    // Start an RMI registry on port 3000.
    //
    if (!registriesCreated.contains(port)) {
        log.info("Creating RMI registry on port " + port);
        LocateRegistry.createRegistry(port);
        registriesCreated.add(port);
    } else {
        //there's no way to shut it dnwn? So if we run a sequence of tests we clean up by unexporting the
        //listener object, and have to reuse the registry instance
        log.info("RMI registry was already running on port " + port);
    }

    // Retrieve the PlatformMBeanServer.
    //
    log.info(usePlatformMBeanServer ? "Using Platform MBean Server" : "Creating the MBean server");
    mBeanServer = usePlatformMBeanServer ? ManagementFactory.getPlatformMBeanServer()
            : MBeanServerFactory.createMBeanServer();

    // Environment map.
    //
    log.info("Initialize the environment map");
    HashMap<String, Object> env = new HashMap<String, Object>();

    // Provide SSL-based RMI socket factories.
    //
    // The protocol and cipher suites to be enabled will be the ones
    // defined by the default JSSE implementation and only server
    // authentication will be required.
    //
    //SslRMIClientSocketFactory csf = new SslRMIClientSocketFactory();
    //SslRMIServerSocketFactory ssf = new SslRMIServerSocketFactory();
    //env.put(RMIConnectorServer.RMI_CLIENT_SOCKET_FACTORY_ATTRIBUTE, csf);
    //env.put(RMIConnectorServer.RMI_SERVER_SOCKET_FACTORY_ATTRIBUTE, ssf);

    // Provide the password file used by the connector server to
    // perform user authentication. The password file is a properties
    // based text file specifying username/password pairs.
    //
    //env.put("jmx.remote.x.password.file", "password.properties");

    // Provide the access level file used by the connector server to
    // perform user authorization. The access level file is a properties
    // based text file specifying username/access level pairs where
    // access level is either "readonly" or "readwrite" access to the
    // MBeanServer operations.
    //
    //env.put("jmx.remote.x.access.file", "access.properties");

    // Create an RMI connector server.
    //
    // As specified in the JMXServiceURL the RMIServer stub will be
    // registered in the RMI registry running in the local host on
    // port 3000 with the name "jmxrmi". This is the same name the
    // out-of-the-box management agent uses to register the RMIServer
    // stub too.
    //
    log.info("Create an RMI connector server");
    JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://:" + port + "/jmxrmi");
    jmxConnectorServer = JMXConnectorServerFactory.newJMXConnectorServer(url, env, mBeanServer);

    // Start the RMI connector server.
    //
    log.info("Start the JMX connector server on port " + port);
    jmxConnectorServer.start();
}

From source file:uk.co.gidley.jmxmonitor.services.InternalJmx.java

private void startRmiRegistry(Configuration configuration) throws InitialisationException {
    try {//from w  w w . j a  v a 2  s.c om
        int port = configuration.getInt(PROPERTY_PREFIX + LOCAL_RMI_PORT);
        logger.debug("Creating RMI Registry on {}", port);
        registry = LocateRegistry.createRegistry(port);
    } catch (RemoteException e) {
        logger.error("{}", e);
        throw new InitialisationException(e);
    }
}

From source file:org.openadaptor.auxil.connector.rmi.RMIReadConnector.java

public void connect() {

    if (createRegistry) {
        try {// w  w  w  .  ja  va2s. c om
            log.info("starting rmi registry on port " + registryPort);
            LocateRegistry.createRegistry(registryPort);
            registryHost = NetUtil.getLocalHostname();
        } catch (RemoteException e) {
            throw new ConnectionException("failed to create rmi registry", e, this);
        }
    }

    try {
        rmiServer = new RMIDataProcessor();
    } catch (RemoteException e) {
        throw new ConnectionException("failed to create rmi server", e, this);
    }

    try {
        log.info("binding rmi server " + serviceName + " on " + registryHost + ":" + registryPort);
        LocateRegistry.getRegistry(registryHost, registryPort).rebind(serviceName, rmiServer);
    } catch (Exception e) {
        rmiServer = null;
        throw new ConnectionException("failed to bind ", e, this);
    }
}

From source file:org.openadaptor.util.JVMNeutralMBeanServerFactory.java

/**
 * Use reflection to get an MBeanServer. Avoids compile issue where 1.4 jdk doesn't
 * have java.lang.management.ManagementFactory
 *
 * <pre>//www .j a v  a2 s.co m
 *
 * Note: For 1.4, an RMI registry may have to be manually started.
 * This may be achieved by something similar to ...
 * RJMX_LIB=<i>oa3_lib</i>
 * JMX_LIB=<i>oa3_lib</i>
 * CP=${RJMXLIB}/jmxremote.jar:${JMXLIB}/jmxri.jar
 * export CLASSPATH=.:$CP ; rmiregistry 51410 &
 *
 * Does <b>not</b> apply to 1.5+
 * </pre>
 * @return MBeanServer instance.
 */
public static MBeanServer getMBeanServer() {
    String jvmVersion = System.getProperties().getProperty("java.version");
    log.info("Getting MBeanServer [for jvm " + jvmVersion + "]");
    boolean isjvm1_4 = jvmVersion.startsWith("1.4.");
    String factory = isjvm1_4 ? FACTORY_1_4 : FACTORY_1_5;
    String method = isjvm1_4 ? METHOD_1_4 : METHOD_1_5;
    if (server == null) {
        server = getMBeanServer(factory, method);
        if (isjvm1_4) {
            //Todo: Add some kind of access mechanism to change the port!
            int port = DEFAULT_RMI_PORT;

            String serviceURLString = "service:jmx:rmi:///jndi/rmi://localhost:" + port + "/server";
            try {
                log.info("starting rmi registry on " + port);
                rmiRegistry = LocateRegistry.createRegistry(port);
                Runtime.getRuntime().addShutdownHook(new Thread() {
                    public void run() {
                    }
                });
            } catch (RemoteException e) {
                log.warn(e);
            }
            JMXServiceURL url;
            try {
                url = new JMXServiceURL(serviceURLString);
                //url = new JMXServiceURL("jmxmp", null, 5555);
                jmxConnectorServer_1_4 = JMXConnectorServerFactory.newJMXConnectorServer(url, null, server);

                // Start the RMI connector server
                log.info("Starting the RMI connector server");
                jmxConnectorServer_1_4.start();
                //Add a shutdownHook to make sure it stops also.
                addJMXShutdownHook();
                //ToDo: Remember to shut this baby down also!
                log.info("RMI connector server successfully started.");
                log.info("JMX clients may use the serviceURL: " + serviceURLString);

                //Start a Html Connection server - disabled for now.
                //It's just being explored.
                //startHtmlConnectorServer();
            } catch (Exception e) {
                log.warn("Failed to get RMI connector server started - " + e);
                e.printStackTrace();
            }
        }
    }
    return server;
}

From source file:org.tinygroup.cache.jcs.applicationprocessor.JcsCacheProcessor.java

private void startCluster() {
    int registryPort = DEFAULT_REGISTRY_PORT;
    try {/*from   w w  w . j  ava  2 s  . c  o m*/
        Properties props = PropertyLoader.loadProperties(DEFAULT_PROPS_FILE_NAME);
        if (props != null) {
            String portS = props.getProperty("registry.port", String.valueOf(DEFAULT_REGISTRY_PORT));

            try {
                registryPort = Integer.parseInt(portS);
            } catch (NumberFormatException e) {
                LOGGER.errorMessage("Problem converting port to an int.", e);
            }
        }
    } catch (Exception e) {
        LOGGER.errorMessage("Problem loading props.", e);
    }

    // we will always use the local machine for the registry
    String registryHost;
    try {
        registryHost = InetAddress.getLocalHost().getHostAddress();
        LOGGER.logMessage(LogLevel.DEBUG, "registryHost =[{}]", registryHost);

        if ("localhost".equals(registryHost) || "127.0.0.1".equals(registryHost)) {
            LOGGER.logMessage(LogLevel.WARN,
                    "The local address [{}] is INVALID.  Other machines must be able to use the address to reach this server.",
                    registryHost);
        }

        try {
            LocateRegistry.createRegistry(registryPort);
        } catch (RemoteException e) {
            LOGGER.errorMessage("Problem creating registry.  It may already be started. {}", e, e.getMessage());
        } catch (Exception t) {
            LOGGER.errorMessage("Problem creating registry.", t);
        }

        try {
            RemoteCacheServerFactory.startup(registryHost, registryPort, "/" + DEFAULT_PROPS_FILE_NAME);
        } catch (IOException e) {
            LOGGER.errorMessage("Problem starting remote cache server.", e);
        }

        catch (Exception t) {
            LOGGER.errorMessage("Problem starting remote cache server.", t);
        }
    } catch (UnknownHostException e) {
        LOGGER.errorMessage("Could not get local address to use for the registry!", e);
    }
}

From source file:org.teavm.idea.daemon.TeaVMBuildDaemon.java

TeaVMBuildDaemon(boolean incremental) throws RemoteException {
    super();//  ww w.  j a  va 2  s .c  o m
    this.incremental = incremental;
    Random random = new Random();
    for (int i = 0; i < 20; ++i) {
        port = random.nextInt(MAX_PORT - MIN_PORT) + MIN_PORT;
        try {
            registry = LocateRegistry.createRegistry(port);
        } catch (RemoteException e) {
            continue;
        }
        try {
            registry.bind(TeaVMRemoteBuildService.ID, this);
        } catch (RemoteException | AlreadyBoundException e) {
            throw new IllegalStateException("Could not bind remote build assistant service", e);
        }

        setupIncrementalCache();

        return;
    }
    throw new IllegalStateException("Could not create RMI registry");
}