I'm trying to update simple Vaadin UI Component from Singleton:
Here is component:
public class MaintenanceModeLogoutMessageLayout extends HorizontalLayout {
public MaintenanceModeLogoutMessageLayout() {
addComponent(new Label("test"));
}
public void changeVisibility(final Boolean visible) {
setVisible(visible);
}
Here is my singleton, which i need to run on startup
@Singleton
@Startup
public class SingletonTest {
private void executeMaintenanceModeChange(final Boolean maintenance) {
try {
final BeanManager beanManager = InitialContext.doLookup("java:comp/BeanManager");
final Set<Bean<?>> beans = beanManager.getBeans(MaintenanceModeLogoutMessageLayout.class);
final Bean<?> bean = beanManager.resolve(beans);
final CreationalContext<?> cc = beanManager.createCreationalContext(bean);
final MaintenanceModeLogoutMessageLayout object = (MaintenanceModeLogoutMessageLayout) beanManager.getReference(bean,
MaintenanceModeLogoutMessageLayout.class, cc);
if (object == null) {
LOG.warning("Cant find any bean for class " + MaintenanceModeLogoutMessageLayout.class.getSimpleName());
return;
}
Method method = bean.getBeanClass().getDeclaredMethod("changeVisibility", Boolean.class);
method.invoke(object, maintenance);
} catch (final NamingException | IllegalArgumentException | NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
LOG.log(Level.SEVERE, "Can't lookup object ");
}
}
}
As you can see I'm firing event, trying to find existing bean and invoke method of this object.
But the problem is that even if it's current bean there the UI.getCurrent()
is null
.
if i will make component @UIScoped
i'm getting exception
Caused by: java.lang.IllegalStateException: Session data not recoverable for Managed Bean
How can i access UI? And how can i update Vaadin component this way?