/** * Create a new holder */ public JolokiaMBeanServerHolder() { jolokiaMBeanServer = new JolokiaMBeanServer(); }
private JsonMBean extractJsonMBeanAnnotation(Object object) { // Try directly Class<?> clazz = object.getClass(); JsonMBean anno = clazz.getAnnotation(JsonMBean.class); if (anno == null && ModelMBean.class.isAssignableFrom(object.getClass())) { // For ModelMBean we try some heuristic to get to the managed resource // This works for all subclasses of RequiredModelMBean as provided by the JDK // but maybe for other ModelMBean classes as well Boolean isAccessible = null; Field field = null; try { field = findField(clazz, "managedResource"); if (field != null) { isAccessible = field.isAccessible(); field.setAccessible(true); Object managedResource = field.get(object); anno = managedResource.getClass().getAnnotation(JsonMBean.class); } } catch (IllegalAccessException e) { // Ignored silently, but we tried it at least } finally { if (isAccessible != null) { field.setAccessible(isAccessible); } } } return anno; }
private Object fromJson(OpenType<?> pType, String pValue) { return jolokiaMBeanServer.fromJson(pType, pValue); }
@Override public ObjectInstance registerMBean(Object object, ObjectName name) throws InstanceAlreadyExistsException, MBeanRegistrationException, NotCompliantMBeanException { // Register MBean first on this MBean Server ObjectInstance ret = super.registerMBean(object, name); // Check, whether it is annotated with @JsonMBean. Only the outermost class of an inheritance is // considered. JsonMBean anno = extractJsonMBeanAnnotation(object); if (anno != null) { // The real name can be different than the given one in case the default // domain was omitted and/or the MBean implements MBeanRegistration ObjectName realName = ret.getObjectName(); try { // Fetch real MBeanInfo and create a dynamic MBean with modified signature MBeanInfo info = super.getMBeanInfo(realName); JsonDynamicMBeanImpl mbean = new JsonDynamicMBeanImpl(this,realName,info,getJsonConverterOptions(anno)); // Register MBean on delegate MBeanServer delegatedMBeans.add(realName); delegateServer.registerMBean(mbean,realName); } catch (InstanceNotFoundException e) { throw new MBeanRegistrationException(e,"Cannot obtain MBeanInfo from Jolokia-Server for " + realName); } catch (IntrospectionException e) { throw new MBeanRegistrationException(e,"Cannot obtain MBeanInfo from Jolokia-Server for " + realName); } catch (ReflectionException e) { throw new MBeanRegistrationException(e,"Cannot obtain MBeanInfo from Jolokia-Server for " + realName); } } return ret; }
/** {@inheritDoc} */ public Object invoke(String pOperation, Object[] pParams, String[] pSignature) throws MBeanException, ReflectionException { OperationMapInfo opMapInfo = getOperationMapInfo(pOperation, pSignature); try { if (opMapInfo == null) { return jolokiaMBeanServer.invoke(objectName, pOperation, pParams, pSignature); } else { return mapAndInvoke(pOperation, pParams, pSignature, opMapInfo); } } catch (InstanceNotFoundException e) { // Should not happen, since the Jolokia MBeanServer and the delegate MBeanServer this bean is registered // at are in sync. throw new IllegalStateException("Internal: Could find MBean " + objectName + " on Jolokia MBeanServer. Should be in sync",e); } }
/** * Create a private MBean server */ public JolokiaMBeanServer() { MBeanServer mBeanServer = MBeanServerFactory.newMBeanServer(); delegatedMBeans = new HashSet<ObjectName>(); delegateServer = ManagementFactory.getPlatformMBeanServer(); converters = new Converters(); init(mBeanServer); }
private Object toJson(Object pValue) { return jolokiaMBeanServer.toJson(pValue, jsonConvertOptions); }
/** {@inheritDoc} */ public Object getAttribute(String pAttribute) throws AttributeNotFoundException, MBeanException, ReflectionException { try { if (!attributeInfoMap.containsKey(pAttribute)) { return jolokiaMBeanServer.getAttribute(objectName, pAttribute); } else { return toJson(jolokiaMBeanServer.getAttribute(objectName, pAttribute)); } } catch (InstanceNotFoundException e) { AttributeNotFoundException exp = new AttributeNotFoundException("MBean " + objectName + " not found for attribute " + pAttribute); exp.initCause(e); throw exp; } }
/** {@inheritDoc} */ public void setAttribute(Attribute pAttribute) throws AttributeNotFoundException, InvalidAttributeValueException, MBeanException, ReflectionException { try { if (!attributeInfoMap.containsKey(pAttribute.getName())) { jolokiaMBeanServer.setAttribute(objectName, pAttribute); } else { String name = pAttribute.getName(); MBeanAttributeInfo info = attributeInfoMap.get(name); Object value; if (info instanceof OpenMBeanAttributeInfo) { value = fromJson(((OpenMBeanAttributeInfo) info).getOpenType(), (String) pAttribute.getValue()); } else { value = fromJson(info.getType(), (String) pAttribute.getValue()); } Attribute attr = new Attribute(name, value); jolokiaMBeanServer.setAttribute(objectName, attr); } } catch (InstanceNotFoundException e) { AttributeNotFoundException exp = new AttributeNotFoundException("MBean " + objectName + " not found for attribute " + pAttribute); exp.initCause(e); throw exp; } }
@Override public ObjectInstance registerMBean(Object object, ObjectName name) throws InstanceAlreadyExistsException, MBeanRegistrationException, NotCompliantMBeanException { // Register MBean first on this MBean Server ObjectInstance ret = super.registerMBean(object, name); // Check, whether it is annotated with @JsonMBean. Only the outermost class of an inheritance is // considered. JsonMBean anno = extractJsonMBeanAnnotation(object); if (anno != null) { // The real name can be different than the given one in case the default // domain was omitted and/or the MBean implements MBeanRegistration ObjectName realName = ret.getObjectName(); try { // Fetch real MBeanInfo and create a dynamic MBean with modified signature MBeanInfo info = super.getMBeanInfo(realName); JsonDynamicMBeanImpl mbean = new JsonDynamicMBeanImpl(this,realName,info,getJsonConverterOptions(anno)); // Register MBean on delegate MBeanServer delegatedMBeans.add(realName); delegateServer.registerMBean(mbean,realName); } catch (InstanceNotFoundException e) { throw new MBeanRegistrationException(e,"Cannot obtain MBeanInfo from Jolokia-Server for " + realName); } catch (IntrospectionException e) { throw new MBeanRegistrationException(e,"Cannot obtain MBeanInfo from Jolokia-Server for " + realName); } catch (ReflectionException e) { throw new MBeanRegistrationException(e,"Cannot obtain MBeanInfo from Jolokia-Server for " + realName); } } return ret; }
private Object mapAndInvoke(String pOperation, Object[] pParams, String[] pSignature, OperationMapInfo pOpMapInfo) throws InstanceNotFoundException, MBeanException, ReflectionException { // Map parameters Object realParams[] = new Object[pSignature.length]; String realSignature[] = new String[pSignature.length]; for (int i = 0; i < pSignature.length; i++) { if (pOpMapInfo.isParamMapped(i)) { String origType = pOpMapInfo.getOriginalType(i); OpenType openType = pOpMapInfo.getOpenMBeanType(i); if (openType != null) { realParams[i] = fromJson(openType, (String) pParams[i]); } else { realParams[i] = fromJson(origType, (String) pParams[i]); } realSignature[i] = origType; } else { realParams[i] = pParams[i]; realSignature[i] = pSignature[i]; } } Object ret = jolokiaMBeanServer.invoke(objectName, pOperation, realParams, realSignature); return pOpMapInfo.isRetTypeMapped() ? toJson(ret) : ret; }
/** * Create a private MBean server */ public JolokiaMBeanServer() { MBeanServer mBeanServer = MBeanServerFactory.newMBeanServer(); delegatedMBeans = new HashSet<ObjectName>(); delegateServer = ManagementFactory.getPlatformMBeanServer(); converters = new Converters(); init(mBeanServer); }
private Object toJson(Object pValue) { return jolokiaMBeanServer.toJson(pValue, jsonConvertOptions); }
/** {@inheritDoc} */ public Object getAttribute(String pAttribute) throws AttributeNotFoundException, MBeanException, ReflectionException { try { if (!attributeInfoMap.containsKey(pAttribute)) { return jolokiaMBeanServer.getAttribute(objectName, pAttribute); } else { return toJson(jolokiaMBeanServer.getAttribute(objectName, pAttribute)); } } catch (InstanceNotFoundException e) { AttributeNotFoundException exp = new AttributeNotFoundException("MBean " + objectName + " not found for attribute " + pAttribute); exp.initCause(e); throw exp; } }
/** {@inheritDoc} */ public void setAttribute(Attribute pAttribute) throws AttributeNotFoundException, InvalidAttributeValueException, MBeanException, ReflectionException { try { if (!attributeInfoMap.containsKey(pAttribute.getName())) { jolokiaMBeanServer.setAttribute(objectName, pAttribute); } else { String name = pAttribute.getName(); MBeanAttributeInfo info = attributeInfoMap.get(name); Object value; if (info instanceof OpenMBeanAttributeInfo) { value = fromJson(((OpenMBeanAttributeInfo) info).getOpenType(), (String) pAttribute.getValue()); } else { value = fromJson(info.getType(), (String) pAttribute.getValue()); } Attribute attr = new Attribute(name, value); jolokiaMBeanServer.setAttribute(objectName, attr); } } catch (InstanceNotFoundException e) { AttributeNotFoundException exp = new AttributeNotFoundException("MBean " + objectName + " not found for attribute " + pAttribute); exp.initCause(e); throw exp; } }
private Object fromJson(String pType, String pValue) { return jolokiaMBeanServer.fromJson(pType, pValue); }
/** {@inheritDoc} */ public Object invoke(String pOperation, Object[] pParams, String[] pSignature) throws MBeanException, ReflectionException { OperationMapInfo opMapInfo = getOperationMapInfo(pOperation, pSignature); try { if (opMapInfo == null) { return jolokiaMBeanServer.invoke(objectName, pOperation, pParams, pSignature); } else { return mapAndInvoke(pOperation, pParams, pSignature, opMapInfo); } } catch (InstanceNotFoundException e) { // Should not happen, since the Jolokia MBeanServer and the delegate MBeanServer this bean is registered // at are in sync. throw new IllegalStateException("Internal: Could find MBean " + objectName + " on Jolokia MBeanServer. Should be in sync",e); } }
/** * Create a new holder */ public JolokiaMBeanServerHolder() { jolokiaMBeanServer = new JolokiaMBeanServer(); }
private JsonMBean extractJsonMBeanAnnotation(Object object) { // Try directly Class<?> clazz = object.getClass(); JsonMBean anno = clazz.getAnnotation(JsonMBean.class); if (anno == null && ModelMBean.class.isAssignableFrom(object.getClass())) { // For ModelMBean we try some heuristic to get to the managed resource // This works for all subclasses of RequiredModelMBean as provided by the JDK // but maybe for other ModelMBean classes as well Boolean isAccessible = null; Field field = null; try { field = findField(clazz, "managedResource"); if (field != null) { isAccessible = field.isAccessible(); field.setAccessible(true); Object managedResource = field.get(object); anno = managedResource.getClass().getAnnotation(JsonMBean.class); } } catch (IllegalAccessException e) { // Ignored silently, but we tried it at least } finally { if (isAccessible != null) { field.setAccessible(isAccessible); } } } return anno; }
private Object fromJson(String pType, String pValue) { return jolokiaMBeanServer.fromJson(pType, pValue); }