LocalThread in Vaadin 7

Hi all,

after googling two hours, I’m quite confused … I googled all about getRoot, getWrappedRoot, HttpServletListener, TransactionListener … and so on …

Most examples I found are for Vaadin 6 and other examples I found are for Vaadin 7 using “Application” as main class (and also overdated).

The book-of-vaadin-7 pages only tell me, that a lot has changed and give me the link to mini-examples …

But I didn’t find any examples about ThreadLocal - which already should be already integrated in the vaadin framework somewhere - and main class UI … As I know, Application doesn’t exist anymore and has been renamed to ApplicationLegacy and UI should be used …

Please, just one example and a lot of people would be very happy :slight_smile:

All the best,
Thomas

Is this what you are looking for
https://vaadin.com/wiki/-/wiki/Main/Finding%20the%20current%20UI%20and%20Page%20and%20VaadinSession
?

Hmm … At first glance, I was not sure if it was what I’m looking for …

I try to explain, how I understood the links you attached and I hope, I’m right …

Imagine, my application has a Logger UI-Component “Log” which provides methods for writing logs to a log window.

In order to
not
having to give every UI component the reference to this Log-class-object, I would like to access it in a static way. Of course simply declaring it static doesn’t solve anything because every application must have its own Log-class-object.

For this purpose, there was ThreadLocal and an
example how to use it
.

I implemented it in a Vaadin6 project and figured out that this implementation doesn’t work anymore in Vaadin7, so I searched a replacement for it.

Your link suggests to simply use UI.getCurrent() for getting the current object of my Main-Ui-class …

So, I don’t need all of these ThreadLocal-stuff anymore and only access my Log-object with Ui.getCurrent().getLog()?

That would be fantastic news!

Am I correct?

All the best,
Thomas

edit: I think this really is, what I’m searching for: "Vaadin uses the ThreadLocal pattern for allowing global access to the Application, UI, and Page objects of the currently processed server request with a static getCurrent() method in all the respective classes. "

VaadinSession.getCurrent.setAttribut(“logWindow”, YouLogWindow);

Later from any class, VaadinSession.getCurrent.getAttribute(“logWindow”);

What Artur posted is exactly what you need. What you needed ThreadLocal for in Vaadin 6 is built in into Vaadin 7. You can make a getter for the logger into the UI. Then, anywhere in the application you can call UI.getCurrent(), cast it to your UI class and call ui.getLogger();.

On top of that, I usually do a static method into my own UI class that does the casting for me.

Something like: (pseudo code without checking it)

public static MyUI get(){
  UI ui = UI.getCurrent();
  if(ui instanceof MyUI){
    return (MyUI) ui;
  }
  return null;
}

After that, you can anywhere call MyUI.get().getLogger(); without having to cast it every single time.