![]() | |
|
The EJBContext interface provides an instance with access to the container-provided runtime context of an enterprise Bean instance.
This interface is extended by the SessionContext, EntityContext, MessageDrivenContext interfaces to provide additional methods specific to the enterprise interface Bean type.
package javax.ejb; public interface EJBContext { // Lookup a resource within the component's private naming context // "name" - Name of the entry (relative to java:comp/env) public Object lookup(String name); // Get access to the EJB Timer Service // IllegalStateException is thrown for Stateful Session Beans public TimerService getTimerService() throws IllegalStateException; // Security methods public java.security.Principal getCallerPrincipal(); public boolean isCallerInRole(java.lang.String roleName); // Transaction methods public javax.transaction.UserTransaction getUserTransaction() throws IllegalStateException; public boolean getRollbackOnly() throws IllegalStateException; public void setRollbackOnly() throws IllegalStateException; // Deprecated and Obsolete methods public java.security.Identity getCallerIdentity(); public boolean isCallerInRole(java.security.Identity role); public java.util.Properties getEnvironment(); // Obtain the enterprise bean's Remote Home interface public EJBHome getEJBHome() IllegalStateException; // Obtain the enterprise bean's Local Home interface. public EJBLocalHome getEJBLocalHome() IllegalStateException;
The SessionContext Interface
If the bean specifies a dependency on the SessionContext interface (or if the bean class implements the SessionBean interface), the container MUST provide the session bean instance with a SessionContext. This gives the session bean instance access to the instance's context maintained by the container. The SessionContext interface has the following methods:
The getCallerPrincipal method returns the java.security.Principal that identifies the invoker.
The isCallerInRole method tests if the session bean instance's caller has a particular role.
The setRollbackOnly method allows the instance to mark the current transaction such that the only outcome of the transaction is a ROLLBACK. Only instances of a session bean with container-managed transaction (CMT) demarcation can use this method.
The getRollbackOnly method allows the instance to test if the current transaction has been marked for ROLLBACK. Only instances of a session bean with container-managed transaction (CMT) demarcation can use this method.
The getUserTransaction method returns the javax.transaction.UserTransaction interface. The instance can use this interface to demarcate transactions and to obtain transaction status. Only instances of a session bean with bean-managed transaction (BMT) demarcation can use this method.
The getTimerService method returns the javax.ejb.TimerService interface. Only STATELESS session beans can use this method. Stateful session beans CANNOT be timed objects.
The getMessageContext method returns the javax.xml.rpc.handler.MessageContext interface of a stateless session bean that implements a JAX-RPC web service endpoint. Only STATELESS session beans with web service endpoint interfaces can use this method.
The getBusinessObject(Class businessInterface) method returns the session bean's business interface. Only session beans with an EJB 3.0 business interface can call this method.
The getInvokedBusinessInterface method returns the session bean business interface through which the bean was invoked.
The getEJBObject method returns the session bean's remote interface. Only session beans with a remote EJBObject interface can call this method.
The getEJBHome method returns the session bean's remote home interface. Only session beans with a remote home interface can call this method.
The getEJBLocalObject method returns the session bean's local interface. Only session beans with a local EJBLocalObject interface can call this method.
The getEJBLocalHome method returns the session bean's local home interface. Only session beans with a local home interface can call this method.
The lookup method enables the session bean to look up its environment entries in the JNDI naming context.
Explicit Dependency Lookup Through the EJBContext API
A new method, "lookup", has been added to the javax.ejb.EJBContext interface. This method can be used to lookup the resources or references bound in a bean's JNDI environment naming context. The method's signature is:
Object lookup(String name)
The lookup method internally wraps a call to InitialContext.lookup() so that a developer doesn't need to additionally learn the JNDI API. The SessionContext is injected by the EJB Container immediately after the bean is created, and the SessionContext object is then used to lookup resources.
Here's a comparison of explicitly looking up a bean reference in EJB 2.1 and explicitly looking up a bean reference in EJB 3.0 (without annotation).
EJB 2.1:
InitialContext ic = new InitialContext(); Object homeObj = ic.lookup("java:comp/env/ejb/FooEJB"); FooHome fooHome = (FooHome) PortableRemoteObject.narrow(homeObj, FooHome.class); Foo foo = fooHome.create(...);
EJB 3.0:
@Resource SessionContext sc; public void setup(){ Foo foo = (Foo) sc.lookup("ejb/FooEJB"); }
The MessageDrivenContext Interface
If the bean specifies a dependency on the MessageDrivenContext interface (or if the bean class implements the MessageDrivenBean interface), the container must provide the message-driven bean instance with a MessageDrivenContext. This gives the message-driven bean instance access to the instance's context maintained by the container. The MessageDrivenContext interface has the following methods:
The setRollbackOnly method allows the instance to mark the current transaction such that the only outcome of the transaction is a ROLLBACK. Only instances of a message-driven bean with container-managed transaction (CMT) demarcation can use this method.
The getRollbackOnly method allows the instance to test if the current transaction has been marked for ROLLBACK. Only instances of a message-driven bean with container-managed transaction (CMT) demarcation can use this method.
The getUserTransaction method returns the javax.transaction.UserTransaction interface that the instance can use to demarcate transactions, and to obtain transaction status. Only instances of a message-driven bean with bean-managed transaction (BMT) demarcation can use this method.
The getTimerService method returns the javax.ejb.TimerService interface.
The getCallerPrincipal method returns the java.security.Principal that is associated with the invocation.
The isCallerInRole method is inherited from the EJBContext interface. Message-driven bean instances MUST NOT call this method.
The getEJBHome and getEJBLocalHome methods are inherited from the EJBContext interface. Message-driven bean instances MUST NOT call these methods.
The lookup method enables the message-driven bean to look up its environment entries in the JNDI naming context.
Enterprise Bean Context and Environment
The enterprise bean's context comprises its container context and its resource and environment context.
The bean may gain access to references to resources and other environment entries in its context by having the container supply it with those references. In this case, bean instance variables or setter methods are annotated as target for dependency injection.
Alternatively, the lookup method added to the javax.ejb.EJBContext interface or the JNDI APIs may be used to look up resources in the bean's environment.
The same set of metadata annotations are used to express context dependencies for both these approaches.
Annotation of Context Dependencies
A bean declares a dependency upon a resource or other entry in its environment context through a dependency annotation.
A dependency annotation specifies the type of object or resource on which the bean is dependent, its characteristics, and the name through which it is to be accessed.
The following are examples of dependency annotations:
@EJB(name="mySessionBean", beanInterface=MySessionIF.class) @Resource(name="myDB", type=javax.sql.DataSource.class)
Dependency annotations may be attached to the bean class or to its instance variables or methods.
The amount of information that needs to be specified for a dependency annotation depends upon its usage context and how much information can be inferred from that context.
Annotation of Instance Variables
The developer may annotate instance variables of the enterprise bean class to indicate dependencies upon resources or other objects in the bean's environment. The container automatically initializes these annotated variables with the external references to the specified environment objects. This initialization occurs BEFORE any business methods are invoked on the bean instance and AFTER the time the the bean's EJBContext is set.
Example:
@Stateless public class MySessionBean implements MySession { @Resource(name="myDB") //type is inferred from variable public DataSource customerDB; @EJB //reference name and type inferred from variable public AddressHome addressHome; public void myMethod1(String myString) { try { Connection conn = customerDB.getConnection(); ... } catch (Exception ex) { ... } } public void myMethod2(String myString) { Address a = addressHome.create(myString); } }
When the resource type can be determined by the variable type, the annotation need not contain the type of the object to be accessed. If the name for the resource reference in the bean's environment is the same as the variable name, it does not need to be explicitly specified:
@EJB public ShoppingCart myShoppingCart; @Resource public DataSource myDB; @Resource public UserTransaction utx; @Resource SessionContext ctx;
Setter Injection
Setter injection provides an alternative to the container's initialization of variables described above.
When setter injection is to be used, the dependency annotations are applied to setter methods of the bean class defined for that purpose:
@Resource(name="customerDB") public void setDataSource(DataSource myDB) { this.ds = myDB; } @Resource // reference name is inferred from the property name public void setCustomerDB(DataSource myDB) { this.customerDB = myDB; } @Resource public void setSessionContext(SessionContext ctx) { this.ctx = ctx; }
When the resource type can be determined by the parameter type, the annotation need not specify the type of the object to be accessed. If the name of the resource is the same as the property name corresponding to the setter method, it does not need to be explicitly specified.
A setter method that is annotated with the Resource or other dependency annotation will be used by the container for dependency injection. Such setter injection methods will be called by the container BEFORE any business methods are invoked on the bean instance and AFTER the bean's EJBContext is set.
Injection and Lookup
Resources, references to components, and other objects that may be looked up in the JNDI name space may be injected by means of the injection mechanisms listed above.
References to injected objects are looked up name. These lookups are performed in the referencing bean's java:comp/env namespace.
EJBContext
The method Object lookup(String name) is added to the javax.ejb.EJBContext interface. This method can be used to lookup resources and other environment entries bound in the bean's JNDI environment naming context.
Injection of the bean's EJBContext object may be obtained as described above.
![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |