Search in sources :

Example 6 with AlreadyBoundException

use of java.rmi.AlreadyBoundException in project tomcat by apache.

the class JmxRemoteLifecycleListener method createServer.

private JMXConnectorServer createServer(String serverName, String bindAddress, int theRmiRegistryPort, int theRmiServerPort, HashMap<String, Object> theEnv, RMIClientSocketFactory registryCsf, RMIServerSocketFactory registrySsf, RMIClientSocketFactory serverCsf, RMIServerSocketFactory serverSsf) {
    // Create the RMI registry
    Registry registry;
    try {
        registry = LocateRegistry.createRegistry(theRmiRegistryPort, registryCsf, registrySsf);
    } catch (RemoteException e) {
        log.error(sm.getString("jmxRemoteLifecycleListener.createRegistryFailed", serverName, Integer.toString(theRmiRegistryPort)), e);
        return null;
    }
    if (bindAddress == null) {
        bindAddress = "localhost";
    }
    String url = "service:jmx:rmi://" + bindAddress;
    JMXServiceURL serviceUrl;
    try {
        serviceUrl = new JMXServiceURL(url);
    } catch (MalformedURLException e) {
        log.error(sm.getString("jmxRemoteLifecycleListener.invalidURL", serverName, url), e);
        return null;
    }
    RMIConnectorServer cs = null;
    try {
        RMIJRMPServerImpl server = new RMIJRMPServerImpl(rmiServerPortPlatform, serverCsf, serverSsf, theEnv);
        cs = new RMIConnectorServer(serviceUrl, theEnv, server, ManagementFactory.getPlatformMBeanServer());
        cs.start();
        registry.bind("jmxrmi", server.toStub());
        log.info(sm.getString("jmxRemoteLifecycleListener.start", Integer.toString(theRmiRegistryPort), Integer.toString(theRmiServerPort), serverName));
    } catch (IOException | AlreadyBoundException e) {
        log.error(sm.getString("jmxRemoteLifecycleListener.createServerFailed", serverName), e);
    }
    return cs;
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL) RMIJRMPServerImpl(javax.management.remote.rmi.RMIJRMPServerImpl) MalformedURLException(java.net.MalformedURLException) RMIConnectorServer(javax.management.remote.rmi.RMIConnectorServer) AlreadyBoundException(java.rmi.AlreadyBoundException) Registry(java.rmi.registry.Registry) LocateRegistry(java.rmi.registry.LocateRegistry) IOException(java.io.IOException) RemoteException(java.rmi.RemoteException)

Example 7 with AlreadyBoundException

use of java.rmi.AlreadyBoundException in project geode by apache.

the class ManagementAgent method configureAndStart.

/**
   * http://docs.oracle.com/javase/6/docs/technotes/guides/management/agent.html #gdfvq
   * https://blogs.oracle.com/jmxetc/entry/java_5_premain_rmi_connectors
   * https://blogs.oracle.com/jmxetc/entry/building_a_remotely_stoppable_connector
   * https://blogs.oracle.com/jmxetc/entry/jmx_connecting_through_firewalls_using
   * https://blogs.oracle.com/jmxetc/entry/java_5_premain_rmi_connectors
   */
