Search in sources :

Example 1 with AgentConfigurationError

use of sun.management.AgentConfigurationError in project jdk8u_jdk by JetBrains.

the class ConnectorBootstrap method startLocalConnectorServer.

/*
     * Creates and starts a RMI Connector Server for "local" monitoring
     * and management.
     */
public static JMXConnectorServer startLocalConnectorServer() {
    // Ensure cryptographically strong random number generater used
    // to choose the object number - see java.rmi.server.ObjID
    System.setProperty("java.rmi.server.randomIDs", "true");
    // This RMI server should not keep the VM alive
    Map<String, Object> env = new HashMap<>();
    env.put(RMIExporter.EXPORTER_ATTRIBUTE, new PermanentExporter());
    env.put(EnvHelp.CREDENTIAL_TYPES, new String[] { String[].class.getName(), String.class.getName() });
    // The local connector server need only be available via the
    // loopback connection.
    String localhost = "localhost";
    InetAddress lh = null;
    try {
        lh = InetAddress.getByName(localhost);
        localhost = lh.getHostAddress();
    } catch (UnknownHostException x) {
    }
    // a loopback address.
    if (lh == null || !lh.isLoopbackAddress()) {
        localhost = "127.0.0.1";
    }
    MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
    try {
        JMXServiceURL url = new JMXServiceURL("rmi", localhost, 0);
        // Do we accept connections from local interfaces only?
        Properties props = Agent.getManagementProperties();
        if (props == null) {
            props = new Properties();
        }
        String useLocalOnlyStr = props.getProperty(PropertyNames.USE_LOCAL_ONLY, DefaultValues.USE_LOCAL_ONLY);
        boolean useLocalOnly = Boolean.valueOf(useLocalOnlyStr).booleanValue();
        if (useLocalOnly) {
            env.put(RMIConnectorServer.RMI_SERVER_SOCKET_FACTORY_ATTRIBUTE, new LocalRMIServerSocketFactory());
        }
        JMXConnectorServer server = JMXConnectorServerFactory.newJMXConnectorServer(url, env, mbs);
        server.start();
        return server;
    } catch (Exception e) {
        throw new AgentConfigurationError(AGENT_EXCEPTION, e, e.toString());
    }
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL) UnknownHostException(java.net.UnknownHostException) HashMap(java.util.HashMap) Properties(java.util.Properties) RemoteException(java.rmi.RemoteException) NoSuchObjectException(java.rmi.NoSuchObjectException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) JMXConnectorServer(javax.management.remote.JMXConnectorServer) AgentConfigurationError(sun.management.AgentConfigurationError) UnicastRemoteObject(java.rmi.server.UnicastRemoteObject) RemoteObject(java.rmi.server.RemoteObject) InetAddress(java.net.InetAddress) MBeanServer(javax.management.MBeanServer)

Example 2 with AgentConfigurationError

use of sun.management.AgentConfigurationError in project jdk8u_jdk by JetBrains.

the class AdaptorBootstrap method getAdaptorBootstrap.

private static AdaptorBootstrap getAdaptorBootstrap(int port, int trapPort, String bindAddress, boolean useAcl, String aclFileName) {
    final InetAddress address;
    try {
        address = InetAddress.getByName(bindAddress);
    } catch (UnknownHostException e) {
        throw new AgentConfigurationError(UNKNOWN_SNMP_INTERFACE, e, bindAddress);
    }
    if (log.isDebugOn()) {
        log.debug("initialize", Agent.getText("jmxremote.AdaptorBootstrap.getTargetList.starting" + "\n\t" + PropertyNames.PORT + "=" + port + "\n\t" + PropertyNames.TRAP_PORT + "=" + trapPort + "\n\t" + PropertyNames.BIND_ADDRESS + "=" + address + (useAcl ? ("\n\t" + PropertyNames.ACL_FILE_NAME + "=" + aclFileName) : "\n\tNo ACL") + ""));
    }
    final InetAddressAcl acl;
    try {
        acl = useAcl ? new SnmpAcl(System.getProperty("user.name"), aclFileName) : null;
    } catch (UnknownHostException e) {
        throw new AgentConfigurationError(UNKNOWN_SNMP_INTERFACE, e, e.getMessage());
    }
    // Create adaptor
    final SnmpAdaptorServer adaptor = new SnmpAdaptorServer(acl, port, address);
    adaptor.setUserDataFactory(new JvmContextFactory());
    adaptor.setTrapPort(trapPort);
    // Create MIB
    //
    final JVM_MANAGEMENT_MIB_IMPL mib = new JVM_MANAGEMENT_MIB_IMPL();
    try {
        mib.init();
    } catch (IllegalAccessException x) {
        throw new AgentConfigurationError(SNMP_MIB_INIT_FAILED, x, x.getMessage());
    }
    // Configure the trap destinations.
    //
    mib.addTargets(getTargetList(acl, trapPort));
    //
    try {
        // Will wait until the adaptor starts or fails to start.
        // If the adaptor fails to start, a CommunicationException or
        // an InterruptedException is thrown.
        //
        adaptor.start(Long.MAX_VALUE);
    } catch (Exception x) {
        Throwable t = x;
        if (x instanceof com.sun.jmx.snmp.daemon.CommunicationException) {
            final Throwable next = t.getCause();
            if (next != null)
                t = next;
        }
        throw new AgentConfigurationError(SNMP_ADAPTOR_START_FAILED, t, address + ":" + port, "(" + t.getMessage() + ")");
    }
    //
    if (!adaptor.isActive()) {
        throw new AgentConfigurationError(SNMP_ADAPTOR_START_FAILED, address + ":" + port);
    }
    try {
        // Add MIB to adaptor
        //
        adaptor.addMib(mib);
        // Add Adaptor to the MIB
        //
        mib.setSnmpAdaptor(adaptor);
    } catch (RuntimeException x) {
        new AdaptorBootstrap(adaptor, mib).terminate();
        throw x;
    }
    log.debug("initialize", Agent.getText("jmxremote.AdaptorBootstrap.getTargetList.initialize1"));
    log.config("initialize", Agent.getText("jmxremote.AdaptorBootstrap.getTargetList.initialize2", address.toString(), java.lang.Integer.toString(adaptor.getPort())));
    return new AdaptorBootstrap(adaptor, mib);
}
Also used : SnmpAcl(com.sun.jmx.snmp.IPAcl.SnmpAcl) UnknownHostException(java.net.UnknownHostException) InetAddressAcl(com.sun.jmx.snmp.InetAddressAcl) JVM_MANAGEMENT_MIB_IMPL(sun.management.snmp.jvminstr.JVM_MANAGEMENT_MIB_IMPL) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) JvmContextFactory(sun.management.snmp.util.JvmContextFactory) SnmpAdaptorServer(com.sun.jmx.snmp.daemon.SnmpAdaptorServer) AgentConfigurationError(sun.management.AgentConfigurationError) InetAddress(java.net.InetAddress)

