Package javolution.context

Run-time contexts to facilitate separation of concerns and achieve higher level of performance and flexibility.

See: Description

Package javolution.context Description

Run-time contexts to facilitate separation of concerns and achieve higher level of performance and flexibility.

Separation of Concerns

Separation of concerns is an important design principle greatly misunderstood. Most developers think it is limited to modularity and encapsulation or it requires special programming tools (e.g. Aspect programming).

Separation of concerns is very powerful and easier than it looks. Basically, it could be summarized as the "pass the buck principle". If you don't know what to do with some information, just give it to someone else who might know.

A frequent example is the catching of exceptions too early (with some logging processing) instead of throwing a checked exception. Unfortunately, they are still plenty of cases where the separation of concerns is not as good as it could be. For example logging! Why low-level code need to know which logging facility to use (e.g. standard logging, Log4J library, OSGi LogService or anything else)? Why logging should have to be based upon the class hierarchy (standard logging)? Why can't we attach some relevant information (such as user id, session number, etc.) to the logging content ?

Separation of concerns can be addressed through "Aspect Programming", but there is a rather simpler solution "Context Programming"!

It does not require any particular tool, it basically says that every threads has a context which can be customized by someone else (the one who knows what to do, when running OSGi it is typically a separate bundle). Then, your code looks a lot cleaner and is way more flexible as you don't have to worry about logging, security, performance etc. in your low level methods.

void myMethod() {
    ...
    LogContext.info("Don't know where this is going to be logged to");
    ...
}

Used properly Javolution's contexts greatly facilitate the separation of concerns. Contexts are fully integrated with OSGi (they are published services). Applications may dynamically plug-in the "right context" using OSGi mechanisms. For example, the SecurityContext might only be known at runtime.

Predefined Contexts:

Javolution provides several useful runtime contexts:

FAQ:

  1. In my application I create new threads on-the-fly and I would like them to inherit the current context environment. How can I do that?

    Context is automatically inherited when performing concurrent executions using ConcurrentContext. If you create new threads yourself you can easily setup their context as shown below.

    //Spawns a new thread inheriting the context of the current thread.
    MyThread myThread = new MyThread();
    myThread.inherited = AbstractContext.current(); 
    myThread.start(); 
     ...
    class MyThread extends Thread {
        AbstractContext inherited;
        public void run() {
            AbstractContext.inherit(inherited); // Sets current context. 
            ...
        }
    }

  2. Is it possible to configure the context of all running threads (global configuration) ?

    Yes by publishing an OSGi implementation of the customized context (published context services are the default contexts of all running threads). Otherwise, you can only configure a context that you have entered (for obvious safety reasons).

Copyright © 2005-2013 Javolution. All Rights Reserved.