/** * Get the Jolokia MBeanServer. This call is delegated to the corresponding * static utility method and will register a new MBeanServer if not already * present * * @return the Jolokia MBeanServer */ public MBeanServer getObject() { return JolokiaMBeanServerUtil.getJolokiaMBeanServer(); }
/** * Create a new holder */ public JolokiaMBeanServerHolder() { jolokiaMBeanServer = new JolokiaMBeanServer(); }
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; }
private JsonConvertOptions getJsonConverterOptions(JsonMBean pAnno) { // Extract conversion options from the annotation if (pAnno == null) { return JsonConvertOptions.DEFAULT; } else { ValueFaultHandler faultHandler = pAnno.faultHandling() == JsonMBean.FaultHandler.IGNORE_ERRORS ? ValueFaultHandler.IGNORING_VALUE_FAULT_HANDLER : ValueFaultHandler.THROWING_VALUE_FAULT_HANDLER; return new JsonConvertOptions.Builder() .maxCollectionSize(pAnno.maxCollectionSize()) .maxDepth(pAnno.maxDepth()) .maxObjects(pAnno.maxObjects()) .faultHandler(faultHandler) .build(); } }
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; }
/** {@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); } }
static MBeanServer registerJolokiaMBeanServerHolderMBean(MBeanServer pServer) { JolokiaMBeanServerHolder holder = new JolokiaMBeanServerHolder(); ObjectName holderName = createObjectName(JolokiaMBeanServerHolderMBean.OBJECT_NAME); MBeanServer jolokiaMBeanServer; try { pServer.registerMBean(holder,holderName); jolokiaMBeanServer = holder.getJolokiaMBeanServer(); } catch (InstanceAlreadyExistsException e) { // If the instance already exist, we look it up and fetch the MBeanServerHolder from there. // Might happen in race conditions. try { jolokiaMBeanServer = (MBeanServer) pServer.getAttribute(holderName,JOLOKIA_MBEAN_SERVER_ATTRIBUTE); } catch (JMException e1) { throw new IllegalStateException("Internal: Cannot get JolokiaMBean server in fallback JMX lookup " + "while trying to register the holder MBean: " + e1,e1); } } catch (JMException e) { throw new IllegalStateException("Internal: JolokiaMBeanHolder cannot be registered to JMX: " + e,e); } return jolokiaMBeanServer; }
/** {@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 AttributeList setAttributes(AttributeList attributes) { final AttributeList ret = new AttributeList(attributes.size()); for (Object o : attributes) { Attribute attr = (Attribute) o; try { setAttribute(attr); ret.add(new Attribute(attr.getName(), getAttribute(attr.getName()))); } catch (Exception e) { // Attribute is not included in returned list. The spec says so } } return ret; }
private MBeanInfo getWrappedInfo(MBeanInfo pMBeanInfo) { MBeanAttributeInfo[] attrInfo = getWrappedAttributeInfo(pMBeanInfo); MBeanOperationInfo[] opInfo = getWrappedOperationInfo(pMBeanInfo); return new MBeanInfo(pMBeanInfo.getClassName(), pMBeanInfo.getDescription(), attrInfo, null, /* We dont allow construction of this MBean, hence null-constructors */ opInfo, pMBeanInfo.getNotifications() ); }
/** * @author roland * @since 27.01.13 */ @JsonMBean public class JsonChecking implements JsonCheckingMBean { ComplexTestData data; public JsonChecking() { data = new ComplexTestData(); } public ComplexTestData getData() { return data; } public ComplexTestData execute(String[] args) { ComplexTestData data = new ComplexTestData(); data.setStringArray(args); return data; } }
private Object toJson(Object pValue) { return jolokiaMBeanServer.toJson(pValue, jsonConvertOptions); }
@Override public void unregisterMBean(ObjectName name) throws InstanceNotFoundException, MBeanRegistrationException { super.unregisterMBean(name); if (delegatedMBeans.contains(name)) { delegatedMBeans.remove(name); delegateServer.unregisterMBean(name); } }
/** * Construct a DynamicMBean wrapping an original MBean object. For attributes * and operations all non-trivial data types are translated into Strings * for a JSON representation * * @param pJolokiaMBeanServer the hosting Jolokia MBean Server * @param pObjectName object name of this MBean * @param pInfo the original MBeanInfo * @param pConvertOptions options used for converting return values to JSON */ public JsonDynamicMBeanImpl(JolokiaMBeanServer pJolokiaMBeanServer, ObjectName pObjectName, MBeanInfo pInfo, JsonConvertOptions pConvertOptions) { jolokiaMBeanServer = pJolokiaMBeanServer; objectName = pObjectName; jsonConvertOptions = pConvertOptions != null ? pConvertOptions : JsonConvertOptions.DEFAULT; attributeInfoMap = new HashMap<String, MBeanAttributeInfo>(); operationInfoMap = new HashMap<String, List<OperationMapInfo>>(); wrappedMBeanInfo = getWrappedInfo(pInfo); }
/** {@inheritDoc} */ public AttributeList getAttributes(String[] attributes /* cannot be null */) { final AttributeList ret = new AttributeList(attributes.length); for (String attrName : attributes) { try { final Object attrValue = getAttribute(attrName); ret.add(new Attribute(attrName, attrValue)); } catch (Exception e) { // Ignore this attribute. As specified in the JMX Spec } } return ret; }
/** * Unregister an MBean at the JolokiaMBeanServer. This call is directly delegated * to the JolokiaMBeanServer * * @param name objectname of the MBean to unregister * @throws InstanceNotFoundException * @throws MBeanRegistrationException */ public static void unregisterMBean(ObjectName name) throws InstanceNotFoundException, MBeanRegistrationException { getJolokiaMBeanServer().unregisterMBean(name); }
private Object fromJson(String pType, String pValue) { return jolokiaMBeanServer.fromJson(pType, pValue); }
/** * @author roland * @since 27.01.13 */ @JsonMBean public class JsonChecking2 implements JsonChecking2MXBean { ComplexTestData data; public JsonChecking2() { data = new ComplexTestData(); } public ComplexTestData getData() { return data; } public ComplexTestData execute(String[] args) { ComplexTestData data = new ComplexTestData(); data.setStringArray(args); return data; } }
/** * Register an MBean at the JolokiaMBeanServer. This call is directly delegated * to the JolokiaMBeanServer * * @param object object to register * @param name object name under which to register the MBean * @return the object instance created * @throws InstanceAlreadyExistsException * @throws MBeanRegistrationException * @throws NotCompliantMBeanException */ public static ObjectInstance registerMBean(Object object, ObjectName name) throws InstanceAlreadyExistsException, MBeanRegistrationException, NotCompliantMBeanException { return getJolokiaMBeanServer().registerMBean(object, name); }