Search in sources :

Example 1 with ClassData

use of org.jabsorb.reflect.ClassData in project wonder-slim by undur.

the class JSONRPCBridge method allInstanceMethods.

/**
 * Add all instance methods that can be invoked on this bridge to a HashSet.
 *
 * @param m HashSet to add all static methods to.
 */
private void allInstanceMethods(HashSet m) {
    synchronized (state) {
        HashMap objectMap = state.getObjectMap();
        Iterator i = objectMap.entrySet().iterator();
        while (i.hasNext()) {
            Map.Entry oientry = (Map.Entry) i.next();
            Object key = oientry.getKey();
            if (!(key instanceof String)) {
                continue;
            }
            String name = (String) key;
            ObjectInstance oi = (ObjectInstance) oientry.getValue();
            ClassData cd = ClassAnalyzer.getClassData(oi.clazz);
            uniqueMethods(m, name + ".", cd.getMethodMap());
            uniqueMethods(m, name + ".", cd.getStaticMethodMap());
        }
    }
}
Also used : ClassData(org.jabsorb.reflect.ClassData) HashMap(java.util.HashMap) Iterator(java.util.Iterator) JSONObject(org.json.JSONObject) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with ClassData

use of org.jabsorb.reflect.ClassData in project wonder-slim by undur.

the class JSONRPCBridge method call.

/**
 * Call a method using a JSON-RPC request object.
 *
 * @param context The transport context (the HttpServletRequest object in the
 *          case of the HTTP transport).
 * @param jsonReq The JSON-RPC request structured as a JSON object tree.
 * @return a JSONRPCResult object with the result of the invocation or an
 *         error.
 */
