SeamFramework.orgCommunity Documentation

Chapter 14. Extending Web Beans

14.1. The Manager object
14.2. The Bean class
14.3. The Context interface

Web Beans is intended to be a platform for frameworks, extensions and integration with other technologies. Therefore, Web Beans exposes a set of SPIs for the use of developers of portable extensions to Web Beans. For example, the following kinds of extensions were envisaged by the designers of Web Beans:

The nerve center for extending Web Beans is the Manager object.

The Manager interface lets us register and obtain Web Beans, interceptors, decorators, observers and contexts programatically.

public interface Manager

{
   public <T> Set<Bean<T>> resolveByType(Class<T> type, Annotation... bindings);
   public <T> Set<Bean<T>> resolveByType(TypeLiteral<T> apiType,
         Annotation... bindings);
   public <T> T getInstanceByType(Class<T> type, Annotation... bindings);
   public <T> T getInstanceByType(TypeLiteral<T> type,
         Annotation... bindings);
   public Set<Bean<?>> resolveByName(String name);
   public Object getInstanceByName(String name);
   public <T> T getInstance(Bean<T> bean);
   public void fireEvent(Object event, Annotation... bindings);
   public Context getContext(Class<? extends Annotation> scopeType);
   public Manager addContext(Context context);
   public Manager addBean(Bean<?> bean);
   public Manager addInterceptor(Interceptor interceptor);
   public Manager addDecorator(Decorator decorator);
   public <T> Manager addObserver(Observer<T> observer, Class<T> eventType,
         Annotation... bindings);
   public <T> Manager addObserver(Observer<T> observer, TypeLiteral<T> eventType,
         Annotation... bindings);
   public <T> Manager removeObserver(Observer<T> observer, Class<T> eventType,
         Annotation... bindings);
   public <T> Manager removeObserver(Observer<T> observer,
         TypeLiteral<T> eventType, Annotation... bindings);
   public <T> Set<Observer<T>> resolveObservers(T event, Annotation... bindings);
   public List<Interceptor> resolveInterceptors(InterceptionType type,
         Annotation... interceptorBindings);
   public List<Decorator> resolveDecorators(Set<Class<?>> types,
         Annotation... bindings);
}

We can obtain an instance of Manager via injection:

@Current Manager manager

Instances of the abstract class Bean represent Web Beans. There is an instance of Bean registered with the Manager object for every Web Bean in the application.

public abstract class Bean<T> {

    
    private final Manager manager;
    
    protected Bean(Manager manager) {
        this.manager=manager;
    }
    
    protected Manager getManager() {
        return manager;
    }
    
    public abstract Set<Class> getTypes();
    public abstract Set<Annotation> getBindingTypes();
    public abstract Class<? extends Annotation> getScopeType();
    public abstract Class<? extends Annotation> getDeploymentType(); 
    public abstract String getName();
    
    public abstract boolean isSerializable();
    public abstract boolean isNullable();
    public abstract T create();
    public abstract void destroy(T instance);
    
}

It's possible to extend the Bean class and register instances by calling Manager.addBean() to provide support for new kinds of Web Beans, beyond those defined by the Web Beans specification (simple and enterprise Web Beans, producer methods and JMS endpoints). For example, we could use the Bean class to allow objects managed by another framework to be injected into Web Beans.

There are two subclasses of Bean defined by the Web Beans specification: Interceptor and Decorator.

The Context interface supports addition of new scopes to Web Beans, or extension of the built-in scopes to new environments.

public interface Context {

    
    public Class<? extends Annotation> getScopeType();
    
    public <T> T get(Bean<T> bean, boolean create);
    
    boolean isActive();
    
}

For example, we might implement Context to add a business process scope to Web Beans, or to add support for the conversation scope to an application that uses Wicket.