use of javax.management.MBeanException in project tomcat by apache.
the class MBeanUtils method createMBean.
/**
* Create, register, and return an MBean for this
* <code>ContextEnvironment</code> object.
*
* @param environment The ContextEnvironment to be managed
* @return a new MBean
* @exception Exception if an MBean cannot be created or registered
*/
public static DynamicMBean createMBean(ContextEnvironment environment) throws Exception {
String mname = createManagedName(environment);
ManagedBean managed = registry.findManagedBean(mname);
if (managed == null) {
Exception e = new Exception("ManagedBean is not found with " + mname);
throw new MBeanException(e);
}
String domain = managed.getDomain();
if (domain == null)
domain = mserver.getDefaultDomain();
DynamicMBean mbean = managed.createMBean(environment);
ObjectName oname = createObjectName(domain, environment);
if (mserver.isRegistered(oname)) {
mserver.unregisterMBean(oname);
}
mserver.registerMBean(mbean, oname);
return (mbean);
}
use of javax.management.MBeanException in project voldemort by voldemort.
the class JmxUtils method createModelMBean.
/**
* Create a model mbean from an object using the description given in the
* Jmx annotation if present. Only operations are supported so far, no
* attributes, constructors, or notifications
*
* @param o The object to create an MBean for
* @return The ModelMBean for the given object
*/
public static ModelMBean createModelMBean(Object o) {
try {
ModelMBean mbean = new RequiredModelMBean();
JmxManaged annotation = o.getClass().getAnnotation(JmxManaged.class);
String description = annotation == null ? "" : annotation.description();
ModelMBeanInfo info = new ModelMBeanInfoSupport(o.getClass().getName(), description, extractAttributeInfo(o), new ModelMBeanConstructorInfo[0], extractOperationInfo(o), new ModelMBeanNotificationInfo[0]);
mbean.setModelMBeanInfo(info);
mbean.setManagedResource(o, "ObjectReference");
return mbean;
} catch (MBeanException e) {
throw new VoldemortException(e);
} catch (InvalidTargetObjectTypeException e) {
throw new VoldemortException(e);
} catch (InstanceNotFoundException e) {
throw new VoldemortException(e);
}
}
use of javax.management.MBeanException in project hadoop by apache.
the class JMXJsonServlet method listBeans.
// --------------------------------------------------------- Private Methods
private void listBeans(JsonGenerator jg, ObjectName qry, String attribute, HttpServletResponse response) throws IOException {
LOG.debug("Listing beans for " + qry);
Set<ObjectName> names = null;
names = mBeanServer.queryNames(qry, null);
jg.writeArrayFieldStart("beans");
Iterator<ObjectName> it = names.iterator();
while (it.hasNext()) {
ObjectName oname = it.next();
MBeanInfo minfo;
String code = "";
Object attributeinfo = null;
try {
minfo = mBeanServer.getMBeanInfo(oname);
code = minfo.getClassName();
String prs = "";
try {
if ("org.apache.commons.modeler.BaseModelMBean".equals(code)) {
prs = "modelerType";
code = (String) mBeanServer.getAttribute(oname, prs);
}
if (attribute != null) {
prs = attribute;
attributeinfo = mBeanServer.getAttribute(oname, prs);
}
} catch (AttributeNotFoundException e) {
// If the modelerType attribute was not found, the class name is used
// instead.
LOG.error("getting attribute " + prs + " of " + oname + " threw an exception", e);
} catch (MBeanException e) {
// The code inside the attribute getter threw an exception so log it,
// and fall back on the class name
LOG.error("getting attribute " + prs + " of " + oname + " threw an exception", e);
} catch (RuntimeException e) {
// For some reason even with an MBeanException available to them
// Runtime exceptionscan still find their way through, so treat them
// the same as MBeanException
LOG.error("getting attribute " + prs + " of " + oname + " threw an exception", e);
} catch (ReflectionException e) {
// This happens when the code inside the JMX bean (setter?? from the
// java docs) threw an exception, so log it and fall back on the
// class name
LOG.error("getting attribute " + prs + " of " + oname + " threw an exception", e);
}
} catch (InstanceNotFoundException e) {
//Ignored for some reason the bean was not found so don't output it
continue;
} catch (IntrospectionException e) {
// This is an internal error, something odd happened with reflection so
// log it and don't output the bean.
LOG.error("Problem while trying to process JMX query: " + qry + " with MBean " + oname, e);
continue;
} catch (ReflectionException e) {
// This happens when the code inside the JMX bean threw an exception, so
// log it and don't output the bean.
LOG.error("Problem while trying to process JMX query: " + qry + " with MBean " + oname, e);
continue;
}
jg.writeStartObject();
jg.writeStringField("name", oname.toString());
jg.writeStringField("modelerType", code);
if ((attribute != null) && (attributeinfo == null)) {
jg.writeStringField("result", "ERROR");
jg.writeStringField("message", "No attribute with name " + attribute + " was found.");
jg.writeEndObject();
jg.writeEndArray();
jg.close();
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
return;
}
if (attribute != null) {
writeAttribute(jg, attribute, attributeinfo);
} else {
MBeanAttributeInfo[] attrs = minfo.getAttributes();
for (int i = 0; i < attrs.length; i++) {
writeAttribute(jg, oname, attrs[i]);
}
}
jg.writeEndObject();
}
jg.writeEndArray();
}
use of javax.management.MBeanException in project geode by apache.
the class MX4JModelMBean method invokeMethod.
private Object invokeMethod(Object target, String methodName, Class[] params, Object[] args) throws MBeanException, ReflectionException {
// First try on this instance, then on the target
Object realTarget = null;
Method method = null;
try {
realTarget = this;
method = realTarget.getClass().getMethod(methodName, params);
} catch (NoSuchMethodException x) {
realTarget = target;
}
if (realTarget == null)
throw new MBeanException(new ServiceNotFoundException(LocalizedStrings.MX4JModelMBean_COULD_NOT_FIND_TARGET.toLocalizedString()));
if (method == null) {
try {
method = realTarget.getClass().getMethod(methodName, params);
} catch (NoSuchMethodException x) {
throw new ReflectionException(x);
}
}
try {
Object value = method.invoke(realTarget, args);
Logger logger = getLogger();
if (logger.isEnabledFor(Logger.DEBUG))
logger.debug("Method invocation returned value: " + value);
return value;
} catch (IllegalAccessException x) {
throw new ReflectionException(x);
} catch (IllegalArgumentException x) {
throw new MBeanException(x);
} catch (InvocationTargetException x) {
Throwable t = x.getTargetException();
if (t instanceof Error)
throw new MBeanException(new RuntimeErrorException((Error) t));
else
throw new MBeanException((Exception) t);
}
}
use of javax.management.MBeanException in project geode by apache.
the class MX4JModelMBean method checkAssignability.
private void checkAssignability(Class parameter, Class declared) throws MBeanException {
Logger logger = getLogger();
if (logger.isEnabledFor(Logger.DEBUG)) {
logger.debug("The class of the parameter is: " + parameter);
if (parameter != null)
logger.debug("The classloder of the parameter's class is: " + parameter.getClassLoader());
logger.debug("The class declared as type of the attribute is: " + declared);
if (declared != null)
logger.debug("The classloader of the declared parameter's class is: " + declared.getClassLoader());
}
boolean assignable = false;
if (declared == null || parameter == null)
assignable = false;
else if (declared == boolean.class && parameter == Boolean.class)
assignable = true;
else if (declared == byte.class && parameter == Byte.class)
assignable = true;
else if (declared == char.class && parameter == Character.class)
assignable = true;
else if (declared == short.class && parameter == Short.class)
assignable = true;
else if (declared == int.class && parameter == Integer.class)
assignable = true;
else if (declared == long.class && parameter == Long.class)
assignable = true;
else if (declared == float.class && parameter == Float.class)
assignable = true;
else if (declared == double.class && parameter == Double.class)
assignable = true;
else
assignable = declared.isAssignableFrom(parameter);
if (!assignable) {
if (logger.isEnabledFor(Logger.TRACE))
logger.trace("Parameter value's class and attribute's declared return class are not assignable");
throw new MBeanException(new InvalidAttributeValueException(LocalizedStrings.MX4JModelMBean_RETURNED_TYPE_AND_DECLARED_TYPE_ARE_NOT_ASSIGNABLE.toLocalizedString()));
}
}
Aggregations