jdk/test/javax/management/monitor/ThreadPoolAccTest.java
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 2 90ce3da70b43
child 21650 390b5df3fcaf
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy, weijun
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) 2005, 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 6222826
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Test that each thread in the thread pool runs
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *          in the context of the monitor.start() caller.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * @author Luis-Miguel Alventosa
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * @run clean ThreadPoolAccTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * @run build ThreadPoolAccTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * @run main ThreadPoolAccTest
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.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.security.Principal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.security.PrivilegedAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.util.Set;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import javax.management.MBeanServer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import javax.management.MBeanServerFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import javax.management.ObjectName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import javax.management.monitor.CounterMonitor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import javax.management.monitor.GaugeMonitor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import javax.management.monitor.Monitor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import javax.management.monitor.StringMonitor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import javax.management.remote.JMXPrincipal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import javax.security.auth.Subject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
public class ThreadPoolAccTest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    // MBean class
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    public class ObservedObject implements ObservedObjectMBean {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        public String principal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        public Integer getInteger() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
            setPrincipal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        public Double getDouble() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            setPrincipal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            return 0.0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        public String getString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            setPrincipal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            return "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        private void setPrincipal() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            Subject subject = Subject.getSubject(AccessController.getContext());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            Set principals = subject.getPrincipals(JMXPrincipal.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            principal = ((Principal) principals.iterator().next()).getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    // MBean interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    public interface ObservedObjectMBean {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        public Integer getInteger();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        public Double getDouble();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        public String getString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * Run test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    public int runTest() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        ObjectName[] mbeanNames = new ObjectName[6];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        ObservedObject[] monitored = new ObservedObject[6];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        ObjectName[] monitorNames = new ObjectName[6];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        Monitor[] monitor = new Monitor[6];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        String[] principals = { "role1", "role2" };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        String[] attributes = { "Integer", "Double", "String" };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            echo(">>> CREATE MBeanServer");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            MBeanServer server = MBeanServerFactory.newMBeanServer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            String domain = server.getDefaultDomain();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            for (int i = 0; i < 6; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                mbeanNames[i] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                    new ObjectName(":type=ObservedObject,instance=" + i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                monitored[i] = new ObservedObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                echo(">>> CREATE ObservedObject = " + mbeanNames[i].toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                server.registerMBean(monitored[i], mbeanNames[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                switch (i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                    case 0:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                    case 3:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                        monitorNames[i] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                            new ObjectName(":type=CounterMonitor,instance=" + i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                        monitor[i] = new CounterMonitor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                    case 1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                    case 4:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                        monitorNames[i] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                            new ObjectName(":type=GaugeMonitor,instance=" + i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                        monitor[i] = new GaugeMonitor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                    case 2:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                    case 5:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                        monitorNames[i] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                            new ObjectName(":type=StringMonitor,instance=" + i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                        monitor[i] = new StringMonitor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                echo(">>> CREATE Monitor = " + monitorNames[i].toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                server.registerMBean(monitor[i], monitorNames[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                monitor[i].addObservedObject(mbeanNames[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                monitor[i].setObservedAttribute(attributes[i % 3]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                monitor[i].setGranularityPeriod(500);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                final Monitor m = monitor[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                Subject subject = new Subject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                echo(">>> RUN Principal = " + principals[i / 3]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                subject.getPrincipals().add(new JMXPrincipal(principals[i / 3]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                PrivilegedAction action = new PrivilegedAction() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                    public Object run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                        m.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                Subject.doAs(subject, action);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            // Wait for all tasks to be submitted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                Thread.sleep(2000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                echo("I fell asleep but someone woke me up");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            // Check if task principal is correct
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            for (int i = 0; i < 6; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                echo(">>> Monitor = " + monitorNames[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                echo(">>> ObservedObject = " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                     monitor[i].getObservedObject());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                echo(">>> ObservedAttribute = " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                     monitor[i].getObservedAttribute());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                echo(">>> Principal = " + monitored[i].principal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                if (monitored[i].principal.equals(principals[i / 3])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                    echo("\tOK: Got Expected Principal");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                    echo("\tKO: Got Unexpected Principal");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                    return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            for (int i = 0; i < 6; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                if (monitor[i] != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                    monitor[i].stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * Print message
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    private static void echo(String message) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        System.out.println(message);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * Standalone entry point.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * Run the test and report to stdout.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    public static void main (String args[]) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        ThreadPoolAccTest test = new ThreadPoolAccTest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        int error = test.runTest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        if (error > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            echo(">>> Unhappy Bye, Bye!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            throw new IllegalStateException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                "Test FAILED: Monitor task ran on wrong security context!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            echo(">>> Happy Bye, Bye!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
}