blob: 66bae41e9a681db6d0611a1013f8c390cd9b5a49 [file] [log] [blame]
package org.eclipse.osbp.runtime.common.session;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.function.Function;
import org.eclipse.osbp.runtime.common.session.ISessionManager.FutureResult;
/**
* A base implementation of the session using the SessionUnitOfWork to provide
* session scope.
*/
public abstract class AbstractSession implements ISession {
private Type sessionType = Type.MASTER;
@Override
public <T> T apply(Function<ISession, T> function) {
if (function == null) {
throw new NullPointerException("Function is null!");
}
return function.apply(this);
}
@Override
public <T> CompletableFuture<FutureResult<T>> async(final Function<ISession, T> function,
SessionCallback<T> callback) {
return async(function, callback, null);
}
@Override
public <T> CompletableFuture<FutureResult<T>> async(Function<ISession, T> function, SessionCallback<T> callback,
ExecutorService executor) {
CompletableFuture<T> promise = doAsync(function, executor);
CompletableFuture<FutureResult<T>> transformer = promise.handle((t, e) -> {
return new FutureResult<T>(this, t, e);
});
CompletableFuture<FutureResult<T>> notifier = transformer.whenComplete((r, t) -> {
if (callback == null) {
return;
}
callback.accept(r);
});
return notifier;
}
/**
* The implementation needs to synchronize the runnable with the underlying
* frameworks. For instance Vaadin UI. This method MUST NOT block.
*
* @param function
* @param executor
*/
protected abstract <T> CompletableFuture<T> doAsync(final Function<ISession, T> function, ExecutorService executor);
@Override
public void sendData(Map<String, Object> data) {
// override if used
}
public Type getType() {
return sessionType;
}
public void setType(Type sessionType) {
this.sessionType = sessionType;
}
}