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

The following examples show how to use com.vaadin.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: WebDateFieldTest.java    From cuba with Apache License 2.0 6 votes vote down vote up
@Override
protected void initExpectations() {
    super.initExpectations();

    new Expectations() {
        {
            vaadinSession.getLocale(); result = Locale.ENGLISH; minTimes = 0;
            VaadinSession.getCurrent(); result = vaadinSession; minTimes = 0;

            globalConfig.getAvailableLocales(); result = ImmutableMap.of("en", Locale.ENGLISH); minTimes = 0;
            AppContext.getProperty("cuba.mainMessagePack"); result = "com.haulmont.cuba.web"; minTimes = 0;
        }
    };

    this.uiComponents = new TestUiComponents(applicationContext);
}
 
Example 2
Source File: WebLookupPickerFieldTest.java    From cuba with Apache License 2.0 6 votes vote down vote up
@Override
protected void initExpectations() {
    super.initExpectations();

    new Expectations() {
        {
            vaadinSession.getLocale(); result = Locale.ENGLISH; minTimes = 0;
            VaadinSession.getCurrent(); result = vaadinSession; minTimes = 0;

            vaadinSession.getConverterFactory(); result = new DefaultConverterFactory(); minTimes = 0;

            globalConfig.getAvailableLocales(); result = ImmutableMap.of("en", Locale.ENGLISH); minTimes = 0;
            AppContext.getProperty("cuba.mainMessagePack"); result = "com.haulmont.cuba.web"; minTimes = 0;
        }
    };

    this.uiComponents = new TestUiComponents(applicationContext);
}
 
Example 3
Source File: WebOptionsGroupTest.java    From cuba with Apache License 2.0 6 votes vote down vote up
@Override
protected void initExpectations() {
    super.initExpectations();

    new Expectations() {
        {
            vaadinSession.getLocale(); result = Locale.ENGLISH; minTimes = 0;
            VaadinSession.getCurrent(); result = vaadinSession; minTimes = 0;

            vaadinSession.getConverterFactory(); result = new DefaultConverterFactory(); minTimes = 0;

            globalConfig.getAvailableLocales(); result = ImmutableMap.of("en", Locale.ENGLISH); minTimes = 0;
            AppContext.getProperty("cuba.mainMessagePack"); result = "com.haulmont.cuba.web"; minTimes = 0;
        }
    };

    this.uiComponents = new TestUiComponents(applicationContext);
}
 
Example 4
Source File: WebPickerFieldTest.java    From cuba with Apache License 2.0 6 votes vote down vote up
@Override
protected void initExpectations() {
    super.initExpectations();

    new Expectations() {
        {
            vaadinSession.getLocale(); result = Locale.ENGLISH; minTimes = 0;
            VaadinSession.getCurrent(); result = vaadinSession; minTimes = 0;

            vaadinSession.getConverterFactory(); result = new DefaultConverterFactory(); minTimes = 0;

            globalConfig.getAvailableLocales(); result = ImmutableMap.of("en", Locale.ENGLISH); minTimes = 0;
            AppContext.getProperty("cuba.mainMessagePack"); result = "com.haulmont.cuba.web"; minTimes = 0;
        }
    };

    this.uiComponents = new TestUiComponents(applicationContext);
}
 
Example 5
Source File: WebFieldGroupTest.java    From cuba with Apache License 2.0 6 votes vote down vote up
@Override
protected void initExpectations() {
    super.initExpectations();

    new Expectations() {
        {
            vaadinSession.getLocale(); result = Locale.ENGLISH; minTimes = 0;
            VaadinSession.getCurrent(); result = vaadinSession; minTimes = 0;

            vaadinSession.getConverterFactory(); result = new DefaultConverterFactory(); minTimes = 0;

            globalConfig.getAvailableLocales(); result = ImmutableMap.of("en", Locale.ENGLISH); minTimes = 0;
            AppContext.getProperty("cuba.mainMessagePack"); result = "com.haulmont.cuba.web"; minTimes = 0;
        }
    };
}
 
Example 6
Source File: WebLookupFieldTest.java    From cuba with Apache License 2.0 6 votes vote down vote up
@Override
protected void initExpectations() {
    super.initExpectations();

    new Expectations() {
        {
            vaadinSession.getLocale(); result = Locale.ENGLISH; minTimes = 0;
            VaadinSession.getCurrent(); result = vaadinSession; minTimes = 0;

            vaadinSession.getConverterFactory(); result = new DefaultConverterFactory(); minTimes = 0;

            globalConfig.getAvailableLocales(); result = ImmutableMap.of("en", Locale.ENGLISH); minTimes = 0;
            AppContext.getProperty("cuba.mainMessagePack"); result = "com.haulmont.cuba.web"; minTimes = 0;
        }
    };

    this.uiComponents = new TestUiComponents(applicationContext);
}
 
