Where to place application-wide initialization-code?

Hi,

I’m just playing around with beta11 and checking out some of the new Vaadin-7-features. While building some example code for BootstrapListener and ConverterFactory I started to wonder where the best place for such application-wide initialization-code might be. At the moment I have extended the VaadinServlet and added a SessionInitListener which does the necessary stuff as pointed out in the
wiki
. But this feels ugly and tedious to do …

So: Where is the best place to put the initializations I mentioned? Is it the intention to make us extend Vaadin-Servlet and work with low-level-API for such basic things like changing the converter-factory?

Some other places I could think of (although I didn’t find any of them) would be:

  • A Servlet-Parameter at least for the ConverterFactory (BootstrapListeners aren’t that common in my opinion)
  • An ApplicationConfig-class with several Getters for BootstrapListeners and ConverterFactories which gets loaded (more or less) automatically by the Servlet

kind regards
Tobias

A
ServletContextListener
is a good choice.

Don’t I have to add BootstrapListeners and ConverterFactories to each individual User-Session? In that case, a ServletContextListener won’t help as it doesn’t get notified about new Sessions, does it?

Anyway I am able to find a way around this by using the low-level-Servlet- and Portlet-APIs, but actually I was hoping that some way “inside” the Vaadin-library exists, like setting a Servlet-Parameter or so. If there is no such way, I am fine with it, but it makes things harder to understand for the “non-Web-people” starting with Vaadin (who actually choosed Vaadin in order to get around HTML, CSS, Javascript, servlet-spec and the Request-Response-model in most cases).

best regards
Tobias

It really depends on what you want to do. ServletContextListeners are run once when the app is deployed and once when it is undeployed; it has nothing to do with user sessions. Then you have the init()-methods, which run each time a new UI is created. Of course, new UIs are not the same thing as a new HTTP session.

To do something only once per HTTP session, you can check the VaadinRequest parameter in the init method; getWrappedSession() returns the current HTTP session where you can set and get parameters. I don’t know if there is a direct API for listening for Sessions, as you usually don’t need to do that (the init() method and context listener are usually enough).

At the moment to listen to session initialization and set VaadinSession parameters, the correct way would be the one described in the wiki, i.e. extending VaadinServlet and adding a SessionInitListener. This could be improved in many different ways, e.g. by allowing a servlet parameter for setting a SessionInitListener.