One of the most exciting features of JBoss Seam is the contextual component model that radically changes the way we design applications. It has become so important, it is now part of the JEE6 specification through the JSR-299.
static final ThreadLocal<context> applicationContext = new ThreadLocal<context>(); static final ThreadLocal<context> methodContext = new ThreadLocal<context(); static final ThreadLocal<context> eventContext = new ThreadLocal<context>(); static final ThreadLocal<context> pageContext = new ThreadLocal<context>(); static final ThreadLocal<context> sessionContext = new ThreadLocal<context>(); static final ThreadLocal<context> conversationContext = new ThreadLocal<context>(); static final ThreadLocal<context> businessProcessContext = new ThreadLocal<context>();
For example, to save the current user information in the session context you use the following code:
User user = new User(); Contexts.getSessionContext().set("user", user);Then, when you need to retrieve the user information, you use:
User user = (User)Contexts.getSessionContext().get("user");Really simple, ah? You can also use the @Out and @In annotations in class attributes to save and retrieve information:
@Out(value="user",scope=ScopeType.SESSION) User user;What the @Out annotation is telling Seam is to save the User object in the Session context with the key “user”. Then, you can retrieve the user with the following code:
@In(value="user",scope=ScopeType.SESSION); User user;Again, the @In attribute is telling Seam to inject the User object from the Session context saved with the “user” key. You don’t have to write any getters or setters in your class, Seam will just outject or inject the information whenever an @Out or @In annotation is present. If no object is found under the “user” key, null will be injected.
As you can see, the value attribute of the @In and @Out annotations are used as a key for searching or saving data in the contexts. The scope attribute is used to specify the context in which the value should be injected or outjected. This attributes are optional; so, what happens if we don’t specify them as in the following example?
@In
When you don’t specify the value attribute, Seam uses the attribute’s name as key by default. In the preceding code fragment, Seam will use “name” as the key for the @In annotation and “user” for the @Out annotation.
If you don’t specify the scope of an @In annotation, Seam will scan all the contexts in the following order: event, page,conversation, session, business process and application. If the key is not found, it will inject null. You can use this feature programmatically by calling the Contexts.lookupInStatefulContexts() method.
If you don’t specify the scope of an @Out annotation … well, as the @Out JavaDoc states: “If no scope is explicitly specified, the default scope depends upon whether the value is an instance of a Seam component. If it is, the component scope is used. Otherwise, the scope of the component with the @Out attribute is used.” I know, I haven’t talked anything about Seam components as it is not the topic of this article. I probably post my next topic on Seam Componet model.