Hello there!
First of all let me say that you’ve made a really good job! Gorgeous tool.
To the point:
I’m newer in web app development so maybe my question it’s not necessarily related to Vaadin itself.
I’ve created a Vaadin Application where I define three different classes as singleton. This approach works fine in desktop environments and I’m able to access to the singletons anywhere in my code. On the other hand this singletons are placed in a common jar used for many applications w/o problems.
But now, once I created them under init() application’ method I can use in my first created window (login) but after I set the new window I loose these singletons. I loose them even if I reload/refresh the application using F5. So all my calls to TheSingleton.getInstance().anyMethod() raises an Invalid Pointer Exception.
The code (I write it w/o having the code in front of me so I write it w/o syntax checking):
public class MyApplication extends Application
{
…
public void init() {
UIHandler.initialize(some parameters);
DataFacade.initialize(some parameters);
....
loginWindow = new LoginWindow();
mainWindow = new Window();
...
}
…
}
public class UIHandler implements Serializable {
private static UIHandler instance;
…
private UIHandler() {
....
}
public static void initialize(some parameters) {
if (instance == null) {
instance = new UIHandler();
}
}
public static UIHandler getInstance() {
....
return instance;
}
}
The same apply to the other singleton classes. As you can see it’s a regular singleton pattern.
I’m using this pattern to allow my new vaadin application to use the same structure I’ve in a desktop application. My goal is to create a central UIController and handler that will be used to start the application under desktop or web based with vaadin. So I must centralize all the UI handling in a common point and inject classe dependencies depending on the scenario. Maybe it exists a better approach to it but it’s the one I just selected.
Regarding to the DataFacade to use singleton is to allow all my DAOControllers to connect with a DataProvider without taking care of which one is behind: iBatis, Spring, direct JDBC, dummy… So I create a DataFacade as singleton, configured on application startup and then I use the single instance anywhere in the code as a singleton data facade.
Now…
Once I run the application the first loginWindow it’s shown correctly and it uses, internally, all my singleton classes. After logging, my application calls setMainWindow(mainWindow); if it’s a valid user. But all the calls to UIHandler.getInstance() and DataFacade.getInstance() returns null!
Is it a problem with the singletons under web containers? Maybe loaded in different threads? Different VM? Is it better to put this kind of objects as properties in the session (for user) or context (for all app users)? Better to avoid the singleton pattern under web development?..
I’m so confused and I cant’ figure out how to solve it. I becoming crazy!! I’ve lost 5 days investigating but I’ve got nothing.
If you can give me any kind of help it’ll be really appreciated.
Thank you in advance.