Example 3 with AgentConfigurationError

use of sun.management.AgentConfigurationError in project jdk8u_jdk by JetBrains.

the class AdaptorBootstrap method initialize.

/**
     * Initializes and starts the SNMP Adaptor Server.
     **/
public static synchronized AdaptorBootstrap initialize(String portStr, Properties props) {
    // Get port number
    if (portStr.length() == 0)
        portStr = DefaultValues.PORT;
    final int port;
    try {
        port = Integer.parseInt(portStr);
    } catch (NumberFormatException x) {
        throw new AgentConfigurationError(INVALID_SNMP_PORT, x, portStr);
    }
    if (port < 0) {
        throw new AgentConfigurationError(INVALID_SNMP_PORT, portStr);
    }
    // Get trap port number
    final String trapPortStr = props.getProperty(PropertyNames.TRAP_PORT, DefaultValues.TRAP_PORT);
    final int trapPort;
    try {
        trapPort = Integer.parseInt(trapPortStr);
    } catch (NumberFormatException x) {
        throw new AgentConfigurationError(INVALID_SNMP_TRAP_PORT, x, trapPortStr);
    }
    if (trapPort < 0) {
        throw new AgentConfigurationError(INVALID_SNMP_TRAP_PORT, trapPortStr);
    }
    // Get bind address
    final String addrStr = props.getProperty(PropertyNames.BIND_ADDRESS, DefaultValues.BIND_ADDRESS);
    // Get ACL File
    final String defaultAclFileName = getDefaultFileName(DefaultValues.ACL_FILE_NAME);
    final String aclFileName = props.getProperty(PropertyNames.ACL_FILE_NAME, defaultAclFileName);
    final String useAclStr = props.getProperty(PropertyNames.USE_ACL, DefaultValues.USE_ACL);
    final boolean useAcl = Boolean.valueOf(useAclStr).booleanValue();
    if (useAcl)
        checkAclFile(aclFileName);
    AdaptorBootstrap adaptor = null;
    try {
        adaptor = getAdaptorBootstrap(port, trapPort, addrStr, useAcl, aclFileName);
    } catch (Exception e) {
        throw new AgentConfigurationError(AGENT_EXCEPTION, e, e.getMessage());
    }
    return adaptor;
}
Also used : AgentConfigurationError(sun.management.AgentConfigurationError) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException)

Example 4 with AgentConfigurationError

use of sun.management.AgentConfigurationError in project jdk8u_jdk by JetBrains.

the class ConnectorBootstrap method checkRestrictedFile.

