JSF Application Scope

I was asked a question on if Vaadin has something similar to JSF Application scope and decided to post my thoughts here also just to make them reusable.

JSF saves application scope to a named attribute in ServletContext (when the application has been deployed as servlet). No magic there. In Vaadin you can use this as well - in many cases this is much more clean than using statics.

Quick example


package com.example.scoper;

import com.vaadin.Application;
import com.vaadin.terminal.gwt.server.WebApplicationContext;
import com.vaadin.ui.*;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;

@SuppressWarnings("serial")
public class ScoperApplication extends Application {
	
	TextField text = new TextField("Value to store in application scope");
	Button store = new Button("Store above value in application scope");
	Button show = new Button("Show value currently stored in application scope");
	
	@Override
	public void init() {
		Window mainWindow = new Window("Scoper Application");
		
		mainWindow.addComponent(text);
		mainWindow.addComponent(store);
		mainWindow.addComponent(show);
		
		store.addListener(new ClickListener() {
			public void buttonClick(ClickEvent event) {
				storeValueToApplicationScope(text.toString());
			}
		});
		show.addListener(new ClickListener() {
			public void buttonClick(ClickEvent event) {
				getMainWindow().showNotification(getValueInApplicationScope());
			}
		});
		
		setMainWindow(mainWindow);
	}

	private void storeValueToApplicationScope(String string) {
		((WebApplicationContext)getContext()).getHttpSession().getServletContext().setAttribute("scoper-app-value", string);
	}

	private String getValueInApplicationScope() {
		return (String) ((WebApplicationContext)getContext()).getHttpSession().getServletContext().getAttribute("scoper-app-value");
	}
}

Hello Joonas,

I’m new to Vaadin. I’ve been evaluating it for some weeks to find out as much as possible if it will fit our organization’s needs. So far so good.

It’s been a couple of days that I’am testing the IcePush addon and it works OK for a single application instance notification. What I need is to push some changes driven by one user to all the other users log in or to a group of them.

I have seen the example implementation of the MultiUserCalendar and the BlackBoard add-on. But as our future application will reside into a clustered Glassfish Server, I can not use static variables as MultiUserCalendar example does and as Blackboard example does.

So I thought to use part of you code below to make it possible to store the list of all the application instances created by all the users and whenever an event of just one application gets fired, I could simple get the application list from the ServletContext and for each instance I could call a method that call pusher.push() for each application instance.

The question is. Is it possible to store objects (as List) into the servletContext with the method:

((WebApplicationContext)getContext()).getHttpSession().getServletContext().setAttribute(“app-list”, appList);

I tried it, no error but nothing gets stored. I get just a null value when I call directly after the store:

((WebApplicationContext)getContext()).getHttpSession().getServletContext().getAttribute(“app-list”)

Any help would be appreciated.
I like Vaadin and I’m trying to gather proved and tested facts before I can suggest it to my organization as the Web Development Framework to use for our near future application.

Thanks
Klaudeta

It sounds like you want an object (Vaadin Application class) in a servlet in one JVM to be able to communicate with objects in a servlet in another JVM, since each instance is running in its own JVM. You might want to use JMS to handle this; I can’t think of a simpler way to do it (e.g., storing/polling data in a filesystem or database).

You may want to ask the GlassFish users alias about this since it’s not specific to Vaadin. There is probably a wealth of new info out there since we
just released GF 3.1
. Here’s one blog about
the new JMS features
.

Cheers,
Bobby