jdk/src/share/classes/sun/management/snmp/AdaptorBootstrap.java
author mchung
Tue, 17 Jan 2012 15:55:40 -0800
changeset 11530 a9d059c15b80
parent 5506 202f599c92aa
child 14342 8435a30053c1
permissions -rw-r--r--
7117570: Warnings in sun.mangement.* and its subpackages Reviewed-by: mchung, dsamersoff Contributed-by: kurchi.subhra.hazra@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2003, 2006, 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: 2
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: 2
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: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.management.snmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import com.sun.jmx.snmp.daemon.SnmpAdaptorServer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import com.sun.jmx.snmp.InetAddressAcl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import com.sun.jmx.snmp.IPAcl.SnmpAcl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import sun.management.snmp.jvmmib.JVM_MANAGEMENT_MIB;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import sun.management.snmp.jvminstr.JVM_MANAGEMENT_MIB_IMPL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import sun.management.snmp.jvminstr.NotificationTarget;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import sun.management.snmp.jvminstr.NotificationTargetImpl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import sun.management.snmp.util.MibLogger;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import sun.management.snmp.util.JvmContextFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import sun.management.Agent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import sun.management.AgentConfigurationError;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import static sun.management.AgentConfigurationError.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import sun.management.FileSystem;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.util.Enumeration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import java.util.Properties;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import java.io.File;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import java.io.FileInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
import java.net.InetAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
import java.net.UnknownHostException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * This class initializes and starts the SNMP Adaptor for JSR 163 SNMP
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * Monitoring.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
public final class AdaptorBootstrap {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    private static final MibLogger log = new MibLogger(AdaptorBootstrap.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * Default values for SNMP configuration properties.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    public static interface DefaultValues {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        public static final String PORT="161";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        public static final String CONFIG_FILE_NAME="management.properties";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        public static final String TRAP_PORT="162";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        public static final String USE_ACL="true";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        public static final String ACL_FILE_NAME="snmp.acl";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        public static final String BIND_ADDRESS="localhost";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * Names of SNMP configuration properties.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    public static interface PropertyNames {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        public static final String PORT="com.sun.management.snmp.port";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        public static final String CONFIG_FILE_NAME=
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            "com.sun.management.config.file";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        public static final String TRAP_PORT=
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            "com.sun.management.snmp.trap";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        public static final String USE_ACL=
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            "com.sun.management.snmp.acl";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        public static final String ACL_FILE_NAME=
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            "com.sun.management.snmp.acl.file";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        public static final String BIND_ADDRESS=
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            "com.sun.management.snmp.interface";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * We keep a reference - so that we can possibly call
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * terminate(). As of now, terminate() is only called by unit tests
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * (makes it possible to run several testcases sequentially in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * same JVM).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    private SnmpAdaptorServer       adaptor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    private JVM_MANAGEMENT_MIB_IMPL jvmmib;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    private AdaptorBootstrap(SnmpAdaptorServer snmpas,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                             JVM_MANAGEMENT_MIB_IMPL mib) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        jvmmib  = mib;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        adaptor = snmpas;
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
     * Compute the full path name for a default file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * @param basename basename (with extension) of the default file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * @return ${JRE}/lib/management/${basename}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    private static String getDefaultFileName(String basename) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        final String fileSeparator = File.separator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        return System.getProperty("java.home") + fileSeparator + "lib" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            fileSeparator + "management" + fileSeparator + basename;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * Retrieve the Trap Target List from the ACL file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     **/
11530
a9d059c15b80 7117570: Warnings in sun.mangement.* and its subpackages
mchung
parents: 5506
diff changeset
   121
    @SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    private static List<NotificationTarget> getTargetList(InetAddressAcl acl,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                                                          int defaultTrapPort) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        final ArrayList<NotificationTarget> result =
11530
a9d059c15b80 7117570: Warnings in sun.mangement.* and its subpackages
mchung
parents: 5506
diff changeset
   125
                new ArrayList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        if (acl != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            if (log.isDebugOn())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                log.debug("getTargetList",Agent.getText("jmxremote.AdaptorBootstrap.getTargetList.processing"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
11530
a9d059c15b80 7117570: Warnings in sun.mangement.* and its subpackages
mchung
parents: 5506
diff changeset
   130
            final Enumeration td = acl.getTrapDestinations();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            for (; td.hasMoreElements() ;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                final InetAddress targetAddr = (InetAddress)td.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                final Enumeration tc =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                    acl.getTrapCommunities(targetAddr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                for (;tc.hasMoreElements() ;) {
11530
a9d059c15b80 7117570: Warnings in sun.mangement.* and its subpackages
mchung
parents: 5506
diff changeset
   136
                    final String community = (String)tc.nextElement();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                    final NotificationTarget target =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                        new NotificationTargetImpl(targetAddr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                                                   defaultTrapPort,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                                                   community);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                    if (log.isDebugOn())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                        log.debug("getTargetList",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                                  Agent.getText("jmxremote.AdaptorBootstrap.getTargetList.adding",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                                                target.toString()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                    result.add(target);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * Initializes and starts the SNMP Adaptor Server.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * If the com.sun.management.snmp.port property is not defined,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * simply return. Otherwise, attempts to load the config file, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * then calls {@link #initialize(java.lang.String, java.util.Properties)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    public static synchronized AdaptorBootstrap initialize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        // Load a new properties
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        final Properties props = Agent.loadManagementProperties();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        if (props == null) return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        final String portStr = props.getProperty(PropertyNames.PORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        return initialize(portStr,props);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * Initializes and starts the SNMP Adaptor Server.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    public static synchronized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        AdaptorBootstrap initialize(String portStr, Properties props) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        // Get port number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        if (portStr.length()==0) portStr=DefaultValues.PORT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        final int port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            port = Integer.parseInt(portStr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        } catch (NumberFormatException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            throw new AgentConfigurationError(INVALID_SNMP_PORT, x, portStr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        if (port < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            throw new AgentConfigurationError(INVALID_SNMP_PORT, portStr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        // Get trap port number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        final String trapPortStr =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            props.getProperty(PropertyNames.TRAP_PORT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                              DefaultValues.TRAP_PORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        final int trapPort;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            trapPort = Integer.parseInt(trapPortStr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        } catch (NumberFormatException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            throw new AgentConfigurationError(INVALID_SNMP_TRAP_PORT, x, trapPortStr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        if (trapPort < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            throw new AgentConfigurationError(INVALID_SNMP_TRAP_PORT, trapPortStr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        // Get bind address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        final String addrStr =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            props.getProperty(PropertyNames.BIND_ADDRESS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                              DefaultValues.BIND_ADDRESS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        // Get ACL File
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        final String defaultAclFileName   =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            getDefaultFileName(DefaultValues.ACL_FILE_NAME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        final String aclFileName =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            props.getProperty(PropertyNames.ACL_FILE_NAME,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                               defaultAclFileName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        final String  useAclStr =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            props.getProperty(PropertyNames.USE_ACL,DefaultValues.USE_ACL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        final boolean useAcl =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            Boolean.valueOf(useAclStr).booleanValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        if (useAcl) checkAclFile(aclFileName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        AdaptorBootstrap adaptor = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            adaptor = getAdaptorBootstrap(port, trapPort, addrStr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                                          useAcl, aclFileName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            throw new AgentConfigurationError(AGENT_EXCEPTION, e, e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        return adaptor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    private static AdaptorBootstrap getAdaptorBootstrap
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        (int port, int trapPort, String bindAddress, boolean useAcl,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
         String aclFileName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        final InetAddress address;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            address = InetAddress.getByName(bindAddress);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        } catch (UnknownHostException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            throw new AgentConfigurationError(UNKNOWN_SNMP_INTERFACE, e, bindAddress);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        if (log.isDebugOn()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            log.debug("initialize",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                      Agent.getText("jmxremote.AdaptorBootstrap.getTargetList.starting" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                      "\n\t" + PropertyNames.PORT + "=" + port +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                      "\n\t" + PropertyNames.TRAP_PORT + "=" + trapPort +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                      "\n\t" + PropertyNames.BIND_ADDRESS + "=" + address +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                      (useAcl?("\n\t" + PropertyNames.ACL_FILE_NAME + "="
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                               + aclFileName):"\n\tNo ACL")+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                      ""));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        final InetAddressAcl acl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            acl = useAcl ? new SnmpAcl(System.getProperty("user.name"),aclFileName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                         : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        } catch (UnknownHostException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            throw new AgentConfigurationError(UNKNOWN_SNMP_INTERFACE, e, e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        // Create adaptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        final SnmpAdaptorServer adaptor =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            new SnmpAdaptorServer(acl, port, address);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        adaptor.setUserDataFactory(new JvmContextFactory());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        adaptor.setTrapPort(trapPort);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        // Create MIB
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        final JVM_MANAGEMENT_MIB_IMPL mib = new JVM_MANAGEMENT_MIB_IMPL();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            mib.init();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        } catch (IllegalAccessException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            throw new AgentConfigurationError(SNMP_MIB_INIT_FAILED, x, x.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        // Configure the trap destinations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        mib.addTargets(getTargetList(acl,trapPort));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        // Start Adaptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            // Will wait until the adaptor starts or fails to start.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            // If the adaptor fails to start, a CommunicationException or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            // an InterruptedException is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            adaptor.start(Long.MAX_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        } catch (Exception x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            Throwable t=x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            if (x instanceof com.sun.jmx.snmp.daemon.CommunicationException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                final Throwable next = t.getCause();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                if (next != null) t = next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            throw new AgentConfigurationError(SNMP_ADAPTOR_START_FAILED, t,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                                              address + ":" + port,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                                              "(" + t.getMessage() + ")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        // double check that adaptor is actually started (should always
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        // be active, so that exception should never be thrown from here)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        if (!adaptor.isActive()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            throw new AgentConfigurationError(SNMP_ADAPTOR_START_FAILED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                                              address + ":" + port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            // Add MIB to adaptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            adaptor.addMib(mib);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            // Add Adaptor to the MIB
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            mib.setSnmpAdaptor(adaptor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        } catch (RuntimeException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            new AdaptorBootstrap(adaptor,mib).terminate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            throw x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        log.debug("initialize",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                  Agent.getText("jmxremote.AdaptorBootstrap.getTargetList.initialize1"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        log.config("initialize",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                   Agent.getText("jmxremote.AdaptorBootstrap.getTargetList.initialize2",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                                 address.toString(), java.lang.Integer.toString(adaptor.getPort())));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        return new AdaptorBootstrap(adaptor,mib);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    private static void checkAclFile(String aclFileName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        if (aclFileName == null || aclFileName.length()==0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            throw new AgentConfigurationError(SNMP_ACL_FILE_NOT_SET);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        final File file = new File(aclFileName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        if (!file.exists()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            throw new AgentConfigurationError(SNMP_ACL_FILE_NOT_FOUND, aclFileName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        if (!file.canRead()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            throw new AgentConfigurationError(SNMP_ACL_FILE_NOT_READABLE, aclFileName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        FileSystem fs = FileSystem.open();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            if (fs.supportsFileSecurity(file)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                if (!fs.isAccessUserOnly(file)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                    throw new AgentConfigurationError(SNMP_ACL_FILE_ACCESS_NOT_RESTRICTED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                        aclFileName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            throw new AgentConfigurationError(SNMP_ACL_FILE_READ_FAILED, aclFileName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * Get the port on which the adaptor is bound.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * Returns 0 if the adaptor is already terminated.
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 synchronized int getPort() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        if (adaptor != null) return adaptor.getPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * Stops the adaptor server.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    public synchronized void terminate() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        if (adaptor == null) return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        // Terminate the MIB (deregister NotificationListener from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        // MemoryMBean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            jvmmib.terminate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        } catch (Exception x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            // Must not prevent to stop...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            log.debug("jmxremote.AdaptorBootstrap.getTargetList.terminate",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                      x.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            jvmmib=null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        // Stop the adaptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            adaptor.stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            adaptor = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
}