private void configureAndStart() throws IOException {
    // get the port for RMI Registry and RMI Connector Server
    final int port = this.config.getJmxManagerPort();
    final String hostname;
    final InetAddress bindAddr;
    if (StringUtils.isBlank(this.config.getJmxManagerBindAddress())) {
        hostname = SocketCreator.getLocalHost().getHostName();
        bindAddr = null;
    } else {
        hostname = this.config.getJmxManagerBindAddress();
        bindAddr = InetAddress.getByName(hostname);
    }
    String jmxManagerHostnameForClients = this.config.getJmxManagerHostnameForClients();
    if (StringUtils.isNotBlank(jmxManagerHostnameForClients)) {
        System.setProperty("java.rmi.server.hostname", jmxManagerHostnameForClients);
    }
    final SocketCreator socketCreator = SocketCreatorFactory.getSocketCreatorForComponent(SecurableCommunicationChannel.JMX);
    final boolean ssl = socketCreator.useSSL();
    if (logger.isDebugEnabled()) {
        logger.debug("Starting jmx manager agent on port {}{}", port, (bindAddr != null ? (" bound to " + bindAddr) : "") + (ssl ? " using SSL" : ""));
    }
    // RMISocketFactory.getDefaultSocketFactory();
    RMIClientSocketFactory rmiClientSocketFactory = ssl ? new SslRMIClientSocketFactory() : null;
    RMIServerSocketFactory rmiServerSocketFactory = new GemFireRMIServerSocketFactory(socketCreator, bindAddr);
    // Following is done to prevent rmi causing stop the world gcs
    System.setProperty("sun.rmi.dgc.server.gcInterval", Long.toString(Long.MAX_VALUE - 1));
    // Create the RMI Registry using the SSL socket factories above.
    // In order to use a single port, we must use these factories
    // everywhere, or nowhere. Since we want to use them in the JMX
    // RMI Connector server, we must also use them in the RMI Registry.
    // Otherwise, we wouldn't be able to use a single port.
    // Start an RMI registry on port <port>.
    registry = LocateRegistry.createRegistry(port, rmiClientSocketFactory, rmiServerSocketFactory);
    // Retrieve the PlatformMBeanServer.
    MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
    // Environment map. why is this declared as HashMap?
    final HashMap<String, Object> env = new HashMap<String, Object>();
    // Manually creates and binds a JMX RMI Connector Server stub with the
    // registry created above: the port we pass here is the port that can
    // be specified in "service:jmx:rmi://"+hostname+":"+port - where the
    // RMI server stub and connection objects will be exported.
    // Here we choose to use the same port as was specified for the
    // RMI Registry. We can do so because we're using \*the same\* client
    // and server socket factories, for the registry itself \*and\* for this
    // object.
    final RMIServerImpl stub = new RMIJRMPServerImpl(port, rmiClientSocketFactory, rmiServerSocketFactory, env);
    // 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 <port> with the name "jmxrmi". This is the same name the
    // out-of-the-box management agent uses to register the RMIServer
    // stub too.
    //
    // The port specified in "service:jmx:rmi://"+hostname+":"+port
    // is the second port, where RMI connection objects will be exported.
    // Here we use the same port as that we choose for the RMI registry.
    // The port for the RMI registry is specified in the second part
    // of the URL, in "rmi://"+hostname+":"+port
    //
    // We construct a JMXServiceURL corresponding to what we have done
    // for our stub...
    final JMXServiceURL url = new JMXServiceURL("service:jmx:rmi://" + hostname + ":" + port + "/jndi/rmi://" + hostname + ":" + port + "/jmxrmi");
    // Create an RMI connector server with the JMXServiceURL
    //
    // JDK 1.5 cannot use JMXConnectorServerFactory because of
    // http://bugs.sun.com/view_bug.do?bug_id=5107423
    // but we're using JDK 1.6
    jmxConnectorServer = new RMIConnectorServer(new JMXServiceURL("rmi", hostname, port), env, stub, mbs) {

        @Override
        public JMXServiceURL getAddress() {
            return url;
        }

        @Override
        public synchronized void start() throws IOException {
            try {
                registry.bind("jmxrmi", stub);
            } catch (AlreadyBoundException x) {
                final IOException io = new IOException(x.getMessage());
                io.initCause(x);
                throw io;
            }
            super.start();
        }
    };
    if (securityService.isIntegratedSecurity()) {
        shiroAuthenticator = new JMXShiroAuthenticator();
        env.put(JMXConnectorServer.AUTHENTICATOR, shiroAuthenticator);
        jmxConnectorServer.addNotificationListener(shiroAuthenticator, null, jmxConnectorServer.getAttributes());
        // always going to assume authorization is needed as well, if no custom AccessControl, then
        // the CustomAuthRealm
        // should take care of that
        MBeanServerWrapper mBeanServerWrapper = new MBeanServerWrapper();
        jmxConnectorServer.setMBeanServerForwarder(mBeanServerWrapper);
        registerAccessControlMBean();
    } else {
        /* Disable the old authenticator mechanism */
        String pwFile = this.config.getJmxManagerPasswordFile();
        if (pwFile != null && pwFile.length() > 0) {
            env.put("jmx.remote.x.password.file", pwFile);
        }
        String accessFile = this.config.getJmxManagerAccessFile();
        if (accessFile != null && accessFile.length() > 0) {
            // Lets not use default connector based authorization
            // env.put("jmx.remote.x.access.file", accessFile);
            // Rewire the mbs hierarchy to set accessController
            ReadOpFileAccessController controller = new ReadOpFileAccessController(accessFile);
            controller.setMBeanServer(mbs);
            mbs = controller;
        }
    }
    jmxConnectorServer.start();
    if (logger.isDebugEnabled()) {
        logger.debug("Finished starting jmx manager agent.");
    }
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL) RMIServerImpl(javax.management.remote.rmi.RMIServerImpl) HashMap(java.util.HashMap) IOException(java.io.IOException) ReadOpFileAccessController(org.apache.geode.management.internal.unsafe.ReadOpFileAccessController) SocketCreator(org.apache.geode.internal.net.SocketCreator) SslRMIClientSocketFactory(javax.rmi.ssl.SslRMIClientSocketFactory) RMIClientSocketFactory(java.rmi.server.RMIClientSocketFactory) SslRMIClientSocketFactory(javax.rmi.ssl.SslRMIClientSocketFactory) RMIJRMPServerImpl(javax.management.remote.rmi.RMIJRMPServerImpl) MBeanServerWrapper(org.apache.geode.management.internal.security.MBeanServerWrapper) JMXShiroAuthenticator(org.apache.geode.internal.security.shiro.JMXShiroAuthenticator) RMIConnectorServer(javax.management.remote.rmi.RMIConnectorServer) AlreadyBoundException(java.rmi.AlreadyBoundException) RMIServerSocketFactory(java.rmi.server.RMIServerSocketFactory) UnicastRemoteObject(java.rmi.server.UnicastRemoteObject) InetAddress(java.net.InetAddress) MBeanServer(javax.management.MBeanServer)

