jdk/src/java.management/share/classes/sun/management/Agent.java
author chegar
Wed, 03 Dec 2014 14:22:58 +0000
changeset 27565 729f9700483a
parent 26488 5b5fccd9d016
child 27957 24b4e6082f19
permissions -rw-r--r--
8049367: Modular Run-Time Images Reviewed-by: chegar, dfuchs, ihse, joehw, mullan, psandoz, wetmore Contributed-by: alan.bateman@oracle.com, alex.buckley@oracle.com, bradford.wetmore@oracle.com, chris.hegarty@oracle.com, erik.joelsson@oracle.com, james.laskey@oracle.com, jonathan.gibbons@oracle.com, karen.kinnear@oracle.com, magnus.ihse.bursie@oracle.com, mandy.chung@oracle.com, mark.reinhold@oracle.com, paul.sandoz@oracle.com, sundararajan.athijegannathan@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 21298
diff changeset
     2
 * Copyright (c) 2003, 2013, 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
package sun.management;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
11991
800d0ff7b043 7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents: 11530
diff changeset
    27
import java.io.BufferedInputStream;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.File;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.FileInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.FileNotFoundException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.IOException;
11991
800d0ff7b043 7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents: 11530
diff changeset
    32
import java.io.InputStream;
15531
071efc9f31ad 8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents: 12694
diff changeset
    33
import java.lang.management.ManagementFactory;
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;
11991
800d0ff7b043 7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents: 11530
diff changeset
    35
import java.lang.reflect.Method;
15531
071efc9f31ad 8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents: 12694
diff changeset
    36
import java.net.InetAddress;
071efc9f31ad 8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents: 12694
diff changeset
    37
import java.net.UnknownHostException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.text.MessageFormat;
11991
800d0ff7b043 7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents: 11530
diff changeset
    39
import java.util.MissingResourceException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.util.Properties;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.util.ResourceBundle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import javax.management.remote.JMXConnectorServer;
15531
071efc9f31ad 8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents: 12694
diff changeset
    44
import javax.management.remote.JMXServiceURL;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
11991
800d0ff7b043 7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents: 11530
diff changeset
    46
import static sun.management.AgentConfigurationError.*;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import sun.management.jmxremote.ConnectorBootstrap;
15531
071efc9f31ad 8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents: 12694
diff changeset
    48
import sun.management.jdp.JdpController;
071efc9f31ad 8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents: 12694
diff changeset
    49
