In EAR or WAR deployments, the Web Bean manager is automatically initialized when the EAR or WAR is deployed by the Java EE container. In the Java SE environment, the Web Bean manager is automatically initialized when the embeddable EJB Lite container is initialized.
Web Bean discovery is the process of determining:
What Web Beans, interceptors and decorators exist in the deployment archive
Which Web Beans, interceptors and decorators are enabled for this deployment
The precedence of the enabled Web Beans, and the ordering of enabled interceptors and decorators
The Web Bean manager automatically discovers Web Beans when the Web Bean manager initializes.
Web Bean classes must be deployed in an EAR, WAR, EJB-JAR or JAR archive or directory in the application classpath that has a file named web-beans.xml in the root directory. If Web Beans are deployed to a location that is not in the application classpath, or does not contain a file named web-beans.xml in the root directory, they will not be discovered by the Web Bean manager.
Additional Web Beans may be registered programatically with the Web Bean manager by the application after the manager initializes.
When the Web Bean manager is initialized, it considers:
any web-beans.xml file in any root directory of the application classpath,
any ejb-jar.xml file in any root directory of the application classpath that also has a web-beans.xml file, and
any Java class in any archive or directory in the classpath that has a web-beans.xml file in the root directory.
The Web Bean manager automatically discovers simple Web Beans (according to the rules of Section 3.2.1, “Which Java classes are simple Web Beans?”) and enterprise Web Beans (according to the rules of Section 3.3.1, “Which EJBs are enterprise Web Beans?”) deployed and/or declared in these locations and searches the implementation classes for producer methods and observer methods declared using annotations.
The Web Bean manager discovers Web Beans and observer methods defined using XML by parsing the web-beans.xml files according to the rules of Chapter 9, XML based metadata.
The Web Bean manager validates the Web Bean classes and metadata and aborts initialization if any definition errors exist, as defined in Section 11.1, “Definition errors”.
Next, the Web Bean manager determines which Web Beans, interceptors and decorators are enabled, according to the rules defined in Section 2.5.6, “Enabled deployment types”, Section 6.2.7, “Interceptor enablement and ordering” and Section 6.3.5, “Decorator enablement and ordering”, taking into account any <Deploy>, <Interceptors> and <Decorators> declarations in the web-beans.xml files.
Finally, the Web Bean manager creates and registers Bean objects (that implement the rules of Chapter 5, Web Bean lifecycle) and Observer objects.
For each enabled Web Bean that is not an interceptor or decorator, the Web Bean manager creates an instance of Bean, and registers it by calling Manager.addBean().
For each enabled interceptor, the Web Bean manager creates an instance of Interceptor and registers it by calling Manager.addInterceptor().
For each enabled decorator, the Web Bean manager creates an instance of Decorator and registers it by calling Manager.addDecorator().
For each observer method of an enabled Web Bean, the Web Bean manager creates an instance of Observer that implements the rules of Section 7.5.7, “Observer object for an observer method” and registers it by calling Manager.addObserver().
The Web Bean manager validates the Web Bean dependencies and specialization and aborts initialization if any deployment problems exist, as defined in Section 11.2, “Deployment problems”.
The Manager API provides methods for registering a new Web Bean with the Web Bean manager.
public interface Manager { public Manager addBean(Bean<?> bean); public Manager addInterceptor(Interceptor interceptor); public Manager addDecorator(Decorator decorator); ... }
These methods may be called at any time by the application or third-party framework.
When the Web Bean manager creates a new instance of an enterprise Web Bean, the Web Bean manager obtains the new instance from the EJB container via JNDI or internal Java EE container APIs.
At initialization time, the Web Bean manager inspects the EJB bean class annotations to determine the EJB name. The Web Beans manager uses the EJB name whenever it obtains a new instance of the EJB bean.
The EnterpriseBeanLookup interface provides a single method for obtaining EJB bean instances:
public interface EnterpriseBeanLookup { public Object lookup(String ejbName); }
At initialization time, the Web Bean manager must obtain an instance of EnterpriseBeanLookup by calling Manager.getInstanceByType(), passing EnterpriseBeanLookup as the API type and @Current as the only binding type.
The Web Bean manager must call lookup() on this instance whenever it needs to to obtain a new instance of the EJB bean.
If a null value is returned by lookup(), a CreationException is thrown by the Web Bean manager.
All Web Bean managers must provide a built-in Web Bean that implements EnterpriseBeanLookup.
A plugin Web Bean manager must include a built-in implementation that attempts to obtain the EJB from JNDI.
An integrated Web Bean manager must include a built-in implementation that uses container-specific APIs or default JNDI names.
Any built-in implementation of EnterpriseBeanLookup should have deployment type @Standard and binding type @Current.
The EnterpriseBeanLookup.lookup() method of a built-in implementation for a plugin Web Bean manager obtains EJB beans by JNDI lookup.
If there is an EJB link for the EJB in the web.xml file, the Web Bean manager must use the JNDI name specified by the EJB link during all servlet requests to that web application context, and during any invocation of a remote method of an EJB deployed in the same WAR, any EJB timeout for an EJB deployed in that WAR and any message delivery to a message driven bean deployed in that WAR.
If there is an EJB link for the EJB in an ejb-jar.xml file, the Web Bean manager must use the JNDI name specified by the EJB link during any invocation of a remote method of an EJB deployed in the same EJB-JAR, any EJB timeout for an EJB deployed in that EJB-JAR and any message delivery to a message driven bean deployed in that EJB-JAR.
Otherwise, the Web Bean manager must use the portable global JNDI name defined by the EJB 3.1 specification.
Open issue: is it possible to compute the portable global JNDI name from within the application?
The application may override the built-in EnterpriseBeanLookup implementation by deploying a simple Web Bean that implements EnterpriseBeanLookup and has the binding type @Current. This will usually be necessary when a plugin Web Bean manager is used in a Java EE 5 environment. It is never necessary when an integrated Web Bean manager is used.
Third party frameworks and application components may require notification that the Web Bean manager has been initialized. The Web Bean manager must fire an event when it has fully completed initialization and Web Bean discovery. The event object must be the Manager object, and the event must have the following binding type:
@BindingType @Retention(RUNTIME) @Target( { FIELD, PARAMETER }) public @interface Initialized {}
Any Web Bean may observe this event.
public void managerInitialized(@Observes @Initialized Manager manager) { ... }
A third party framework might take advantage of this event to register Web Beans and interceptors with the Web Bean manager.
The request and application contexts are active when the initialization event is fired.
The Web Bean manager integrates with the Java EE container or embeddable EJB Lite implementation via standard Java EE APIs, such as JNDI, Servlet filters and listeners, EJB interceptors, JSF phase listeners and Unified EL resolvers.
When a plugin Web Bean manager is used in a Java EE 5 environment, certain Web Bean manager specific entries may be required in web.xml and ejb-jar.xml. These entries are never needed when an integrated Web Bean manager is used.
Open issue: currently, entries in Java EE XML deployment descriptors are required to register servlet filters, startup listeners and EJB interceptors. We need a way for a plugin Web Bean manager to register these things programatically, or using XML embedded in the Web Bean manager JAR.