Example 7
Source File: WebLookupFieldDsTest.java    From cuba with Apache License 2.0 6 votes vote down vote up
@Override
protected void initExpectations() {
    super.initExpectations();

    new Expectations() {
        {
            vaadinSession.getLocale(); result = Locale.ENGLISH; minTimes = 0;
            VaadinSession.getCurrent(); result = vaadinSession; minTimes = 0;

            vaadinSession.getConverterFactory(); result = new DefaultConverterFactory(); minTimes = 0;

            globalConfig.getAvailableLocales(); result = ImmutableMap.of("en", Locale.ENGLISH); minTimes = 0;
            AppContext.getProperty("cuba.mainMessagePack"); result = "com.haulmont.cuba.web"; minTimes = 0;
        }
    };
    this.uiComponents = new TestUiComponents(applicationContext);
}
 
Example 8
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();
    }
}
 
Example 9
Source File: WebLookupPickerFieldDsTest.java    From cuba with Apache License 2.0 5 votes vote down vote up
@Override
protected void initExpectations() {
    super.initExpectations();

    new Expectations() {
        {
            VaadinSession.getCurrent(); result = vaadinSession; minTimes = 0;
            vaadinSession.getConverterFactory(); result = new DefaultConverterFactory(); minTimes = 0;
            AppContext.getProperty("cuba.mainMessagePack"); result = "com.haulmont.cuba.web"; minTimes = 0;
            configuration.getConfig(ClientConfig.class); result = clientConfig; minTimes = 0;
            clientConfig.getPickerShortcutModifiers(); result = "CTRL-ALT"; minTimes = 0;
        }
    };
    this.uiComponents = new TestUiComponents(applicationContext);
}
 
Example 10
Source File: VaadinScope.java    From jdal with Apache License 2.0 5 votes vote down vote up
private String getConversationId(Integer id) {
	VaadinSession session = VaadinSession.getCurrent();
	if (session == null) {
		log.info("Request Conversation id without session");
		return null;
	}
	
	return session.getSession().getId() + ":" + id.toString();
}
 
Example 11
Source File: WebLabelDsTest.java    From cuba with Apache License 2.0 5 votes vote down vote up
@Override
protected void initExpectations() {
    super.initExpectations();

    new Expectations() {
        {
            VaadinSession.getCurrent(); result = vaadinSession; minTimes = 0;
            AppContext.getProperty("cuba.mainMessagePack"); result = "com.haulmont.cuba.web"; minTimes = 0;
        }
    };
    uiComponents = new TestUiComponents(applicationContext);
}
 
Example 12
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 13
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 14
Source File: App.java    From cuba with Apache License 2.0 5 votes vote down vote up
/**
 * Called when <em>the first</em> UI of the session is initialized.
 */
protected void init(Locale requestLocale) {
    VaadinSession vSession = VaadinSession.getCurrent();
    vSession.setAttribute(App.class, this);

    vSession.setLocale(messageTools.getDefaultLocale());

    // set root error handler for all session
    vSession.setErrorHandler(event -> {
        try {
            getExceptionHandlers().handle(event);
            getAppLog().log(event);
        } catch (Throwable e) {
            log.error("Error handling exception\nOriginal exception:\n{}\nException in handlers:\n{}",
                    ExceptionUtils.getStackTrace(event.getThrowable()), ExceptionUtils.getStackTrace(e)
            );
        }
    });

    log.debug("Initializing application");

    appLog = new AppLog(webConfig.getAppLogMaxItemsCount(), beanLocator.get(TimeSource.NAME));

    connection = createConnection();
    exceptionHandlers = new ExceptionHandlers(this, beanLocator);
    cookies = new AppCookies();

    themeConstants = loadTheme();

    // get default locale from config
    Locale targetLocale = resolveLocale(requestLocale);
    setLocale(targetLocale);
}
 
Example 15
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 16
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 17
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 18
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 19
Source File: VaadinSessionScope.java    From cuba with Apache License 2.0 5 votes vote down vote up
@Override
public String getConversationId() {
    if (VaadinSession.getCurrent() == null || !VaadinSession.getCurrent().hasLock()) {
        throw new IllegalStateException("Unable to use VaadinSessionScope from non-Vaadin thread");
    }

    return VaadinSession.getCurrent().getSession().getId();
}
 
Example 20
Source File: WebPickerFieldDsTest.java    From cuba with Apache License 2.0 5 votes vote down vote up
@Override
protected void initExpectations() {
    super.initExpectations();

    new Expectations() {
        {
            VaadinSession.getCurrent(); result = vaadinSession;  minTimes = 0;
            vaadinSession.getConverterFactory(); result = new DefaultConverterFactory(); minTimes = 0;
            AppContext.getProperty("cuba.mainMessagePack"); result = "com.haulmont.cuba.web"; minTimes = 0;
        }
    };

    this.uiComponents = new TestUiComponents(applicationContext);
}