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