import sun.management.jdp.JdpException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import sun.misc.VMSupport;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
/**
15531
071efc9f31ad 8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents: 12694
diff changeset
    53
 * This Agent is started by the VM when -Dcom.sun.management.snmp or
071efc9f31ad 8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents: 12694
diff changeset
    54
 * -Dcom.sun.management.jmxremote is set. This class will be loaded by the
071efc9f31ad 8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents: 12694
diff changeset
    55
 * system class loader. Also jmx framework could be started by jcmd
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
public class Agent {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    // management properties
15531
071efc9f31ad 8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents: 12694
diff changeset
    59
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    private static Properties mgmtProps;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    private static ResourceBundle messageRB;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    private static final String CONFIG_FILE =
15531
071efc9f31ad 8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents: 12694
diff changeset
    63
            "com.sun.management.config.file";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    private static final String SNMP_PORT =
15531
071efc9f31ad 8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents: 12694
diff changeset
    65
            "com.sun.management.snmp.port";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    private static final String JMXREMOTE =
15531
071efc9f31ad 8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents: 12694
diff changeset
    67
            "com.sun.management.jmxremote";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    private static final String JMXREMOTE_PORT =
15531
071efc9f31ad 8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents: 12694
diff changeset
    69
            "com.sun.management.jmxremote.port";
071efc9f31ad 8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents: 12694
diff changeset
    70
    private static final String RMI_PORT =
071efc9f31ad 8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents: 12694
diff changeset
    71
            "com.sun.management.jmxremote.rmi.port";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    private static final String ENABLE_THREAD_CONTENTION_MONITORING =
15531
071efc9f31ad 8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents: 12694
diff changeset
    73
            "com.sun.management.enableThreadContentionMonitoring";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    private static final String LOCAL_CONNECTOR_ADDRESS_PROP =
15531
071efc9f31ad 8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents: 12694
diff changeset
    75
            "com.sun.management.jmxremote.localConnectorAddress";
071efc9f31ad 8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents: 12694
diff changeset
    76
    private static final String SNMP_ADAPTOR_BOOTSTRAP_CLASS_NAME =
071efc9f31ad 8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents: 12694
diff changeset
    77
            "sun.management.snmp.AdaptorBootstrap";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
17721
1d6516794d05 8014420: Default JDP address does not match the one assigned by IANA
dsamersoff
parents: 15531
diff changeset
    79
    private static final String JDP_DEFAULT_ADDRESS = "224.0.23.178";
15531
071efc9f31ad 8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents: 12694
diff changeset
    80
    private static final int JDP_DEFAULT_PORT = 7095;
11991
800d0ff7b043 7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents: 11530
diff changeset
    81
800d0ff7b043 7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents: 11530
diff changeset
    82
    // The only active agent allowed
800d0ff7b043 7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents: 11530
diff changeset
    83
    private static JMXConnectorServer jmxServer = null;
800d0ff7b043 7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents: 11530
diff changeset
    84
800d0ff7b043 7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents: 11530
diff changeset
    85
    // Parse string com.sun.management.prop=xxx,com.sun.management.prop=yyyy
800d0ff7b043 7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents: 11530
diff changeset
    86
    // and return property set if args is null or empty
800d0ff7b043 7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents: 11530
diff changeset
    87
    // return empty property set
15531
071efc9f31ad 8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents: 12694
diff changeset
    88
    private static Properties parseString(String args) {
11991
800d0ff7b043 7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents: 11530
diff changeset
    89
        Properties argProps = new Properties();
24980
dbf4ae2cafa4 8046778: Better error messages when starting JMX agent via attach or jcmd
sla
parents: 24969
diff changeset
    90
        if (args != null && !args.trim().equals("")) {
15531
071efc9f31ad 8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents: 12694
diff changeset
    91
            for (String option : args.split(",")) {
071efc9f31ad 8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents: 12694
diff changeset
    92
                String s[] = option.split("=", 2);
071efc9f31ad 8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents: 12694
diff changeset
    93
                String name = s[0].trim();
071efc9f31ad 8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents: 12694
diff changeset
    94
                String value = (s.length > 1) ? s[1].trim() : "";
11991
800d0ff7b043 7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents: 11530
diff changeset
    95
15531
071efc9f31ad 8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents: 12694
diff changeset
    96
                if (!name.startsWith("com.sun.management.")) {
071efc9f31ad 8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents: 12694
diff changeset
    97
                    error(INVALID_OPTION, name);
071efc9f31ad 8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents: 12694
diff changeset
    98
                }
11991
800d0ff7b043 7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents: 11530
diff changeset
    99
15531
071efc9f31ad 8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents: 12694
diff changeset
   100
                argProps.setProperty(name, value);
071efc9f31ad 8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents: 12694
diff changeset
   101
            }
11991
800d0ff7b043 7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents: 11530
diff changeset
   102
        }
800d0ff7b043 7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents: 11530
diff changeset
   103
800d0ff7b043 7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents: 11530
diff changeset
   104
        return argProps;
800d0ff7b043 7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents: 11530
diff changeset
   105
    }
800d0ff7b043 7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents: 11530
diff changeset
   106
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    // invoked by -javaagent or -Dcom.sun.management.agent.class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    public static void premain(String args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        agentmain(args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    // invoked by attach mechanism
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    public static void agentmain(String args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        if (args == null || args.length() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            args = JMXREMOTE;           // default to local management
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
11991
800d0ff7b043 7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents: 11530
diff changeset
   118
        Properties arg_props = parseString(args);
800d0ff7b043 7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents: 11530
diff changeset
   119
800d0ff7b043 7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents: 11530
diff changeset
   120
        // Read properties from the config file
15531
071efc9f31ad 8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents: 12694
diff changeset
   121
        Properties config_props = new Properties();
071efc9f31ad 8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents: 12694
diff changeset
   122
        String fname = arg_props.getProperty(CONFIG_FILE);
071efc9f31ad 8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents: 12694
diff changeset
   123
        readConfiguration(fname, config_props);
11991
800d0ff7b043 7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents: 11530
diff changeset
   124
15531
071efc9f31ad 8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents: 12694
diff changeset
   125
        // Arguments override config file
071efc9f31ad 8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents: 12694
diff changeset
   126
        config_props.putAll(arg_props);
071efc9f31ad 8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents: 12694
diff changeset
   127
        startAgent(config_props);
11991
800d0ff7b043 7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents: 11530
diff changeset
   128
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
11991
800d0ff7b043 7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents: 11530
diff changeset
   130
    // jcmd ManagementAgent.start_local entry point
800d0ff7b043 7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents: 11530
diff changeset
   131
    // Also called due to command-line via startAgent()
15531
071efc9f31ad 8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents: 12694
diff changeset
   132
    private static synchronized void startLocalManagementAgent() {
11991
800d0ff7b043 7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents: 11530
diff changeset
   133
        Properties agentProps = VMSupport.getAgentProperties();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
11991
800d0ff7b043 7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents: 11530
diff changeset
   135
        // start local connector if not started
800d0ff7b043 7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents: 11530
diff changeset
   136
        if (agentProps.get(LOCAL_CONNECTOR_ADDRESS_PROP) == null) {
800d0ff7b043 7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents: 11530
diff changeset
   137
            JMXConnectorServer cs = ConnectorBootstrap.startLocalConnectorServer();
800d0ff7b043 7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents: 11530
diff changeset
   138
            String address = cs.getAddress().toString();
800d0ff7b043 7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents: 11530
diff changeset
   139
            // Add the local connector address to the agent properties
800d0ff7b043 7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents: 11530
diff changeset
   140
            agentProps.put(LOCAL_CONNECTOR_ADDRESS_PROP, address);
800d0ff7b043 7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents: 11530
diff changeset
   141
800d0ff7b043 7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents: 11530
diff changeset
   142
            try {
800d0ff7b043 7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents: 11530
diff changeset
   143
                // export the address to the instrumentation buffer
800d0ff7b043 7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents: 11530
diff changeset
   144
                ConnectorAddressLink.export(address);
800d0ff7b043 7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents: 11530
diff changeset
   145
            } catch (Exception x) {
800d0ff7b043 7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents: 11530
diff changeset
   146
                // Connector server started but unable to export address
800d0ff7b043 7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents: 11530
diff changeset
   147
                // to instrumentation buffer - non-fatal error.
800d0ff7b043 7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents: 11530
diff changeset
   148
                warning(EXPORT_ADDRESS_FAILED, x.getMessage());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        }
11991
800d0ff7b043 7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents: 11530
diff changeset
   151
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
11991
800d0ff7b043 7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents: 11530
diff changeset
   153
    // jcmd ManagementAgent.start entry point
800d0ff7b043 7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents: 11530
diff changeset
   154
    // This method starts the remote JMX agent and starts neither
800d0ff7b043 7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents: 11530
diff changeset
   155
    // the local JMX agent nor the SNMP agent
800d0ff7b043 7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents: 11530
diff changeset
   156
    // @see #startLocalManagementAgent and also @see #startAgent.
800d0ff7b043 7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents: 11530
diff changeset
   157
    private static synchronized void startRemoteManagementAgent(String args) throws Exception {
800d0ff7b043 7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents: 11530
diff changeset
   158
        if (jmxServer != null) {
800d0ff7b043 7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents: 11530
diff changeset
   159
            throw new RuntimeException(getText(INVALID_STATE, "Agent already started"));
800d0ff7b043 7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents: 11530
diff changeset
   160
        }
800d0ff7b043 7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents: 11530
diff changeset
   161
24980
dbf4ae2cafa4 8046778: Better error messages when starting JMX agent via attach or jcmd
sla
parents: 24969
diff changeset
   162
        try {
dbf4ae2cafa4 8046778: Better error messages when starting JMX agent via attach or jcmd
sla
parents: 24969
diff changeset
   163
            Properties argProps = parseString(args);
dbf4ae2cafa4 8046778: Better error messages when starting JMX agent via attach or jcmd
sla
parents: 24969
diff changeset
   164
            Properties configProps = new Properties();
11991
800d0ff7b043 7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents: 11530
diff changeset
   165
24980
dbf4ae2cafa4 8046778: Better error messages when starting JMX agent via attach or jcmd
sla
parents: 24969
diff changeset
   166
            // Load the management properties from the config file
dbf4ae2cafa4 8046778: Better error messages when starting JMX agent via attach or jcmd
sla
parents: 24969
diff changeset
   167
            // if config file is not specified readConfiguration implicitly
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 26488
diff changeset
   168
            // reads <java.home>/conf/management/management.properties
24980
dbf4ae2cafa4 8046778: Better error messages when starting JMX agent via attach or jcmd
sla
parents: 24969
diff changeset
   169
dbf4ae2cafa4 8046778: Better error messages when starting JMX agent via attach or jcmd
sla
parents: 24969
diff changeset
   170
            String fname = System.getProperty(CONFIG_FILE);
dbf4ae2cafa4 8046778: Better error messages when starting JMX agent via attach or jcmd
sla
parents: 24969
diff changeset
   171
            readConfiguration(fname, configProps);
11991
800d0ff7b043 7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents: 11530
diff changeset
   172
24980
dbf4ae2cafa4 8046778: Better error messages when starting JMX agent via attach or jcmd
sla
parents: 24969
diff changeset
   173
            // management properties can be overridden by system properties
dbf4ae2cafa4 8046778: Better error messages when starting JMX agent via attach or jcmd
sla
parents: 24969
diff changeset
   174
            // which take precedence
dbf4ae2cafa4 8046778: Better error messages when starting JMX agent via attach or jcmd
sla
parents: 24969
diff changeset
   175
            Properties sysProps = System.getProperties();
dbf4ae2cafa4 8046778: Better error messages when starting JMX agent via attach or jcmd
sla
parents: 24969
diff changeset
   176
            synchronized (sysProps) {
dbf4ae2cafa4 8046778: Better error messages when starting JMX agent via attach or jcmd
sla
parents: 24969
diff changeset
   177
                configProps.putAll(sysProps);
dbf4ae2cafa4 8046778: Better error messages when starting JMX agent via attach or jcmd
sla
parents: 24969
diff changeset
   178
            }
11991
800d0ff7b043 7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents: 11530
diff changeset
   179
24980
dbf4ae2cafa4 8046778: Better error messages when starting JMX agent via attach or jcmd
sla
parents: 24969
diff changeset
   180
            // if user specifies config file into command line for either
dbf4ae2cafa4 8046778: Better error messages when starting JMX agent via attach or jcmd
sla
parents: 24969
diff changeset
   181
            // jcmd utilities or attach command it overrides properties set in
dbf4ae2cafa4 8046778: Better error messages when starting JMX agent via attach or jcmd
sla
parents: 24969
diff changeset
   182
            // command line at the time of VM start
dbf4ae2cafa4 8046778: Better error messages when starting JMX agent via attach or jcmd
sla
parents: 24969
diff changeset
   183
            String fnameUser = argProps.getProperty(CONFIG_FILE);
dbf4ae2cafa4 8046778: Better error messages when starting JMX agent via attach or jcmd
sla
parents: 24969
diff changeset
   184
            if (fnameUser != null) {
dbf4ae2cafa4 8046778: Better error messages when starting JMX agent via attach or jcmd
sla
parents: 24969
diff changeset
   185
                readConfiguration(fnameUser, configProps);
dbf4ae2cafa4 8046778: Better error messages when starting JMX agent via attach or jcmd
sla
parents: 24969
diff changeset
   186
            }
11991
800d0ff7b043 7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents: 11530
diff changeset
   187
24980
dbf4ae2cafa4 8046778: Better error messages when starting JMX agent via attach or jcmd
sla
parents: 24969
diff changeset
   188
            // arguments specified in command line of jcmd utilities
dbf4ae2cafa4 8046778: Better error messages when starting JMX agent via attach or jcmd
sla
parents: 24969
diff changeset
   189
            // override both system properties and one set by config file
dbf4ae2cafa4 8046778: Better error messages when starting JMX agent via attach or jcmd
sla
parents: 24969
diff changeset
   190
            // specified in jcmd command line
dbf4ae2cafa4 8046778: Better error messages when starting JMX agent via attach or jcmd
sla
parents: 24969
diff changeset
   191
            configProps.putAll(argProps);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
24980
dbf4ae2cafa4 8046778: Better error messages when starting JMX agent via attach or jcmd
sla
parents: 24969
diff changeset
   193
            // jcmd doesn't allow to change ThreadContentionMonitoring, but user
dbf4ae2cafa4 8046778: Better error messages when starting JMX agent via attach or jcmd
sla
parents: 24969
diff changeset
   194
            // can specify this property inside config file, so enable optional
dbf4ae2cafa4 8046778: Better error messages when starting JMX agent via attach or jcmd
sla
parents: 24969
diff changeset
   195
            // monitoring functionality if this property is set
dbf4ae2cafa4 8046778: Better error messages when starting JMX agent via attach or jcmd
sla
parents: 24969
diff changeset
   196
            final String enableThreadContentionMonitoring =
dbf4ae2cafa4 8046778: Better error messages when starting JMX agent via attach or jcmd
sla
parents: 24969
diff changeset
   197
                    configProps.getProperty(ENABLE_THREAD_CONTENTION_MONITORING);
11991
800d0ff7b043 7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents: 11530
diff changeset
   198
24980
dbf4ae2cafa4 8046778: Better error messages when starting JMX agent via attach or jcmd
sla
parents: 24969
diff changeset
   199
            if (enableThreadContentionMonitoring != null) {
dbf4ae2cafa4 8046778: Better error messages when starting JMX agent via attach or jcmd
sla
parents: 24969
diff changeset
   200
                ManagementFactory.getThreadMXBean().
dbf4ae2cafa4 8046778: Better error messages when starting JMX agent via attach or jcmd
sla
parents: 24969
diff changeset
   201
                        setThreadContentionMonitoringEnabled(true);
dbf4ae2cafa4 8046778: Better error messages when starting JMX agent via attach or jcmd
sla
parents: 24969
diff changeset
   202
            }
11991
800d0ff7b043 7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents: 11530
diff changeset
   203
24980
dbf4ae2cafa4 8046778: Better error messages when starting JMX agent via attach or jcmd
sla
parents: 24969
diff changeset
   204
            String jmxremotePort = configProps.getProperty(JMXREMOTE_PORT);
dbf4ae2cafa4 8046778: Better error messages when starting JMX agent via attach or jcmd
sla
parents: 24969
diff changeset
   205
            if (jmxremotePort != null) {
dbf4ae2cafa4 8046778: Better error messages when starting JMX agent via attach or jcmd
sla
parents: 24969
diff changeset
   206
                jmxServer = ConnectorBootstrap.
dbf4ae2cafa4 8046778: Better error messages when starting JMX agent via attach or jcmd
sla
parents: 24969
diff changeset
   207
                        startRemoteConnectorServer(jmxremotePort, configProps);
11991
800d0ff7b043 7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents: 11530
diff changeset
   208
24980
dbf4ae2cafa4 8046778: Better error messages when starting JMX agent via attach or jcmd
sla
parents: 24969
diff changeset
   209
                startDiscoveryService(configProps);
dbf4ae2cafa4 8046778: Better error messages when starting JMX agent via attach or jcmd
sla
parents: 24969
diff changeset
   210
            } else {
dbf4ae2cafa4 8046778: Better error messages when starting JMX agent via attach or jcmd
sla
parents: 24969
diff changeset
   211
                throw new AgentConfigurationError(INVALID_JMXREMOTE_PORT, "No port specified");
dbf4ae2cafa4 8046778: Better error messages when starting JMX agent via attach or jcmd
sla
parents: 24969
diff changeset
   212
            }
26488
5b5fccd9d016 8057746: Cannot handle JdpException in JMX agent initialization.
dsamersoff
parents: 25859
diff changeset
   213
        } catch (JdpException e) {
5b5fccd9d016 8057746: Cannot handle JdpException in JMX agent initialization.
dsamersoff
parents: 25859
diff changeset
   214
            error(e);
24980
dbf4ae2cafa4 8046778: Better error messages when starting JMX agent via attach or jcmd
sla
parents: 24969
diff changeset
   215
        } catch (AgentConfigurationError err) {
dbf4ae2cafa4 8046778: Better error messages when starting JMX agent via attach or jcmd
sla
parents: 24969
diff changeset
   216
            error(err.getError(), err.getParams());
11991
800d0ff7b043 7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents: 11530
diff changeset
   217
        }
800d0ff7b043 7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents: 11530
diff changeset
   218
    }
800d0ff7b043 7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents: 11530
diff changeset
   219
800d0ff7b043 7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents: 11530
diff changeset
   220
    private static synchronized void stopRemoteManagementAgent() throws Exception {
15531
071efc9f31ad 8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents: 12694
diff changeset
   221
071efc9f31ad 8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents: 12694
diff changeset
   222
        JdpController.stopDiscoveryService();
071efc9f31ad 8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents: 12694
diff changeset
   223
11991
800d0ff7b043 7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents: 11530
diff changeset
   224
        if (jmxServer != null) {
800d0ff7b043 7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents: 11530
diff changeset
   225
            ConnectorBootstrap.unexportRegistry();
800d0ff7b043 7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents: 11530
diff changeset
   226
800d0ff7b043 7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents: 11530
diff changeset
   227
            // Attempt to stop already stopped agent
800d0ff7b043 7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents: 11530
diff changeset
   228
            // Don't cause any errors.
800d0ff7b043 7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents: 11530
diff changeset
   229
            jmxServer.stop();
800d0ff7b043 7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents: 11530
diff changeset
   230
            jmxServer = null;
800d0ff7b043 7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents: 11530
diff changeset
   231
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    private static void startAgent(Properties props) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        String snmpPort = props.getProperty(SNMP_PORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        String jmxremote = props.getProperty(JMXREMOTE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        String jmxremotePort = props.getProperty(JMXREMOTE_PORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        // Enable optional monitoring functionality if requested
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        final String enableThreadContentionMonitoring =
15531
071efc9f31ad 8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents: 12694
diff changeset
   241
                props.getProperty(ENABLE_THREAD_CONTENTION_MONITORING);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        if (enableThreadContentionMonitoring != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            ManagementFactory.getThreadMXBean().
15531
071efc9f31ad 8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents: 12694
diff changeset
   244
                    setThreadContentionMonitoringEnabled(true);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            if (snmpPort != null) {
15531
071efc9f31ad 8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents: 12694
diff changeset
   249
                loadSnmpAgent(snmpPort, props);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
             * If the jmxremote.port property is set then we start the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
             * RMIConnectorServer for remote M&M.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
             *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
             * If the jmxremote or jmxremote.port properties are set then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
             * we start a RMIConnectorServer for local M&M. The address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
             * of this "local" server is exported as a counter to the jstat
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
             * instrumentation buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
             */
