@POST
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
    @Path("{key}/{op}")
    public Response invokeMbeanOperation(
            MultivaluedMap<String, String> formParams,
            @PathParam("key") String key, @QueryParam("jsonp") String jsonp,
            @PathParam("op") String name) throws Exception {

        LOG.info("invoke " + key + " op=" + name);
        MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
        Map<String, String> params = new TreeMap<String, String>();
        for (Entry<String, List<String>> entry : formParams.entrySet()) {
            if (entry.getKey().equals("op"))
                continue;
            if (entry.getValue().size() > 0)
                params.put(entry.getKey(), entry.getValue().get(0));
            else
                params.put(entry.getKey(), "");
        }

        ObjectName objName = new ObjectName(key);
        MBeanInfo info = mBeanServer.getMBeanInfo(objName);
        for (MBeanOperationInfo op : info.getOperations()) {
            if (op.getName().equals(name)) {
                List<String> signature = new ArrayList<String>();
                for (MBeanParameterInfo s : op.getSignature()) {
                    signature.add(s.getType());
                }
                Object result = mBeanServer.invoke(objName, name, params
                        .values().toArray(new String[params.size()]), signature
                        .toArray(new String[signature.size()]));
                JSONObject json = new JSONObject();
                json.put("key", key);
                json.put("operation", name);
                if (result != null) {
                    json.put("response", result.toString());
                }
                json.put("type", op.getReturnType());

                StringWriter out = new StringWriter();
                if (jsonp.isEmpty()) {
                    json.write(out);
                } else {
                    out.append(jsonp).append("(");
                    json.write(out);
                    out.append(");");
                }
                return Response.ok(out.toString())
                        .type(MediaType.APPLICATION_JSON).build();
            }
        }

        return Response.serverError().build();
    }
@ POST @ Consumes ( MediaType . APPLICATION_FORM_URLENCODED ) @ Path ( "{key}/{op}" ) public Response invokeMbeanOperation ( MultivaluedMap < String , String > formParams , @ PathParam ( "key" ) String key , @ QueryParam ( "jsonp" ) String jsonp , @ PathParam ( "op" ) String name ) throws Exception { LOG . info ( "invoke " + key + " op=" + name ) ; MBeanServer mBeanServer = ManagementFactory . getPlatformMBeanServer ( ) ; Map < String , String > params = new TreeMap < String , String > ( ) ; for ( Entry < String , List < String > > entry : formParams . entrySet ( ) ) { if ( entry . getKey ( ) . equals ( "op" ) ) continue ; if ( entry . getValue ( ) . size ( ) > 0 ) params . put ( entry . getKey ( ) , entry . getValue ( ) . get ( 0 ) ) ; else params . put ( entry . getKey ( ) , "" ) ; } ObjectName objName = new ObjectName ( key ) ; MBeanInfo info = mBeanServer . getMBeanInfo ( objName ) ; for ( MBeanOperationInfo op : info . getOperations ( ) ) { if ( op . getName ( ) . equals ( name ) ) { List < String > signature = new ArrayList < String > ( ) ; for ( MBeanParameterInfo s : op . getSignature ( ) ) { signature . add ( s . getType ( ) ) ; } Object result = mBeanServer . invoke ( objName , name , params . values ( ) . toArray ( new String [ params . size ( ) ] ) , signature . toArray ( new String [ signature . size ( ) ] ) ) ; JSONObject json = new JSONObject ( ) ; json . put ( "key" , key ) ; json . put ( "operation" , name ) ; if ( result != null ) { json . put ( "response" , result . toString ( ) ) ; } json . put ( "type" , op . getReturnType ( ) ) ; StringWriter out = new StringWriter ( ) ; if ( jsonp . isEmpty ( ) ) { json . write ( out ) ; } else { out . append ( jsonp ) . append ( "(" ) ; json . write ( out ) ; out . append ( ");" ) ; } return Response . ok ( out . toString ( ) ) . type ( MediaType . APPLICATION_JSON ) . build ( ) ; } } return Response . serverError ( ) . build ( ) ; }
Execute an operation on an mbean.

@param formParams
@param key
@param jsonp
@param name