Search in sources :

Example 1 with ExportException

use of java.rmi.server.ExportException in project jdk8u_jdk by JetBrains.

the class AddrInUse method main.

public static void main(String[] args) throws Exception {
    System.err.println("\nRegression test for bug 4111507\n");
    /*
         * Bind a server socket to a port.
         */
    ServerSocket server = new ServerSocket(0);
    port = server.getLocalPort();
    System.err.println("Created a ServerSocket on port " + port + "...");
    /*
         * Start a thread that creates a registry on the same port,
         * and analyze the result.
         */
    System.err.println("create a registry on the same port...");
    System.err.println("(should cause an ExportException)");
    AddrInUse obj = new AddrInUse();
    synchronized (obj) {
        (new Thread(obj, "AddrInUse")).start();
        /*
             * Don't wait forever (original bug is that the export
             * hangs).
             */
        obj.wait(TIMEOUT);
        if (obj.exportSucceeded) {
            throw new RuntimeException("TEST FAILED: export on already-bound port succeeded");
        } else if (obj.exportException != null) {
            obj.exportException.printStackTrace();
            if (obj.exportException instanceof ExportException) {
                System.err.println("TEST PASSED");
            } else {
                throw new RuntimeException("TEST FAILED: unexpected exception occurred", obj.exportException);
            }
        } else {
            throw new RuntimeException("TEST FAILED: export timed out");
        }
    }
}
Also used : ServerSocket(java.net.ServerSocket) ExportException(java.rmi.server.ExportException)

Example 2 with ExportException

use of java.rmi.server.ExportException in project jdk8u_jdk by JetBrains.

the class TCPTransport method listen.

/**
     * Listen on transport's endpoint.
     */
private void listen() throws RemoteException {
    assert Thread.holdsLock(this);
    TCPEndpoint ep = getEndpoint();
    int port = ep.getPort();
    if (server == null) {
        if (tcpLog.isLoggable(Log.BRIEF)) {
            tcpLog.log(Log.BRIEF, "(port " + port + ") create server socket");
        }
        try {
            server = ep.newServerSocket();
            /*
                 * Don't retry ServerSocket if creation fails since
                 * "port in use" will cause export to hang if an
                 * RMIFailureHandler is not installed.
                 */
            Thread t = AccessController.doPrivileged(new NewThreadAction(new AcceptLoop(server), "TCP Accept-" + port, true));
            t.start();
        } catch (java.net.BindException e) {
            throw new ExportException("Port already in use: " + port, e);
        } catch (IOException e) {
            throw new ExportException("Listen failed on port: " + port, e);
        }
    } else {
        // otherwise verify security access to existing server socket
        SecurityManager sm = System.getSecurityManager();
        if (sm != null) {
            sm.checkListen(port);
        }
    }
}
Also used : IOException(java.io.IOException) Endpoint(sun.rmi.transport.Endpoint) NewThreadAction(sun.rmi.runtime.NewThreadAction) ExportException(java.rmi.server.ExportException)

Example 3 with ExportException

use of java.rmi.server.ExportException in project scheduling by ow2-proactive.

the class AbstractJMXHelper method createJMXRMIConnectorServer.

private boolean createJMXRMIConnectorServer(final MBeanServer mbs, final String connectorServerName, final int port, final JMXAuthenticator authenticator) {
    // Create or reuse an RMI registry needed for the connector server
    try {
        LocateRegistry.createRegistry(port);
    } catch (ExportException ee) {
        // do nothing and continue starting JMX
        if (logger.isDebugEnabled()) {
            logger.debug("Reusing existing RMI registry on port " + port);
        }
    } catch (RemoteException e) {
        // This can occur if the port is already occupied
        logger.error(jmxRmiFailureReason = "Unable to create an RMI registry on port " + port, e);
        // do not start JMX service
        return false;
    }
    // Use the same hostname as ProActive (follows properties defined by ProActive configuration)
    final String hostname = ProActiveInet.getInstance().getHostname();
    // The asked address of the new connector server. The actual address can be different due to
    // JMX specification. See {@link JMXConnectorServerFactory} documentation.
    final String jmxConnectorServerURL = "service:jmx:rmi:///jndi/rmi://" + hostname + ":" + port + "/" + connectorServerName;
    JMXServiceURL jmxUrl = null;
    try {
        jmxUrl = new JMXServiceURL(jmxConnectorServerURL);
    } catch (MalformedURLException e) {
        logger.error(jmxRmiFailureReason = "Unable to create the JMXServiceURL from " + jmxConnectorServerURL, e);
        return false;
    }
    final HashMap<String, Object> env = new HashMap<>(1);
    env.put(JMXConnectorServer.AUTHENTICATOR, authenticator);
    // Create the connector server
    try {
        this.rmiCs = JMXConnectorServerFactory.newJMXConnectorServer(jmxUrl, env, mbs);
    } catch (Exception e) {
        logger.error(jmxRmiFailureReason = "Unable to create the JMXConnectorServer at " + jmxUrl, e);
        return false;
    }
    return true;
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL) MalformedURLException(java.net.MalformedURLException) HashMap(java.util.HashMap) RemoteException(java.rmi.RemoteException) MalformedURLException(java.net.MalformedURLException) ExportException(java.rmi.server.ExportException) RemoteException(java.rmi.RemoteException) JMException(javax.management.JMException) ExportException(java.rmi.server.ExportException)

