jdk/src/share/classes/sun/management/Agent.java
author mchung
Tue, 17 Jan 2012 15:55:40 -0800
changeset 11530 a9d059c15b80
parent 5506 202f599c92aa
child 11991 800d0ff7b043
permissions -rw-r--r--
7117570: Warnings in sun.mangement.* and its subpackages Reviewed-by: mchung, dsamersoff Contributed-by: kurchi.subhra.hazra@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2285
diff changeset
     2
 * Copyright (c) 2003, 2005, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2285
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2285
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2285
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2285
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2285
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.management;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.File;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.InputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.FileInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.BufferedInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.FileNotFoundException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.io.IOException;
2285
37fdbed8178f 6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents: 2
diff changeset
    34
import java.lang.reflect.InvocationTargetException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.text.MessageFormat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.util.Properties;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.util.ResourceBundle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.util.MissingResourceException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.lang.management.ManagementFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.lang.reflect.Method;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import javax.management.remote.JMXConnectorServer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import sun.management.jmxremote.ConnectorBootstrap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import static sun.management.AgentConfigurationError.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import sun.misc.VMSupport;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * This Agent is started by the VM when -Dcom.sun.management.snmp
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * or -Dcom.sun.management.jmxremote is set. This class will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * loaded by the system class loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
public class Agent {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    // management properties
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private static Properties mgmtProps;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    private static ResourceBundle messageRB;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private static final String CONFIG_FILE =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        "com.sun.management.config.file";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    private static final String SNMP_PORT =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        "com.sun.management.snmp.port";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    private static final String JMXREMOTE =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        "com.sun.management.jmxremote";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    private static final String JMXREMOTE_PORT =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        "com.sun.management.jmxremote.port";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    private static final String ENABLE_THREAD_CONTENTION_MONITORING =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        "com.sun.management.enableThreadContentionMonitoring";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    private static final String LOCAL_CONNECTOR_ADDRESS_PROP =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        "com.sun.management.jmxremote.localConnectorAddress";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
2285
37fdbed8178f 6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents: 2
diff changeset
    71
    private static final String SNMP_ADAPTOR_BOOTSTRAP_CLASS_NAME =
37fdbed8178f 6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents: 2
diff changeset
    72
            "sun.management.snmp.AdaptorBootstrap";
37fdbed8178f 6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents: 2
diff changeset
    73
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    // invoked by -javaagent or -Dcom.sun.management.agent.class
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    public static void premain(String args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        agentmain(args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    // invoked by attach mechanism
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    public static void agentmain(String args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        if (args == null || args.length() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            args = JMXREMOTE;           // default to local management
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        // Parse agent options into properties
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        Properties arg_props = new Properties();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        if (args != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            String[] options = args.split(",");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            for (int i=0; i<options.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                String[] option = options[i].split("=");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                if (option.length >= 1 && option.length <= 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                    String name = option[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                    String value = (option.length == 1) ? "" : option[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                    if (name != null && name.length() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                        // Assume that any com.sun.management.* options are okay
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                        if (name.startsWith("com.sun.management.")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                            arg_props.setProperty(name, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                            error(INVALID_OPTION, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        // Read properties from the config file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        Properties config_props = new Properties();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        String fname = arg_props.getProperty(CONFIG_FILE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        readConfiguration(fname, config_props);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        // Arguments override config file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        config_props.putAll(arg_props);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        startAgent(config_props);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    private static void startAgent(Properties props) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        String snmpPort = props.getProperty(SNMP_PORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        String jmxremote = props.getProperty(JMXREMOTE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        String jmxremotePort = props.getProperty(JMXREMOTE_PORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        // Enable optional monitoring functionality if requested
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        final String enableThreadContentionMonitoring =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            props.getProperty(ENABLE_THREAD_CONTENTION_MONITORING);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        if (enableThreadContentionMonitoring != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            ManagementFactory.getThreadMXBean().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                setThreadContentionMonitoringEnabled(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            if (snmpPort != null) {
2285
37fdbed8178f 6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents: 2
diff changeset
   133
                loadSnmpAgent(snmpPort, props);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
             * If the jmxremote.port property is set then we start the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
             * RMIConnectorServer for remote M&M.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
             *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
             * If the jmxremote or jmxremote.port properties are set then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
             * we start a RMIConnectorServer for local M&M. The address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
             * of this "local" server is exported as a counter to the jstat
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
             * instrumentation buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            if (jmxremote != null || jmxremotePort != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                if (jmxremotePort != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                    ConnectorBootstrap.initialize(jmxremotePort, props);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                Properties agentProps = VMSupport.getAgentProperties();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                // start local connector if not started
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                // System.out.println("local address : " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                  //     agentProps.get(LOCAL_CONNECTOR_ADDRESS_PROP));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                if (agentProps.get(LOCAL_CONNECTOR_ADDRESS_PROP) == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                    JMXConnectorServer cs = ConnectorBootstrap.startLocalConnectorServer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                    String address = cs.getAddress().toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                    // Add the local connector address to the agent properties
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                    agentProps.put(LOCAL_CONNECTOR_ADDRESS_PROP, address);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                        // export the address to the instrumentation buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                        ConnectorAddressLink.export(address);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                    } catch (Exception x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                        // Connector server started but unable to export address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                        // to instrumentation buffer - non-fatal error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                        warning(EXPORT_ADDRESS_FAILED, x.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        } catch (AgentConfigurationError e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            error(e.getError(), e.getParams());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            error(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    public static Properties loadManagementProperties() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        Properties props = new Properties();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        // Load the management properties from the config file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        String fname = System.getProperty(CONFIG_FILE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        readConfiguration(fname, props);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        // management properties can be overridden by system properties
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        // which take precedence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        props.putAll(System.getProperties());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        return props;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    public static synchronized Properties getManagementProperties() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        if (mgmtProps == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            String configFile = System.getProperty(CONFIG_FILE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            String snmpPort = System.getProperty(SNMP_PORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            String jmxremote = System.getProperty(JMXREMOTE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            String jmxremotePort = System.getProperty(JMXREMOTE_PORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            if (configFile == null && snmpPort == null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                jmxremote == null && jmxremotePort == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                // return if out-of-the-management option is not specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            mgmtProps = loadManagementProperties();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        return mgmtProps;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
2285
37fdbed8178f 6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents: 2
diff changeset
   209
    private static void loadSnmpAgent(String snmpPort, Properties props) {
37fdbed8178f 6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents: 2
diff changeset
   210
        try {
37fdbed8178f 6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents: 2
diff changeset
   211
            // invoke the following through reflection:
37fdbed8178f 6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents: 2
diff changeset
   212
            //     AdaptorBootstrap.initialize(snmpPort, props);
37fdbed8178f 6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents: 2
diff changeset
   213
            final Class<?> adaptorClass =
37fdbed8178f 6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents: 2
diff changeset
   214
                Class.forName(SNMP_ADAPTOR_BOOTSTRAP_CLASS_NAME,true,null);
37fdbed8178f 6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents: 2
diff changeset
   215
            final Method initializeMethod =
37fdbed8178f 6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents: 2
diff changeset
   216
                    adaptorClass.getMethod("initialize",
37fdbed8178f 6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents: 2
diff changeset
   217
                        String.class, Properties.class);
37fdbed8178f 6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents: 2
diff changeset
   218
            initializeMethod.invoke(null,snmpPort,props);
11530
a9d059c15b80 7117570: Warnings in sun.mangement.* and its subpackages
mchung
parents: 5506
diff changeset
   219
        } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException x) {
a9d059c15b80 7117570: Warnings in sun.mangement.* and its subpackages
mchung
parents: 5506
diff changeset
   220
            // snmp runtime doesn't exist - initialization fails
2285
37fdbed8178f 6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents: 2
diff changeset
   221
            throw new UnsupportedOperationException("Unsupported management property: " + SNMP_PORT,x);
37fdbed8178f 6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents: 2
diff changeset
   222
        } catch (InvocationTargetException x) {
37fdbed8178f 6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents: 2
diff changeset
   223
            final Throwable cause = x.getCause();
37fdbed8178f 6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents: 2
diff changeset
   224
            if (cause instanceof RuntimeException)
37fdbed8178f 6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents: 2
diff changeset
   225
                throw (RuntimeException) cause;
37fdbed8178f 6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents: 2
diff changeset
   226
            else if (cause instanceof Error)
37fdbed8178f 6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents: 2
diff changeset
   227
                throw (Error) cause;
37fdbed8178f 6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents: 2
diff changeset
   228
            // should not happen...
37fdbed8178f 6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents: 2
diff changeset
   229
            throw new UnsupportedOperationException("Unsupported management property: " + SNMP_PORT,cause);
37fdbed8178f 6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents: 2
diff changeset
   230
        }
37fdbed8178f 6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents: 2
diff changeset
   231
    }
37fdbed8178f 6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents: 2
diff changeset
   232
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    // read config file and initialize the properties
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    private static void readConfiguration(String fname, Properties p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        if (fname == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            String home = System.getProperty("java.home");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            if (home == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                throw new Error("Can't find java.home ??");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            StringBuffer defaultFileName = new StringBuffer(home);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            defaultFileName.append(File.separator).append("lib");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            defaultFileName.append(File.separator).append("management");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            defaultFileName.append(File.separator).append("management.properties");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            // Set file name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            fname = defaultFileName.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        final File configFile = new File(fname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        if (!configFile.exists()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            error(CONFIG_FILE_NOT_FOUND, fname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        InputStream in = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            in = new FileInputStream(configFile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            BufferedInputStream bin = new BufferedInputStream(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            p.load(bin);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        } catch (FileNotFoundException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            error(CONFIG_FILE_OPEN_FAILED, e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            error(CONFIG_FILE_OPEN_FAILED, e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        } catch (SecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            error(CONFIG_FILE_ACCESS_DENIED, fname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            if (in != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                    in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                    error(CONFIG_FILE_CLOSE_FAILED, fname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                }
11530
a9d059c15b80 7117570: Warnings in sun.mangement.* and its subpackages
mchung
parents: 5506
diff changeset
   270
             }
a9d059c15b80 7117570: Warnings in sun.mangement.* and its subpackages
mchung
parents: 5506
diff changeset
   271
         }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    public static void startAgent() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        String prop = System.getProperty("com.sun.management.agent.class");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        // -Dcom.sun.management.agent.class not set so read management
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        // properties and start agent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        if (prop == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            // initialize management properties
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            Properties props = getManagementProperties();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            if (props != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                startAgent(props);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        // -Dcom.sun.management.agent.class=<agent classname>:<agent args>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        String[] values = prop.split(":");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        if (values.length < 1 || values.length > 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            error(AGENT_CLASS_INVALID, "\"" + prop + "\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        String cname = values[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        String args = (values.length == 2 ? values[1] : null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        if (cname == null || cname.length() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            error(AGENT_CLASS_INVALID, "\"" + prop + "\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        if (cname != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                // Instantiate the named class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                // invoke the premain(String args) method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                Class<?> clz = ClassLoader.getSystemClassLoader().loadClass(cname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                Method premain = clz.getMethod("premain",
11530
a9d059c15b80 7117570: Warnings in sun.mangement.* and its subpackages
mchung
parents: 5506
diff changeset
   306
                                               new Class<?>[] { String.class });
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                premain.invoke(null, /* static */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                               new Object[] { args });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            } catch (ClassNotFoundException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                error(AGENT_CLASS_NOT_FOUND, "\"" + cname + "\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            } catch (NoSuchMethodException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                error(AGENT_CLASS_PREMAIN_NOT_FOUND, "\"" + cname + "\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            } catch (SecurityException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                error(AGENT_CLASS_ACCESS_DENIED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                String msg = (ex.getCause() == null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                                 ? ex.getMessage()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                                 : ex.getCause().getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                error(AGENT_CLASS_FAILED, msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    public static void error(String key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        String keyText = getText(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        System.err.print(getText("agent.err.error") + ": " + keyText);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        throw new RuntimeException(keyText);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    public static void error(String key, String[] params) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        if (params == null || params.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            error(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            StringBuffer message = new StringBuffer(params[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            for (int i = 1; i < params.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                message.append(" " + params[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            error(key, message.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    public static void error(String key, String message) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        String keyText = getText(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        System.err.print(getText("agent.err.error") + ": " + keyText);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        System.err.println(": " + message);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        throw new RuntimeException(keyText);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    public static void error(Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        System.err.println(getText(AGENT_EXCEPTION) + ": " + e.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        throw new RuntimeException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    public static void warning(String key, String message) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        System.err.print(getText("agent.err.warning") + ": " + getText(key));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        System.err.println(": " + message);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    private static void initResource() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            messageRB =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                ResourceBundle.getBundle("sun.management.resources.agent");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        } catch (MissingResourceException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            throw new Error("Fatal: Resource for management agent is missing");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    public static String getText(String key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        if (messageRB == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            initResource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            return messageRB.getString(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        } catch (MissingResourceException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            return "Missing management agent resource bundle: key = \"" + key + "\"";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    public static String getText(String key, String... args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        if (messageRB == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            initResource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        String format = messageRB.getString(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        if (format == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            format = "missing resource key: key = \"" + key + "\", " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                "arguments = \"{0}\", \"{1}\", \"{2}\"";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        return MessageFormat.format(format, (Object[]) args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
}