All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.enodeframework.commanding.impl.CommandHandlerProxy Maven / Gradle / Ivy

There is a newer version: 1.1.10
Show newest version
package org.enodeframework.commanding.impl;

import org.enodeframework.commanding.ICommand;
import org.enodeframework.commanding.ICommandContext;
import org.enodeframework.commanding.ICommandHandlerProxy;
import org.enodeframework.common.container.ObjectContainer;

import java.lang.invoke.MethodHandle;
import java.lang.reflect.Method;
import java.util.concurrent.CompletableFuture;

/**
 * @author [email protected]
 */
public class CommandHandlerProxy implements ICommandHandlerProxy {

    private Class handlerType;
    private Object commandHandler;
    private MethodHandle methodHandle;
    private Method method;

    @Override
    public CompletableFuture handleAsync(ICommandContext context, ICommand command) {
        CompletableFuture future = new CompletableFuture<>();
        try {
            Object result = methodHandle.invoke(getInnerObject(), context, command);
            if (result instanceof CompletableFuture) {
                return (CompletableFuture) result;
            }
            future.complete(null);
        } catch (Throwable throwable) {
            future.completeExceptionally(throwable);
        }
        return future;
    }

    @Override
    public Object getInnerObject() {
        if (commandHandler != null) {
            return commandHandler;
        }
        commandHandler = ObjectContainer.INSTANCE.resolve(handlerType);
        return commandHandler;
    }

    @Override
    public void setHandlerType(Class handlerType) {
        this.handlerType = handlerType;
    }

    @Override
    public void setMethodHandle(MethodHandle methodHandle) {
        this.methodHandle = methodHandle;
    }

    @Override
    public Method getMethod() {
        return method;
    }

    @Override
    public void setMethod(Method method) {
        this.method = method;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy