Identify correct and incorrect statements or examples about the EJB context objects that the container provides to 3.0 Session beans and 3.0 Message-Driven beans, including the security, transaction, timer, and lookup services the context can provide.

[Note]

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:

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:

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.

Professional hosting     Belorussian informational portal         Free SCWCD 1.4 Study Guide     Free SCDJWS 1.4 Study Guide     SCDJWS 1.4 Quiz     Free IBM Certified Associate Developer Study Guide     IBM Test 000-287. Enterprise Application Development with IBM WebSphere Studio, V5.0 Study Guide     Free Mock Exam Engine