src/jdk.management.agent/share/classes/jdk/internal/agent/Agent.java
branchjmx-rest-api
changeset 55985 0c5a02edfdef
parent 47216 71c04702a3d5
child 55994 9721e36abeb0
equal deleted inserted replaced
55984:a43ae4e5fa0a 55985:0c5a02edfdef
    22  * or visit www.oracle.com if you need additional information or have any
    22  * or visit www.oracle.com if you need additional information or have any
    23  * questions.
    23  * questions.
    24  */
    24  */
    25 package jdk.internal.agent;
    25 package jdk.internal.agent;
    26 
    26 
    27 import java.io.BufferedInputStream;
    27 import jdk.internal.agent.spi.AgentProvider;
    28 import java.io.File;
    28 import jdk.internal.vm.VMSupport;
    29 import java.io.FileInputStream;
    29 import sun.management.jdp.JdpController;
    30 import java.io.FileNotFoundException;
    30 import sun.management.jdp.JdpException;
    31 import java.io.IOException;
    31 import sun.management.jmxremote.ConnectorBootstrap;
    32 import java.io.InputStream;
    32 
       
    33 import javax.management.remote.JMXConnectorServer;
       
    34 import javax.management.remote.JMXServiceURL;
       
    35 import javax.management.remote.rest.PlatformRestAdapter;
       
    36 import java.io.*;
    33 import java.lang.management.ManagementFactory;
    37 import java.lang.management.ManagementFactory;
    34 import java.lang.reflect.Method;
    38 import java.lang.reflect.Method;
    35 import java.net.InetAddress;
    39 import java.net.InetAddress;
    36 import java.net.MalformedURLException;
    40 import java.net.MalformedURLException;
    37 import java.net.UnknownHostException;
    41 import java.net.UnknownHostException;
    38 import java.security.AccessController;
    42 import java.security.AccessController;
    39 import java.security.PrivilegedAction;
    43 import java.security.PrivilegedAction;
    40 import java.text.MessageFormat;
    44 import java.text.MessageFormat;
    41 import java.util.HashMap;
    45 import java.util.*;
    42 import java.util.Map;
       
    43 import java.util.MissingResourceException;
       
    44 import java.util.Properties;
       
    45 import java.util.ResourceBundle;
       
    46 import java.util.ServiceLoader;
       
    47 import java.util.function.Function;
    46 import java.util.function.Function;
    48 import java.util.function.Predicate;
    47 import java.util.function.Predicate;
    49 
    48 
    50 import javax.management.remote.JMXConnectorServer;
       
    51 import javax.management.remote.JMXServiceURL;
       
    52 
       
    53 import static jdk.internal.agent.AgentConfigurationError.*;
    49 import static jdk.internal.agent.AgentConfigurationError.*;
    54 import jdk.internal.agent.spi.AgentProvider;
       
    55 import jdk.internal.vm.VMSupport;
       
    56 import sun.management.jdp.JdpController;
       
    57 import sun.management.jdp.JdpException;
       
    58 import sun.management.jmxremote.ConnectorBootstrap;
       
    59 
    50 
    60 /**
    51 /**
    61  * This Agent is started by the VM when -Dcom.sun.management.snmp or
    52  * This Agent is started by the VM when -Dcom.sun.management.snmp or
    62  * -Dcom.sun.management.jmxremote is set. This class will be loaded by the
    53  * -Dcom.sun.management.jmxremote is set. This class will be loaded by the
    63  * system class loader. Also jmx framework could be started by jcmd
    54  * system class loader. Also jmx framework could be started by jcmd
   239     private static ResourceBundle messageRB;
   230     private static ResourceBundle messageRB;
   240     private static final String CONFIG_FILE =
   231     private static final String CONFIG_FILE =
   241             "com.sun.management.config.file";
   232             "com.sun.management.config.file";
   242     private static final String SNMP_PORT =
   233     private static final String SNMP_PORT =
   243             "com.sun.management.snmp.port";
   234             "com.sun.management.snmp.port";
       
   235     private static final String REST_PORT =
       
   236             "com.sun.management.jmxremote.rest.port";
   244     private static final String JMXREMOTE =
   237     private static final String JMXREMOTE =
   245             "com.sun.management.jmxremote";
   238             "com.sun.management.jmxremote";
   246     private static final String JMXREMOTE_PORT =
   239     private static final String JMXREMOTE_PORT =
   247             "com.sun.management.jmxremote.port";
   240             "com.sun.management.jmxremote.port";
   248     private static final String RMI_PORT =
   241     private static final String RMI_PORT =
   417         return new TextStatusCollector().collect();
   410         return new TextStatusCollector().collect();
   418     }
   411     }
   419 
   412 
   420     private static void startAgent(Properties props) throws Exception {
   413     private static void startAgent(Properties props) throws Exception {
   421         String snmpPort = props.getProperty(SNMP_PORT);
   414         String snmpPort = props.getProperty(SNMP_PORT);
       
   415         String restPort = props.getProperty(REST_PORT);
   422         String jmxremote = props.getProperty(JMXREMOTE);
   416         String jmxremote = props.getProperty(JMXREMOTE);
   423         String jmxremotePort = props.getProperty(JMXREMOTE_PORT);
   417         String jmxremotePort = props.getProperty(JMXREMOTE_PORT);
   424 
   418 
   425         // Enable optional monitoring functionality if requested
   419         // Enable optional monitoring functionality if requested
   426         final String enableThreadContentionMonitoring =
   420         final String enableThreadContentionMonitoring =
   431         }
   425         }
   432 
   426 
   433         try {
   427         try {
   434             if (snmpPort != null) {
   428             if (snmpPort != null) {
   435                 loadSnmpAgent(props);
   429                 loadSnmpAgent(props);
       
   430             }
       
   431 
       
   432             if (restPort != null) {
       
   433                 loadRestAdapter(props);
   436             }
   434             }
   437 
   435 
   438             /*
   436             /*
   439              * If the jmxremote.port property is set then we start the
   437              * If the jmxremote.port property is set then we start the
   440              * RMIConnectorServer for remote M&M.
   438              * RMIConnectorServer for remote M&M.
   532 
   530 
   533     public static synchronized Properties getManagementProperties() {
   531     public static synchronized Properties getManagementProperties() {
   534         if (mgmtProps == null) {
   532         if (mgmtProps == null) {
   535             String configFile = System.getProperty(CONFIG_FILE);
   533             String configFile = System.getProperty(CONFIG_FILE);
   536             String snmpPort = System.getProperty(SNMP_PORT);
   534             String snmpPort = System.getProperty(SNMP_PORT);
       
   535             String restPort = System.getProperty(REST_PORT);
   537             String jmxremote = System.getProperty(JMXREMOTE);
   536             String jmxremote = System.getProperty(JMXREMOTE);
   538             String jmxremotePort = System.getProperty(JMXREMOTE_PORT);
   537             String jmxremotePort = System.getProperty(JMXREMOTE_PORT);
   539 
   538 
   540             if (configFile == null && snmpPort == null
   539             if (configFile == null && snmpPort == null && restPort == null
   541                     && jmxremote == null && jmxremotePort == null) {
   540                     && jmxremote == null && jmxremotePort == null) {
   542                 // return if out-of-the-management option is not specified
   541                 // return if out-of-the-management option is not specified
   543                 return null;
   542                 return null;
   544             }
   543             }
   545             mgmtProps = loadManagementProperties();
   544             mgmtProps = loadManagementProperties();
   546         }
   545         }
   547         return mgmtProps;
   546         return mgmtProps;
       
   547     }
       
   548 
       
   549     private static void loadRestAdapter(Properties props) {
       
   550         try {
       
   551             if (props.get(REST_PORT) != null) {
       
   552                 PlatformRestAdapter.init((String) props.get(REST_PORT), props);
       
   553                 PlatformRestAdapter.getInstance().start();
       
   554             }
       
   555         } catch (Throwable ex) {
       
   556             ex.printStackTrace();
       
   557         }
   548     }
   558     }
   549 
   559 
   550     private static void loadSnmpAgent(Properties props) {
   560     private static void loadSnmpAgent(Properties props) {
   551         /*
   561         /*
   552          * Load the jdk.snmp service
   562          * Load the jdk.snmp service