Search in sources :

Example 26 with Registry

use of java.rmi.registry.Registry in project jackrabbit-oak by apache.

the class RepositoryStartupServlet method registerRMI.

/**
     * Registers the repository to an RMI registry configured in the web
     * application. See <a href="#registerAlgo">Registration with RMI</a> in the
     * class documentation for a description of the algorithms used to register
     * the repository with an RMI registry.
     * @throws ServletException if an error occurs.
     */
private void registerRMI() {
    RMIConfig rc = config.getRmiConfig();
    if (!rc.isValid() || !rc.enabled()) {
        return;
    }
    // try to create remote repository
    Remote remote;
    try {
        Class<?> clazz = Class.forName(getRemoteFactoryDelegaterClass());
        RemoteFactoryDelegater rmf = (RemoteFactoryDelegater) clazz.newInstance();
        remote = rmf.createRemoteRepository(repository);
    } catch (RemoteException e) {
        log.warn("Unable to create RMI repository.", e);
        return;
    } catch (Throwable t) {
        log.warn("Unable to create RMI repository." + " The jcr-rmi jar might be missing.", t);
        return;
    }
    try {
        System.setProperty("java.rmi.server.useCodebaseOnly", "true");
        Registry reg = null;
        // or if the rmiHost is not local
        try {
            // find the server socket factory: use the default if the
            // rmiHost is not configured
            RMIServerSocketFactory sf;
            if (rc.getRmiHost().length() > 0) {
                log.debug("Creating RMIServerSocketFactory for host " + rc.getRmiHost());
                InetAddress hostAddress = InetAddress.getByName(rc.getRmiHost());
                sf = getRMIServerSocketFactory(hostAddress);
            } else {
                // have the RMI implementation decide which factory is the
                // default actually
                log.debug("Using default RMIServerSocketFactory");
                sf = null;
            }
            // create a registry using the default client socket factory
            // and the server socket factory retrieved above. This also
            // binds to the server socket to the rmiHost:rmiPort.
            reg = LocateRegistry.createRegistry(rc.rmiPort(), null, sf);
            rmiRegistry = reg;
        } catch (UnknownHostException uhe) {
            // thrown if the rmiHost cannot be resolved into an IP-Address
            // by getRMIServerSocketFactory
            log.info("Cannot create Registry", uhe);
        } catch (RemoteException e) {
            // thrown by createRegistry if binding to the rmiHost:rmiPort
            // fails, for example due to rmiHost being remote or another
            // application already being bound to the port
            log.info("Cannot create Registry", e);
        }
        // registry is actually accessible.
        if (reg == null) {
            log.debug("Trying to access existing registry at " + rc.getRmiHost() + ":" + rc.getRmiPort());
            try {
                reg = LocateRegistry.getRegistry(rc.getRmiHost(), rc.rmiPort());
            } catch (RemoteException re) {
                log.warn("Cannot create the reference to the registry at " + rc.getRmiHost() + ":" + rc.getRmiPort(), re);
            }
        }
        // rmiName
        if (reg != null) {
            log.debug("Registering repository as " + rc.getRmiName() + " to registry " + reg);
            reg.bind(rc.getRmiName(), remote);
            // when successfull, keep references
            this.rmiRepository = remote;
            log.info("Repository bound via RMI with name: " + rc.getRmiUri());
        } else {
            log.info("RMI registry missing, cannot bind repository via RMI");
        }
    } catch (RemoteException e) {
        log.warn("Unable to bind repository via RMI: " + rc.getRmiUri(), e);
    } catch (AlreadyBoundException e) {
        log.warn("Unable to bind repository via RMI: " + rc.getRmiUri(), e);
    }
}
Also used : UnknownHostException(java.net.UnknownHostException) AlreadyBoundException(java.rmi.AlreadyBoundException) RMIServerSocketFactory(java.rmi.server.RMIServerSocketFactory) Remote(java.rmi.Remote) Registry(java.rmi.registry.Registry) LocateRegistry(java.rmi.registry.LocateRegistry) PojoServiceRegistry(org.apache.felix.connect.launch.PojoServiceRegistry) RemoteException(java.rmi.RemoteException) InetAddress(java.net.InetAddress)

Example 27 with Registry

use of java.rmi.registry.Registry in project JMRI by JMRI.

the class LnMessageServer method enable.

public synchronized void enable() {
    Registry localRegistry = null;
    try {
        // NOI18N
        log.debug("Create RMI Registry for: " + serviceName);
        localRegistry = LocateRegistry.createRegistry(Registry.REGISTRY_PORT);
    } catch (java.rmi.RemoteException ex) {
    }
    try {
        if (localRegistry == null) {
            // NOI18N
            log.warn("Could not Create RMI Registry, Attempting to Locate existing Registry for: " + serviceName);
            localRegistry = LocateRegistry.getRegistry(Registry.REGISTRY_PORT);
        }
        // NOI18N
        log.debug("Register LocoNet Server: " + serviceName + " with RMI Registry");
        localRegistry.rebind(serviceName, self);
        // NOI18N
        log.debug("Register LocoNet Server Complete");
    } catch (Exception ex) {
        // NOI18N
        log.warn("LnMessageServer: " + ex);
    }
}
Also used : LocateRegistry(java.rmi.registry.LocateRegistry) Registry(java.rmi.registry.Registry) RemoteException(java.rmi.RemoteException) RemoteException(java.rmi.RemoteException)

Example 28 with Registry

use of java.rmi.registry.Registry in project jdk8u_jdk by JetBrains.