public JSONRPCResult call(Object[] context, JSONObject jsonReq) {
    String encodedMethod;
    Object requestId;
    JSONArray arguments;
    JSONArray fixups;
    try {
        // Get method name, arguments and request id
        encodedMethod = jsonReq.getString("method");
        arguments = jsonReq.getJSONArray("params");
        requestId = jsonReq.opt("id");
        fixups = jsonReq.optJSONArray("fixups");
    } catch (JSONException e) {
        log.error("no method or parameters in request");
        return new JSONRPCResult(JSONRPCResult.CODE_ERR_NOMETHOD, null, JSONRPCResult.MSG_ERR_NOMETHOD);
    }
    if (log.isDebugEnabled()) {
        if (fixups != null) {
            log.debug("call " + encodedMethod + "(" + arguments + ")" + ", requestId=" + requestId);
        } else {
            log.debug("call " + encodedMethod + "(" + arguments + ")" + ", fixups=" + fixups + ", requestId=" + requestId);
        }
    }
    if (fixups != null) {
        try {
            for (int i = 0; i < fixups.length(); i++) {
                JSONArray assignment = fixups.getJSONArray(i);
                JSONArray fixup = assignment.getJSONArray(0);
                JSONArray original = assignment.getJSONArray(1);
                applyFixup(arguments, fixup, original);
            }
        } catch (JSONException e) {
            log.error("error applying fixups", e);
            return new JSONRPCResult(JSONRPCResult.CODE_ERR_FIXUP, requestId, JSONRPCResult.MSG_ERR_FIXUP + ": " + e.getMessage());
        }
    }
    String className = null;
    String methodName = null;
    int objectID = 0;
    // Parse the class and methodName
    StringTokenizer t = new StringTokenizer(encodedMethod, ".");
    if (t.hasMoreElements()) {
        className = t.nextToken();
    }
    if (t.hasMoreElements()) {
        methodName = t.nextToken();
    }
    // See if we have an object method in the format ".obj#<objectID>"
    if (encodedMethod.startsWith(".obj#")) {
        t = new StringTokenizer(className, "#");
        t.nextToken();
        objectID = Integer.parseInt(t.nextToken());
    }
    // one of oi or cd will resolve (first oi is attempted, and if that fails,
    // then cd is attempted)
    // object instance of object being invoked
    ObjectInstance oi = null;
    // ClassData for resolved object instance, or if object instance cannot
    // resolve, class data for
    // class instance (static method) we are resolving to
    ClassData cd = null;
    HashMap methodMap = null;
    Method method = null;
    Object itsThis = null;
    if (objectID == 0) {
        // when a new JSONRpcClient object is initialized.
        if (encodedMethod.equals("system.listMethods")) {
            HashSet m = new HashSet();
            globalBridge.allInstanceMethods(m);
            if (globalBridge != this) {
                globalBridge.allStaticMethods(m);
                globalBridge.allInstanceMethods(m);
            }
            allStaticMethods(m);
            allInstanceMethods(m);
            JSONArray methods = new JSONArray();
            Iterator i = m.iterator();
            while (i.hasNext()) {
                methods.put(i.next());
            }
            return new JSONRPCResult(JSONRPCResult.CODE_SUCCESS, requestId, methods);
        }
        // Look up the class, object instance and method objects
        if (className == null || methodName == null || ((oi = resolveObject(className)) == null && (cd = resolveClass(className)) == null)) {
            return new JSONRPCResult(JSONRPCResult.CODE_ERR_NOMETHOD, requestId, JSONRPCResult.MSG_ERR_NOMETHOD);
        }
        if (oi != null) {
            itsThis = oi.o;
            cd = ClassAnalyzer.getClassData(oi.clazz);
            methodMap = cd.getMethodMap();
        } else {
            if (cd != null) {
                methodMap = cd.getStaticMethodMap();
            }
        }
    } else {
        if ((oi = resolveObject(Integer.valueOf(objectID))) == null) {
            return new JSONRPCResult(JSONRPCResult.CODE_ERR_NOMETHOD, requestId, JSONRPCResult.MSG_ERR_NOMETHOD);
        }
        itsThis = oi.o;
        cd = ClassAnalyzer.getClassData(oi.clazz);
        methodMap = cd.getMethodMap();
        if (methodName != null && methodName.equals("listMethods")) {
            HashSet m = new HashSet();
            uniqueMethods(m, "", cd.getStaticMethodMap());
            uniqueMethods(m, "", cd.getMethodMap());
            JSONArray methods = new JSONArray();
            Iterator i = m.iterator();
            while (i.hasNext()) {
                methods.put(i.next());
            }
            return new JSONRPCResult(JSONRPCResult.CODE_SUCCESS, requestId, methods);
        }
    }
    // Find the specific method
    if ((method = resolveMethod(methodMap, methodName, arguments)) == null) {
        return new JSONRPCResult(JSONRPCResult.CODE_ERR_NOMETHOD, requestId, JSONRPCResult.MSG_ERR_NOMETHOD);
    }
    JSONRPCResult result;
    // Call the method
    try {
        if (log.isDebugEnabled()) {
            log.debug("invoking " + method.getReturnType().getName() + " " + method.getName() + "(" + argSignature(method) + ")");
        }
        // Unmarshall arguments
        Object[] javaArgs = unmarshallArgs(context, method, arguments);
        // Call pre invoke callbacks
        if (cbc != null) {
            for (int i = 0; i < context.length; i++) {
                cbc.preInvokeCallback(context[i], itsThis, method, javaArgs);
            }
        }
        // Invoke the method
        Object returnObj = method.invoke(itsThis, javaArgs);
        // Call post invoke callbacks
        if (cbc != null) {
            for (int i = 0; i < context.length; i++) {
                cbc.postInvokeCallback(context[i], itsThis, method, returnObj);
            }
        }
        // Marshall the result
        SerializerState serializerState = new SerializerState();
        Object json = ser.marshall(serializerState, null, returnObj, "r");
        result = new JSONRPCResult(JSONRPCResult.CODE_SUCCESS, requestId, json, serializerState.getFixUps());
    // Handle exceptions creating exception results and
    // calling error callbacks
    } catch (UnmarshallException e) {
        if (cbc != null) {
            for (int i = 0; i < context.length; i++) {
                cbc.errorCallback(context[i], itsThis, method, e);
            }
        }
        log.error("exception occured", e);
        result = new JSONRPCResult(JSONRPCResult.CODE_ERR_UNMARSHALL, requestId, e.getMessage());
    } catch (MarshallException e) {
        if (cbc != null) {
            for (int i = 0; i < context.length; i++) {
                cbc.errorCallback(context[i], itsThis, method, e);
            }
        }
        log.error("exception occured", e);
        result = new JSONRPCResult(JSONRPCResult.CODE_ERR_MARSHALL, requestId, e.getMessage());
    } catch (Throwable e) {
        if (e instanceof InvocationTargetException) {
            e = ((InvocationTargetException) e).getTargetException();
        }
        if (cbc != null) {
            for (int i = 0; i < context.length; i++) {
                cbc.errorCallback(context[i], itsThis, method, e);
            }
        }
        log.error("exception occured", e);
        result = new JSONRPCResult(JSONRPCResult.CODE_REMOTE_EXCEPTION, requestId, exceptionTransformer.transform(e));
    }
    // Return the results
    return result;
}
Also used : HashMap(java.util.HashMap) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) StringTokenizer(java.util.StringTokenizer) ClassData(org.jabsorb.reflect.ClassData) MarshallException(org.jabsorb.serializer.MarshallException) Iterator(java.util.Iterator) JSONObject(org.json.JSONObject) SerializerState(org.jabsorb.serializer.SerializerState) UnmarshallException(org.jabsorb.serializer.UnmarshallException) HashSet(java.util.HashSet)

