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:edu.hku.sdb.driver.SdbDriver.java

private static void startConnectionPool(SdbConf sdbConf) {
    try {//from   w  ww. j  a v a 2 s .  c om
        ConnectionPool connectionPool = new ConnectionPool(sdbConf);
        ConnectionConf connectionConf = sdbConf.getConnectionConf();
        LocateRegistry.createRegistry(connectionConf.getSdbPort());
        String sdbConnectionUrl = connectionConf.getSdbAddress() + ":" + connectionConf.getSdbPort() + "/"
                + SERVICE_NAME;

        LOG.info("Starting SDB Proxy");
        Naming.rebind(sdbConnectionUrl, connectionPool);

    } catch (RemoteException | MalformedURLException e) {
        e.printStackTrace();
    }
}

From source file:gridool.util.remoting.RemoteBase.java

private static Registry prepareRegistry(int port) throws RemoteException {
    Registry registry;/*from  w ww  .j  ava  2  s  .  c om*/
    try {
        registry = LocateRegistry.createRegistry(port);
        LOG.info("created local rmi-registry at port: " + port);
    } catch (RemoteException e) {
        registry = LocateRegistry.getRegistry(port);
        LOG.info("re-used existing local rmi-registry of port: " + port);
    }
    assert (registry != null);
    return registry;
}

From source file:com.okidokiteam.gouken.macosx.MacOSBoot.java

private void bind() {
    try {/* w  ww.j  a va 2s.co m*/
        // try to find port from property
        int port = getPort();
        LOG.debug("Starting up RMI registry on port [" + port + "]");
        m_registry = LocateRegistry.createRegistry(port);
        LOG.debug("Binding " + m_vault.getClass() + " to RMI registry");
        m_registry.bind(Vault.class.getName(), UnicastRemoteObject.exportObject(this, port));
        LOG.info("RMI registry started on port [" + port + "]");
    } catch (Exception e) {
        throw new RuntimeException("Cannot setup RMI registry", e);
    }
}

From source file:org.apache.jcs.auxiliary.remote.server.RemoteCacheStartupServlet.java

/**
 * Starts the registry and then tries to bind to it.
 * <p>/*from   w ww. ja  v  a  2s.c  o  m*/
 * Gets the port from a props file. Uses the local host name for the rgistry
 * host. Tries to start the registry, ignoreing failure. Starts the server.
 */
