📄 defaultmbeanserverinterceptor.java
字号:
} catch (Throwable t) { rethrow(t); throw new AssertionError(); } } public void setAttribute(ObjectName name, Attribute attribute) throws InstanceNotFoundException, AttributeNotFoundException, InvalidAttributeValueException, MBeanException, ReflectionException { if (name == null) { throw new RuntimeOperationsException(new IllegalArgumentException("ObjectName name cannot be null"), "Exception occurred trying to invoke the setter on the MBean"); } if (attribute == null) { throw new RuntimeOperationsException(new IllegalArgumentException("Attribute cannot be null"), "Exception occurred trying to invoke the setter on the MBean"); } name = nonDefaultDomain(name); if (isTraceOn()) { trace("setAttribute", "Object= " + name + ", attribute=" + attribute.getName()); } DynamicMBean instance = getMBean(name); checkMBeanPermission(instance, attribute.getName(), name, "setAttribute"); try { instance.setAttribute(attribute); } catch (AttributeNotFoundException e) { throw e; } catch (InvalidAttributeValueException e) { throw e; } catch (Throwable t) { rethrowMaybeMBeanException(t); throw new AssertionError(); } } public AttributeList setAttributes(ObjectName name, AttributeList attributes) throws InstanceNotFoundException, ReflectionException { if (name == null) { throw new RuntimeOperationsException(new IllegalArgumentException("ObjectName name cannot be null"), "Exception occurred trying to invoke the setter on the MBean"); } if (attributes == null) { throw new RuntimeOperationsException(new IllegalArgumentException("AttributeList cannot be null"), "Exception occurred trying to invoke the setter on the MBean"); } name = nonDefaultDomain(name); final DynamicMBean instance = getMBean(name); final AttributeList allowedAttributes; final SecurityManager sm = System.getSecurityManager(); if (sm == null) allowedAttributes = attributes; else { String classname = getClassName(instance); // Check if the caller has the right to invoke 'setAttribute' // checkMBeanPermission(classname, null, name, "setAttribute"); // Check if the caller has the right to invoke 'setAttribute' // on each specific attribute // allowedAttributes = new AttributeList(attributes.size()); for (Iterator i = attributes.iterator(); i.hasNext();) { try { Attribute attribute = (Attribute) i.next(); checkMBeanPermission(classname, attribute.getName(), name, "setAttribute"); allowedAttributes.add(attribute); } catch (SecurityException e) { // OK: Do not add this attribute to the list } } } try { return instance.setAttributes(allowedAttributes); } catch (Throwable t) { rethrow(t); throw new AssertionError(); } } public Object invoke(ObjectName name, String operationName, Object params[], String signature[]) throws InstanceNotFoundException, MBeanException, ReflectionException { name = nonDefaultDomain(name); DynamicMBean instance = getMBean(name); checkMBeanPermission(instance, operationName, name, "invoke"); try { return instance.invoke(operationName, params, signature); } catch (Throwable t) { rethrowMaybeMBeanException(t); throw new AssertionError(); } } /* Centralize some of the tedious exception wrapping demanded by the JMX spec. */ private static void rethrow(Throwable t) throws ReflectionException { try { throw t; } catch (ReflectionException e) { throw e; } catch (RuntimeOperationsException e) { throw e; } catch (RuntimeErrorException e) { throw e; } catch (RuntimeException e) { throw new RuntimeMBeanException(e, e.toString()); } catch (Error e) { throw new RuntimeErrorException(e, e.toString()); } catch (Throwable t2) { // should not happen throw new RuntimeException("Unexpected exception", t2); } } private static void rethrowMaybeMBeanException(Throwable t) throws ReflectionException, MBeanException { if (t instanceof MBeanException) throw (MBeanException) t; rethrow(t); } /** * Register <code>object</code> in the repository, with the * given <code>name</code>. * This method is called by the various createMBean() flavours * and by registerMBean() after all MBean compliance tests * have been performed. * <p> * This method does not performed any kind of test compliance, * and the caller should make sure that the given <code>object</code> * is MBean compliant. * <p> * This methods performed all the basic steps needed for object * registration: * <ul> * <li>If the <code>object</code> implements the MBeanRegistration * interface, it invokes preRegister() on the object.</li> * <li>Then the object is added to the repository with the given * <code>name</code>.</li> * <li>Finally, if the <code>object</code> implements the * MBeanRegistration interface, it invokes postRegister() * on the object.</li> * </ul> * @param object A reference to a MBean compliant object. * @param name The ObjectName of the <code>object</code> MBean. * @return the actual ObjectName with which the object was registered. * @exception InstanceAlreadyExistsException if an object is already * registered with that name. * @exception MBeanRegistrationException if an exception occurs during * registration. **/ private ObjectInstance registerObject(String classname, Object object, ObjectName name) throws InstanceAlreadyExistsException, MBeanRegistrationException, NotCompliantMBeanException { if (object == null) { final RuntimeException wrapped = new IllegalArgumentException("Cannot add null object"); throw new RuntimeOperationsException(wrapped, "Exception occurred trying to register the MBean"); } DynamicMBean mbean = Introspector.makeDynamicMBean(object); return registerDynamicMBean(classname, mbean, name); } private ObjectInstance registerDynamicMBean(String classname, DynamicMBean mbean, ObjectName name) throws InstanceAlreadyExistsException, MBeanRegistrationException, NotCompliantMBeanException { name = nonDefaultDomain(name); if (isTraceOn()) { trace(dbgTag, "registerMBean", "ObjectName = " + name); } ObjectName logicalName = name; if (mbean instanceof MBeanRegistration) { MBeanRegistration reg = (MBeanRegistration) mbean; logicalName = preRegisterInvoke(reg, name, server); if (mbean instanceof DynamicMBean2) { try { ((DynamicMBean2) mbean).preRegister2(server, logicalName); } catch (Exception e) { postRegisterInvoke(reg, false, false); if (e instanceof RuntimeException) throw (RuntimeException) e; if (e instanceof InstanceAlreadyExistsException) throw (InstanceAlreadyExistsException) e; throw new RuntimeException(e); } } if (logicalName != name && logicalName != null) { logicalName = ObjectName.getInstance(nonDefaultDomain(logicalName)); } } checkMBeanPermission(classname, null, logicalName, "registerMBean"); final ObjectInstance result; if (logicalName!=null) { result = new ObjectInstance(logicalName, classname); internal_addObject(mbean, logicalName); } else { if (mbean instanceof MBeanRegistration) postRegisterInvoke((MBeanRegistration) mbean, false, true); final RuntimeException wrapped = new IllegalArgumentException("No object name specified"); throw new RuntimeOperationsException(wrapped, "Exception occurred trying to register the MBean"); } if (mbean instanceof MBeanRegistration) postRegisterInvoke((MBeanRegistration) mbean, true, false); /** * Checks if the newly registered MBean is a ClassLoader * If so, tell the ClassLoaderRepository (CLR) about it. We do * this even if the object is a PrivateClassLoader. In that * case, the CLR remembers the loader for use when it is * explicitly named (e.g. as the loader in createMBean) but * does not add it to the list that is consulted by * ClassLoaderRepository.loadClass. */ final Object resource = getResource(mbean); if (resource instanceof ClassLoader) { final ModifiableClassLoaderRepository clr = instantiator.getClassLoaderRepository(); if (clr == null) { final RuntimeException wrapped = new IllegalArgumentException( "Dynamic addition of class loaders is not supported"); throw new RuntimeOperationsException(wrapped, "Exception occurred trying to register the MBean as a class loader"); } clr.addClassLoader(logicalName, (ClassLoader) resource); } return result; } private static ObjectName preRegisterInvoke(MBeanRegistration moi, ObjectName name, MBeanServer mbs) throws InstanceAlreadyExistsException, MBeanRegistrationException { final ObjectName newName; try { newName = moi.preRegister(mbs, name); } catch (RuntimeException e) { throw new RuntimeMBeanException((RuntimeException)e, "RuntimeException thrown in preRegister method"); } catch (Error er) { throw new RuntimeErrorException((Error) er, "Error thrown in preRegister method"); } catch (MBeanRegistrationException r) { throw (MBeanRegistrationException)r; } catch (Exception ex) { throw new MBeanRegistrationException((Exception) ex, "Exception thrown in preRegister method"); } if (newName != null) return newName; else return name; } private static void postRegisterInvoke(MBeanRegistration moi, boolean registrationDone, boolean registerFailed) { if (registerFailed && moi instanceof DynamicMBean2) ((DynamicMBean2) moi).registerFailed(); try { moi.postRegister(new Boolean(registrationDone)); } catch (RuntimeException e) { throw new RuntimeMBeanException((RuntimeException)e, "RuntimeException thrown in postRegister method"); } catch (Error er) { throw new RuntimeErrorException((Error) er, "Error thrown in postRegister method"); } } private static void preDeregisterInvoke(MBeanRegistration moi) throws MBeanRegistrationException { try { moi.preDeregister(); } catch (RuntimeException e) { throw new RuntimeMBeanException((RuntimeException) e, "RuntimeException thrown in preDeregister method"); } catch (Error er) { throw new RuntimeErrorException((Error) er, "Error thrown in preDeregister method"); } catch (MBeanRegistrationException t) { throw (MBeanRegistrationException)t; } catch (Exception ex) { throw new MBeanRegistrationException((Exception)ex, "Exception thrown in preDeregister method"); } } private static void postDeregisterInvoke(MBeanRegistration moi) { try { moi.postDeregister(); } catch (RuntimeException e) { throw new RuntimeMBeanException((RuntimeException)e, "RuntimeException thrown in postDeregister method"); } catch (Error er) { throw new RuntimeErrorException((Error) er, "Error thrown in postDeregister method"); } } /** * Gets a specific MBean controlled by the DefaultMBeanServerInterceptor. * The name must have a non-default domain. */ private DynamicMBean getMBean(ObjectName name) throws InstanceNotFoundException { if (name == null) { throw new RuntimeOperationsException(new IllegalArgumentException("Object name cannot be null"), "Exception occurred trying to get an MBean"); } DynamicMBean obj = null; synchronized(this) { obj = repository.retrieve(name);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -