001package org.consensusj.jsonrpc.introspection;
002
003import org.consensusj.jsonrpc.AsyncSupport;
004
005import java.lang.reflect.Method;
006import java.util.Map;
007import java.util.concurrent.CompletableFuture;
008
009/**
010 * GraalVM-compatible implementation of {@link JsonRpcServiceWrapper} that takes a map of {@link Method}s in the constructor
011 * so that introspection can be done at static initialization time (which means during native image generation).
012 * Since {@link #getServiceObject()} returns {@code this} you typically directly subclass {@code AbstractJsonRpcService}.
013 * For example see {@link org.consensusj.jsonrpc.introspection.sample.MathService}.
014 */
015public abstract class AbstractJsonRpcService implements JsonRpcServiceWrapper {
016    protected final Map<String, Method> methods;
017
018    public AbstractJsonRpcService(Map<String, Method> methods) {
019        this.methods = methods;
020    }
021
022    @Override
023    public Object getServiceObject() {
024        return this;
025    }
026
027    @Override
028    public Method getMethod(String methodName) {
029        return methods.get(methodName);
030    }
031
032    protected <RSLT> CompletableFuture<RSLT> result(RSLT result) {
033        return CompletableFuture.completedFuture(result);
034    }
035
036    protected <RSLT> CompletableFuture<RSLT> exception(Throwable exception) {
037        return CompletableFuture.failedFuture(exception);
038    }
039}