Search in sources :

Example 1 with SocketCreator

use of org.apache.geode.internal.net.SocketCreator in project geode by apache.

the class ManagementAgent method startHttpService.

private void startHttpService(boolean isServer) {
    final SystemManagementService managementService = (SystemManagementService) ManagementService.getManagementService(CacheFactory.getAnyInstance());
    final ManagerMXBean managerBean = managementService.getManagerMXBean();
    if (this.config.getHttpServicePort() != 0) {
        if (logger.isDebugEnabled()) {
            logger.debug("Attempting to start HTTP service on port ({}) at bind-address ({})...", this.config.getHttpServicePort(), this.config.getHttpServiceBindAddress());
        }
        // Find the Management WAR file
        final String gemfireWar = agentUtil.findWarLocation("geode-web");
        if (gemfireWar == null) {
            if (logger.isDebugEnabled()) {
                logger.debug("Unable to find GemFire Management REST API WAR file; the Management REST Interface for GemFire will not be accessible.");
            }
        }
        // Find the Pulse WAR file
        final String pulseWar = agentUtil.findWarLocation("geode-pulse");
        if (pulseWar == null) {
            final String message = "Unable to find Pulse web application WAR file; Pulse for GemFire will not be accessible";
            setStatusMessage(managerBean, message);
            if (logger.isDebugEnabled()) {
                logger.debug(message);
            }
        } else if (securityService.isIntegratedSecurity()) {
            System.setProperty("spring.profiles.active", "pulse.authentication.gemfire");
        }
        // Find developer REST WAR file
        final String gemfireAPIWar = agentUtil.findWarLocation("geode-web-api");
        if (gemfireAPIWar == null) {
            final String message = "Unable to find GemFire Developer REST API WAR file; the Developer REST Interface for GemFire will not be accessible.";
            setStatusMessage(managerBean, message);
            if (logger.isDebugEnabled()) {
                logger.debug(message);
            }
        }
        try {
            if (agentUtil.isWebApplicationAvailable(gemfireWar, pulseWar, gemfireAPIWar)) {
                final String bindAddress = this.config.getHttpServiceBindAddress();
                final int port = this.config.getHttpServicePort();
                boolean isRestWebAppAdded = false;
                this.httpServer = JettyHelper.initJetty(bindAddress, port, SSLConfigurationFactory.getSSLConfigForComponent(SecurableCommunicationChannel.WEB));
                if (agentUtil.isWebApplicationAvailable(gemfireWar)) {
                    this.httpServer = JettyHelper.addWebApplication(this.httpServer, "/gemfire", gemfireWar);
                    this.httpServer = JettyHelper.addWebApplication(this.httpServer, "/geode-mgmt", gemfireWar);
                }
                if (agentUtil.isWebApplicationAvailable(pulseWar)) {
                    this.httpServer = JettyHelper.addWebApplication(this.httpServer, "/pulse", pulseWar);
                }
                if (isServer && this.config.getStartDevRestApi()) {
                    if (agentUtil.isWebApplicationAvailable(gemfireAPIWar)) {
                        this.httpServer = JettyHelper.addWebApplication(this.httpServer, "/geode", gemfireAPIWar);
                        this.httpServer = JettyHelper.addWebApplication(this.httpServer, "/gemfire-api", gemfireAPIWar);
                        isRestWebAppAdded = true;
                    }
                } else {
                    final String message = "Developer REST API web application will not start when start-dev-rest-api is not set and node is not server";
                    setStatusMessage(managerBean, message);
                    if (logger.isDebugEnabled()) {
                        logger.debug(message);
                    }
                }
                if (logger.isDebugEnabled()) {
                    logger.debug("Starting HTTP embedded server on port ({}) at bind-address ({})...", ((ServerConnector) this.httpServer.getConnectors()[0]).getPort(), bindAddress);
                }
                System.setProperty(PULSE_EMBEDDED_PROP, "true");
                System.setProperty(PULSE_PORT_PROP, "" + config.getJmxManagerPort());
                final SocketCreator jmxSocketCreator = SocketCreatorFactory.getSocketCreatorForComponent(SecurableCommunicationChannel.JMX);
                final SocketCreator locatorSocketCreator = SocketCreatorFactory.getSocketCreatorForComponent(SecurableCommunicationChannel.LOCATOR);
                System.setProperty(PULSE_USESSL_MANAGER, jmxSocketCreator.useSSL() + "");
                System.setProperty(PULSE_USESSL_LOCATOR, locatorSocketCreator.useSSL() + "");
                this.httpServer = JettyHelper.startJetty(this.httpServer);
                // clients to connect to Pulse
                if (agentUtil.isWebApplicationAvailable(pulseWar)) {
                    managerBean.setPulseURL("http://".concat(getHost(bindAddress)).concat(":").concat(String.valueOf(port)).concat("/pulse/"));
                }
                // set cache property for developer REST service running
                if (isRestWebAppAdded) {
                    InternalCache cache = (InternalCache) CacheFactory.getAnyInstance();
                    cache.setRESTServiceRunning(true);
                    // create region to hold query information (queryId, queryString).
                    // Added for the developer REST APIs
                    RestAgent.createParameterizedQueryRegion();
                }
                // set true for HTTP service running
                setHttpServiceRunning(true);
            }
        } catch (Exception e) {
            // Jetty needs to be stopped even if it has failed to
            stopHttpService();
            // start. Some of the threads are left behind even if
            // server.start() fails due to an exception
            setStatusMessage(managerBean, "HTTP service failed to start with " + e.getClass().getSimpleName() + " '" + e.getMessage() + "'");
            throw new ManagementException("HTTP service failed to start", e);
        }
    } else {
        setStatusMessage(managerBean, "Embedded HTTP server configured not to start (http-service-port=0) or (jmx-manager-http-port=0)");
    }
}
Also used : ManagementException(org.apache.geode.management.ManagementException) InternalCache(org.apache.geode.internal.cache.InternalCache) ManagerMXBean(org.apache.geode.management.ManagerMXBean) SocketCreator(org.apache.geode.internal.net.SocketCreator) InstanceAlreadyExistsException(javax.management.InstanceAlreadyExistsException) NotCompliantMBeanException(javax.management.NotCompliantMBeanException) ManagementException(org.apache.geode.management.ManagementException) MalformedObjectNameException(javax.management.MalformedObjectNameException) GemFireConfigException(org.apache.geode.GemFireConfigException) MBeanRegistrationException(javax.management.MBeanRegistrationException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) AlreadyBoundException(java.rmi.AlreadyBoundException)