private static void checkRestrictedFile(String restrictedFileName) {
    if (restrictedFileName == null || restrictedFileName.length() == 0) {
        throw new AgentConfigurationError(FILE_NOT_SET);
    }
    File file = new File(restrictedFileName);
    if (!file.exists()) {
        throw new AgentConfigurationError(FILE_NOT_FOUND, restrictedFileName);
    }
    if (!file.canRead()) {
        throw new AgentConfigurationError(FILE_NOT_READABLE, restrictedFileName);
    }
    FileSystem fs = FileSystem.open();
    try {
        if (fs.supportsFileSecurity(file)) {
            if (!fs.isAccessUserOnly(file)) {
                final String msg = Agent.getText("jmxremote.ConnectorBootstrap.file.readonly", restrictedFileName);
                log.config("startRemoteConnectorServer", msg);
                throw new AgentConfigurationError(FILE_ACCESS_NOT_RESTRICTED, restrictedFileName);
            }
        }
    } catch (IOException e) {
        throw new AgentConfigurationError(FILE_READ_FAILED, e, restrictedFileName);
    }
}
Also used : AgentConfigurationError(sun.management.AgentConfigurationError) FileSystem(sun.management.FileSystem) IOException(java.io.IOException) File(java.io.File)

Example 5 with AgentConfigurationError

use of sun.management.AgentConfigurationError in project jdk8u_jdk by JetBrains.

the class ConnectorBootstrap method createSslRMIServerSocketFactory.

private static SslRMIServerSocketFactory createSslRMIServerSocketFactory(String sslConfigFileName, String[] enabledCipherSuites, String[] enabledProtocols, boolean sslNeedClientAuth, String bindAddress) {
    if (sslConfigFileName == null) {
        return new HostAwareSslSocketFactory(enabledCipherSuites, enabledProtocols, sslNeedClientAuth, bindAddress);
    } else {
        checkRestrictedFile(sslConfigFileName);
        try {
            // Load the SSL keystore properties from the config file
            Properties p = new Properties();
            try (InputStream in = new FileInputStream(sslConfigFileName)) {
                BufferedInputStream bin = new BufferedInputStream(in);
                p.load(bin);
            }
            String keyStore = p.getProperty("javax.net.ssl.keyStore");
            String keyStorePassword = p.getProperty("javax.net.ssl.keyStorePassword", "");
            String trustStore = p.getProperty("javax.net.ssl.trustStore");
            String trustStorePassword = p.getProperty("javax.net.ssl.trustStorePassword", "");
            char[] keyStorePasswd = null;
            if (keyStorePassword.length() != 0) {
                keyStorePasswd = keyStorePassword.toCharArray();
            }
            char[] trustStorePasswd = null;
            if (trustStorePassword.length() != 0) {
                trustStorePasswd = trustStorePassword.toCharArray();
            }
            KeyStore ks = null;
            if (keyStore != null) {
                ks = KeyStore.getInstance(KeyStore.getDefaultType());
                try (FileInputStream ksfis = new FileInputStream(keyStore)) {
                    ks.load(ksfis, keyStorePasswd);
                }
            }
            KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
            kmf.init(ks, keyStorePasswd);
            KeyStore ts = null;
            if (trustStore != null) {
                ts = KeyStore.getInstance(KeyStore.getDefaultType());
                try (FileInputStream tsfis = new FileInputStream(trustStore)) {
                    ts.load(tsfis, trustStorePasswd);
                }
            }
            TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
            tmf.init(ts);
            SSLContext ctx = SSLContext.getInstance("SSL");
            ctx.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
            return new HostAwareSslSocketFactory(ctx, enabledCipherSuites, enabledProtocols, sslNeedClientAuth, bindAddress);
        } catch (Exception e) {
            throw new AgentConfigurationError(AGENT_EXCEPTION, e, e.toString());
        }
    }
}
Also used : BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) SSLContext(javax.net.ssl.SSLContext) Properties(java.util.Properties) KeyStore(java.security.KeyStore) FileInputStream(java.io.FileInputStream) RemoteException(java.rmi.RemoteException) NoSuchObjectException(java.rmi.NoSuchObjectException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) KeyManagerFactory(javax.net.ssl.KeyManagerFactory) BufferedInputStream(java.io.BufferedInputStream) TrustManagerFactory(javax.net.ssl.TrustManagerFactory) AgentConfigurationError(sun.management.AgentConfigurationError)

Aggregations

AgentConfigurationError (sun.management.AgentConfigurationError)10 IOException (java.io.IOException)9 UnknownHostException (java.net.UnknownHostException)6 JMXServiceURL (javax.management.remote.JMXServiceURL)4 File (java.io.File)3 InetAddress (java.net.InetAddress)3 MalformedURLException (java.net.MalformedURLException)3 NoSuchObjectException (java.rmi.NoSuchObjectException)3 RemoteException (java.rmi.RemoteException)3 HashMap (java.util.HashMap)3 JMXConnectorServer (javax.management.remote.JMXConnectorServer)3 FileSystem (sun.management.FileSystem)3 RemoteObject (java.rmi.server.RemoteObject)2 UnicastRemoteObject (java.rmi.server.UnicastRemoteObject)2 Properties (java.util.Properties)2 MBeanServer (javax.management.MBeanServer)2 SnmpAcl (com.sun.jmx.snmp.IPAcl.SnmpAcl)1 InetAddressAcl (com.sun.jmx.snmp.InetAddressAcl)1 SnmpAdaptorServer (com.sun.jmx.snmp.daemon.SnmpAdaptorServer)1 BufferedInputStream (java.io.BufferedInputStream)1