contexts
to facilitate
separation of concerns and achieve higher level of performance and flexibility.See: Description
Class | Description |
---|---|
AbstractContext |
The parent class for all contexts.
|
ConcurrentContext |
A context able to take advantage of concurrent algorithms on
multi-processors systems.
|
FormatContext |
The parent class for all serializer/deserializer contexts.
|
LocalContext |
A context holding locally scoped
parameters values. |
LocalContext.Parameter<T> |
A
configurable parameter whose value can
be locally superseded within the scope of LocalContext . |
LogContext |
Asynchronous logging context integrated with the OSGi logging framework.
|
SecurityContext |
A high-level security context integrated with OSGi.
|
SecurityContext.Permission<T> |
A permission associated to a specific class/action/instance.
|
Enum | Description |
---|---|
LogContext.Level |
Defines the logging levels.
|
Run-time contexts
to facilitate
separation of concerns and achieve higher level of performance and flexibility.
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.
Javolution provides several useful runtime contexts:
ConcurrentContext
- To take advantage of concurrent algorithms on multi-cores systems.LogContext
- To log events according to the runtime environment (e.g. LogService
when running OSGi).LocalContext
- To define locally scoped environment settings.SecurityContext
- To address application-level security concerns.FormatContext
- For plugable objects parsing/formatting. Such as TextContext
for plain text,
or XMLContext
for XML serialization/deserialization. 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. ... } }
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.