Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package me.shakiba.jsonrpc.server;
  
  import java.io.Reader;
  import java.util.HashMap;
 
 
Overloading is not supported. Only Maps of <String, ?> is supported.
 
 public class JsonRpcServer {
 
     public JsonRpcResponse serve(Object objectReader reader)
             throws IOException {
         return serve(objectnullreader);
     }
 
     public JsonRpcResponse serve(Object objectInputStream inString encoding)
             throws IOException {
         return serve(objectnullnew InputStreamReader(inencoding));
     }
 
     public JsonRpcResponse serve(Object objectString strthrows IOException {
         return serve(objectstrnull);
     }
 
     public JsonRpcResponse serve(Object objectString strReader reader)
             throws IOException {
 
         if (str == null && reader == null) {
             throw new NullPointerException();
         }
 
         JsonRpcTarget target = target(object.getClass());
         JsonRpcResponse res = new JsonRpcResponse();
         try {
             JsonRpcRequest req = target.fromJson(strreader);
             try {
 
                 res.setId(req.getId());
 
                 String method = req.getMethod();
                 Object params[] = req.getParams();
 
                 // int modifiers = m.getModifiers();
                 // static method? no param method?
                 Method javamethod = target.getMethod(method);
                 Object result = javamethod.invoke(objectparams);
                 res.setResult(result);
 
                 if (JsonRpcSyncable.class.isInstance(object)) {
                     try {
                         res.setSync(((JsonRpcSyncableobject).sync());
                     } catch (Throwable e) {
                         .warn(e + ""e);
                     }
                 }
 
             } catch (InvocationTargetException e) {
                 Throwable cause = e.getCause();
                 if (onException(reqrescause)) {
                     res.setError(.);
                 }
             }
         } catch (JsonRpcException e) {
             res.setId(e.getRequestId());
             res.setError(e.getError());
 
         } catch (JsonParseException e) {
             res.setError(.);
 
         } catch (Exception e) {
             .error(e + ""e);
             res.setError(.);
         }
 
         return res;
     }
 
     protected boolean onException(JsonRpcRequest reqJsonRpcResponse res,
             Throwable t) {
         return true;
     }
 
     private HashMap<Class<?>, JsonRpcTargettargets = new HashMap<Class<?>, JsonRpcTarget>();
 
     private synchronized JsonRpcTarget target(Class<?> c) {
         JsonRpcTarget server = .get(c);
         if (server == null) {
             server = new JsonRpcTarget(c);
             // if (targets.size() > 20) {
            // logger.error("More than 20 targets!");
            // targets.clear();
            // }
            .put(cserver);
        }
        return server;
    }
    private static Logger logger = LoggerFactory.getLogger(JsonRpcServer.class);
New to GrepCode? Check out our FAQ X