The session context

Our EJB have with them a context containing all the memory information of the current bean. The state of the bean is propagated thanks to this context and we can manipulate it inside our EJB container. The session context can be injected in our bean as a resource:

 @Resource
private SessionContext context;

From it we can receive the following objects:

  • Principal: The object containing the user logged information if the authentication is present
  • Homes: We can estrapolate the implemented home interfaces if they are present
  • Remote and local interfaces: We can estrapolate the local and remote interfaces if they are present
  • Context data: The map where we register the variables of our bean. If the bean is a stateful they will be persisted in all the lifecycles of the bean

We have the isCallerInRole() function too. In our EJB we can query a role and see if our EJB has the correct permissions.

The session context can be manipulated implementing the SessionBean interface too:

@Stateless(name = "ejb21EngineLocal")
@LocalHome(value = Ejb21LocalHome.class)
@Local(value = Ejb21Local.class)
public class Ejb21EngineLocalBean implements Ejb21Local, SessionBean {
...
@Override
public void setSessionContext(SessionContext ctx) throws EJBException, RemoteException {... }
@Override
public void ejbRemove() throws EJBException, RemoteException {... }
@Override
public void ejbActivate() throws EJBException, RemoteException {... }
@Override
public void ejbPassivate() throws EJBException, RemoteException {... }
...}

Through this interface we can:

  • Set a custom session context. It will be set during the first operation of a bean.
  • Define new operations after the removal of the bean.
  • Define new operations after the activation or passivation of the bean. These operations work when the bean exits the pool or enter again depending on the invocations of the clients. They are useful only for the statefuls.
This class is a very old feature deriving from EJB 1.1. The RemoteException has no sense in a local bean. So it will be ignored by the local beans.

A modern and better mode to manage the activation and the passivation is provided since EJB 3.0. Simply add these annotations to your methods:

@Remove
public void remove() {
logger.info("the bean is removing");
}
@PostActivate
public void activate() {
logger.info("the bean is active");
}
@PrePassivate
public void passivate() {
logger.info("the bean is passivating");
}

EJB 3.x provides the EntityBean interface to create an old style Entity 2.x. Here's a sample:

public class Topic implements EntityBean {
@Override
public void setEntityContext(EntityContext ctx) throws EJBException, RemoteException {...}
@Override
public void unsetEntityContext() throws EJBException, RemoteException {...}
@Override
public void ejbRemove() throws RemoveException, EJBException, RemoteException {...}
@Override
public void ejbActivate() throws EJBException, RemoteException {...}
@Override
public void ejbPassivate() throws EJBException, RemoteException {...}
@Override
public void ejbLoad() throws EJBException, RemoteException {...}
@Override
public void ejbStore() throws EJBException, RemoteException {...}

This monumental class finds its sense during a migration from the Entity 2.x to the JPA API. Since Java EE 6, they decided to remove the retro compatibility and leave only the essentials for the migrations. This class as the SessionBean is one of them.

Implementing the remote interface as EJBObject, we find other old methods useful to manipulate the session context:

@Override
public EJBHome getEJBHome() throws RemoteException {...}
@Override
public Object getPrimaryKey() throws RemoteException {...}
@Override
public void remove() throws RemoteException, RemoveException {...}
@Override
public Handle getHandle() throws RemoteException {...}
@Override
public boolean isIdentical(EJBObject ejbo) throws RemoteException {...}

They can be considered as utilities. They are an alternative mode to get the order of the methods:

  • The remote home interface
  • An identifier of the bean as primary key
  • An additional remove operations
  • An handler that returns the remote interface of the bean
  • An utility to compare the remote interfaces of the bean

All these utilities don't work in the container. They can be used only programmatically. It's a similar thing for the EJBLocalObject:

@Override
public EJBLocalHome getEJBLocalHome() throws EJBException {...}
@Override
public Object getPrimaryKey() throws EJBException {...}
@Override
public void remove() throws RemoveException, EJBException {... }
@Override
public boolean isIdentical(EJBLocalObject obj) throws EJBException {... }

It's a local interface so the RemoteException is excluded.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.225.31.77