Path: blob/master/test/jdk/javax/management/monitor/ThreadPoolAccTest.java
41149 views
/*1* Copyright (c) 2005, 2015, 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*30* @run clean ThreadPoolAccTest31* @run build ThreadPoolAccTest32* @run main ThreadPoolAccTest33*/3435import java.security.AccessController;36import java.security.PrivilegedAction;37import java.util.Date;38import java.util.Set;39import javax.management.MBeanServer;40import javax.management.MBeanServerFactory;41import javax.management.ObjectName;42import javax.management.monitor.CounterMonitor;43import javax.management.monitor.GaugeMonitor;44import javax.management.monitor.Monitor;45import javax.management.monitor.StringMonitor;46import javax.management.remote.JMXPrincipal;47import javax.security.auth.Subject;4849public class ThreadPoolAccTest {5051// MBean class52public static class ObservedObject implements ObservedObjectMBean {53public volatile String principal;54public Integer getInteger() {55setPrincipal();56return 0;57}58public Double getDouble() {59setPrincipal();60return 0.0;61}62public String getString() {63setPrincipal();64return "";65}66private void setPrincipal() {67Subject subject = Subject.getSubject(AccessController.getContext());68Set<JMXPrincipal> principals = subject.getPrincipals(JMXPrincipal.class);69principal = principals.iterator().next().getName();70}71}7273// MBean interface74public interface ObservedObjectMBean {75public Integer getInteger();76public Double getDouble();77public String getString();78}7980public static void main (String args[]) throws Exception {8182ObjectName[] mbeanNames = new ObjectName[6];83ObservedObject[] monitored = new ObservedObject[6];84ObjectName[] monitorNames = new ObjectName[6];85Monitor[] monitor = new Monitor[6];86String[] principals = { "role1", "role2" };87String[] attributes = { "Integer", "Double", "String" };8889try {90echo(">>> CREATE MBeanServer");91MBeanServer server = MBeanServerFactory.newMBeanServer();9293for (int i = 0; i < 6; i++) {94mbeanNames[i] =95new ObjectName(":type=ObservedObject,instance=" + i);96monitored[i] = new ObservedObject();97echo(">>> CREATE ObservedObject = " + mbeanNames[i].toString());98server.registerMBean(monitored[i], mbeanNames[i]);99100switch (i) {101case 0:102case 3:103monitorNames[i] =104new ObjectName(":type=CounterMonitor,instance=" + i);105monitor[i] = new CounterMonitor();106break;107case 1:108case 4:109monitorNames[i] =110new ObjectName(":type=GaugeMonitor,instance=" + i);111monitor[i] = new GaugeMonitor();112break;113case 2:114case 5:115monitorNames[i] =116new ObjectName(":type=StringMonitor,instance=" + i);117monitor[i] = new StringMonitor();118break;119}120121echo(">>> CREATE Monitor = " + monitorNames[i].toString());122server.registerMBean(monitor[i], monitorNames[i]);123monitor[i].addObservedObject(mbeanNames[i]);124monitor[i].setObservedAttribute(attributes[i % 3]);125monitor[i].setGranularityPeriod(500);126final Monitor m = monitor[i];127Subject subject = new Subject();128echo(">>> RUN Principal = " + principals[i / 3]);129subject.getPrincipals().add(new JMXPrincipal(principals[i / 3]));130PrivilegedAction<Void> action = new PrivilegedAction<Void>() {131public Void run() {132m.start();133return null;134}135};136Subject.doAs(subject, action);137}138139while(!testPrincipals(monitored, monitorNames, monitor, principals));140141} finally {142for (int i = 0; i < 6; i++)143if (monitor[i] != null)144monitor[i].stop();145}146}147148private static boolean testPrincipals(ObservedObject[] monitored, ObjectName[] monitorNames,149Monitor[] monitor, String[] principals) throws Exception {150for (int i = 0; i < 6; i++) {151String principal = monitored[i].principal;152String expected = principals[i / 3];153if (principal == null) {154echo("Task not submitted " + new Date() + ". RETRY");155return false;156}157echo(">>> Monitor = " + monitorNames[i]);158echo(">>> ObservedObject = " + monitor[i].getObservedObject());159echo(">>> ObservedAttribute = " + monitor[i].getObservedAttribute());160echo(">>> Principal = " + principal);161162if (expected.equals(principal)) {163echo("\tOK: Got Expected principal");164} else {165throw new Exception("Unexpected principal. Got: " + principal + " Expected: " + expected);166}167}168return true;169}170171private static void echo(String message) {172System.out.println(message);173}174}175176177