You are currently browsing the Philipp Meier's weblog blog archives for October, 2003



AOP in scripting languages

Roman Porotnikov writes in deep:code on how to implement AOP in javascript. I just want to point out that this principle can be applied to ruby and perl, too. AFAIK in smalltalk this should also work. What have this languages in common? Functions can be redefined at runtime and functions can be referenced. This way, interceptions can be made by memorizing the original (intercepted) method and passing it to the actual interceptor implementation.
I don’t want to trigger YAAINJIT (Yet Another AOP Is Not Just Interception Thread), so please don’t comment on this oversimplification.

Use the request as a key in a ThreadLocal map

Anthony Eden writes in All Things: ThreadLocal and thread pooling

Now apply this to servlets. You can have one request which sets the thread local object’s state. If the first request completes and the thread local state is not cleared then another request may be able to access the thread local object with it’s state intact.

My suggestet fix is to store a map in the ThreadLocal and use the request as the key to get the actual data. Even if one Thread works on multiple requests, they are protected from eachother because each request has no reference to another request:

class RequestThreadLocalHelper {
  private static ThreadLocal data = new ThreadLocal();

  public RequestThreadLocalHelper {
    data.put(new HashMap()));

  public void put(Request request, Object value) {
    ((Map) data.get()).put(request, value);

  public Object get(Request request) {
    return  ((Map) data.get()).get(request);