jdk/test/javax/management/remote/mandatory/passwordAccessFile/NonJMXPrincipalsTest.java
author dbuck
Tue, 18 Aug 2015 04:29:28 -0700
changeset 32417 6859107fc6c3
parent 30376 2ccf2cf7ea48
child 43503 bc7f8619ab70
permissions -rw-r--r--
8133666: OperatingSystemMXBean reports abnormally high machine CPU consumption on Linux Reviewed-by: sla, mgronlun
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) 2004, 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 5025141
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Tests that MBeanServerFileAccessController supports
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *          principals other than JMXPrincipal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * @author Luis-Miguel Alventosa
30376
2ccf2cf7ea48 8078896: Add @modules as needed to the jdk_svc tests
ykantser
parents: 5506
diff changeset
    30
 * @modules java.management
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * @run clean NonJMXPrincipalsTest SimpleStandard SimpleStandardMBean
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * @run build NonJMXPrincipalsTest SimpleStandard SimpleStandardMBean
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * @run main NonJMXPrincipalsTest
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.io.Serializable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.security.Principal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.util.HashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import javax.management.Attribute;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import javax.management.MBeanServer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import javax.management.MBeanServerConnection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import javax.management.MBeanServerFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import javax.management.MBeanServerInvocationHandler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import javax.management.Notification;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import javax.management.NotificationListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import javax.management.ObjectName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import javax.management.remote.JMXAuthenticator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import javax.management.remote.JMXConnector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import javax.management.remote.JMXConnectorFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
import javax.management.remote.JMXConnectorServer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
import javax.management.remote.JMXConnectorServerFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
import javax.management.remote.JMXPrincipal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
import javax.management.remote.JMXServiceURL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
import javax.security.auth.Subject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
public class NonJMXPrincipalsTest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private static class OtherPrincipal implements Principal, Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        private String name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        public OtherPrincipal(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            if (name == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                throw new NullPointerException("illegal null input");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            this.name = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        public String getName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            return("OtherPrincipal:  " + name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        public boolean equals(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            if (o == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            if (this == o)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            if (!(o instanceof OtherPrincipal))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            OtherPrincipal that = (OtherPrincipal)o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            return (this.getName().equals(that.getName()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            return name.hashCode();
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
    private static class OtherPrincipalAuthenticator
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        implements JMXAuthenticator {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        public Subject authenticate(Object credentials) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            final String[] aCredentials = (String[]) credentials;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            final String username = (String) aCredentials[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            final Subject subject = new Subject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            subject.getPrincipals().add(new JMXPrincipal("dummy"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            subject.getPrincipals().add(new OtherPrincipal(username));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            return subject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    private static class NoPrincipalAuthenticator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        implements JMXAuthenticator {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        public Subject authenticate(Object credentials) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            return new Subject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    public static void runTest(JMXAuthenticator authenticator)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        //------------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        // SERVER
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        //------------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        // Instantiate the MBean server
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        System.out.println("Create the MBean server");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        MBeanServer mbs = MBeanServerFactory.createMBeanServer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        // Create SimpleStandard MBean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        ObjectName mbeanName = new ObjectName("MBeans:type=SimpleStandard");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        System.out.println("Create SimpleStandard MBean...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        mbs.createMBean("SimpleStandard", mbeanName, null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        // Server's environment map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        System.out.println(">>> Initialize the server's environment map");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        HashMap sEnv = new HashMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        // Provide a JMX Authenticator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        sEnv.put("jmx.remote.authenticator", authenticator);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        // Provide the access level file used by the connector server to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        // perform user authorization. The access level file is a properties
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        // based text file specifying username/access level pairs where
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        // access level is either "readonly" or "readwrite" access to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        // MBeanServer operations. This properties based access control
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        // checker has been implemented using the MBeanServerForwarder
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        // interface which wraps the real MBean server inside an access
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        // controller MBean server which performs the access control checks
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        // before forwarding the requests to the real MBean server.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        // This property is implementation-dependent and might not be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        // supported by all implementations of the JMX Remote API.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        sEnv.put("jmx.remote.x.access.file",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                 System.getProperty("test.src") +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                 File.separator +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                 "access.properties");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        // Create an RMI connector server
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        System.out.println("Create an RMI connector server");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        JMXServiceURL url = new JMXServiceURL("service:jmx:rmi://");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        JMXConnectorServer cs =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            JMXConnectorServerFactory.newJMXConnectorServer(url, sEnv, mbs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        // Start the RMI connector server
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        System.out.println("Start the RMI connector server");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        cs.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        System.out.println("RMI connector server successfully started");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        System.out.println("Waiting for incoming connections...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        //------------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        // CLIENT (admin)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        //------------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        // Admin client environment map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        String[] adminCreds = new String[] { "admin" , "adminPassword" };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        System.out.println(">>> Initialize the client environment map for" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                           " user [" + adminCreds[0] + "] with " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                           "password [" + adminCreds[1] + "]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        HashMap adminEnv = new HashMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        adminEnv.put("jmx.remote.credentials", adminCreds);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        // Create an RMI connector client and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        // connect it to the RMI connector server
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        System.out.println("Create an RMI connector client and " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                           "connect it to the RMI connector server");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        JMXConnector adminConnector =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            JMXConnectorFactory.connect(cs.getAddress(), adminEnv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        // Get an MBeanServerConnection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        System.out.println("Get an MBeanServerConnection");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        MBeanServerConnection adminConnection =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            adminConnector.getMBeanServerConnection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        // Get the proxy for the Simple MBean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        SimpleStandardMBean adminProxy = (SimpleStandardMBean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            MBeanServerInvocationHandler.newProxyInstance(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                                                 adminConnection,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                                                 mbeanName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                                                 SimpleStandardMBean.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                                                 false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        // Get State attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        System.out.println("State = " + adminProxy.getState());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        // Set State attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        adminProxy.setState("changed state");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        // Get State attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        System.out.println("State = " + adminProxy.getState());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        // Invoke "reset" in SimpleStandard MBean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        System.out.println("Invoke reset() in SimpleStandard MBean...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        adminProxy.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        // Close MBeanServer connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        System.out.println("Close the admin connection to the server");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        adminConnector.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        //------------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        // CLIENT (user)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        //------------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        // User client environment map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        String[] userCreds = new String[] { "user" , "userPassword" };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        System.out.println(">>> Initialize the client environment map for" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                           " user [" + userCreds[0] + "] with " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                           "password [" + userCreds[1] + "]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        HashMap userEnv = new HashMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        userEnv.put("jmx.remote.credentials", userCreds);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        // Create an RMI connector client and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        // connect it to the RMI connector server
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        System.out.println("Create an RMI connector client and " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                           "connect it to the RMI connector server");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        JMXConnector userConnector =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            JMXConnectorFactory.connect(cs.getAddress(), userEnv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        // Get an MBeanServerConnection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        System.out.println("Get an MBeanServerConnection");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        MBeanServerConnection userConnection =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            userConnector.getMBeanServerConnection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        // Get the proxy for the Simple MBean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        SimpleStandardMBean userProxy = (SimpleStandardMBean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            MBeanServerInvocationHandler.newProxyInstance(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                                                 userConnection,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                                                 mbeanName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                                                 SimpleStandardMBean.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                                                 false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        // Get State attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        System.out.println("State = " + userProxy.getState());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        // Set State attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            userProxy.setState("changed state");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        } catch (SecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            System.out.println("Got expected security exception: " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            System.out.println("Got unexpected exception: " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            e.printStackTrace(System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        // Get State attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        System.out.println("State = " + userProxy.getState());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        // Invoke "reset" in SimpleStandard MBean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            System.out.println("Invoke reset() in SimpleStandard MBean...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            userProxy.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        } catch (SecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            System.out.println("Got expected security exception: " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            System.out.println("Got unexpected exception: " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            e.printStackTrace(System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        // Close MBeanServer connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        System.out.println("Close the user connection to the server");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        userConnector.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        //------------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        // SERVER
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        //------------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        // Stop the connector server
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        System.out.println(">>> Stop the connector server");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        cs.stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    public static void main(String[] args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        int errorCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        // Runt tests
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        System.out.println("\n>>> Run NoPrincipalAuthenticator test...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            NonJMXPrincipalsTest.runTest(new NoPrincipalAuthenticator());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            System.out.println("Did not get expected SecurityException");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            errorCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            if (e instanceof SecurityException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                System.out.println("Got expected exception: " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                System.out.println("Got unexpected exception: " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                errorCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            e.printStackTrace(System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        System.out.println("\n>>> Run OtherPrincipalAuthenticator test...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            NonJMXPrincipalsTest.runTest(new OtherPrincipalAuthenticator());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            errorCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            System.out.println("Got unexpected exception: " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            e.printStackTrace(System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        if (errorCount > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            System.out.println("\nTEST FAILED! Error count = " + errorCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            System.exit(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        System.out.println("\nTEST PASSED!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        System.out.println("\nBye! Bye!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
}