jdk/src/share/classes/sun/management/Agent.java
author dfuchs
Thu, 12 Mar 2009 15:36:14 +0100
changeset 2285 37fdbed8178f
parent 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false Reviewed-by: mchung, ohair
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2003-2005 Sun Microsystems, Inc.  All Rights Reserved.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
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);
37fdbed8178f 6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents: 2
diff changeset
   219
        } catch (ClassNotFoundException x) {
37fdbed8178f 6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents: 2
diff changeset
   220
            // The SNMP packages are not present: throws an exception.
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 (NoSuchMethodException x) {
37fdbed8178f 6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents: 2
diff changeset
   223
            // should not happen...
37fdbed8178f 6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents: 2
diff changeset
   224
            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
   225
        } catch (InvocationTargetException x) {
37fdbed8178f 6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents: 2
diff changeset
   226
            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
   227
            if (cause instanceof RuntimeException)
37fdbed8178f 6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents: 2
diff changeset
   228
                throw (RuntimeException) cause;
37fdbed8178f 6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents: 2
diff changeset
   229
            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
   230
                throw (Error) cause;
37fdbed8178f 6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents: 2
diff changeset
   231
            // should not happen...
37fdbed8178f 6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents: 2
diff changeset
   232
            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
   233
        } catch (IllegalAccessException x) {
37fdbed8178f 6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents: 2
diff changeset
   234
            // should not happen...
37fdbed8178f 6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents: 2
diff changeset
   235
            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
   236
        }
37fdbed8178f 6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents: 2
diff changeset
   237
    }
37fdbed8178f 6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents: 2
diff changeset
   238
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    // read config file and initialize the properties
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    private static void readConfiguration(String fname, Properties p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        if (fname == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            String home = System.getProperty("java.home");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            if (home == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                throw new Error("Can't find java.home ??");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            StringBuffer defaultFileName = new StringBuffer(home);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            defaultFileName.append(File.separator).append("lib");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            defaultFileName.append(File.separator).append("management");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            defaultFileName.append(File.separator).append("management.properties");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            // Set file name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            fname = defaultFileName.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        final File configFile = new File(fname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        if (!configFile.exists()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            error(CONFIG_FILE_NOT_FOUND, fname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        InputStream in = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            in = new FileInputStream(configFile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            BufferedInputStream bin = new BufferedInputStream(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            p.load(bin);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        } catch (FileNotFoundException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            error(CONFIG_FILE_OPEN_FAILED, e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            error(CONFIG_FILE_OPEN_FAILED, e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        } catch (SecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            error(CONFIG_FILE_ACCESS_DENIED, fname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            if (in != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                    in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                    error(CONFIG_FILE_CLOSE_FAILED, fname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    public static void startAgent() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        String prop = System.getProperty("com.sun.management.agent.class");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        // -Dcom.sun.management.agent.class not set so read management
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        // properties and start agent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        if (prop == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            // initialize management properties
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            Properties props = getManagementProperties();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            if (props != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                startAgent(props);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        // -Dcom.sun.management.agent.class=<agent classname>:<agent args>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        String[] values = prop.split(":");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        if (values.length < 1 || values.length > 2) {
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
        String cname = values[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        String args = (values.length == 2 ? values[1] : null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        if (cname == null || cname.length() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            error(AGENT_CLASS_INVALID, "\"" + prop + "\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        if (cname != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                // Instantiate the named class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                // invoke the premain(String args) method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                Class<?> clz = ClassLoader.getSystemClassLoader().loadClass(cname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                Method premain = clz.getMethod("premain",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                                               new Class[] { String.class });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                premain.invoke(null, /* static */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                               new Object[] { args });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            } catch (ClassNotFoundException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                error(AGENT_CLASS_NOT_FOUND, "\"" + cname + "\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            } catch (NoSuchMethodException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                error(AGENT_CLASS_PREMAIN_NOT_FOUND, "\"" + cname + "\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            } catch (SecurityException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                error(AGENT_CLASS_ACCESS_DENIED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                String msg = (ex.getCause() == null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                                 ? ex.getMessage()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                                 : ex.getCause().getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                error(AGENT_CLASS_FAILED, msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        }
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) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        String keyText = getText(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        System.err.print(getText("agent.err.error") + ": " + keyText);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        throw new RuntimeException(keyText);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    public static void error(String key, String[] params) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        if (params == null || params.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            error(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            StringBuffer message = new StringBuffer(params[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            for (int i = 1; i < params.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                message.append(" " + params[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            error(key, message.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    public static void error(String key, String message) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        String keyText = getText(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        System.err.print(getText("agent.err.error") + ": " + keyText);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        System.err.println(": " + message);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        throw new RuntimeException(keyText);
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 error(Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        System.err.println(getText(AGENT_EXCEPTION) + ": " + e.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        throw new RuntimeException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    public static void warning(String key, String message) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        System.err.print(getText("agent.err.warning") + ": " + getText(key));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        System.err.println(": " + message);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    private static void initResource() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            messageRB =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                ResourceBundle.getBundle("sun.management.resources.agent");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        } catch (MissingResourceException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            throw new Error("Fatal: Resource for management agent is missing");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    public static String getText(String key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        if (messageRB == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            initResource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            return messageRB.getString(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        } catch (MissingResourceException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            return "Missing management agent resource bundle: key = \"" + key + "\"";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    public static String getText(String key, String... args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        if (messageRB == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            initResource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        String format = messageRB.getString(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        if (format == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            format = "missing resource key: key = \"" + key + "\", " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                "arguments = \"{0}\", \"{1}\", \"{2}\"";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        return MessageFormat.format(format, (Object[]) args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
}