Hi!

I'm trying to make a Binding that retrieves a localized text from the
database with information from the currently logged in user as parameters,
like this.

<div id="myLocalizedText">${m:my.localized.text.from.db}</div>

The problem is, I can't seem to find a way to make my binding or
bindingfactory act as a per-thread service. Is there any way to do that?
I've tried lots of things, even tried making the Binding into a service.
That didn't work :) While debugging, I see that the newBinding method is
only called once, so the same instance of Request is always used, causing
the information for the first logged in user to be used by all users.

Since the BindingFactory acts like it does, with the newBinding method being
called once anyway, I don't see how I could possibly override this?

My code so far:

public static void bind(ServiceBinder binder) {
    binder.bind(BindingFactory.class,
MyMessageBindingFactory.class).withId("MyMessageBindingFactory");
  }

public static void contributeBindingSource(MappedConfiguration<String,
BindingFactory> configuration, @InjectService("MyMessageBindingFactory")
BindingFactory factory) {
    configuration.add("m", factory);
  }


public class MyMessageBindingFactory implements BindingFactory {

  private final HttpServletRequest request;

  public MyMessageBindingFactory(HttpServletRequest request) {
    this.request = request;
  }

  public Binding newBinding(String description, ComponentResources
container, ComponentResources component,
                            String expression, Location location) {


    return new MyMessageBinding(expression, request);
  }
}

public class MyMessageBinding extends AbstractBinding {

  private final String key;
  private final HttpServletRequest request;


  public MyMessageBinding(String key, HttpServletRequest request) {
    this.key = key;
    this.request = request;
  }

  public Object get() {
    String message = Lang.get(request, key); // Connects to the database to
get the text for the key, based on information about the user from
request.getSession
    return message;
  }

  public boolean isInvariant() {
    return false;
  }

  public Class getBindingType() {
    return String.class;
  }
}

Reply via email to