下面列出了怎么用javax.management.MBeanException的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Invokes an operation on an MBean.
*
* @param onameStr The name of the MBean.
* @param operation The name of the operation to invoke.
* @param parameters An array of Strings containing the parameters to the
* operation. They will be converted to the appropriate types to
* call the requested operation.
* @return The value returned by the requested operation.
*/
private Object invokeOperationInternal(String onameStr, String operation, String[] parameters)
throws OperationsException, MBeanException, ReflectionException {
ObjectName oname = new ObjectName(onameStr);
MBeanOperationInfo methodInfo = registry.getMethodInfo(oname, operation);
MBeanParameterInfo[] signature = methodInfo.getSignature();
String[] signatureTypes = new String[signature.length];
Object[] values = new Object[signature.length];
for (int i = 0; i < signature.length; i++) {
MBeanParameterInfo pi = signature[i];
signatureTypes[i] = pi.getType();
values[i] = registry.convertValue(pi.getType(), parameters[i]);
}
return mBeanServer.invoke(oname, operation, values, signatureTypes);
}
/**
* Call <code>checkCreate(className)</code>, then forward this method to the
* wrapped object.
*/
public ObjectInstance createMBean(String className, ObjectName name)
throws
ReflectionException,
InstanceAlreadyExistsException,
MBeanRegistrationException,
MBeanException,
NotCompliantMBeanException {
checkCreate(className);
SecurityManager sm = System.getSecurityManager();
if (sm == null) {
Object object = getMBeanServer().instantiate(className);
checkClassLoader(object);
return getMBeanServer().registerMBean(object, name);
} else {
return getMBeanServer().createMBean(className, name);
}
}
/**
* Call <code>checkCreate(className)</code>, then forward this method to the
* wrapped object.
*/
public ObjectInstance createMBean(String className, ObjectName name)
throws
ReflectionException,
InstanceAlreadyExistsException,
MBeanRegistrationException,
MBeanException,
NotCompliantMBeanException {
checkCreate(className);
SecurityManager sm = System.getSecurityManager();
if (sm == null) {
Object object = getMBeanServer().instantiate(className);
checkClassLoader(object);
return getMBeanServer().registerMBean(object, name);
} else {
return getMBeanServer().createMBean(className, name);
}
}
Object getAttribute(Object resource, String attribute, Object cookie)
throws AttributeNotFoundException,
MBeanException,
ReflectionException {
final M cm = getters.get(attribute);
if (cm == null) {
final String msg;
if (setters.containsKey(attribute))
msg = "Write-only attribute: " + attribute;
else
msg = "No such attribute: " + attribute;
throw new AttributeNotFoundException(msg);
}
return introspector.invokeM(cm, resource, (Object[]) null, cookie);
}
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();
}
}
/**
* Always fails since the MBeanServerDelegate MBean has no operation.
*
* @param actionName The name of the action to be invoked.
* @param params An array containing the parameters to be set when the
* action is invoked.
* @param signature An array containing the signature of the action.
*
* @return The object returned by the action, which represents
* the result of invoking the action on the MBean specified.
*
* @exception MBeanException Wraps a <CODE>java.lang.Exception</CODE>
* thrown by the MBean's invoked method.
* @exception ReflectionException Wraps a
* <CODE>java.lang.Exception</CODE> thrown while trying to invoke
* the method.
*/
public Object invoke(String actionName, Object params[],
String signature[])
throws MBeanException, ReflectionException {
// Check that operation name is not null.
//
if (actionName == null) {
final RuntimeException r =
new IllegalArgumentException("Operation name cannot be null");
throw new RuntimeOperationsException(r,
"Exception occurred trying to invoke the operation on the MBean");
}
throw new ReflectionException(
new NoSuchMethodException(actionName),
"The operation with name " + actionName +
" could not be found");
}
Object getAttribute(Object resource, String attribute, Object cookie)
throws AttributeNotFoundException,
MBeanException,
ReflectionException {
final M cm = getters.get(attribute);
if (cm == null) {
final String msg;
if (setters.containsKey(attribute))
msg = "Write-only attribute: " + attribute;
else
msg = "No such attribute: " + attribute;
throw new AttributeNotFoundException(msg);
}
return introspector.invokeM(cm, resource, (Object[]) null, cookie);
}
/**
* Call <code>checkCreate(className)</code>, then forward this method to the
* wrapped object.
*/
public ObjectInstance createMBean(String className, ObjectName name,
Object params[], String signature[])
throws
ReflectionException,
InstanceAlreadyExistsException,
MBeanRegistrationException,
MBeanException,
NotCompliantMBeanException {
checkCreate(className);
SecurityManager sm = System.getSecurityManager();
if (sm == null) {
Object object = getMBeanServer().instantiate(className,
params,
signature);
checkClassLoader(object);
return getMBeanServer().registerMBean(object, name);
} else {
return getMBeanServer().createMBean(className, name,
params, signature);
}
}
/**
* Call <code>checkCreate(className)</code>, then forward this method to the
* wrapped object.
*/
public ObjectInstance createMBean(String className, ObjectName name)
throws
ReflectionException,
InstanceAlreadyExistsException,
MBeanRegistrationException,
MBeanException,
NotCompliantMBeanException {
checkCreate(className);
SecurityManager sm = System.getSecurityManager();
if (sm == null) {
Object object = getMBeanServer().instantiate(className);
checkClassLoader(object);
return getMBeanServer().registerMBean(object, name);
} else {
return getMBeanServer().createMBean(className, name);
}
}
public ObjectInstance createMBean(String className, ObjectName name,
Object[] params, String[] signature)
throws ReflectionException, InstanceAlreadyExistsException,
MBeanRegistrationException, MBeanException,
NotCompliantMBeanException {
try {
return createMBean(className, name, null, true,
params, signature);
} catch (InstanceNotFoundException e) {
/* Can only happen if loaderName doesn't exist, but we just
passed null, so we shouldn't get this exception. */
throw EnvHelp.initCause(
new IllegalArgumentException("Unexpected exception: " + e), e);
}
}
private Notification createNotication(Recording recording) {
try {
Long id = recording.getId();
Object oldValue = changes.get(recording.getId());
Object newValue = getAttribute(ATTRIBUTE_RECORDINGS);
if (recording.getState() != RecordingState.CLOSED) {
changes.put(id, newValue);
} else {
changes.remove(id);
}
return new AttributeChangeNotification(getObjectName(), sequenceNumber.incrementAndGet(), System.currentTimeMillis(), "Recording " + recording.getName() + " is "
+ recording.getState(), ATTRIBUTE_RECORDINGS, newValue.getClass().getName(), oldValue, newValue);
} catch (AttributeNotFoundException | MBeanException | ReflectionException e) {
throw new RuntimeException("Could not create notifcation for FlightRecorderMXBean. " + e.getMessage(), e);
}
}
/**
* Call <code>checkCreate(className)</code>, then forward this method to the
* wrapped object.
*/
public ObjectInstance createMBean(String className, ObjectName name,
Object params[], String signature[])
throws
ReflectionException,
InstanceAlreadyExistsException,
MBeanRegistrationException,
MBeanException,
NotCompliantMBeanException {
checkCreate(className);
SecurityManager sm = System.getSecurityManager();
if (sm == null) {
Object object = getMBeanServer().instantiate(className,
params,
signature);
checkClassLoader(object);
return getMBeanServer().registerMBean(object, name);
} else {
return getMBeanServer().createMBean(className, name,
params, signature);
}
}
/**
* Return the set of application parameters for this application.
* @return a string array with a representation of each parameter
* @throws MBeanException propagated from the managed resource access
*/
public String[] findApplicationParameters() throws MBeanException {
Context context = doGetManagedResource();
ApplicationParameter[] params = context.findApplicationParameters();
String[] stringParams = new String[params.length];
for (int counter = 0; counter < params.length; counter++) {
stringParams[counter] = params[counter].toString();
}
return stringParams;
}
@Override
Object invokeM2(ConvertingMethod m, Object target, Object[] args,
Object cookie)
throws InvocationTargetException, IllegalAccessException,
MBeanException {
return m.invokeWithOpenReturn((MXBeanLookup) cookie, target, args);
}
public ObjectInstance createMBean(
String className, ObjectName name, Object[] params, String[] signature)
throws ReflectionException, InstanceAlreadyExistsException,
MBeanRegistrationException, MBeanException,
NotCompliantMBeanException {
try {
return createMBean(className, name, clrName, params, signature);
} catch (InstanceNotFoundException ex) {
throw new RuntimeException(ex); // can't happen
}
}
/**
* Call <code>checkRead()</code>, then forward this method to the
* wrapped object.
*/
public Object getAttribute(ObjectName name, String attribute)
throws
MBeanException,
AttributeNotFoundException,
InstanceNotFoundException,
ReflectionException {
checkRead();
return getMBeanServer().getAttribute(name, attribute);
}
/**
* Retrieves all executors.
* @return an array of string representations of the executors
* @throws MBeanException error accessing the associated service
*/
public String[] findExecutors() throws MBeanException {
Service service = doGetManagedResource();
Executor[] executors = service.findExecutors();
String[] str = new String[executors.length];
for(int i = 0; i < executors.length; i++){
str[i] = executors[i].toString();
}
return str;
}
public void setMBeanDescriptor(Descriptor inMBeanDescriptor)
throws MBeanException, RuntimeOperationsException {
if (MODELMBEAN_LOGGER.isLoggable(Level.FINER)) {
MODELMBEAN_LOGGER.logp(Level.FINER,
ModelMBeanInfoSupport.class.getName(),
"setMBeanDescriptor(Descriptor)", "Entry");
}
modelMBeanDescriptor = validDescriptor(inMBeanDescriptor);
}
public ObjectInstance createMBean(
String className, ObjectName name, ObjectName loaderName)
throws ReflectionException, InstanceAlreadyExistsException,
MBeanRegistrationException, MBeanException,
NotCompliantMBeanException, InstanceNotFoundException {
return createMBean(className, name, loaderName, null, null);
}
public ObjectInstance createMBean(String className, ObjectName name)
throws ReflectionException, InstanceAlreadyExistsException,
MBeanRegistrationException, MBeanException,
NotCompliantMBeanException {
return createMBean(className, name, (Object[]) null, (String[]) null);
}
public ModelMBeanNotificationInfo getNotification(String inName)
throws MBeanException, RuntimeOperationsException {
ModelMBeanNotificationInfo retInfo = null;
if (MODELMBEAN_LOGGER.isLoggable(Level.FINER)) {
MODELMBEAN_LOGGER.logp(Level.FINER,
ModelMBeanInfoSupport.class.getName(),
"getNotification(String)", "Entry");
}
if (inName == null) {
throw new RuntimeOperationsException(
new IllegalArgumentException("Notification name is null"),
"Exception occurred trying to get the " +
"ModelMBeanNotificationInfo of the MBean");
}
MBeanNotificationInfo[] notifList = modelMBeanNotifications; //this.getNotifications();
int numNotifs = 0;
if (notifList != null) numNotifs = notifList.length;
for (int i=0; (i < numNotifs) && (retInfo == null); i++) {
if (inName.equals(notifList[i].getName())) {
retInfo = ((ModelMBeanNotificationInfo) notifList[i].clone());
}
}
if (MODELMBEAN_LOGGER.isLoggable(Level.FINER)) {
MODELMBEAN_LOGGER.logp(Level.FINER,
ModelMBeanInfoSupport.class.getName(),
"getNotification(String)", "Exit");
}
return retInfo;
}
Object invokeWithOpenReturn(MXBeanLookup lookup,
Object obj, Object[] params)
throws MBeanException, IllegalAccessException,
InvocationTargetException {
MXBeanLookup old = MXBeanLookup.getLookup();
try {
MXBeanLookup.setLookup(lookup);
return invokeWithOpenReturn(obj, params);
} finally {
MXBeanLookup.setLookup(old);
}
}
public ModelMBeanOperationInfo getOperation(String inName)
throws MBeanException, RuntimeOperationsException {
ModelMBeanOperationInfo retInfo = null;
if (MODELMBEAN_LOGGER.isLoggable(Level.FINER)) {
MODELMBEAN_LOGGER.logp(Level.FINER,
ModelMBeanInfoSupport.class.getName(),
"getOperation(String)", "Entry");
}
if (inName == null) {
throw new RuntimeOperationsException(
new IllegalArgumentException("inName is null"),
"Exception occurred trying to get the " +
"ModelMBeanOperationInfo of the MBean");
}
MBeanOperationInfo[] operList = modelMBeanOperations; //this.getOperations();
int numOpers = 0;
if (operList != null) numOpers = operList.length;
for (int i=0; (i < numOpers) && (retInfo == null); i++) {
if (inName.equals(operList[i].getName())) {
retInfo = ((ModelMBeanOperationInfo) operList[i].clone());
}
}
if (MODELMBEAN_LOGGER.isLoggable(Level.FINER)) {
MODELMBEAN_LOGGER.logp(Level.FINER,
ModelMBeanInfoSupport.class.getName(),
"getOperation(String)", "Exit");
}
return retInfo;
}
@Override
public Object getAttribute(String attribute)
throws AttributeNotFoundException, MBeanException, ReflectionException {
if (attribute == null) {
throw new RuntimeOperationsException(new IllegalArgumentException("The attribute name cannot be null."),
"Cannot invoke getAttribute on " + MBEAN_OBJECT_NAME + " with null as attribute name.");
}
Counter c = counterMap.get(attribute);
if (c == null) {
throw new AttributeNotFoundException("Could not find the attribute " + attribute);
}
return c.getValue();
}
public ObjectInstance createMBean(String className,
ObjectName name,
ObjectName loaderName,
Object params[],
String signature[])
throws ReflectionException,
InstanceAlreadyExistsException,
MBeanRegistrationException,
MBeanException,
NotCompliantMBeanException,
InstanceNotFoundException,
IOException {
if (logger.debugOn()) logger.debug(
"createMBean(String,ObjectName,ObjectName,Object[],String[])",
"className=" + className + ", name=" + name + ", loaderName="
+ loaderName + ", signature=" + strings(signature));
final MarshalledObject<Object[]> sParams =
new MarshalledObject<Object[]>(params);
final ClassLoader old = pushDefaultClassLoader();
try {
return connection.createMBean(className,
name,
loaderName,
sParams,
signature,
delegationSubject);
} catch (IOException ioe) {
communicatorAdmin.gotIOException(ioe);
return connection.createMBean(className,
name,
loaderName,
sParams,
signature,
delegationSubject);
} finally {
popDefaultClassLoader(old);
}
}
public Object getAttribute(ObjectName name, String attribute)
throws MBeanException, AttributeNotFoundException,
InstanceNotFoundException, ReflectionException {
if (name == null) {
throw new RuntimeOperationsException(new
IllegalArgumentException("Object name cannot be null"),
"Exception occurred trying to invoke the getter on the MBean");
}
if (attribute == null) {
throw new RuntimeOperationsException(new
IllegalArgumentException("Attribute cannot be null"),
"Exception occurred trying to invoke the getter on the MBean");
}
name = nonDefaultDomain(name);
if (MBEANSERVER_LOGGER.isLoggable(Level.FINER)) {
MBEANSERVER_LOGGER.logp(Level.FINER,
DefaultMBeanServerInterceptor.class.getName(),
"getAttribute",
"Attribute = " + attribute + ", ObjectName = " + name);
}
final DynamicMBean instance = getMBean(name);
checkMBeanPermission(instance, attribute, name, "getAttribute");
try {
return instance.getAttribute(attribute);
} catch (AttributeNotFoundException e) {
throw e;
} catch (Throwable t) {
rethrowMaybeMBeanException(t);
throw new AssertionError(); // not reached
}
}
/**
* Call <code>checkWrite()</code>, then forward this method to the
* wrapped object.
*/
public void setAttribute(ObjectName name, Attribute attribute)
throws
InstanceNotFoundException,
AttributeNotFoundException,
InvalidAttributeValueException,
MBeanException,
ReflectionException {
checkWrite();
getMBeanServer().setAttribute(name, attribute);
}
public Object invoke(String opName, Object[] params, String[] sig)
throws MBeanException {
assert params == null && sig == null;
if (opName.equals("UncheckedException"))
throw theUncheckedException;
else
throw new AssertionError();
}
/**
* Call <code>checkCreate(className)</code>, then forward this method to the
* wrapped object.
*/
public ObjectInstance createMBean(String className,
ObjectName name,
ObjectName loaderName,
Object params[],
String signature[])
throws
ReflectionException,
InstanceAlreadyExistsException,
MBeanRegistrationException,
MBeanException,
NotCompliantMBeanException,
InstanceNotFoundException {
checkCreate(className);
SecurityManager sm = System.getSecurityManager();
if (sm == null) {
Object object = getMBeanServer().instantiate(className,
loaderName,
params,
signature);
checkClassLoader(object);
return getMBeanServer().registerMBean(object, name);
} else {
return getMBeanServer().createMBean(className, name, loaderName,
params, signature);
}
}
@Override
Object invokeM2(ConvertingMethod m, Object target, Object[] args,
Object cookie)
throws InvocationTargetException, IllegalAccessException,
MBeanException {
return m.invokeWithOpenReturn((MXBeanLookup) cookie, target, args);
}