1 7 8 package com.sun.jmx.remote.security; 9 10 import com.sun.jmx.mbeanserver.GetPropertyAction; 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.InstanceNotFoundException ; 18 import javax.management.InstanceAlreadyExistsException ; 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 37 68 public abstract class MBeanServerAccessController 69 implements MBeanServerForwarder { 70 71 public MBeanServer getMBeanServer() { 72 return mbs; 73 } 74 75 public void setMBeanServer(MBeanServer mbs) { 76 if (mbs == null) 77 throw new IllegalArgumentException ("Null MBeanServer"); 78 if (this.mbs != null) 79 throw new IllegalArgumentException ("MBeanServer object already " + 80 "initialized"); 81 this.mbs = mbs; 82 } 83 84 88 protected abstract void checkRead(); 89 90 94 protected abstract void checkWrite(); 95 96 104 108 public void addNotificationListener(ObjectName name, 109 NotificationListener listener, 110 NotificationFilter filter, 111 Object handback) 112 throws InstanceNotFoundException { 113 checkRead(); 114 getMBeanServer().addNotificationListener(name, listener, 115 filter, handback); 116 } 117 118 122 public void addNotificationListener(ObjectName name, 123 ObjectName listener, 124 NotificationFilter filter, 125 Object handback) 126 throws InstanceNotFoundException { 127 checkRead(); 128 getMBeanServer().addNotificationListener(name, listener, 129 filter, handback); 130 } 131 132 136 public ObjectInstance createMBean(String className, ObjectName name) 137 throws 138 ReflectionException , 139 InstanceAlreadyExistsException , 140 MBeanRegistrationException , 141 MBeanException , 142 NotCompliantMBeanException { 143 checkWrite(); 144 SecurityManager sm = System.getSecurityManager(); 145 if (sm == null) { 146 Object object = getMBeanServer().instantiate(className); 147 checkClassLoader(object); 148 return getMBeanServer().registerMBean(object, name); 149 } else { 150 return getMBeanServer().createMBean(className, name); 151 } 152 } 153 154 158 public ObjectInstance createMBean(String className, ObjectName name, 159 Object params[], String signature[]) 160 throws 161 ReflectionException , 162 InstanceAlreadyExistsException , 163 MBeanRegistrationException , 164 MBeanException , 165 NotCompliantMBeanException { 166 checkWrite(); 167 SecurityManager sm = System.getSecurityManager(); 168 if (sm == null) { 169 Object object = getMBeanServer().instantiate(className, 170 params, 171 signature); 172 checkClassLoader(object); 173 return getMBeanServer().registerMBean(object, name); 174 } else { 175 return getMBeanServer().createMBean(className, name, 176 params, signature); 177 } 178 } 179 180 184 public ObjectInstance createMBean(String className, 185 ObjectName name, 186 ObjectName loaderName) 187 throws 188 ReflectionException , 189 InstanceAlreadyExistsException , 190 MBeanRegistrationException , 191 MBeanException , 192 NotCompliantMBeanException , 193 InstanceNotFoundException { 194 checkWrite(); 195 SecurityManager sm = System.getSecurityManager(); 196 if (sm == null) { 197 Object object = getMBeanServer().instantiate(className, 198 loaderName); 199 checkClassLoader(object); 200 return getMBeanServer().registerMBean(object, name); 201 } else { 202 return getMBeanServer().createMBean(className, name, loaderName); 203 } 204 } 205 206 210 public ObjectInstance createMBean(String className, 211 ObjectName name, 212 ObjectName loaderName, 213 Object params[], 214 String signature[]) 215 throws 216 ReflectionException , 217 InstanceAlreadyExistsException , 218 MBeanRegistrationException , 219 MBeanException , 220 NotCompliantMBeanException , 221 InstanceNotFoundException { 222 checkWrite(); 223 SecurityManager sm = System.getSecurityManager(); 224 if (sm == null) { 225 Object object = getMBeanServer().instantiate(className, 226 loaderName, 227 params, 228 signature); 229 checkClassLoader(object); 230 return getMBeanServer().registerMBean(object, name); 231 } else { 232 return getMBeanServer().createMBean(className, name, loaderName, 233 params, signature); 234 } 235 } 236 237 241 public ObjectInputStream deserialize(ObjectName name, byte[] data) 242 throws InstanceNotFoundException , OperationsException { 243 checkRead(); 244 return getMBeanServer().deserialize(name, data); 245 } 246 247 251 public ObjectInputStream deserialize(String className, byte[] data) 252 throws OperationsException , ReflectionException { 253 checkRead(); 254 return getMBeanServer().deserialize(className, data); 255 } 256 257 261 public ObjectInputStream deserialize(String className, 262 ObjectName loaderName, 263 byte[] data) 264 throws 265 InstanceNotFoundException , 266 OperationsException , 267 ReflectionException { 268 checkRead(); 269 return getMBeanServer().deserialize(className, loaderName, data); 270 } 271 272 276 public Object getAttribute(ObjectName name, String attribute) 277 throws 278 MBeanException , 279 AttributeNotFoundException , 280 InstanceNotFoundException , 281 ReflectionException { 282 checkRead(); 283 return getMBeanServer().getAttribute(name, attribute); 284 } 285 286 290 public AttributeList getAttributes(ObjectName name, String [] attributes) 291 throws InstanceNotFoundException , ReflectionException { 292 checkRead(); 293 return getMBeanServer().getAttributes(name, attributes); 294 } 295 296 300 public ClassLoader getClassLoader(ObjectName loaderName) 301 throws InstanceNotFoundException { 302 checkRead(); 303 return getMBeanServer().getClassLoader(loaderName); 304 } 305 306 310 public ClassLoader getClassLoaderFor(ObjectName mbeanName) 311 throws InstanceNotFoundException { 312 checkRead(); 313 return getMBeanServer().getClassLoaderFor(mbeanName); 314 } 315 316 320 public ClassLoaderRepository getClassLoaderRepository() { 321 checkRead(); 322 return getMBeanServer().getClassLoaderRepository(); 323 } 324 325 329 public String getDefaultDomain() { 330 checkRead(); 331 return getMBeanServer().getDefaultDomain(); 332 } 333 334 338 public String [] getDomains() { 339 checkRead(); 340 return getMBeanServer().getDomains(); 341 } 342 343 347 public Integer getMBeanCount() { 348 checkRead(); 349 return getMBeanServer().getMBeanCount(); 350 } 351 352 356 public MBeanInfo getMBeanInfo(ObjectName name) 357 throws 358 InstanceNotFoundException , 359 IntrospectionException , 360 ReflectionException { 361 checkRead(); 362 return getMBeanServer().getMBeanInfo(name); 363 } 364 365 369 public ObjectInstance getObjectInstance(ObjectName name) 370 throws InstanceNotFoundException { 371 checkRead(); 372 return getMBeanServer().getObjectInstance(name); 373 } 374 375 379 public Object instantiate(String className) 380 throws ReflectionException , MBeanException { 381 checkWrite(); 382 return getMBeanServer().instantiate(className); 383 } 384 385 389 public Object instantiate(String className, 390 Object params[], 391 String signature[]) 392 throws ReflectionException , MBeanException { 393 checkWrite(); 394 return getMBeanServer().instantiate(className, params, signature); 395 } 396 397 401 public Object instantiate(String className, ObjectName loaderName) 402 throws ReflectionException , MBeanException , InstanceNotFoundException { 403 checkWrite(); 404 return getMBeanServer().instantiate(className, loaderName); 405 } 406 407 411 public Object instantiate(String className, ObjectName loaderName, 412 Object params[], String signature[]) 413 throws ReflectionException , MBeanException , InstanceNotFoundException { 414 checkWrite(); 415 return getMBeanServer().instantiate(className, loaderName, 416 params, signature); 417 } 418 419 423 public Object invoke(ObjectName name, String operationName, 424 Object params[], String signature[]) 425 throws 426 InstanceNotFoundException , 427 MBeanException , 428 ReflectionException { 429 checkWrite(); 430 checkMLetMethods(name, operationName); 431 return getMBeanServer().invoke(name, operationName, params, signature); 432 } 433 434 438 public boolean isInstanceOf(ObjectName name, String className) 439 throws InstanceNotFoundException { 440 checkRead(); 441 return getMBeanServer().isInstanceOf(name, className); 442 } 443 444 448 public boolean isRegistered(ObjectName name) { 449 checkRead(); 450 return getMBeanServer().isRegistered(name); 451 } 452 453 457 public Set queryMBeans(ObjectName name, QueryExp query) { 458 checkRead(); 459 return getMBeanServer().queryMBeans(name, query); 460 } 461 462 466 public Set queryNames(ObjectName name, QueryExp query) { 467 checkRead(); 468 return getMBeanServer().queryNames(name, query); 469 } 470 471 475 public ObjectInstance registerMBean(Object object, ObjectName name) 476 throws 477 InstanceAlreadyExistsException , 478 MBeanRegistrationException , 479 NotCompliantMBeanException { 480 checkWrite(); 481 return getMBeanServer().registerMBean(object, name); 482 } 483 484 488 public void removeNotificationListener(ObjectName name, 489 NotificationListener listener) 490 throws InstanceNotFoundException , ListenerNotFoundException { 491 checkRead(); 492 getMBeanServer().removeNotificationListener(name, listener); 493 } 494 495 499 public void removeNotificationListener(ObjectName name, 500 NotificationListener listener, 501 NotificationFilter filter, 502 Object handback) 503 throws InstanceNotFoundException , ListenerNotFoundException { 504 checkRead(); 505 getMBeanServer().removeNotificationListener(name, listener, 506 filter, handback); 507 } 508 509 513 public void removeNotificationListener(ObjectName name, 514 ObjectName listener) 515 throws InstanceNotFoundException , ListenerNotFoundException { 516 checkRead(); 517 getMBeanServer().removeNotificationListener(name, listener); 518 } 519 520 524 public void removeNotificationListener(ObjectName name, 525 ObjectName listener, 526 NotificationFilter filter, 527 Object handback) 528 throws InstanceNotFoundException , ListenerNotFoundException { 529 checkRead(); 530 getMBeanServer().removeNotificationListener(name, listener, 531 filter, handback); 532 } 533 534 538 public void setAttribute(ObjectName name, Attribute attribute) 539 throws 540 InstanceNotFoundException , 541 AttributeNotFoundException , 542 InvalidAttributeValueException , 543 MBeanException , 544 ReflectionException { 545 checkWrite(); 546 getMBeanServer().setAttribute(name, attribute); 547 } 548 549 553 public AttributeList setAttributes(ObjectName name, 554 AttributeList attributes) 555 throws InstanceNotFoundException , ReflectionException { 556 checkWrite(); 557 return getMBeanServer().setAttributes(name, attributes); 558 } 559 560 564 public void unregisterMBean(ObjectName name) 565 throws InstanceNotFoundException , MBeanRegistrationException { 566 checkWrite(); 567 getMBeanServer().unregisterMBean(name); 568 } 569 570 574 private void checkClassLoader(Object object) { 575 if (object instanceof ClassLoader ) 576 throw new SecurityException ("Access denied! Creating an " + 577 "MBean that is a ClassLoader " + 578 "is forbidden unless a security " + 579 "manager is installed."); 580 } 581 582 private void checkMLetMethods(ObjectName name, String operation) 583 throws InstanceNotFoundException { 584 SecurityManager sm = System.getSecurityManager(); 586 if (sm != null) { 587 return; 588 } 589 if (!operation.equals("addURL") && 591 !operation.equals("getMBeansFromURL")) { 592 return; 593 } 594 if (!getMBeanServer().isInstanceOf(name, 596 "javax.management.loading.MLet")) { 597 return; 598 } 599 if (operation.equals("addURL")) { throw new SecurityException ("Access denied! MLet method addURL " + 602 "cannot be invoked unless a security manager is installed."); 603 } else { final String propName = "jmx.remote.x.mlet.allow.getMBeansFromURL"; 610 GetPropertyAction propAction = new GetPropertyAction(propName); 611 String propValue = (String ) AccessController.doPrivileged(propAction); 612 boolean allowGetMBeansFromURL = "true".equalsIgnoreCase(propValue); 613 if (!allowGetMBeansFromURL) { 614 throw new SecurityException ("Access denied! MLet method " + 615 "getMBeansFromURL cannot be invoked unless a " + 616 "security manager is installed or the system property " + 617 "-Djmx.remote.x.mlet.allow.getMBeansFromURL=true " + 618 "is specified."); 619 } 620 } 621 } 622 623 627 private MBeanServer mbs; 628 } 629 | Popular Tags |