/** * Converter used by JsonMBean for converting from Object to JSON representation * * @param object object to serialize * @param pConvertOptions options used for conversion * @return serialized object */ String toJson(Object object, JsonConvertOptions pConvertOptions) { try { Object ret = converters.getToJsonConverter().convertToJson(object,null,pConvertOptions); return ret.toString(); } catch (AttributeNotFoundException exp) { // Cannot happen, since we dont use a path return ""; } }
/** * Create a private MBean server */ public JolokiaMBeanServer() { MBeanServer mBeanServer = MBeanServerFactory.newMBeanServer(); delegatedMBeans = new HashSet<ObjectName>(); delegateServer = ManagementFactory.getPlatformMBeanServer(); converters = new Converters(); init(mBeanServer); }
/** * Execute an JMX operation. The operation name is taken from the request, as well as the * arguments to use. If the operation is given in the format "op(type1,type2,...)" * (e.g "getText(java.lang.String,int)" then the argument types are taken into account * as well. This way, overloaded JMX operation can be used. If an overloaded JMX operation * is called without specifying the argument types, then an exception is raised. * * * @param server server to try * @param request request to process from where the operation and its arguments are extracted. * @return the return value of the operation call */ @Override public Object doHandleRequest(MBeanServerConnection server, JmxExecRequest request) throws InstanceNotFoundException, AttributeNotFoundException, ReflectionException, MBeanException, IOException { OperationAndParamType types = extractOperationTypes(server,request); int nrParams = types.paramClasses.length; Object[] params = new Object[nrParams]; List<Object> args = request.getArguments(); verifyArguments(request, types, nrParams, args); for (int i = 0;i < nrParams; i++) { if (types.paramOpenTypes != null && types.paramOpenTypes[i] != null) { params[i] = converters.getToOpenTypeConverter().convertToObject(types.paramOpenTypes[i], args.get(i)); } else { params[i] = converters.getToObjectConverter().prepareValue(types.paramClasses[i], args.get(i)); } } // TODO: Maybe allow for a path as well which could be applied on the return value ... return server.invoke(request.getObjectName(),types.operationName,params,types.paramClasses); }
converters.getToJsonConverter().setInnerValue(pCurrentValue, newValue, pathParts) }; converters.getToObjectConverter().prepareValue(pType, newValue), pCurrentValue };
/** * Convert from JSON for OpenType objects. Throws an {@link IllegalArgumentException} if * * @param type open type * @param json JSON representation to convert from * @return the converted object */ Object fromJson(OpenType type, String json) { return converters.getToOpenTypeConverter().convertToObject(type,json); }
/** * Convert from a JSON or other string representation to real object. Used when preparing operation * argument. If the JSON structure cannot be converted, an {@link IllegalArgumentException} is thrown. * * @param type type to convert to * @param json string to deserialize * @return the deserialized object */ Object fromJson(String type, String json) { return converters.getToObjectConverter().convertFromString(type,json); }
converters.getToJsonConverter().setInnerValue(pCurrentValue, newValue, pathParts) }; converters.getToObjectConverter().prepareValue(pType, newValue), pCurrentValue };
private Object[] getValues(OpenMBeanAttributeInfo pOpenTypeInfo, Object pCurrentValue, JmxWriteRequest pRequest) { // TODO: What to do when path is not null ? Simplest: Throw exception. Advanced: Extract other values and create // a new CompositeData with old values and the new value. // However, since this is probably out of scope, we will simply throw an exception if the path is not empty. List<String> pathParts = pRequest.getPathParts(); if (pathParts != null && pathParts.size() > 0) { throw new IllegalArgumentException("Cannot set value for OpenType " + pOpenTypeInfo.getOpenType() + " with inner path " + pRequest.getPath() + " since OpenTypes are immutable"); } return new Object[] { converters.getToOpenTypeConverter().convertToObject(pOpenTypeInfo.getOpenType(), pRequest.getValue()), pCurrentValue }; } }
/** * Convert from a JSON or other string representation to real object. Used when preparing operation * argument. If the JSON structure cannot be converted, an {@link IllegalArgumentException} is thrown. * * @param type type to convert to * @param json string to deserialize * @return the deserialized object */ Object fromJson(String type, String json) { return converters.getToObjectConverter().convertFromString(type,json); }
/** * Convert a Throwable to a JSON object so that it can be included in an error response * * @param pExp throwable to convert * @param pJmxReq the request from where to take the serialization options * @return the exception. */ public Object convertExceptionToJson(Throwable pExp, JmxRequest pJmxReq) { JsonConvertOptions opts = getJsonConvertOptions(pJmxReq); try { JSONObject expObj = (JSONObject) converters.getToJsonConverter().convertToJson(pExp,null,opts); return expObj; } catch (AttributeNotFoundException e) { // Cannot happen, since we dont use a path return null; } }
converters.getToJsonConverter().setInnerValue(pCurrentValue, newValue, pathParts) }; converters.getToObjectConverter().prepareValue(pType, newValue), pCurrentValue };
/** * Execute an JMX operation. The operation name is taken from the request, as well as the * arguments to use. If the operation is given in the format "op(type1,type2,...)" * (e.g "getText(java.lang.String,int)" then the argument types are taken into account * as well. This way, overloaded JMX operation can be used. If an overloaded JMX operation * is called without specifying the argument types, then an exception is raised. * * * @param server server to try * @param request request to process from where the operation and its arguments are extracted. * @return the return value of the operation call */ @Override public Object doHandleRequest(MBeanServerConnection server, JmxExecRequest request) throws InstanceNotFoundException, AttributeNotFoundException, ReflectionException, MBeanException, IOException { OperationAndParamType types = extractOperationTypes(server,request); int nrParams = types.paramClasses.length; Object[] params = new Object[nrParams]; List<Object> args = request.getArguments(); verifyArguments(request, types, nrParams, args); for (int i = 0;i < nrParams; i++) { if (types.paramOpenTypes != null && types.paramOpenTypes[i] != null) { params[i] = converters.getToOpenTypeConverter().convertToObject(types.paramOpenTypes[i], args.get(i)); } else { params[i] = converters.getToObjectConverter().prepareValue(types.paramClasses[i], args.get(i)); } } // TODO: Maybe allow for a path as well which could be applied on the return value ... return server.invoke(request.getObjectName(),types.operationName,params,types.paramClasses); }
/** * Convert from JSON for OpenType objects. Throws an {@link IllegalArgumentException} if * * @param type open type * @param json JSON representation to convert from * @return the converted object */ Object fromJson(OpenType type, String json) { return converters.getToOpenTypeConverter().convertToObject(type,json); }
private void init(Configuration pConfig) { // Central objects converters = new Converters(); initLimits(pConfig); // Create and remember request dispatchers localDispatcher = new LocalRequestDispatcher(converters, restrictor, pConfig, logHandler); ServerHandle serverHandle = localDispatcher.getServerHandle(); requestDispatchers = createRequestDispatchers(pConfig, converters,serverHandle,restrictor); requestDispatchers.add(localDispatcher); // Backendstore for remembering agent state initMBeans(pConfig); agentDetails.setServerInfo(serverHandle.getVendor(),serverHandle.getProduct(),serverHandle.getVersion()); }
private JSONObject callRequestDispatcher(JmxRequest pJmxReq) throws InstanceNotFoundException, AttributeNotFoundException, ReflectionException, MBeanException, IOException, NotChangedException { Object retValue = null; boolean useValueWithPath = false; boolean found = false; for (RequestDispatcher dispatcher : requestDispatchers) { if (dispatcher.canHandle(pJmxReq)) { retValue = dispatcher.dispatchRequest(pJmxReq); useValueWithPath = dispatcher.useReturnValueWithPath(pJmxReq); found = true; break; } } if (!found) { throw new IllegalStateException("Internal error: No dispatcher found for handling " + pJmxReq); } JsonConvertOptions opts = getJsonConvertOptions(pJmxReq); Object jsonResult = converters.getToJsonConverter() .convertToJson(retValue, useValueWithPath ? pJmxReq.getPathParts() : null, opts); JSONObject jsonObject = new JSONObject(); jsonObject.put("value",jsonResult); jsonObject.put("request",pJmxReq.toJSON()); return jsonObject; }
/** * Execute an JMX operation. The operation name is taken from the request, as well as the * arguments to use. If the operation is given in the format "op(type1,type2,...)" * (e.g "getText(java.lang.String,int)" then the argument types are taken into account * as well. This way, overloaded JMX operation can be used. If an overloaded JMX operation * is called without specifying the argument types, then an exception is raised. * * * @param server server to try * @param request request to process from where the operation and its arguments are extracted. * @return the return value of the operation call */ @Override public Object doHandleRequest(MBeanServerConnection server, JmxExecRequest request) throws InstanceNotFoundException, AttributeNotFoundException, ReflectionException, MBeanException, IOException { OperationAndParamType types = extractOperationTypes(server,request); int nrParams = types.paramClasses.length; Object[] params = new Object[nrParams]; List<Object> args = request.getArguments(); verifyArguments(request, types, nrParams, args); for (int i = 0;i < nrParams; i++) { if (types.paramOpenTypes != null && types.paramOpenTypes[i] != null) { params[i] = converters.getToOpenTypeConverter().convertToObject(types.paramOpenTypes[i], args.get(i)); } else { params[i] = converters.getToObjectConverter().prepareValue(types.paramClasses[i], args.get(i)); } } // TODO: Maybe allow for a path as well which could be applied on the return value ... return server.invoke(request.getObjectName(),types.operationName,params,types.paramClasses); }
public static Object toOpenData(OpenType<?> otype, ClassLoader classLoader, Object value) throws OpenDataException { Object result; if (otype instanceof CompositeType && value instanceof JSONObject) { StringToOpenTypeConverter converter = converters.getToOpenTypeConverter(); result = converter.convertToObject(otype, value); } else if (otype instanceof TabularType && value instanceof JSONObject) { StringToOpenTypeConverter converter = converters.getToOpenTypeConverter(); result = converter.convertToObject(otype, value); } else if (otype instanceof ArrayType && value instanceof JSONArray) { StringToOpenTypeConverter converter = converters.getToOpenTypeConverter(); result = converter.convertToObject(otype, value); } else { result = OpenTypeGenerator.toOpenData(otype, value); } boolean isAssignable = result == null || otype.isValue(result); IllegalStateAssertion.assertTrue(isAssignable, "Value " + result + " is not a value of: " + otype); return result; } }
import org.junit.Assert; import org.junit.Test; public class ConvertersTest { @Test public void testInteger() { Converters converters = new Converters(); Assert.assertTrue(converters.isValueOfType("123", "java.lang.Integer")); Assert.assertFalse(converters.isValueOfType("ABC", "java.lang.Integer")); } @Test public void testString() { Converters converters = new Converters(); Assert.assertTrue(converters.isValueOfType("123", "java.lang.String")); Assert.assertTrue(converters.isValueOfType("ABC", "java.lang.String")); } }
/** * Converter used by JsonMBean for converting from Object to JSON representation * * @param object object to serialize * @param pConvertOptions options used for conversion * @return serialized object */ String toJson(Object object, JsonConvertOptions pConvertOptions) { try { Object ret = converters.getToJsonConverter().convertToJson(object,null,pConvertOptions); return ret.toString(); } catch (AttributeNotFoundException exp) { // Cannot happen, since we dont use a path return ""; } }
public static Object toOpenData(OpenType<?> otype, ClassLoader classLoader, Object value) throws OpenDataException { Object result; if (otype instanceof CompositeType && value instanceof JSONObject) { StringToOpenTypeConverter converter = converters.getToOpenTypeConverter(); result = converter.convertToObject(otype, value); } else if (otype instanceof TabularType && value instanceof JSONObject) { StringToOpenTypeConverter converter = converters.getToOpenTypeConverter(); result = converter.convertToObject(otype, value); } else if (otype instanceof ArrayType && value instanceof JSONArray) { StringToOpenTypeConverter converter = converters.getToOpenTypeConverter(); result = converter.convertToObject(otype, value); } else { result = OpenTypeGenerator.toOpenData(otype, value); } boolean isAssignable = result == null || otype.isValue(result); IllegalStateAssertion.assertTrue(isAssignable, "Value " + result + " is not a value of: " + otype); return result; } }