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

The following examples show how to use com.vaadin.server.VaadinSession#hasLock() . 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: VaadinSessionScope.java    From cuba with Apache License 2.0 5 votes vote down vote up
@Override
public Object get(String name, ObjectFactory<?> objectFactory) {
    VaadinSession session = VaadinSession.getCurrent();
    if (session == null || !session.hasLock()) {
        throw new IllegalStateException("Unable to use VaadinSessionScope from non-Vaadin thread");
    }

    Object object = session.getAttribute(name);
    if (object == null) {
        object = objectFactory.getObject();
        session.setAttribute(name, object);
    }
    return object;
}
 
Example 2
Source File: VaadinSessionScope.java    From cuba with Apache License 2.0 5 votes vote down vote up
@Override
public Object remove(String name) {
    VaadinSession session = VaadinSession.getCurrent();
    if (session == null || !session.hasLock()) {
        throw new IllegalStateException("Unable to use VaadinSessionScope from non-Vaadin thread");
    }

    Object bean = session.getAttribute(name);
    session.setAttribute(name, null);
    return bean;
}
 
Example 3
Source File: WebVaadinCompatibleSecurityContextHolder.java    From cuba with Apache License 2.0 5 votes vote down vote up
@Override
public SecurityContext get() {
    VaadinSession vaadinSession = VaadinSession.getCurrent();
    if (vaadinSession != null && vaadinSession.hasLock()) {
        return vaadinSession.getAttribute(SecurityContext.class);
    }

    return super.get();
}
 
Example 4
Source File: WebVaadinCompatibleSecurityContextHolder.java    From cuba with Apache License 2.0 5 votes vote down vote up
@Override
public void set(SecurityContext securityContext) {
    VaadinSession vaadinSession = VaadinSession.getCurrent();
    if (vaadinSession != null && vaadinSession.hasLock()) {
        vaadinSession.setAttribute(SecurityContext.class, securityContext);
    } else {
        super.set(securityContext);
    }
}
 
Example 5
Source File: WebSettingsClient.java    From cuba with Apache License 2.0 5 votes vote down vote up
public void clearCache() {
    VaadinSession session = VaadinSession.getCurrent();
    if (session == null || !session.hasLock()) {
        throw new IllegalConcurrentAccessException("Illegal access to settings client from background thread");
    }

    session.setAttribute(SettingsClient.NAME, null);
}
 
Example 6
Source File: WebSettingsClient.java    From cuba with Apache License 2.0 5 votes vote down vote up
protected Map<String, Optional<String>> getCache() {
    VaadinSession session = VaadinSession.getCurrent();
    if (session == null || !session.hasLock()) {
        throw new IllegalConcurrentAccessException("Illegal access to settings client from background thread");
    }

    @SuppressWarnings("unchecked")
    Map<String, Optional<String>> settings = (Map<String, Optional<String>>) session.getAttribute(SettingsClient.NAME);
    if (settings == null) {
        settings = new HashMap<>();
        session.setAttribute(SettingsClient.NAME, settings);
    }
    return settings;
}
 
Example 7
Source File: App.java    From cuba with Apache License 2.0 5 votes vote down vote up
/**
 * @return Current App instance. Can be invoked anywhere in application code.
 * @throws IllegalStateException if no application instance is bound to the current {@link VaadinSession}
 */
public static App getInstance() {
    VaadinSession vSession = VaadinSession.getCurrent();
    if (vSession == null) {
        throw new IllegalStateException("No VaadinSession found");
    }
    if (!vSession.hasLock()) {
        throw new IllegalStateException("VaadinSession is not owned by the current thread");
    }
    App app = vSession.getAttribute(App.class);
    if (app == null) {
        throw new IllegalStateException("No App is bound to the current VaadinSession");
    }
    return app;
}
 
Example 8
Source File: App.java    From cuba with Apache License 2.0 5 votes vote down vote up
/**
 * @return true if an {@link App} instance is currently bound and can be safely obtained by {@link #getInstance()}
 */
public static boolean isBound() {
    VaadinSession vSession = VaadinSession.getCurrent();
    return vSession != null
            && vSession.hasLock()
            && vSession.getAttribute(App.class) != null;
}
 
Example 9
Source File: WebBackgroundWorker.java    From cuba with Apache License 2.0 5 votes vote down vote up
@Override
public void checkUIAccess() {
    VaadinSession vaadinSession = VaadinSession.getCurrent();

    if (vaadinSession == null || !vaadinSession.hasLock()) {
        throw new IllegalConcurrentAccessException();
    }
}