Example 2 with SocketCreator

use of org.apache.geode.internal.net.SocketCreator 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)

Aggregations

IOException (java.io.IOException)2 AlreadyBoundException (java.rmi.AlreadyBoundException)2 SocketCreator (org.apache.geode.internal.net.SocketCreator)2 InetAddress (java.net.InetAddress)1 UnknownHostException (java.net.UnknownHostException)1 RMIClientSocketFactory (java.rmi.server.RMIClientSocketFactory)1 RMIServerSocketFactory (java.rmi.server.RMIServerSocketFactory)1 UnicastRemoteObject (java.rmi.server.UnicastRemoteObject)1 HashMap (java.util.HashMap)1 InstanceAlreadyExistsException (javax.management.InstanceAlreadyExistsException)1 MBeanRegistrationException (javax.management.MBeanRegistrationException)1 MBeanServer (javax.management.MBeanServer)1 MalformedObjectNameException (javax.management.MalformedObjectNameException)1 NotCompliantMBeanException (javax.management.NotCompliantMBeanException)1 JMXServiceURL (javax.management.remote.JMXServiceURL)1 RMIConnectorServer (javax.management.remote.rmi.RMIConnectorServer)1 RMIJRMPServerImpl (javax.management.remote.rmi.RMIJRMPServerImpl)1 RMIServerImpl (javax.management.remote.rmi.RMIServerImpl)1 SslRMIClientSocketFactory (javax.rmi.ssl.SslRMIClientSocketFactory)1 GemFireConfigException (org.apache.geode.GemFireConfigException)1