15531
071efc9f31ad 8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents: 12694
diff changeset
   261
            if (jmxremote != null || jmxremotePort != null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                if (jmxremotePort != null) {
15531
071efc9f31ad 8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents: 12694
diff changeset
   263
                    jmxServer = ConnectorBootstrap.
071efc9f31ad 8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents: 12694
diff changeset
   264
                            startRemoteConnectorServer(jmxremotePort, props);
071efc9f31ad 8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents: 12694
diff changeset
   265
                    startDiscoveryService(props);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                }
11991
800d0ff7b043 7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents: 11530
diff changeset
   267
                startLocalManagementAgent();
15531
071efc9f31ad 8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents: 12694
diff changeset
   268
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        } catch (AgentConfigurationError e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            error(e.getError(), e.getParams());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            error(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
15531
071efc9f31ad 8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents: 12694
diff changeset
   277
    private static void startDiscoveryService(Properties props)
26488
5b5fccd9d016 8057746: Cannot handle JdpException in JMX agent initialization.
dsamersoff
parents: 25859
diff changeset
   278
            throws IOException, JdpException {
15531
071efc9f31ad 8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents: 12694
diff changeset
   279
        // Start discovery service if requested
071efc9f31ad 8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents: 12694
diff changeset
   280
        String discoveryPort = props.getProperty("com.sun.management.jdp.port");
071efc9f31ad 8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents: 12694
diff changeset
   281
        String discoveryAddress = props.getProperty("com.sun.management.jdp.address");
071efc9f31ad 8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents: 12694
diff changeset
   282
        String discoveryShouldStart = props.getProperty("com.sun.management.jmxremote.autodiscovery");
071efc9f31ad 8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents: 12694
diff changeset
   283
071efc9f31ad 8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents: 12694
diff changeset
   284
        // Decide whether we should start autodicovery service.
071efc9f31ad 8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents: 12694
diff changeset
   285
        // To start autodiscovery following conditions should be met:
071efc9f31ad 8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents: 12694
diff changeset
   286
        // autodiscovery==true OR (autodicovery==null AND jdp.port != NULL)
071efc9f31ad 8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents: 12694
diff changeset
   287
071efc9f31ad 8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents: 12694
diff changeset
   288
        boolean shouldStart = false;
071efc9f31ad 8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents: 12694
diff changeset
   289
        if (discoveryShouldStart == null){
071efc9f31ad 8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents: 12694
diff changeset
   290
            shouldStart = (discoveryPort != null);
071efc9f31ad 8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents: 12694
diff changeset
   291
        }
071efc9f31ad 8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents: 12694
diff changeset
   292
        else{
071efc9f31ad 8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents: 12694
diff changeset
   293
            try{
071efc9f31ad 8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents: 12694
diff changeset
   294
               shouldStart = Boolean.parseBoolean(discoveryShouldStart);
071efc9f31ad 8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents: 12694
diff changeset
   295
            } catch (NumberFormatException e) {
26488
5b5fccd9d016 8057746: Cannot handle JdpException in JMX agent initialization.
dsamersoff
parents: 25859
diff changeset
   296
                throw new AgentConfigurationError(AGENT_EXCEPTION, "Couldn't parse autodiscovery argument");
15531
071efc9f31ad 8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents: 12694
diff changeset
   297
            }
071efc9f31ad 8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents: 12694
diff changeset
   298
        }
071efc9f31ad 8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents: 12694
diff changeset
   299
071efc9f31ad 8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents: 12694
diff changeset
   300
        if (shouldStart) {
071efc9f31ad 8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents: 12694
diff changeset
   301
            // port and address are required arguments and have no default values
071efc9f31ad 8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents: 12694
diff changeset
   302
            InetAddress address;
071efc9f31ad 8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents: 12694
diff changeset
   303
            try {
071efc9f31ad 8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents: 12694
diff changeset
   304
                address = (discoveryAddress == null) ?
071efc9f31ad 8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents: 12694
diff changeset
   305
                        InetAddress.getByName(JDP_DEFAULT_ADDRESS) : InetAddress.getByName(discoveryAddress);
071efc9f31ad 8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents: 12694
diff changeset
   306
            } catch (UnknownHostException e) {
26488
5b5fccd9d016 8057746: Cannot handle JdpException in JMX agent initialization.
dsamersoff
parents: 25859
diff changeset
   307
                throw new AgentConfigurationError(AGENT_EXCEPTION, e, "Unable to broadcast to requested address");
15531
071efc9f31ad 8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents: 12694
diff changeset
   308
            }
071efc9f31ad 8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents: 12694
diff changeset
   309
071efc9f31ad 8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents: 12694
diff changeset
   310
            int port = JDP_DEFAULT_PORT;
071efc9f31ad 8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents: 12694
diff changeset
   311
            if (discoveryPort != null) {
071efc9f31ad 8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents: 12694
diff changeset
   312
               try {
071efc9f31ad 8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents: 12694
diff changeset
   313
                  port = Integer.parseInt(discoveryPort);
071efc9f31ad 8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents: 12694
diff changeset
   314
               } catch (NumberFormatException e) {
26488
5b5fccd9d016 8057746: Cannot handle JdpException in JMX agent initialization.
dsamersoff
parents: 25859
diff changeset
   315
                 throw new AgentConfigurationError(AGENT_EXCEPTION, "Couldn't parse JDP port argument");
15531
071efc9f31ad 8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents: 12694
diff changeset
   316
               }
071efc9f31ad 8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents: 12694
diff changeset
   317
            }
071efc9f31ad 8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents: 12694
diff changeset
   318
071efc9f31ad 8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents: 12694
diff changeset
   319
            // Rebuilding service URL to broadcast it
071efc9f31ad 8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents: 12694
diff changeset
   320
            String jmxremotePort = props.getProperty(JMXREMOTE_PORT);
071efc9f31ad 8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents: 12694
diff changeset
   321
            String rmiPort = props.getProperty(RMI_PORT);
071efc9f31ad 8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents: 12694
diff changeset
   322
071efc9f31ad 8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents: 12694
diff changeset
   323
            JMXServiceURL url = jmxServer.getAddress();
071efc9f31ad 8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents: 12694
diff changeset
   324
            String hostname = url.getHost();
071efc9f31ad 8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents: 12694
diff changeset
   325
071efc9f31ad 8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents: 12694
diff changeset
   326
            String jmxUrlStr = (rmiPort != null)
071efc9f31ad 8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents: 12694
diff changeset
   327
                    ? String.format(
071efc9f31ad 8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents: 12694
diff changeset
   328
                    "service:jmx:rmi://%s:%s/jndi/rmi://%s:%s/jmxrmi",
071efc9f31ad 8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents: 12694
diff changeset
   329
                    hostname, rmiPort, hostname, jmxremotePort)
071efc9f31ad 8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents: 12694
diff changeset
   330
                    : String.format(
071efc9f31ad 8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents: 12694
diff changeset
   331
                    "service:jmx:rmi:///jndi/rmi://%s:%s/jmxrmi", hostname, jmxremotePort);
071efc9f31ad 8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents: 12694
diff changeset
   332
21298
d8a11a962ee3 8024071: In ManagementAgent.start it should be possible to set the jdp.name parameter.
dsamersoff
parents: 17721
diff changeset
   333
            String instanceName = props.getProperty("com.sun.management.jdp.name");
15531
071efc9f31ad 8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents: 12694
diff changeset
   334
26488
5b5fccd9d016 8057746: Cannot handle JdpException in JMX agent initialization.
dsamersoff
parents: 25859
diff changeset
   335
            JdpController.startDiscoveryService(address, port, instanceName, jmxUrlStr);
15531
071efc9f31ad 8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents: 12694
diff changeset
   336
        }
071efc9f31ad 8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents: 12694
diff changeset
   337
    }
071efc9f31ad 8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents: 12694
diff changeset
   338
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    public static Properties loadManagementProperties() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        Properties props = new Properties();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        // Load the management properties from the config file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        String fname = System.getProperty(CONFIG_FILE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        readConfiguration(fname, props);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        // management properties can be overridden by system properties
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        // which take precedence
12694
543c5e6c5ed0 7164191: properties.putAll API may fail with ConcurrentModifcationException on multi-thread scenario
dsamersoff
parents: 11991
diff changeset
   349
        Properties sysProps = System.getProperties();
15531
071efc9f31ad 8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents: 12694
diff changeset
   350
        synchronized (sysProps) {
12694
543c5e6c5ed0 7164191: properties.putAll API may fail with ConcurrentModifcationException on multi-thread scenario
dsamersoff
parents: 11991
diff changeset
   351
            props.putAll(sysProps);
543c5e6c5ed0 7164191: properties.putAll API may fail with ConcurrentModifcationException on multi-thread scenario
dsamersoff
parents: 11991
diff changeset
   352
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        return props;
15531
071efc9f31ad 8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents: 12694
diff changeset
   355
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
15531
071efc9f31ad 8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents: 12694
diff changeset
   357
    public static synchronized Properties getManagementProperties() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        if (mgmtProps == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            String configFile = System.getProperty(CONFIG_FILE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            String snmpPort = System.getProperty(SNMP_PORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            String jmxremote = System.getProperty(JMXREMOTE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            String jmxremotePort = System.getProperty(JMXREMOTE_PORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
15531
071efc9f31ad 8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents: 12694
diff changeset
   364
            if (configFile == null && snmpPort == null
071efc9f31ad 8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents: 12694
diff changeset
   365
                    && jmxremote == null && jmxremotePort == null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                // return if out-of-the-management option is not specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            mgmtProps = loadManagementProperties();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        return mgmtProps;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
2285
37fdbed8178f 6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents: 2
diff changeset
   374
    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
   375
        try {
37fdbed8178f 6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents: 2
diff changeset
   376
            // 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
   377
            //     AdaptorBootstrap.initialize(snmpPort, props);
37fdbed8178f 6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents: 2
diff changeset
   378
            final Class<?> adaptorClass =
15531
071efc9f31ad 8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents: 12694
diff changeset
   379
                    Class.forName(SNMP_ADAPTOR_BOOTSTRAP_CLASS_NAME, true, null);
2285
37fdbed8178f 6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents: 2
diff changeset
   380
            final Method initializeMethod =
37fdbed8178f 6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents: 2
diff changeset
   381
                    adaptorClass.getMethod("initialize",
15531
071efc9f31ad 8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents: 12694
diff changeset
   382
                    String.class, Properties.class);
071efc9f31ad 8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents: 12694
diff changeset
   383
            initializeMethod.invoke(null, snmpPort, props);
11530
a9d059c15b80 7117570: Warnings in sun.mangement.* and its subpackages
mchung
parents: 5506
diff changeset
   384
        } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException x) {
a9d059c15b80 7117570: Warnings in sun.mangement.* and its subpackages
mchung
parents: 5506
diff changeset
   385
            // snmp runtime doesn't exist - initialization fails
15531
071efc9f31ad 8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents: 12694
diff changeset
   386
            throw new UnsupportedOperationException("Unsupported management property: " + SNMP_PORT, x);
2285
37fdbed8178f 6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents: 2
diff changeset
   387
        } catch (InvocationTargetException x) {
37fdbed8178f 6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents: 2
diff changeset
   388
            final Throwable cause = x.getCause();
15531
071efc9f31ad 8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents: 12694
diff changeset
   389
            if (cause instanceof RuntimeException) {
2285
37fdbed8178f 6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents: 2
diff changeset
   390
                throw (RuntimeException) cause;
15531
071efc9f31ad 8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents: 12694
diff changeset
   391
            } else if (cause instanceof Error) {
2285
37fdbed8178f 6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents: 2
diff changeset
   392
                throw (Error) cause;
15531
071efc9f31ad 8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents: 12694
diff changeset
   393
            }
2285
37fdbed8178f 6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents: 2
diff changeset
   394
            // should not happen...
15531
071efc9f31ad 8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents: 12694
diff changeset
   395
            throw new UnsupportedOperationException("Unsupported management property: " + SNMP_PORT, cause);
2285
37fdbed8178f 6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents: 2
diff changeset
   396
        }
37fdbed8178f 6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents: 2
diff changeset
   397
    }
37fdbed8178f 6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents: 2
diff changeset
   398
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    // read config file and initialize the properties
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
    private static void readConfiguration(String fname, Properties p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        if (fname == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            String home = System.getProperty("java.home");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            if (home == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                throw new Error("Can't find java.home ??");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            }
24969
afa6934dd8e8 8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents: 23010
diff changeset
   406
            StringBuilder defaultFileName = new StringBuilder(home);
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 26488
diff changeset
   407
            defaultFileName.append(File.separator).append("conf");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            defaultFileName.append(File.separator).append("management");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            defaultFileName.append(File.separator).append("management.properties");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            // Set file name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
            fname = defaultFileName.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        final File configFile = new File(fname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        if (!configFile.exists()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            error(CONFIG_FILE_NOT_FOUND, fname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        InputStream in = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
            in = new FileInputStream(configFile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
            BufferedInputStream bin = new BufferedInputStream(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            p.load(bin);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        } catch (FileNotFoundException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            error(CONFIG_FILE_OPEN_FAILED, e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
            error(CONFIG_FILE_OPEN_FAILED, e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        } catch (SecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
            error(CONFIG_FILE_ACCESS_DENIED, fname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
            if (in != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                    in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                    error(CONFIG_FILE_CLOSE_FAILED, fname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                }
15531
071efc9f31ad 8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents: 12694
diff changeset
   436
            }
071efc9f31ad 8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents: 12694
diff changeset
   437
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
    public static void startAgent() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        String prop = System.getProperty("com.sun.management.agent.class");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        // -Dcom.sun.management.agent.class not set so read management
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        // properties and start agent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        if (prop == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
            // initialize management properties
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
            Properties props = getManagementProperties();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
            if (props != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                startAgent(props);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        // -Dcom.sun.management.agent.class=<agent classname>:<agent args>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        String[] values = prop.split(":");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        if (values.length < 1 || values.length > 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
            error(AGENT_CLASS_INVALID, "\"" + prop + "\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        String cname = values[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        String args = (values.length == 2 ? values[1] : null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        if (cname == null || cname.length() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
            error(AGENT_CLASS_INVALID, "\"" + prop + "\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        if (cname != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                // Instantiate the named class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                // invoke the premain(String args) method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                Class<?> clz = ClassLoader.getSystemClassLoader().loadClass(cname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                Method premain = clz.getMethod("premain",
15531
071efc9f31ad 8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents: 12694
diff changeset
   472
                        new Class<?>[]{String.class});
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                premain.invoke(null, /* static */
15531
071efc9f31ad 8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents: 12694
diff changeset
   474
                        new Object[]{args});
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            } catch (ClassNotFoundException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                error(AGENT_CLASS_NOT_FOUND, "\"" + cname + "\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
            } catch (NoSuchMethodException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
                error(AGENT_CLASS_PREMAIN_NOT_FOUND, "\"" + cname + "\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
            } catch (SecurityException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                error(AGENT_CLASS_ACCESS_DENIED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
            } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                String msg = (ex.getCause() == null
15531
071efc9f31ad 8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents: 12694
diff changeset
   483
                        ? ex.getMessage()
071efc9f31ad 8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents: 12694
diff changeset
   484
                        : ex.getCause().getMessage());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                error(AGENT_CLASS_FAILED, msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
    public static void error(String key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        String keyText = getText(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        System.err.print(getText("agent.err.error") + ": " + keyText);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        throw new RuntimeException(keyText);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
    public static void error(String key, String[] params) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        if (params == null || params.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
            error(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        } else {
24969
afa6934dd8e8 8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents: 23010
diff changeset
   500
            StringBuilder message = new StringBuilder(params[0]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
            for (int i = 1; i < params.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                message.append(" " + params[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
            error(key, message.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
    public static void error(String key, String message) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        String keyText = getText(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        System.err.print(getText("agent.err.error") + ": " + keyText);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        System.err.println(": " + message);
24980
dbf4ae2cafa4 8046778: Better error messages when starting JMX agent via attach or jcmd
sla
parents: 24969
diff changeset
   512
        throw new RuntimeException(keyText + ": " + message);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
    public static void error(Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
        e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
        System.err.println(getText(AGENT_EXCEPTION) + ": " + e.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        throw new RuntimeException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
    public static void warning(String key, String message) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        System.err.print(getText("agent.err.warning") + ": " + getText(key));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        System.err.println(": " + message);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
    private static void initResource() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
            messageRB =
15531
071efc9f31ad 8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents: 12694
diff changeset
   529
                    ResourceBundle.getBundle("sun.management.resources.agent");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        } catch (MissingResourceException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
            throw new Error("Fatal: Resource for management agent is missing");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
    public static String getText(String key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        if (messageRB == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
            initResource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
            return messageRB.getString(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
        } catch (MissingResourceException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
            return "Missing management agent resource bundle: key = \"" + key + "\"";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
    public static String getText(String key, String... args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        if (messageRB == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
            initResource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        String format = messageRB.getString(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        if (format == null) {
15531
071efc9f31ad 8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents: 12694
diff changeset
   552
            format = "missing resource key: key = \"" + key + "\", "
071efc9f31ad 8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents: 12694
diff changeset
   553
                    + "arguments = \"{0}\", \"{1}\", \"{2}\"";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
        return MessageFormat.format(format, (Object[]) args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
}