Example 4 with ExportException

use of java.rmi.server.ExportException in project che by eclipse.

the class RmiServer method start.

protected static void start(Remote remote) throws RemoteException {
    System.setProperty("java.rmi.server.hostname", "localhost");
    System.setProperty("java.rmi.server.disableHttp", "true");
    System.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.eclipse.che.rmi.JNDI");
    if (RmiServer.remote != null) {
        throw new AssertionError("This server is already started!");
    }
    RmiServer.remote = remote;
    int port = -1;
    Random random = new Random();
    Registry registry = null;
    while (port == -1) {
        int tmpPort = random.nextInt(65535);
        if (tmpPort < 4000) {
            continue;
        }
        try {
            registry = LocateRegistry.createRegistry(tmpPort);
            port = tmpPort;
        } catch (ExportException ignored) {
        }
    }
    Remote exportObject = UnicastRemoteObject.exportObject(remote, port);
    String exportName = remote.getClass().getSimpleName() + Integer.toHexString(exportObject.hashCode());
    try {
        registry.bind(exportName, exportObject);
        String portName = port + "/" + exportName;
        System.out.println("Port/Name:" + portName);
        int twoMinutes = 2 * 60 * 1000;
        Object lock = new Object();
        while (true) {
            synchronized (lock) {
                lock.wait(twoMinutes);
            }
        // TODO add ping
        }
    } catch (AlreadyBoundException | InterruptedException e) {
        e.printStackTrace();
        System.exit(42);
    }
}
Also used : Random(java.util.Random) AlreadyBoundException(java.rmi.AlreadyBoundException) Remote(java.rmi.Remote) UnicastRemoteObject(java.rmi.server.UnicastRemoteObject) LocateRegistry(java.rmi.registry.LocateRegistry) Registry(java.rmi.registry.Registry) ExportException(java.rmi.server.ExportException)

Example 5 with ExportException

use of java.rmi.server.ExportException in project intellij-community by JetBrains.

the class RemoteServer method start.

@SuppressWarnings("UseOfSystemOutOrSystemErr")
protected static void start(Remote remote) throws Exception {
    setupRMI();
    banJNDI();
    setupSSL();
    setupDomainAuth();
    if (ourRemote != null)
        throw new AssertionError("Already started");
    ourRemote = remote;
    Registry registry;
    int port;
    for (Random random = new Random(); ; ) {
        port = random.nextInt(0xffff);
        if (port < 4000)
            continue;
        try {
            registry = LocateRegistry.createRegistry(port);
            break;
        } catch (ExportException ignored) {
        }
    }
    try {
        Remote stub = UnicastRemoteObject.exportObject(ourRemote, 0);
        final String name = remote.getClass().getSimpleName() + Integer.toHexString(stub.hashCode());
        registry.bind(name, stub);
        String id = port + "/" + name;
        System.out.println("Port/ID: " + id);
        long waitTime = RemoteDeadHand.PING_TIMEOUT;
        Object lock = new Object();
        //noinspection InfiniteLoopStatement
        while (true) {
            //noinspection SynchronizationOnLocalVariableOrMethodParameter
            synchronized (lock) {
                lock.wait(waitTime);
            }
            RemoteDeadHand deadHand = (RemoteDeadHand) registry.lookup(RemoteDeadHand.BINDING_NAME);
            waitTime = deadHand.ping(id);
        }
    } catch (Throwable e) {
        e.printStackTrace(System.err);
        System.exit(1);
    }
}
Also used : Random(java.util.Random) Remote(java.rmi.Remote) UnicastRemoteObject(java.rmi.server.UnicastRemoteObject) Registry(java.rmi.registry.Registry) LocateRegistry(java.rmi.registry.LocateRegistry) ExportException(java.rmi.server.ExportException)

Aggregations

ExportException (java.rmi.server.ExportException)8 Remote (java.rmi.Remote)3 Random (java.util.Random)3 LocateRegistry (java.rmi.registry.LocateRegistry)2 Registry (java.rmi.registry.Registry)2 UnicastRemoteObject (java.rmi.server.UnicastRemoteObject)2 JMXServiceURL (javax.management.remote.JMXServiceURL)2 IOException (java.io.IOException)1 BindException (java.net.BindException)1 MalformedURLException (java.net.MalformedURLException)1 ServerSocket (java.net.ServerSocket)1 AlreadyBoundException (java.rmi.AlreadyBoundException)1 RemoteException (java.rmi.RemoteException)1 RemoteStub (java.rmi.server.RemoteStub)1 HashMap (java.util.HashMap)1 JMException (javax.management.JMException)1 MBeanServer (javax.management.MBeanServer)1 ObjectName (javax.management.ObjectName)1 JMXConnectorServer (javax.management.remote.JMXConnectorServer)1 NewThreadAction (sun.rmi.runtime.NewThreadAction)1