Class BrowserlessApplicationContext
- All Implemented Interfaces:
AutoCloseable
- Direct Known Subclasses:
SecuredBrowserlessApplicationContext
Manages a shared VaadinServletService and servlet that are shared
across all users and their windows. This models the application level of the
Vaadin hierarchy: one application contains multiple user sessions, each with
multiple UI instances (browser windows).
Instances of this class are thread-affine: they must be
created, used, and closed on the same thread. The active context is held in a
ThreadLocal and is not visible to other threads. This class is not
safe for concurrent access from multiple threads; driving the same context
from parallel test threads is unsupported.
This base class is unsecured: it has no SecurityContextHandler. For
multi-user security isolation, configure a handler via
BrowserlessApplicationContext.Builder.withSecurityContextHandler(SecurityContextHandler) — the
builder transitions to SecuredBrowserlessApplicationContext.Builder
and build()
returns the typed SecuredBrowserlessApplicationContext, which exposes
credential-aware newUser(...) overloads. Framework-specific modules
(Spring, Quarkus) provide convenience factory methods for both paths.
Session locking. A user's
VaadinSession lock is held only while one of
that user's windows is active, the way a real request holds only the lock of
its own session:
- Application code running in one user's window that changes another user's
UI directly fails, as it does in a running application. Use
UI.access()for such changes. As in a development-mode server, the failure is anIllegalStateExceptionwith the message "Cannot access state in VaadinSession or UI without locking the session". - A
UI.access()command for another user's UI runs at once on the calling thread, with that user's session and UI current, because nothing else holds that session's lock. As in a running application, an exception thrown by such a command goes to that session'sErrorHandlerand is only logged by the default one; it does not fail the test. - Switching to another user's window, or closing the active window, releases the outgoing session's lock and runs the commands still queued for it. Switching then takes the incoming session's lock. Switching between windows of the same user keeps the lock. As with the commands run before a component lookup, an exception thrown by a queued command fails the test. A switch rethrows it at once and leaves the outgoing session locked; a close completes first and then rethrows it.
- Switching to another user's window, or closing a window, a user or the
application context, from inside a
UI.access()command of the active user fails with anIllegalStateExceptionand changes nothing, because Flow still holds that session's lock while the command runs. If the test itself locks the active user's session once more, a switch releases only its own hold, and the queued commands run when the test releases its. An exception from them then goes to the session'sErrorHandler, as with any plainunlock(). To have it fail the test, callMockVaadin.runUIQueue(false, session)for that session after the switch, before releasing the extra hold. - A test that changes a component of another user directly has to activate
one of that user's windows first, for example with
BrowserlessUIContext.activate().
var app = BrowserlessApplicationContext.create(MyView.class); var user1 = app.newUser(); var window1 = user1.newWindow(); window1.navigate(MyView.class); // ... app.close();
- Since:
- 1.1
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classBuilder for creating a customizedBrowserlessApplicationContext. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Closes this application context and all its user contexts.Creates a plain Java application context, scanning the packages of the given classes for@Route-annotated views.Creates a plain Java application context, scanning the given packages for@Route-annotated views.create(UnaryOperator<BrowserlessApplicationContext.Builder> configurer) Creates a plain Java application context, applying the given configurer to a fresh builder beforebuildingit.static <C> SecuredBrowserlessApplicationContext<C> createSecured(Function<BrowserlessApplicationContext.Builder, SecuredBrowserlessApplicationContext.Builder<C>> configurer) Creates a credential-typed application context.forComponent(BrowserlessConfiguration configuration, Supplier<Component> componentFactory) Creates a self-contained application context for ad-hoc testing of a single component, applying the given custom Vaadin configuration (application properties, feature flags, lookup services) to the mocked environment.forComponent(Supplier<Component> componentFactory) Creates a self-contained application context for ad-hoc testing of a single component, without requiring a@Routeview.newUser()Creates a new user context representing an anonymous user session.
-
Method Details
-
create
Creates a plain Java application context, scanning the given packages for@Route-annotated views.- Parameters:
viewPackages- package names to scan for views; an empty array falls back to a full classpath scan- Returns:
- a new application context
-
create
Creates a plain Java application context, scanning the packages of the given classes for@Route-annotated views.- Parameters:
viewPackageClasses- classes whose packages should be scanned for views- Returns:
- a new application context
-
create
public static BrowserlessApplicationContext create(UnaryOperator<BrowserlessApplicationContext.Builder> configurer) Creates a plain Java application context, applying the given configurer to a fresh builder beforebuildingit. The configurer cannot benull; passUnaryOperator.identity()to accept all defaults.- Parameters:
configurer- builder configurer- Returns:
- a new application context
-
createSecured
public static <C> SecuredBrowserlessApplicationContext<C> createSecured(Function<BrowserlessApplicationContext.Builder, SecuredBrowserlessApplicationContext.Builder<C>> configurer) Creates a credential-typed application context. The configurer must callBrowserlessApplicationContext.Builder.withSecurityContextHandler(SecurityContextHandler)on the supplied builder so that it returns aSecuredBrowserlessApplicationContext.Buildercarrying the required handler.Using a separate method name rather than an overload of
create(UnaryOperator)is intentional: Java overload resolution cannot disambiguate two lambdas whose parameter types share theFunctionerasure.- Type Parameters:
C- the credentials type- Parameters:
configurer- builder configurer that installs a security handler and returns the resulting secured builder- Returns:
- a new credential-aware application context
-
forComponent
Creates a self-contained application context for ad-hoc testing of a single component, without requiring a@Routeview. No route discovery is performed (seeBrowserlessApplicationContext.Builder.withoutRoutes()), so creation skips the classpath scan and the component is the sole content of each window's UI.The given factory is invoked once per window: each
newUser().newWindow()call attaches a freshly produced component to that window's UI. The factory runs fromUI.init(), after the Vaadin thread-locals (UI, session, security context) are installed, so a component whose constructor readsUI.getCurrent()observes the live environment. Supply a factory that returns a new instance per call if you intend to open more than one window; reusing a single instance across windows re-parents it and leaves the earlier window empty.The returned context closes itself when one of its windows is closed, so a single try-with-resources — on the returned context, or on the window via
BrowserlessUIContext.forComponent(Component)— tears everything down.- Parameters:
componentFactory- supplies the component to attach to each window; must not benull- Returns:
- a new self-closing application context with no routes
- Throws:
NullPointerException- ifcomponentFactoryisnull
-
forComponent
public static BrowserlessApplicationContext forComponent(BrowserlessConfiguration configuration, Supplier<Component> componentFactory) Creates a self-contained application context for ad-hoc testing of a single component, applying the given custom Vaadin configuration (application properties, feature flags, lookup services) to the mocked environment. Behaves likeforComponent(Supplier)otherwise.var configuration = BrowserlessConfiguration.builder() .withFeatureFlags("myExperimentalFeature").build(); try (var app = BrowserlessApplicationContext.forComponent(configuration, MyForm::new)) { var window = app.newUser().newWindow(); // ... }- Parameters:
configuration- the configuration to apply; must not benullcomponentFactory- supplies the component to attach to each window; must not benull- Returns:
- a new self-closing application context with no routes
- Throws:
NullPointerException- ifconfigurationorcomponentFactoryisnull- See Also:
-
newUser
Creates a new user context representing an anonymous user session.When a
SecurityContextHandleris configured (on aSecuredBrowserlessApplicationContext), the handler is asked to install its anonymous-equivalent state (e.g. Spring sets anAnonymousAuthenticationToken). On this unsecured base class no security setup is performed.- Returns:
- the new user context
- Throws:
IllegalStateException- if this context has been closed
-
close
public void close()Closes this application context and all its user contexts.Closes every
BrowserlessUserContextcreated by this application (which in turn closes their windows), fires service destroy listeners, and resets theVaadinServicethread-local. This method is idempotent: subsequent invocations have no effect.- Specified by:
closein interfaceAutoCloseable
-