blob: 192a353e767e2274956c408f661a4e83c53399db [file] [log] [blame]
/*
*
* Copyright (c) 2011 - 2017 - Loetz GmbH & Co KG, 69115 Heidelberg, Germany
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Initial contribution:
* Loetz GmbH & Co. KG
*
*/
package org.eclipse.osbp.runtime.common.session;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.function.Function;
import java.util.function.Predicate;
import org.eclipse.e4.core.contexts.IEclipseContext;
import org.eclipse.osbp.runtime.common.session.ISessionManager.FutureResult;
/**
* And abstraction above a state. Eg. Vaadin UI.
*/
public interface ISession {
// do not access directly! Use #getCurrent()
public static ThreadLocal<ISession> current = new ThreadLocal<>();
public static ISession getCurrent() {
return current.get();
}
public static void makeCurrent(IEclipseContext context) {
ISession session = context.get(ISession.class);
session.makeDefault();
}
String HOSTNAME = "hostname";
String IS_SLAVE = "slave";
/**
* Registers this session instance in a ThreadLocal variable.
*/
void makeDefault();
/**
* Deregisters this session instance in a ThreadLocal variable.
*/
void unmakeDefault();
/**
* Returns the eclispe context.
*
* @return
*/
IEclipseContext getEclipseContext();
/**
* Returns the host address of the session.
*
* @return
*/
String getHost();
/**
* Executes the function in the scope of the session.
*
* @param function
* @return
*/
<T> T apply(Function<ISession, T> function);
/**
* Creates a {@link CompletableFuture} for the given function. This method
* must NOT block.
*
* @param function
* @param callback
* @return
*/
<T> CompletableFuture<FutureResult<T>> async(final Function<ISession, T> function, SessionCallback<T> callback);
/**
* Creates a {@link CompletableFuture} for the given function. This method
* must NOT block.
*
* @param function
* @param callback
* @return
*/
<T> CompletableFuture<FutureResult<T>> async(final Function<ISession, T> function, SessionCallback<T> callback,
ExecutorService executor);
/**
* Sends the given data to the session. Call must by synced.
*
* @param data
*/
void sendData(Map<String, Object> data);
/**
* Returns the property associated with the key.
*
* @param key
* @return
*/
<T> T get(Class<T> key);
/**
* Returns the property associated with the key.
*
* @param key
* @return
*/
Object get(String key);
void set(String key, Object object);
/**
* Returns true, if the session has slave sessions. Otherwise false.
*
* @return
*/
boolean isMasterSession();
/**
* Returns true, if the session is a slave session and has a master session.
* Otherwise false.
*
* @return
*/
boolean isSlaveSession();
/**
* Returns the type {@link Type} of this session.
*
* @return
*/
Type getType();
/**
* Returns the master session, if the session is a slave session.
* <code>Null</code> otherwise.
*
* @return
*/
ISession getMaster();
/**
* Returns an unmodifiable list of all slave sessions connected to this
* master. Returns never <code>null</code>.
*
* @return
*/
List<ISession> getSlaves();
/**
* Returns a list of slaves filtered by given filter.
*
* @param filter
* @return
*/
List<ISession> getSlaves(Predicate<ISession> filter);
/**
* Adds a session which is a slave. Slaves are used to "paint" on web UIs
* controlled by the master session.
*
* @param name
* @return
*/
void addSlave(ISession slave);
/**
* See {@link #addSlaveSession(ISession)}
*
* @param slave
*/
void removeSlave(ISession slave);
/**
* In case of slave session, the session MUST provide a fragment following
* the pattern: {master-host}{UI-to-display}
*
* @return
*/
String getFragment();
enum Type {
MASTER, SLAVE;
}
/**
* Sets the transaction handler.
*
* @param transaction the new transaction handler
*/
void setTransactionHandler(ITransactionHandler transaction);
/**
* Gets the transaction handler.
*
* @return the transaction handler
*/
ITransactionHandler getTransactionHandler();
/**
* Gets the vaadin UI.
*
* @return the ui
*/
Object getUI();
}