public void init() throws ServletException {
    super.init();
    // TODO load from props file or get as init param or get from jndi, or
    // all three
    int registryPort = DEFAULT_REGISTRY_PORT;

    try {
        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) {
                log.error("Problem converting port to an int.", e);
            }
        }
    } catch (Exception e) {
        log.error("Problem loading props.", e);
    } catch (Throwable t) {
        log.error("Problem loading props.", t);
    }

    // we will always use the local machine for the registry
    String registryHost;
    try {
        registryHost = InetAddress.getLocalHost().getHostAddress();

        if (log.isDebugEnabled()) {
            log.debug("registryHost = [" + registryHost + "]");
        }

        if ("localhost".equals(registryHost) || "127.0.0.1".equals(registryHost)) {
            log.warn("The local address [" + registryHost
                    + "] is INVALID.  Other machines must be able to use the address to reach this server.");
        }

        try {
            LocateRegistry.createRegistry(registryPort);
        } catch (RemoteException e) {
            log.error("Problem creating registry.  It may already be started. " + e.getMessage());
        } catch (Throwable t) {
            log.error("Problem creating registry.", t);
        }

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

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

From source file:au.com.jwatmuff.genericp2p.rmi.RMIPeerManager.java

public RMIPeerManager(int registryPort, File idFile) {
    try {/* w w w . ja  va 2  s  . c  om*/
        registry = LocateRegistry.createRegistry(registryPort);
    } catch (RemoteException e) {
        log.error("Failed to create RMI registry", e);
    }

    this.registryPort = registryPort;
    this.idFile = idFile;
}

From source file:org.nuxeo.runtime.management.ServerLocatorService.java

protected MBeanServer doCreateServer(final ServerLocatorDescriptor descriptor) {
    MBeanServer server = MBeanServerFactory.createMBeanServer();
    JMXServiceURL url = doFormatServerURL(descriptor);
    if (!descriptor.remote) {
        return server;
    }/*from   w  ww.ja va  2  s. com*/
    final RMIConnectorServer connector;
    try {
        connector = new RMIConnectorServer(url, null, server);
    } catch (IOException e) {
        throw new ManagementRuntimeException("Cannot start connector for " + descriptor.domainName, e);
    }
    try {
        connector.start();
    } catch (IOException e) {
        try {
            LocateRegistry.createRegistry(descriptor.rmiPort);
        } catch (RemoteException e2) {
            throw new ManagementRuntimeException("Cannot start RMI connector for " + descriptor.domainName, e);
        }
        try {
            connector.start();
        } catch (IOException e2) {
            throw new ManagementRuntimeException("Cannot start RMI connector for " + descriptor.domainName, e2);
        }
    }
    assert connector.isActive();
    log.info("Started a mbean server : " + url);
    return server;
}

From source file:org.wso2.carbon.container.CarbonTestContainer.java

/**
 * Starts the test container./* www.  j av  a 2s  . com*/
 *
 * @return this container object for api
 */
public synchronized TestContainer start() {
    if (carbonHomeDirectoryOption.getDistributionDirectoryPath() == null
            && carbonHomeDirectoryOption.getDistributionMavenURL() == null
            && carbonHomeDirectoryOption.getDistributionZipPath() == null) {
        throw new TestContainerException("Distribution path need to be set.");
    }
    try {
        String name = system.createID(CARBON_TEST_CONTAINER);
        //get a free port to use for rmi
        FreePort freePort = new FreePort(21000, 21099);
        int port = freePort.getPort();
        logger.debug("using RMI registry at port {}" + name, port);
        registry = LocateRegistry.createRegistry(port);
        String host = InetAddress.getLocalHost().getHostName();

        //Setting RMI related properties
        ExamSystem subsystem = system.fork(options(systemProperty(RMI_HOST_PROPERTY).value(host),
                systemProperty(RMI_PORT_PROPERTY).value(Integer.toString(port)),
                systemProperty(RMI_NAME_PROPERTY).value(name),
                systemProperty(EXAM_INJECT_PROPERTY).value("true")));

        target = new RBCRemoteTarget(name, port, subsystem.getTimeout());
        System.setProperty("java.protocol.handler.pkgs", "org.ops4j.pax.url");

        //setup repositories if there are any
        addRepositories();
        targetDirectory = retrieveFinalTargetDirectory();

        if (carbonHomeDirectoryOption.getDistributionMavenURL() != null) {
            URL sourceDistribution = new URL(carbonHomeDirectoryOption.getDistributionMavenURL().getURL());
            ArchiveExtractor.extract(sourceDistribution, targetDirectory.toFile());
        } else if (carbonHomeDirectoryOption.getDistributionZipPath() != null) {
            Path sourceDistribution = carbonHomeDirectoryOption.getDistributionZipPath();
            ArchiveExtractor.extract(sourceDistribution, targetDirectory.toFile());
        } else if (carbonHomeDirectoryOption.getDistributionDirectoryPath() != null) {
            Path sourceDirectory = carbonHomeDirectoryOption.getDistributionDirectoryPath();
            FileUtils.copyDirectory(sourceDirectory.toFile(), targetDirectory.toFile());
        }

        //install bundles if there are any
        copyOSGiLibBundles(targetDirectory);

        //copy files to the distributions if there are any
        copyFiles(targetDirectory);
        Path carbonBin = targetDirectory.resolve("bin");

        //make the files in the bin directory to be executable
        makeFilesInBinExec(carbonBin.toFile());
        List<String> options = new ArrayList<>();
        String[] environment = new String[] {};

        //set system properties as command line arguments
        setupSystemProperties(options, subsystem);

        //Setup debug configurations if available
        DebugOption debugOption = system.getSingleOption(DebugOption.class);
        if (debugOption != null) {
            options.add(debugOption.getDebugConfiguration());
        }
        runner.exec(environment, targetDirectory, options);
        logger.debug("Wait for test container to finish its initialization " + subsystem.getTimeout());

        //wait for the osgi environment to be active
        waitForState(0, Bundle.ACTIVE, subsystem.getTimeout());
        started = true;
    } catch (IOException e) {
        throw new TestContainerException("Problem starting container", e);
    }
    return this;
}

From source file:org.apache.hadoop.hbase.JMXListener.java

public void startConnectorServer(int rmiRegistryPort, int rmiConnectorPort) throws IOException {
    boolean rmiSSL = false;
    boolean authenticate = true;
    String passwordFile = null;/*  ww w. j a v  a  2s . c o m*/
    String accessFile = null;

    System.setProperty("java.rmi.server.randomIDs", "true");

    String rmiSSLValue = System.getProperty("com.sun.management.jmxremote.ssl", "false");
    rmiSSL = Boolean.parseBoolean(rmiSSLValue);

    String authenticateValue = System.getProperty("com.sun.management.jmxremote.authenticate", "false");
    authenticate = Boolean.parseBoolean(authenticateValue);

    passwordFile = System.getProperty("com.sun.management.jmxremote.password.file");
    accessFile = System.getProperty("com.sun.management.jmxremote.access.file");

    LOG.info("rmiSSL:" + rmiSSLValue + ",authenticate:" + authenticateValue + ",passwordFile:" + passwordFile
            + ",accessFile:" + accessFile);

    // Environment map
    HashMap<String, Object> jmxEnv = new HashMap<String, Object>();

    RMIClientSocketFactory csf = null;
    RMIServerSocketFactory ssf = null;

    if (rmiSSL) {
        if (rmiRegistryPort == rmiConnectorPort) {
            throw new IOException(
                    "SSL is enabled. " + "rmiConnectorPort cannot share with the rmiRegistryPort!");
        }
        csf = new SslRMIClientSocketFactory();
        ssf = new SslRMIServerSocketFactory();
    }

    if (csf != null) {
        jmxEnv.put(RMIConnectorServer.RMI_CLIENT_SOCKET_FACTORY_ATTRIBUTE, csf);
    }
    if (ssf != null) {
        jmxEnv.put(RMIConnectorServer.RMI_SERVER_SOCKET_FACTORY_ATTRIBUTE, ssf);
    }

    // Configure authentication
    if (authenticate) {
        jmxEnv.put("jmx.remote.x.password.file", passwordFile);
        jmxEnv.put("jmx.remote.x.access.file", accessFile);
    }

    // Create the RMI registry
    LocateRegistry.createRegistry(rmiRegistryPort);
    // Retrieve the PlatformMBeanServer.
    MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();

    // Build jmxURL
    JMXServiceURL serviceUrl = buildJMXServiceURL(rmiRegistryPort, rmiConnectorPort);

    try {
        // Start the JMXListener with the connection string
        jmxCS = JMXConnectorServerFactory.newJMXConnectorServer(serviceUrl, jmxEnv, mbs);
        jmxCS.start();
        LOG.info("ConnectorServer started!");
    } catch (IOException e) {
        LOG.error("fail to start connector server!", e);
    }

}

From source file:com.mindquarry.jcr.jackrabbit.JackrabbitRMIRepositoryStandalone.java

private void start(CommandLine line) throws Exception {
    String repoConf;/*w  w w . j a  v  a2  s. com*/
    String repoConfArg = line.getOptionValue(O_CONF);
    if (null == repoConfArg)
        repoConf = createDefaultRepoConf();
    else
        repoConf = new File(repoConfArg).getAbsolutePath();

    File repoLoc = new File(line.getOptionValue(O_LOC));
    if (!repoLoc.exists()) {
        repoLoc.mkdir();
    }
    if (!repoLoc.isDirectory()) {
        throw new IllegalArgumentException("Repository location is not a directory.");
    }
    Repository repo = new TransientRepository(repoConf, repoLoc.getAbsolutePath());

    ServerAdapterFactory factory = new ServerAdapterFactory();
    RemoteRepository remoteRepo = factory.getRemoteRepository(repo);

    reg = LocateRegistry.createRegistry(Registry.REGISTRY_PORT);
    reg.rebind(REMOTE_REPO_NAME, remoteRepo);

    String workspace = line.getOptionValue(O_WS);
    if (null == workspace)
        workspace = "default";

    session = repo.login(
            new SimpleCredentials(line.getOptionValue(O_USER), line.getOptionValue(O_PWD).toCharArray()),
            workspace);

    InputStream nodeTypeDefIn = getClass().getResourceAsStream(MQ_JCR_XML_NODETYPES_FILE);

    JackrabbitInitializerHelper.setupRepository(session, new InputStreamReader(nodeTypeDefIn),
            MQ_JCR_XML_NODETYPES_FILE);
    session.save();
}

From source file:org.springframework.remoting.rmi.RmiServiceExporter.java

/**
 * Register the service as RMI object.//w w  w . j av  a2s.  c o  m
 * Creates an RMI registry on the specified port if none exists.
 */
public void afterPropertiesSet() throws Exception {
    super.afterPropertiesSet();

    if (this.serviceName == null) {
        throw new IllegalArgumentException("serviceName is required");
    }
    if (this.clientSocketFactory instanceof RMIServerSocketFactory) {
        this.serverSocketFactory = (RMIServerSocketFactory) this.clientSocketFactory;
    }
    if ((this.clientSocketFactory != null && this.serverSocketFactory == null)
            || (this.clientSocketFactory == null && this.serverSocketFactory != null)) {
        throw new IllegalArgumentException(
                "Both RMIClientSocketFactory and RMIServerSocketFactory or none required");
    }

    Registry registry = null;
    logger.info("Looking for RMI registry at port '" + this.registryPort + "'");
    try {
        // retrieve registry
        registry = LocateRegistry.getRegistry(this.registryPort);
        registry.list();
    } catch (RemoteException ex) {
        logger.debug("RMI registry access threw exception", ex);
        logger.warn("Could not detect RMI registry - creating new one");
        // assume no registry found -> create new one
        registry = LocateRegistry.createRegistry(this.registryPort);
    }

    // determine remote object
    if (getService() instanceof Remote) {
        // conventional RMI service
        this.exportedObject = (Remote) getService();
    } else {
        // RMI invoker
        logger.info("RMI object '" + this.serviceName + "' is an RMI invoker");
        this.exportedObject = new RmiInvocationWrapper(getProxyForService(), this);
    }

    // export remote object and bind it to registry
    logger.info(
            "Binding RMI service '" + this.serviceName + "' to registry at port '" + this.registryPort + "'");
    if (this.clientSocketFactory != null) {
        UnicastRemoteObject.exportObject(this.exportedObject, this.servicePort, this.clientSocketFactory,
                this.serverSocketFactory);
    } else {
        UnicastRemoteObject.exportObject(this.exportedObject, this.servicePort);
    }
    registry.rebind(this.serviceName, this.exportedObject);
}