Example 8 with AlreadyBoundException

use of java.rmi.AlreadyBoundException in project tomcat70 by apache.

the class JmxRemoteLifecycleListener method createServer.

private JMXConnectorServer createServer(String serverName, String bindAddress, int theRmiRegistryPort, int theRmiServerPort, HashMap<String, Object> theEnv, RMIClientSocketFactory registryCsf, RMIServerSocketFactory registrySsf, RMIClientSocketFactory serverCsf, RMIServerSocketFactory serverSsf) {
    // Create the RMI registry
    Registry registry;
    try {
        registry = LocateRegistry.createRegistry(theRmiRegistryPort, registryCsf, registrySsf);
    } catch (RemoteException e) {
        log.error(sm.getString("jmxRemoteLifecycleListener.createRegistryFailed", serverName, Integer.toString(theRmiRegistryPort)), e);
        return null;
    }
    if (bindAddress == null) {
        bindAddress = "localhost";
    }
    String url = "service:jmx:rmi://" + bindAddress;
    JMXServiceURL serviceUrl;
    try {
        serviceUrl = new JMXServiceURL(url);
    } catch (MalformedURLException e) {
        log.error(sm.getString("jmxRemoteLifecycleListener.invalidURL", serverName, url), e);
        return null;
    }
    RMIConnectorServer cs = null;
    try {
        RMIJRMPServerImpl server = new RMIJRMPServerImpl(rmiServerPortPlatform, serverCsf, serverSsf, theEnv);
        cs = new RMIConnectorServer(serviceUrl, theEnv, server, ManagementFactory.getPlatformMBeanServer());
        cs.start();
        registry.bind("jmxrmi", server.toStub());
        log.info(sm.getString("jmxRemoteLifecycleListener.start", Integer.toString(theRmiRegistryPort), Integer.toString(theRmiServerPort), serverName));
    } catch (IOException e) {
        log.error(sm.getString("jmxRemoteLifecycleListener.createServerFailed", serverName), e);
    } catch (AlreadyBoundException e) {
        log.error(sm.getString("jmxRemoteLifecycleListener.createServerFailed", serverName), e);
    }
    return cs;
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL) RMIJRMPServerImpl(javax.management.remote.rmi.RMIJRMPServerImpl) MalformedURLException(java.net.MalformedURLException) RMIConnectorServer(javax.management.remote.rmi.RMIConnectorServer) AlreadyBoundException(java.rmi.AlreadyBoundException) Registry(java.rmi.registry.Registry) LocateRegistry(java.rmi.registry.LocateRegistry) IOException(java.io.IOException) RemoteException(java.rmi.RemoteException)

Example 9 with AlreadyBoundException

use of java.rmi.AlreadyBoundException in project jackrabbit 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) RemoteException(java.rmi.RemoteException) InetAddress(java.net.InetAddress)

Aggregations

AlreadyBoundException (java.rmi.AlreadyBoundException)9 LocateRegistry (java.rmi.registry.LocateRegistry)7 Registry (java.rmi.registry.Registry)7 RemoteException (java.rmi.RemoteException)6 IOException (java.io.IOException)5 Remote (java.rmi.Remote)4 InetAddress (java.net.InetAddress)3 UnknownHostException (java.net.UnknownHostException)3 RMIServerSocketFactory (java.rmi.server.RMIServerSocketFactory)3 JMXServiceURL (javax.management.remote.JMXServiceURL)3 RMIConnectorServer (javax.management.remote.rmi.RMIConnectorServer)3 RMIJRMPServerImpl (javax.management.remote.rmi.RMIJRMPServerImpl)3 MalformedURLException (java.net.MalformedURLException)2 NotBoundException (java.rmi.NotBoundException)2 UnicastRemoteObject (java.rmi.server.UnicastRemoteObject)2 ServerSocket (java.net.ServerSocket)1 Socket (java.net.Socket)1 SocketException (java.net.SocketException)1 URISyntaxException (java.net.URISyntaxException)1 ExportException (java.rmi.server.ExportException)1