Java Code Examples for com.vaadin.flow.server.VaadinSession#getCurrent()

The following examples show how to use com.vaadin.flow.server.VaadinSession#getCurrent() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ElementAttributeMap.java    From flow with Apache License 2.0 5 votes vote down vote up
private void doSetResource(String attribute,
        AbstractStreamResource resource) {
    final URI targetUri;
    if (VaadinSession.getCurrent() != null) {
        final StreamResourceRegistry resourceRegistry = VaadinSession
                .getCurrent().getResourceRegistry();
        targetUri = resourceRegistry.getTargetURI(resource);
    } else {
        targetUri = StreamResourceRegistry.getURI(resource);
    }
    set(attribute, targetUri.toASCIIString());
}
 
Example 2
Source File: PendingJavaScriptResult.java    From flow with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a typed completable future that will be completed with the result
 * of the execution. It will be completed asynchronously when the result of
 * the execution is sent back to the server. It is not possible to
 * synchronously wait for the result of the execution while holding the
 * session lock since the request handling thread that makes the result
 * available will also need to lock the session.
 * <p>
 * A completable future can only be created before the execution has been
 * sent to the browser.
 *
 * @param targetType
 *            the type to convert the JavaScript return value to, not
 *            <code>null</code>
 *
 * @return a completable future that will be completed based on the
 *         execution results, not <code>null</code>
 */
default <T> CompletableFuture<T> toCompletableFuture(Class<T> targetType) {
    if (targetType == null) {
        throw new IllegalArgumentException("Target type cannot be null");
    }

    /*
     * Assuming that subscription is only done for the currently locked
     * session. This is quite safe since you cannot subscribe any more after
     * the request has been sent to the client.
     *
     * This is the only way of catching the potentially common case of
     * blocking on a pending result for an element that hasn't yet even been
     * attached to a session.
     */
    VaadinSession session = VaadinSession.getCurrent();

    CompletableFuture<T> completableFuture = new DeadlockDetectingCompletableFuture<>(
            session);

    then(value -> {
        T convertedValue = JsonCodec.decodeAs(value, targetType);
        completableFuture.complete(convertedValue);
    }, errorValue -> {
        JavaScriptException exception = new JavaScriptException(errorValue);
        completableFuture.completeExceptionally(exception);
    });

    return completableFuture;
}
 
Example 3
Source File: Router.java    From flow with Apache License 2.0 5 votes vote down vote up
public RouteRegistry getRegistry() {
    // If we have a session then return the session registry
    // else return router registry
    if (VaadinSession.getCurrent() != null) {
        return SessionRouteRegistry
                .getSessionRegistry(VaadinSession.getCurrent());
    }
    return registry;
}
 
Example 4
Source File: MockUI.java    From flow with Apache License 2.0 5 votes vote down vote up
private static VaadinSession findOrCreateSession() {
    VaadinSession session = VaadinSession.getCurrent();
    if (session == null) {
        session = createSession();
    }
    return session;
}
 
Example 5
Source File: HierarchicalCommunicatorDataTest.java    From flow with Apache License 2.0 5 votes vote down vote up
private static VaadinSession findOrCreateSession() {
    VaadinSession session = VaadinSession.getCurrent();
    if (session == null) {
        session = new DataCommunicatorTest.AlwaysLockedVaadinSession(
                null);
        VaadinSession.setCurrent(session);
    }
    return session;
}
 
Example 6
Source File: DataCommunicatorTest.java    From flow with Apache License 2.0 5 votes vote down vote up
private static VaadinSession findOrcreateSession() {
    VaadinSession session = VaadinSession.getCurrent();
    if (session == null) {
        session = new AlwaysLockedVaadinSession(null);
        VaadinSession.setCurrent(session);
    }
    return session;
}
 
Example 7
Source File: AbstractListDataViewListenerTest.java    From flow with Apache License 2.0 5 votes vote down vote up
private static VaadinSession findOrcreateSession() {
    VaadinSession session = VaadinSession.getCurrent();
    if (session == null) {
        session = new AlwaysLockedVaadinSession(null);
        VaadinSession.setCurrent(session);
    }
    return session;
}
 
Example 8
Source File: MockUI.java    From flow with Apache License 2.0 5 votes vote down vote up
private static VaadinSession findOrCreateSession() {
    VaadinSession session = VaadinSession.getCurrent();
    if (session == null) {
        session = new AlwaysLockedVaadinSession(Mockito.mock(VaadinService.class));
        VaadinSession.setCurrent(session);
    }
    return session;
}