Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/management/monitor/ThreadPoolAccTest.java
38838 views
/*1* Copyright (c) 2005, 2013 Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 622282626* @summary Test that each thread in the thread pool runs27* in the context of the monitor.start() caller.28* @author Luis-Miguel Alventosa29* @run clean ThreadPoolAccTest30* @run build ThreadPoolAccTest31* @run main ThreadPoolAccTest32*/3334import java.security.AccessController;35import java.security.PrivilegedAction;36import java.util.Date;37import java.util.Set;38import javax.management.MBeanServer;39import javax.management.MBeanServerFactory;40import javax.management.ObjectName;41import javax.management.monitor.CounterMonitor;42import javax.management.monitor.GaugeMonitor;43import javax.management.monitor.Monitor;44import javax.management.monitor.StringMonitor;45import javax.management.remote.JMXPrincipal;46import javax.security.auth.Subject;4748public class ThreadPoolAccTest {4950// MBean class51public static class ObservedObject implements ObservedObjectMBean {52public volatile String principal;53public Integer getInteger() {54setPrincipal();55return 0;56}57public Double getDouble() {58setPrincipal();59return 0.0;60}61public String getString() {62setPrincipal();63return "";64}65private void setPrincipal() {66Subject subject = Subject.getSubject(AccessController.getContext());67Set<JMXPrincipal> principals = subject.getPrincipals(JMXPrincipal.class);68principal = principals.iterator().next().getName();69}70}7172// MBean interface73public interface ObservedObjectMBean {74public Integer getInteger();75public Double getDouble();76public String getString();77}7879public static void main (String args[]) throws Exception {8081ObjectName[] mbeanNames = new ObjectName[6];82ObservedObject[] monitored = new ObservedObject[6];83ObjectName[] monitorNames = new ObjectName[6];84Monitor[] monitor = new Monitor[6];85String[] principals = { "role1", "role2" };86String[] attributes = { "Integer", "Double", "String" };8788try {89echo(">>> CREATE MBeanServer");90MBeanServer server = MBeanServerFactory.newMBeanServer();9192for (int i = 0; i < 6; i++) {93mbeanNames[i] =94new ObjectName(":type=ObservedObject,instance=" + i);95monitored[i] = new ObservedObject();96echo(">>> CREATE ObservedObject = " + mbeanNames[i].toString());97server.registerMBean(monitored[i], mbeanNames[i]);9899switch (i) {100case 0:101case 3:102monitorNames[i] =103new ObjectName(":type=CounterMonitor,instance=" + i);104monitor[i] = new CounterMonitor();105break;106case 1:107case 4:108monitorNames[i] =109new ObjectName(":type=GaugeMonitor,instance=" + i);110monitor[i] = new GaugeMonitor();111break;112case 2:113case 5:114monitorNames[i] =115new ObjectName(":type=StringMonitor,instance=" + i);116monitor[i] = new StringMonitor();117break;118}119120echo(">>> CREATE Monitor = " + monitorNames[i].toString());121server.registerMBean(monitor[i], monitorNames[i]);122monitor[i].addObservedObject(mbeanNames[i]);123monitor[i].setObservedAttribute(attributes[i % 3]);124monitor[i].setGranularityPeriod(500);125final Monitor m = monitor[i];126Subject subject = new Subject();127echo(">>> RUN Principal = " + principals[i / 3]);128subject.getPrincipals().add(new JMXPrincipal(principals[i / 3]));129PrivilegedAction<Void> action = new PrivilegedAction<Void>() {130public Void run() {131m.start();132return null;133}134};135Subject.doAs(subject, action);136}137138while(!testPrincipals(monitored, monitorNames, monitor, principals));139140} finally {141for (int i = 0; i < 6; i++)142if (monitor[i] != null)143monitor[i].stop();144}145}146147private static boolean testPrincipals(ObservedObject[] monitored, ObjectName[] monitorNames,148Monitor[] monitor, String[] principals) throws Exception {149for (int i = 0; i < 6; i++) {150String principal = monitored[i].principal;151String expected = principals[i / 3];152if (principal == null) {153echo("Task not submitted " + new Date() + ". RETRY");154return false;155}156echo(">>> Monitor = " + monitorNames[i]);157echo(">>> ObservedObject = " + monitor[i].getObservedObject());158echo(">>> ObservedAttribute = " + monitor[i].getObservedAttribute());159echo(">>> Principal = " + principal);160161if (expected.equals(principal)) {162echo("\tOK: Got Expected principal");163} else {164throw new Exception("Unexpected principal. Got: " + principal + " Expected: " + expected);165}166}167return true;168}169170private static void echo(String message) {171System.out.println(message);172}173}174175176