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