jdk/test/javax/management/remote/mandatory/passwordAccessFile/PasswordAccessFileTest.java
author dfuchs
Thu, 02 Feb 2017 16:50:46 +0000
changeset 43503 bc7f8619ab70
parent 30376 2ccf2cf7ea48
child 44423 306c020eb154
permissions -rw-r--r--
8173607: JMX RMI connector should be in its own module Summary: The JMX RMI connector is moved to a new java.management.rmi module. Reviewed-by: mchung, erikj
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
30376
2ccf2cf7ea48 8078896: Add @modules as needed to the jdk_svc tests
ykantser
parents: 5506
diff changeset
     2
 * Copyright (c) 2003, 2015, 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 4924664
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Tests the use of the "jmx.remote.x.password.file" and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *          "jmx.remote.x.access.file" environment map properties.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * @author Luis-Miguel Alventosa
43503
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 30376
diff changeset
    30
 * @modules java.management.rmi
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * @run clean PasswordAccessFileTest SimpleStandard SimpleStandardMBean
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * @run build PasswordAccessFileTest SimpleStandard SimpleStandardMBean
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * @run main PasswordAccessFileTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.io.File;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.util.HashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import javax.management.Attribute;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import javax.management.MBeanServer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import javax.management.MBeanServerConnection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import javax.management.MBeanServerFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import javax.management.MBeanServerInvocationHandler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import javax.management.Notification;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import javax.management.NotificationListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import javax.management.ObjectName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import javax.management.remote.JMXConnector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import javax.management.remote.JMXConnectorFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import javax.management.remote.JMXConnectorServer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import javax.management.remote.JMXConnectorServerFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import javax.management.remote.JMXServiceURL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
public class PasswordAccessFileTest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    public static void main(String[] args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
            //------------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            // SERVER
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            //------------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            // Instantiate the MBean server
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            System.out.println("Create the MBean server");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            MBeanServer mbs = MBeanServerFactory.createMBeanServer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            // Create SimpleStandard MBean
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            ObjectName mbeanName = new ObjectName("MBeans:type=SimpleStandard");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            System.out.println("Create SimpleStandard MBean...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            mbs.createMBean("SimpleStandard", mbeanName, null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            // Server's environment map
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            System.out.println(">>> Initialize the server's environment map");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            HashMap sEnv = new HashMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            // Provide the password file used by the connector server to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            // perform user authentication. The password file is a properties
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            // based text file specifying username/password pairs. This
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            // properties based password authenticator has been implemented
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            // using the JMXAuthenticator interface and is passed to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            // connector through the "jmx.remote.authenticator" property
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            // in the map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            // This property is implementation-dependent and might not be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            // supported by all implementations of the JMX Remote API.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            sEnv.put("jmx.remote.x.password.file",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                     System.getProperty("test.src") +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                     File.separator +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                     "password.properties");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            // Provide the access level file used by the connector server to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            // perform user authorization. The access level file is a properties
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            // based text file specifying username/access level pairs where
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            // access level is either "readonly" or "readwrite" access to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            // MBeanServer operations. This properties based access control
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            // checker has been implemented using the MBeanServerForwarder
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            // interface which wraps the real MBean server inside an access
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            // controller MBean server which performs the access control checks
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            // before forwarding the requests to the real MBean server.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            // This property is implementation-dependent and might not be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            // supported by all implementations of the JMX Remote API.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            sEnv.put("jmx.remote.x.access.file",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                     System.getProperty("test.src") +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                     File.separator +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                     "access.properties");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            // Create an RMI connector server
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            System.out.println("Create an RMI connector server");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            JMXServiceURL url = new JMXServiceURL("service:jmx:rmi://");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            JMXConnectorServer cs =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                JMXConnectorServerFactory.newJMXConnectorServer(url, sEnv, mbs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            // Start the RMI connector server
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            System.out.println("Start the RMI connector server");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            cs.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            System.out.println("RMI connector server successfully started");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            System.out.println("Waiting for incoming connections...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            //------------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            // CLIENT : Invalid authentication credentials
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            //------------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            final String invalidCreds[][] = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                {"admin1", "adminPassword"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                {"admin",  "adminPassword1"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                {"user1",  "userPassword"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                {"user",   "userPassword1"}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            // Try to connect to the server using the invalid credentials.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            // All the connect calls should get SecurityException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            for (int i = 0 ; i < invalidCreds.length ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                // Client environment map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                System.out.println(">>> Initialize the client environment map" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                                   " for user [" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                                   invalidCreds[i][0] +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                                   "] with password [" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                                   invalidCreds[i][1] + "]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                HashMap cEnv = new HashMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                cEnv.put("jmx.remote.credentials", invalidCreds[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                // Create an RMI connector client and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                // connect it to the RMI connector server
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                System.out.println("Create an RMI connector client and " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                                   "connect it to the RMI connector server");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                    JMXConnector jmxc =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                        JMXConnectorFactory.connect(cs.getAddress(), cEnv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                } catch (SecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                    System.out.println("Got expected security exception: " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                    System.out.println("Got unexpected exception: " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                    e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                    System.exit(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            //------------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            // CLIENT (admin)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            //------------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            // Admin client environment map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            String[] adminCreds = new String[] { "admin" , "adminPassword" };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            System.out.println(">>> Initialize the client environment map for" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                               " user [" + adminCreds[0] + "] with " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                               "password [" + adminCreds[1] + "]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            HashMap adminEnv = new HashMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            adminEnv.put("jmx.remote.credentials", adminCreds);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            // Create an RMI connector client and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            // connect it to the RMI connector server
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            System.out.println("Create an RMI connector client and " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                               "connect it to the RMI connector server");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            JMXConnector adminConnector =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                JMXConnectorFactory.connect(cs.getAddress(), adminEnv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            // Get an MBeanServerConnection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            System.out.println("Get an MBeanServerConnection");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            MBeanServerConnection adminConnection =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                adminConnector.getMBeanServerConnection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            // Get the proxy for the Simple MBean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            SimpleStandardMBean adminProxy = (SimpleStandardMBean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                MBeanServerInvocationHandler.newProxyInstance(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                                             adminConnection,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                                             mbeanName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                                             SimpleStandardMBean.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                                             false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            // Get State attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            System.out.println("State = " + adminProxy.getState());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            // Set State attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            adminProxy.setState("changed state");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            // Get State attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            System.out.println("State = " + adminProxy.getState());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            // Invoke "reset" in SimpleStandard MBean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            System.out.println("Invoke reset() in SimpleStandard MBean...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            adminProxy.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            // Close MBeanServer connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            System.out.println("Close the admin connection to the server");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            adminConnector.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            //------------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            // CLIENT (user)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            //------------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            // User client environment map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            String[] userCreds = new String[] { "user" , "userPassword" };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            System.out.println(">>> Initialize the client environment map for" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                               " user [" + userCreds[0] + "] with " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                               "password [" + userCreds[1] + "]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            HashMap userEnv = new HashMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            userEnv.put("jmx.remote.credentials", userCreds);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            // Create an RMI connector client and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            // connect it to the RMI connector server
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            System.out.println("Create an RMI connector client and " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                               "connect it to the RMI connector server");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            JMXConnector userConnector =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                JMXConnectorFactory.connect(cs.getAddress(), userEnv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            // Get an MBeanServerConnection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            System.out.println("Get an MBeanServerConnection");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            MBeanServerConnection userConnection =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                userConnector.getMBeanServerConnection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            // Get the proxy for the Simple MBean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            SimpleStandardMBean userProxy = (SimpleStandardMBean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                MBeanServerInvocationHandler.newProxyInstance(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                                             userConnection,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                                             mbeanName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                                             SimpleStandardMBean.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                                             false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            // Get State attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            System.out.println("State = " + userProxy.getState());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            // Set State attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                userProxy.setState("changed state");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            } catch (SecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                System.out.println("Got expected security exception: " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                System.out.println("Got unexpected exception: " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                System.exit(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            // Get State attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            System.out.println("State = " + userProxy.getState());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            // Invoke "reset" in SimpleStandard MBean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                System.out.println("Invoke reset() in SimpleStandard MBean...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                userProxy.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            } catch (SecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                System.out.println("Got expected security exception: " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                System.out.println("Got unexpected exception: " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                System.exit(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            // Close MBeanServer connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            System.out.println("Close the user connection to the server");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            userConnector.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            //------------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            // SERVER
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            //------------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            // Stop the connector server
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            System.out.println(">>> Stop the connector server");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            cs.stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            System.out.println("Bye! Bye!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            System.out.println("Got unexpected exception: " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            System.exit(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
}