1 /* 2 * Copyright (C) The MX4J Contributors. 3 * All rights reserved. 4 * 5 * This software is distributed under the terms of the MX4J License version 1.0. 6 * See the terms of the MX4J License in the documentation provided with this software. 7 */ 8 9 package mx4j.examples.remote.interception; 10 11 import java.io.ObjectInputStream; 12 import java.security.AccessController; 13 import java.util.Set; 14 import javax.management.Attribute; 15 import javax.management.AttributeList; 16 import javax.management.AttributeNotFoundException; 17 import javax.management.InstanceAlreadyExistsException; 18 import javax.management.InstanceNotFoundException; 19 import javax.management.IntrospectionException; 20 import javax.management.InvalidAttributeValueException; 21 import javax.management.ListenerNotFoundException; 22 import javax.management.MBeanException; 23 import javax.management.MBeanInfo; 24 import javax.management.MBeanRegistrationException; 25 import javax.management.MBeanServer; 26 import javax.management.NotCompliantMBeanException; 27 import javax.management.NotificationFilter; 28 import javax.management.NotificationListener; 29 import javax.management.ObjectInstance; 30 import javax.management.ObjectName; 31 import javax.management.OperationsException; 32 import javax.management.QueryExp; 33 import javax.management.ReflectionException; 34 import javax.management.loading.ClassLoaderRepository; 35 import javax.management.remote.MBeanServerForwarder; 36 import javax.security.auth.Subject; 37 38 /** 39 * This class tracks the Subject of the current invocation, and prints it to System.out. 40 * It should be better implemented as JDK 1.3 dynamic proxy, but this is left as a simple 41 * exercise to the reader ;) 42 * 43 * @version $Revision: 1.3 $ 44 */ 45 public class SubjectTrackingMBeanServer implements MBeanServerForwarder 46 { 47 private MBeanServer server; 48 getMBeanServer()49 public synchronized MBeanServer getMBeanServer() 50 { 51 return server; 52 } 53 setMBeanServer(MBeanServer server)54 public synchronized void setMBeanServer(MBeanServer server) throws IllegalArgumentException 55 { 56 if (server == null) throw new IllegalArgumentException("Cannot forward to a null MBeanServer"); 57 this.server = server; 58 } 59 trackSubject()60 private void trackSubject() 61 { 62 Subject subject = Subject.getSubject(AccessController.getContext()); 63 System.out.println("Subject = " + subject); 64 } 65 addNotificationListener(ObjectName observed, NotificationListener listener, NotificationFilter filter, Object handback)66 public void addNotificationListener(ObjectName observed, NotificationListener listener, NotificationFilter filter, Object handback) 67 throws InstanceNotFoundException 68 { 69 trackSubject(); 70 getMBeanServer().addNotificationListener(observed, listener, filter, handback); 71 } 72 addNotificationListener(ObjectName observed, ObjectName listener, NotificationFilter filter, Object handback)73 public void addNotificationListener(ObjectName observed, ObjectName listener, NotificationFilter filter, Object handback) 74 throws InstanceNotFoundException 75 { 76 trackSubject(); 77 getMBeanServer().addNotificationListener(observed, listener, filter, handback); 78 } 79 removeNotificationListener(ObjectName observed, ObjectName listener)80 public void removeNotificationListener(ObjectName observed, ObjectName listener) 81 throws InstanceNotFoundException, ListenerNotFoundException 82 { 83 trackSubject(); 84 getMBeanServer().removeNotificationListener(observed, listener); 85 } 86 removeNotificationListener(ObjectName observed, NotificationListener listener)87 public void removeNotificationListener(ObjectName observed, NotificationListener listener) 88 throws InstanceNotFoundException, ListenerNotFoundException 89 { 90 trackSubject(); 91 getMBeanServer().removeNotificationListener(observed, listener); 92 } 93 removeNotificationListener(ObjectName observed, ObjectName listener, NotificationFilter filter, Object handback)94 public void removeNotificationListener(ObjectName observed, ObjectName listener, NotificationFilter filter, Object handback) 95 throws InstanceNotFoundException, ListenerNotFoundException 96 { 97 trackSubject(); 98 getMBeanServer().removeNotificationListener(observed, listener, filter, handback); 99 } 100 removeNotificationListener(ObjectName observed, NotificationListener listener, NotificationFilter filter, Object handback)101 public void removeNotificationListener(ObjectName observed, NotificationListener listener, NotificationFilter filter, Object handback) 102 throws InstanceNotFoundException, ListenerNotFoundException 103 { 104 trackSubject(); 105 getMBeanServer().removeNotificationListener(observed, listener, filter, handback); 106 } 107 getMBeanInfo(ObjectName objectName)108 public MBeanInfo getMBeanInfo(ObjectName objectName) 109 throws InstanceNotFoundException, IntrospectionException, ReflectionException 110 { 111 trackSubject(); 112 return getMBeanServer().getMBeanInfo(objectName); 113 } 114 isInstanceOf(ObjectName objectName, String className)115 public boolean isInstanceOf(ObjectName objectName, String className) 116 throws InstanceNotFoundException 117 { 118 trackSubject(); 119 return getMBeanServer().isInstanceOf(objectName, className); 120 } 121 getDomains()122 public String[] getDomains() 123 { 124 trackSubject(); 125 return getMBeanServer().getDomains(); 126 } 127 getDefaultDomain()128 public String getDefaultDomain() 129 { 130 trackSubject(); 131 return getMBeanServer().getDefaultDomain(); 132 } 133 createMBean(String className, ObjectName objectName)134 public ObjectInstance createMBean(String className, ObjectName objectName) 135 throws ReflectionException, InstanceAlreadyExistsException, MBeanRegistrationException, MBeanException, NotCompliantMBeanException 136 { 137 trackSubject(); 138 return getMBeanServer().createMBean(className, objectName); 139 } 140 createMBean(String className, ObjectName objectName, ObjectName loaderName)141 public ObjectInstance createMBean(String className, ObjectName objectName, ObjectName loaderName) 142 throws ReflectionException, InstanceAlreadyExistsException, MBeanRegistrationException, MBeanException, NotCompliantMBeanException, InstanceNotFoundException 143 { 144 trackSubject(); 145 return getMBeanServer().createMBean(className, objectName, loaderName); 146 } 147 createMBean(String className, ObjectName objectName, Object[] args, String[] parameters)148 public ObjectInstance createMBean(String className, ObjectName objectName, Object[] args, String[] parameters) 149 throws ReflectionException, InstanceAlreadyExistsException, MBeanRegistrationException, MBeanException, NotCompliantMBeanException 150 { 151 trackSubject(); 152 return getMBeanServer().createMBean(className, objectName, args, parameters); 153 } 154 createMBean(String className, ObjectName objectName, ObjectName loaderName, Object[] args, String[] parameters)155 public ObjectInstance createMBean(String className, ObjectName objectName, ObjectName loaderName, Object[] args, String[] parameters) 156 throws ReflectionException, InstanceAlreadyExistsException, MBeanRegistrationException, MBeanException, NotCompliantMBeanException, InstanceNotFoundException 157 { 158 trackSubject(); 159 return getMBeanServer().createMBean(className, objectName, loaderName, args, parameters); 160 } 161 unregisterMBean(ObjectName objectName)162 public void unregisterMBean(ObjectName objectName) 163 throws InstanceNotFoundException, MBeanRegistrationException 164 { 165 trackSubject(); 166 getMBeanServer().unregisterMBean(objectName); 167 } 168 getAttribute(ObjectName objectName, String attribute)169 public Object getAttribute(ObjectName objectName, String attribute) 170 throws MBeanException, AttributeNotFoundException, InstanceNotFoundException, ReflectionException 171 { 172 trackSubject(); 173 return getMBeanServer().getAttribute(objectName, attribute); 174 } 175 setAttribute(ObjectName objectName, Attribute attribute)176 public void setAttribute(ObjectName objectName, Attribute attribute) 177 throws InstanceNotFoundException, AttributeNotFoundException, InvalidAttributeValueException, MBeanException, ReflectionException 178 { 179 trackSubject(); 180 getMBeanServer().setAttribute(objectName, attribute); 181 } 182 getAttributes(ObjectName objectName, String[] attributes)183 public AttributeList getAttributes(ObjectName objectName, String[] attributes) 184 throws InstanceNotFoundException, ReflectionException 185 { 186 trackSubject(); 187 return getMBeanServer().getAttributes(objectName, attributes); 188 } 189 setAttributes(ObjectName objectName, AttributeList attributes)190 public AttributeList setAttributes(ObjectName objectName, AttributeList attributes) 191 throws InstanceNotFoundException, ReflectionException 192 { 193 trackSubject(); 194 return getMBeanServer().setAttributes(objectName, attributes); 195 } 196 invoke(ObjectName objectName, String methodName, Object[] args, String[] parameters)197 public Object invoke(ObjectName objectName, String methodName, Object[] args, String[] parameters) 198 throws InstanceNotFoundException, MBeanException, ReflectionException 199 { 200 trackSubject(); 201 return getMBeanServer().invoke(objectName, methodName, args, parameters); 202 } 203 getMBeanCount()204 public Integer getMBeanCount() 205 { 206 trackSubject(); 207 return getMBeanServer().getMBeanCount(); 208 } 209 isRegistered(ObjectName objectname)210 public boolean isRegistered(ObjectName objectname) 211 { 212 trackSubject(); 213 return getMBeanServer().isRegistered(objectname); 214 } 215 getObjectInstance(ObjectName objectName)216 public ObjectInstance getObjectInstance(ObjectName objectName) 217 throws InstanceNotFoundException 218 { 219 trackSubject(); 220 return getMBeanServer().getObjectInstance(objectName); 221 } 222 queryMBeans(ObjectName patternName, QueryExp filter)223 public Set queryMBeans(ObjectName patternName, QueryExp filter) 224 { 225 trackSubject(); 226 return getMBeanServer().queryMBeans(patternName, filter); 227 } 228 queryNames(ObjectName patternName, QueryExp filter)229 public Set queryNames(ObjectName patternName, QueryExp filter) 230 { 231 trackSubject(); 232 return getMBeanServer().queryNames(patternName, filter); 233 } 234 instantiate(String className)235 public Object instantiate(String className) 236 throws ReflectionException, MBeanException 237 { 238 trackSubject(); 239 return getMBeanServer().instantiate(className); 240 } 241 instantiate(String className, ObjectName loaderName)242 public Object instantiate(String className, ObjectName loaderName) 243 throws ReflectionException, MBeanException, InstanceNotFoundException 244 { 245 trackSubject(); 246 return getMBeanServer().instantiate(className, loaderName); 247 } 248 instantiate(String className, Object[] args, String[] parameters)249 public Object instantiate(String className, Object[] args, String[] parameters) 250 throws ReflectionException, MBeanException 251 { 252 trackSubject(); 253 return getMBeanServer().instantiate(className, args, parameters); 254 } 255 instantiate(String className, ObjectName loaderName, Object[] args, String[] parameters)256 public Object instantiate(String className, ObjectName loaderName, Object[] args, String[] parameters) 257 throws ReflectionException, MBeanException, InstanceNotFoundException 258 { 259 trackSubject(); 260 return getMBeanServer().instantiate(className, loaderName, args, parameters); 261 } 262 registerMBean(Object mbean, ObjectName objectName)263 public ObjectInstance registerMBean(Object mbean, ObjectName objectName) 264 throws InstanceAlreadyExistsException, MBeanRegistrationException, NotCompliantMBeanException 265 { 266 trackSubject(); 267 return registerMBean(mbean, objectName); 268 } 269 deserialize(String className, ObjectName loaderName, byte[] bytes)270 public ObjectInputStream deserialize(String className, ObjectName loaderName, byte[] bytes) 271 throws InstanceNotFoundException, OperationsException, ReflectionException 272 { 273 trackSubject(); 274 return getMBeanServer().deserialize(className, loaderName, bytes); 275 } 276 deserialize(String className, byte[] bytes)277 public ObjectInputStream deserialize(String className, byte[] bytes) 278 throws OperationsException, ReflectionException 279 { 280 trackSubject(); 281 return getMBeanServer().deserialize(className, bytes); 282 } 283 deserialize(ObjectName objectName, byte[] bytes)284 public ObjectInputStream deserialize(ObjectName objectName, byte[] bytes) 285 throws InstanceNotFoundException, OperationsException 286 { 287 trackSubject(); 288 return getMBeanServer().deserialize(objectName, bytes); 289 } 290 getClassLoaderFor(ObjectName mbeanName)291 public ClassLoader getClassLoaderFor(ObjectName mbeanName) 292 throws InstanceNotFoundException 293 { 294 trackSubject(); 295 return getMBeanServer().getClassLoaderFor(mbeanName); 296 } 297 getClassLoader(ObjectName loaderName)298 public ClassLoader getClassLoader(ObjectName loaderName) 299 throws InstanceNotFoundException 300 { 301 trackSubject(); 302 return getMBeanServer().getClassLoader(loaderName); 303 } 304 getClassLoaderRepository()305 public ClassLoaderRepository getClassLoaderRepository() 306 { 307 trackSubject(); 308 return getMBeanServer().getClassLoaderRepository(); 309 } 310 } 311