Obviously I want to write decoupled components. One part is a form engine. I don't want it to be dependent on the servlet API, but I have to inialize it per-request (or at least per-session).
In an application I would use something like
public static void setLocale(Locale l);
then my individual classes could get it with a static getter. This is not feasable in a servlet environment (servlets lack even a static getServletContext()
method thru which static behaviour could be emulated).
I absolutely don't want to use factory (I will have 10+ classes all of which will use some configuration, Locale at least) or worse than that: construct each object with a parameter block (which contains Locale
and other settings).
I would like to know what's the best practive in this situation. Can static behavior emulated in a usable way or does the servlet API has an answer to this problem?
If every other possibility fails I thought of using something like:
class MyParameters {
private Map<Thread, MyParameters> threadParameters = new Map<Thread, MyParameters>();
public static void setParameters(MyParameters parameters) {
threadParameters.put(Thread.getCurrentThread(), parameters);
}
public static MyParameters getParameters() {
return threadParameters.get(Thread.getCurrentThread());
}
}
... but this creates some security concerns (a servlet may fail to initialize it and use the values set up during a previous request served by the same thread). - Although using a different user's Locale is not as much of a threat.