the class Main method setupServer.

/**
     * Setup benchmark server.
     */
static void setupServer() {
    switch(runmode) {
        case SAMEVM:
            try {
                serverImpl = new BenchServerImpl();
                server = (BenchServer) RemoteObject.toStub(serverImpl);
            } catch (RemoteException e) {
                die("Error: failed to create local server: " + e);
            }
            if (verbose)
                System.out.println("Benchmark server created locally");
            break;
        case CLIENT:
            try {
                Registry reg = LocateRegistry.getRegistry(host, port);
                server = (BenchServer) reg.lookup(REGNAME);
            } catch (NotBoundException | RemoteException e) {
                die("Error: failed to connect to server: " + e);
            }
            if (server == null) {
                die("Error: server not found");
            }
            if (verbose) {
                System.out.println("Connected to benchmark server on " + host + ":" + port);
            }
            break;
        case SERVER:
            try {
                Registry reg = LocateRegistry.createRegistry(port);
                serverImpl = new BenchServerImpl();
                reg.bind(REGNAME, serverImpl);
            } catch (AlreadyBoundException | RemoteException e) {
                die("Error: failed to initialize server: " + e);
            }
            if (verbose) {
                System.out.println("Benchmark server started on port " + port);
            }
            break;
        default:
            throw new InternalError("illegal runmode");
    }
}
Also used : AlreadyBoundException(java.rmi.AlreadyBoundException) NotBoundException(java.rmi.NotBoundException) Registry(java.rmi.registry.Registry) LocateRegistry(java.rmi.registry.LocateRegistry) RemoteException(java.rmi.RemoteException)

Example 29 with Registry

use of java.rmi.registry.Registry in project jdk8u_jdk by JetBrains.

the class Client method testStub.

public String testStub() throws Exception {
    try {
        Registry registry = LocateRegistry.getRegistry(port);
        Hello stub = (Hello) registry.lookup("Hello");
        String response = stub.sayHello();
        return response;
    } catch (Exception e) {
        System.err.println("Client exception: " + e.toString());
        throw e;
    }
}
Also used : LocateRegistry(java.rmi.registry.LocateRegistry) Registry(java.rmi.registry.Registry)

Example 30 with Registry

use of java.rmi.registry.Registry in project jdk8u_jdk by JetBrains.

the class LeaseCheckInterval method main.

public static void main(String[] args) throws Exception {
    System.err.println("\nRegression test for bug 4285878\n");
    /*
         * Set the duration of leases granted to a very small value, so that
         * we can test if expirations are detected in a roughly comparable
         * time.
         */
    System.setProperty("java.rmi.dgc.leaseValue", String.valueOf(LEASE_VALUE));
    LeaseCheckInterval obj = new LeaseCheckInterval();
    try {
        UnicastRemoteObject.exportObject(obj);
        System.err.println("exported remote object");
        int registryPort = TestLibrary.getUnusedRandomPort();
        Registry localRegistry = LocateRegistry.createRegistry(registryPort);
        System.err.println("created local registry");
        localRegistry.bind(BINDING, obj);
        System.err.println("bound remote object in local registry");
        synchronized (obj.lock) {
            System.err.println("starting remote client VM...");
            (new JavaVM("SelfTerminator", "-Drmi.registry.port=" + registryPort, "")).start();
            System.err.println("waiting for unreferenced() callback...");
            obj.lock.wait(TIMEOUT);
            if (obj.unreferencedInvoked) {
                System.err.println("TEST PASSED: " + "unreferenced() invoked in timely fashion");
            } else {
                throw new RuntimeException("TEST FAILED: unreferenced() not invoked after " + ((double) TIMEOUT / 1000.0) + " seconds");
            }
        }
    } catch (Exception e) {
        if (e instanceof RuntimeException) {
            throw (RuntimeException) e;
        } else {
            throw new RuntimeException("TEST FAILED: unexpected exception: " + e.toString());
        }
    } finally {
        /*
             * When all is said and done, try to unexport the remote object
             * so that the VM has a chance to exit.
             */
        try {
            UnicastRemoteObject.unexportObject(obj, true);
        } catch (RemoteException e) {
        }
    }
}
Also used : LocateRegistry(java.rmi.registry.LocateRegistry) Registry(java.rmi.registry.Registry) RemoteException(java.rmi.RemoteException) RemoteException(java.rmi.RemoteException)

Aggregations

Registry (java.rmi.registry.Registry)77 LocateRegistry (java.rmi.registry.LocateRegistry)71 RemoteException (java.rmi.RemoteException)35 Remote (java.rmi.Remote)12 HashMap (java.util.HashMap)9 JMXServiceURL (javax.management.remote.JMXServiceURL)9 IOException (java.io.IOException)8 JMXConnector (javax.management.remote.JMXConnector)8 UnknownHostException (java.net.UnknownHostException)7 MBeanServer (javax.management.MBeanServer)7 MBeanServerConnection (javax.management.MBeanServerConnection)7 JMXConnectorServer (javax.management.remote.JMXConnectorServer)7 ObjectName (javax.management.ObjectName)6 JMXPluggableAuthenticator (com.sun.jmx.remote.security.JMXPluggableAuthenticator)5 InetAddress (java.net.InetAddress)5 MalformedURLException (java.net.MalformedURLException)5 AlreadyBoundException (java.rmi.AlreadyBoundException)5 Properties (java.util.Properties)5 Attribute (javax.management.Attribute)5 Notification (javax.management.Notification)5