1 /*
   2  * Copyright (c) 2005, 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.Principal;
  37 import java.security.PrivilegedAction;
  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 class ObservedObject implements ObservedObjectMBean {
  53         public 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 principals = subject.getPrincipals(JMXPrincipal.class);
  69             principal = ((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     /**
  81      * Run test
  82      */
  83     public int runTest() throws Exception {
  84 
  85         ObjectName[] mbeanNames = new ObjectName[6];
  86         ObservedObject[] monitored = new ObservedObject[6];
  87         ObjectName[] monitorNames = new ObjectName[6];
  88         Monitor[] monitor = new Monitor[6];
  89         String[] principals = { "role1", "role2" };
  90         String[] attributes = { "Integer", "Double", "String" };
  91 
  92         try {
  93             echo(">>> CREATE MBeanServer");
  94             MBeanServer server = MBeanServerFactory.newMBeanServer();
  95 
  96             String domain = server.getDefaultDomain();
  97 
  98             for (int i = 0; i < 6; i++) {
  99                 mbeanNames[i] =
 100                     new ObjectName(":type=ObservedObject,instance=" + i);
 101                 monitored[i] = new ObservedObject();
 102                 echo(">>> CREATE ObservedObject = " + mbeanNames[i].toString());
 103                 server.registerMBean(monitored[i], mbeanNames[i]);
 104 
 105                 switch (i) {
 106                     case 0:
 107                     case 3:
 108                         monitorNames[i] =
 109                             new ObjectName(":type=CounterMonitor,instance=" + i);
 110                         monitor[i] = new CounterMonitor();
 111                         break;
 112                     case 1:
 113                     case 4:
 114                         monitorNames[i] =
 115                             new ObjectName(":type=GaugeMonitor,instance=" + i);
 116                         monitor[i] = new GaugeMonitor();
 117                         break;
 118                     case 2:
 119                     case 5:
 120                         monitorNames[i] =
 121                             new ObjectName(":type=StringMonitor,instance=" + i);
 122                         monitor[i] = new StringMonitor();
 123                         break;
 124                 }
 125 
 126                 echo(">>> CREATE Monitor = " + monitorNames[i].toString());
 127                 server.registerMBean(monitor[i], monitorNames[i]);
 128                 monitor[i].addObservedObject(mbeanNames[i]);
 129                 monitor[i].setObservedAttribute(attributes[i % 3]);
 130                 monitor[i].setGranularityPeriod(500);
 131                 final Monitor m = monitor[i];
 132                 Subject subject = new Subject();
 133                 echo(">>> RUN Principal = " + principals[i / 3]);
 134                 subject.getPrincipals().add(new JMXPrincipal(principals[i / 3]));
 135                 PrivilegedAction action = new PrivilegedAction() {
 136                     public Object run() {
 137                         m.start();
 138                         return null;
 139                     }
 140                 };
 141                 Subject.doAs(subject, action);
 142             }
 143 
 144             // Wait for all tasks to be submitted
 145             //
 146             try {
 147                 Thread.sleep(2000);
 148             } catch (InterruptedException e) {
 149                 echo("I fell asleep but someone woke me up");
 150                 return 1;
 151             }
 152 
 153             // Check if task principal is correct
 154             //
 155             for (int i = 0; i < 6; i++) {
 156                 echo(">>> Monitor = " + monitorNames[i]);
 157                 echo(">>> ObservedObject = " +
 158                      monitor[i].getObservedObject());
 159                 echo(">>> ObservedAttribute = " +
 160                      monitor[i].getObservedAttribute());
 161                 echo(">>> Principal = " + monitored[i].principal);
 162                 if (monitored[i].principal.equals(principals[i / 3])) {
 163                     echo("\tOK: Got Expected Principal");
 164                 } else {
 165                     echo("\tKO: Got Unexpected Principal");
 166                     return 1;
 167                 }
 168             }
 169         } finally {
 170             for (int i = 0; i < 6; i++)
 171                 if (monitor[i] != null)
 172                     monitor[i].stop();
 173         }
 174 
 175         return 0;
 176     }
 177 
 178     /*
 179      * Print message
 180      */
 181     private static void echo(String message) {
 182         System.out.println(message);
 183     }
 184 
 185     /*
 186      * Standalone entry point.
 187      *
 188      * Run the test and report to stdout.
 189      */
 190     public static void main (String args[]) throws Exception {
 191         ThreadPoolAccTest test = new ThreadPoolAccTest();
 192         int error = test.runTest();
 193         if (error > 0) {
 194             echo(">>> Unhappy Bye, Bye!");
 195             throw new IllegalStateException(
 196                 "Test FAILED: Monitor task ran on wrong security context!");
 197         } else {
 198             echo(">>> Happy Bye, Bye!");
 199         }
 200     }
 201 }