Example 3 with ClassData

use of org.jabsorb.reflect.ClassData in project wonder-slim by undur.

the class JSONRPCBridge method allStaticMethods.

/**
 * Add all static methods that can be invoked on this bridge to the given
 * HashSet.
 *
 * @param m HashSet to add all static methods to.
 */
private void allStaticMethods(HashSet m) {
    synchronized (state) {
        HashMap classMap = state.getClassMap();
        Iterator i = classMap.entrySet().iterator();
        while (i.hasNext()) {
            Map.Entry cdentry = (Map.Entry) i.next();
            String name = (String) cdentry.getKey();
            Class clazz = (Class) cdentry.getValue();
            ClassData cd = ClassAnalyzer.getClassData(clazz);
            uniqueMethods(m, name + ".", cd.getStaticMethodMap());
        }
    }
}
Also used : ClassData(org.jabsorb.reflect.ClassData) HashMap(java.util.HashMap) Iterator(java.util.Iterator) HashMap(java.util.HashMap) Map(java.util.Map)

Example 4 with ClassData

use of org.jabsorb.reflect.ClassData in project wonder-slim by undur.

the class JSONRPCBridge method resolveClass.

/**
 * Resolves a string to a class
 *
 * @param className The name of the class to resolve
 * @return The data associated with the className
 */
private ClassData resolveClass(String className) {
    Class clazz;
    ClassData cd = null;
    synchronized (state) {
        HashMap classMap = state.getClassMap();
        clazz = (Class) classMap.get(className);
    }
    if (clazz != null) {
        cd = ClassAnalyzer.getClassData(clazz);
    }
    if (cd != null) {
        if (log.isDebugEnabled()) {
            log.debug("found class " + cd.getClazz().getName() + " named " + className);
        }
        return cd;
    }
    if (this != globalBridge) {
        return globalBridge.resolveClass(className);
    }
    return null;
}
Also used : ClassData(org.jabsorb.reflect.ClassData) HashMap(java.util.HashMap)

Aggregations

HashMap (java.util.HashMap)4 ClassData (org.jabsorb.reflect.ClassData)4 Iterator (java.util.Iterator)3 Map (java.util.Map)2 JSONObject (org.json.JSONObject)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 HashSet (java.util.HashSet)1 StringTokenizer (java.util.StringTokenizer)1 MarshallException (org.jabsorb.serializer.MarshallException)1 SerializerState (org.jabsorb.serializer.SerializerState)1 UnmarshallException (org.jabsorb.serializer.UnmarshallException)1 JSONArray (org.json.JSONArray)1 JSONException (org.json.JSONException)1