0N/A/*
2362N/A * Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A * @test
0N/A * @bug 6222826
0N/A * @summary Test that each thread in the thread pool runs
0N/A * in the context of the monitor.start() caller.
0N/A * @author Luis-Miguel Alventosa
0N/A * @run clean ThreadPoolAccTest
0N/A * @run build ThreadPoolAccTest
0N/A * @run main ThreadPoolAccTest
0N/A */
0N/A
0N/Aimport java.security.AccessController;
0N/Aimport java.security.Principal;
0N/Aimport java.security.PrivilegedAction;
0N/Aimport java.util.Set;
0N/Aimport javax.management.MBeanServer;
0N/Aimport javax.management.MBeanServerFactory;
0N/Aimport javax.management.ObjectName;
0N/Aimport javax.management.monitor.CounterMonitor;
0N/Aimport javax.management.monitor.GaugeMonitor;
0N/Aimport javax.management.monitor.Monitor;
0N/Aimport javax.management.monitor.StringMonitor;
0N/Aimport javax.management.remote.JMXPrincipal;
0N/Aimport javax.security.auth.Subject;
0N/A
0N/Apublic class ThreadPoolAccTest {
0N/A
0N/A // MBean class
0N/A public class ObservedObject implements ObservedObjectMBean {
0N/A public String principal;
0N/A public Integer getInteger() {
0N/A setPrincipal();
0N/A return 0;
0N/A }
0N/A public Double getDouble() {
0N/A setPrincipal();
0N/A return 0.0;
0N/A }
0N/A public String getString() {
0N/A setPrincipal();
0N/A return "";
0N/A }
0N/A private void setPrincipal() {
0N/A Subject subject = Subject.getSubject(AccessController.getContext());
0N/A Set principals = subject.getPrincipals(JMXPrincipal.class);
0N/A principal = ((Principal) principals.iterator().next()).getName();
0N/A }
0N/A }
0N/A
0N/A // MBean interface
0N/A public interface ObservedObjectMBean {
0N/A public Integer getInteger();
0N/A public Double getDouble();
0N/A public String getString();
0N/A }
0N/A
0N/A /**
0N/A * Run test
0N/A */
0N/A public int runTest() throws Exception {
0N/A
0N/A ObjectName[] mbeanNames = new ObjectName[6];
0N/A ObservedObject[] monitored = new ObservedObject[6];
0N/A ObjectName[] monitorNames = new ObjectName[6];
0N/A Monitor[] monitor = new Monitor[6];
0N/A String[] principals = { "role1", "role2" };
0N/A String[] attributes = { "Integer", "Double", "String" };
0N/A
0N/A try {
0N/A echo(">>> CREATE MBeanServer");
0N/A MBeanServer server = MBeanServerFactory.newMBeanServer();
0N/A
0N/A String domain = server.getDefaultDomain();
0N/A
0N/A for (int i = 0; i < 6; i++) {
0N/A mbeanNames[i] =
0N/A new ObjectName(":type=ObservedObject,instance=" + i);
0N/A monitored[i] = new ObservedObject();
0N/A echo(">>> CREATE ObservedObject = " + mbeanNames[i].toString());
0N/A server.registerMBean(monitored[i], mbeanNames[i]);
0N/A
0N/A switch (i) {
0N/A case 0:
0N/A case 3:
0N/A monitorNames[i] =
0N/A new ObjectName(":type=CounterMonitor,instance=" + i);
0N/A monitor[i] = new CounterMonitor();
0N/A break;
0N/A case 1:
0N/A case 4:
0N/A monitorNames[i] =
0N/A new ObjectName(":type=GaugeMonitor,instance=" + i);
0N/A monitor[i] = new GaugeMonitor();
0N/A break;
0N/A case 2:
0N/A case 5:
0N/A monitorNames[i] =
0N/A new ObjectName(":type=StringMonitor,instance=" + i);
0N/A monitor[i] = new StringMonitor();
0N/A break;
0N/A }
0N/A
0N/A echo(">>> CREATE Monitor = " + monitorNames[i].toString());
0N/A server.registerMBean(monitor[i], monitorNames[i]);
0N/A monitor[i].addObservedObject(mbeanNames[i]);
0N/A monitor[i].setObservedAttribute(attributes[i % 3]);
0N/A monitor[i].setGranularityPeriod(500);
0N/A final Monitor m = monitor[i];
0N/A Subject subject = new Subject();
0N/A echo(">>> RUN Principal = " + principals[i / 3]);
0N/A subject.getPrincipals().add(new JMXPrincipal(principals[i / 3]));
0N/A PrivilegedAction action = new PrivilegedAction() {
0N/A public Object run() {
0N/A m.start();
0N/A return null;
0N/A }
0N/A };
0N/A Subject.doAs(subject, action);
0N/A }
0N/A
0N/A // Wait for all tasks to be submitted
0N/A //
0N/A try {
0N/A Thread.sleep(2000);
0N/A } catch (InterruptedException e) {
0N/A echo("I fell asleep but someone woke me up");
0N/A return 1;
0N/A }
0N/A
0N/A // Check if task principal is correct
0N/A //
0N/A for (int i = 0; i < 6; i++) {
0N/A echo(">>> Monitor = " + monitorNames[i]);
0N/A echo(">>> ObservedObject = " +
0N/A monitor[i].getObservedObject());
0N/A echo(">>> ObservedAttribute = " +
0N/A monitor[i].getObservedAttribute());
0N/A echo(">>> Principal = " + monitored[i].principal);
0N/A if (monitored[i].principal.equals(principals[i / 3])) {
0N/A echo("\tOK: Got Expected Principal");
0N/A } else {
0N/A echo("\tKO: Got Unexpected Principal");
0N/A return 1;
0N/A }
0N/A }
0N/A } finally {
0N/A for (int i = 0; i < 6; i++)
0N/A if (monitor[i] != null)
0N/A monitor[i].stop();
0N/A }
0N/A
0N/A return 0;
0N/A }
0N/A
0N/A /*
0N/A * Print message
0N/A */
0N/A private static void echo(String message) {
0N/A System.out.println(message);
0N/A }
0N/A
0N/A /*
0N/A * Standalone entry point.
0N/A *
0N/A * Run the test and report to stdout.
0N/A */
0N/A public static void main (String args[]) throws Exception {
0N/A ThreadPoolAccTest test = new ThreadPoolAccTest();
0N/A int error = test.runTest();
0N/A if (error > 0) {
0N/A echo(">>> Unhappy Bye, Bye!");
0N/A throw new IllegalStateException(
0N/A "Test FAILED: Monitor task ran on wrong security context!");
0N/A } else {
0N/A echo(">>> Happy Bye, Bye!");
0N/A }
0N/A }
0N/A}