Bean Adapters
One of the most awkward aspects of the EJB bean interface types is
that, in some cases, the callback methods are never used or are not
relevant to the bean at all. A simple container-managed entity bean
might have empty implementations for its
ejbLoad()
, ejbStore()
,
ejbActivate()
, ejbPassivate()
,
or even its setEntityContext()
methods. Stateless
session beans provide an even better example of unnecessary callback
methods: they must implement the ejbActivate()
and
ejbPassivate()
methods even though these methods
are never invoked!
To simplify the appearance of the bean class definitions, we can
introduce
adapter classes that hide callback methods that
are never used or that have minimal implementations. Here is an
adapter for the entity bean that provides empty implementations of
all the EntityBean
methods:
public class EntityAdapter implements javax.ejb.EntityBean { public EntityContext ejbContext; public void ejbActivate(){} public void ejbPassivate(){} public void ejbLoad(){} public void ejbStore(){} public void ejbRemove(){} public void setEntityContext(EntityContext ctx) { ejbContext = ctx; } public void unsetEntityContext() { ejbContext = null; } public EntityContext getEJBContext() { return ejbContext; } }
We took care of capturing the EntityContext
for
use by the subclass. We can do this because most entity beans
implement the context methods in exactly this way. We simply leverage
the adapter class to manage this logic for our subclasses.
The bean class then ...
Get Enterprise JavaBeans, Second Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.