In this step you will create a stateless session bean Music, which will be called by client applications. A client application has no direct access to the entity bean Band but calls the session bean instead. The Music bean implements the business logic to respond to a client request, invoke the Band bean to obtain data, and return a result to the client. This use of a session bean as an intermediary is a common design pattern called Session Façade.
The tasks in this step are:
The Music EJB will be invoking methods of the Band EJB. In order for the Music bean to invoke the Band bean's methods, it must first locate and obtain a reference to the bean's home. In this step, you modify the ejbCreate method in MusicBean.ejb to locate and obtain a reference to the Band EJB.
Note. To learn more about about EJB interfaces, referencing and JNDI naming, see the Getting Started: Enterprise JavaBeans tutorial.
To modify ejbCreate to obtain a reference to the Band EJB's local home interface:
public void ejbCreate() { try { javax.naming.Context ic = new InitialContext(); bandHome = (BandHome)ic.lookup("java:comp/env/ejb/BandEJB"); } catch (NamingException ne) { throw new EJBException(ne); } }
import javax.naming.InitialContext; import javax.naming.NamingException;
public class MusicBean extends GenericSessionBean implements SessionBean { private BandHome bandHome; public void ejbCreate() { ...
In the above ejbCreate method, in the lookup method, you use BandEJB to reference the Band EJB. At deployment time this reference is mapped to the actual location of the Band bean. This mapping is done in the deployment descriptor. In WebLogic Workshop you don't modify the deployment descriptor directly but use ejbgen tags inserted in the ejb file instead. To map the reference to Band EJB in the Music bean to the actual Band bean location, you need to insert an ejbgen:ejb-local-ref tag in MusicBean.ejb:
* @ejbgen:ejb-local-ref link="BandEJB"
Notice that the attribute specified in the link property corresponds to the ejb-name of the Band bean. To learn more about this tag, place your cursor anywhere inside the tag in Source View and press F1.
Now you will add the business methods to the Music bean that will be called by the client application. Before you add the getBands and addBand methods, you first need to define Music bean's local interfaces:
/** * @ejbgen:local-method */ public Collection getBands() { try { Iterator bands = bandHome.findAll().iterator(); Collection result = new ArrayList(); while (bands.hasNext()) { Band band = (Band)bands.next(); result.add(band.getName()); } return result;
} catch (FinderException fe) { throw new EJBException(fe); } }
Notice that the getBands method calls uses the BandBean to obtain all the bands currently stored in the database. The method uses the Band EJB's findAll method to find all bands, and returns a Collection of band names. To learn more about finder methods, see Query Methods and EJB QL.
import java.util.ArrayList; import java.util.Collection; import java.util.Iterator;
/** * @ejbgen:local-method */ public void addBand(BandInfo bandInfo) { try { bandHome.create(bandInfo.getName(), bandInfo.getFounder(), bandInfo.getStartDate()); } catch (CreateException ce) { throw new EJBException(ce); } }
Notice that the method calls the Band EJB's create method to add a new band to the database.
If you encounter a build error related to the ejbgen:ejb-local-ref tag, make sure that you have specified the BandBean's name correctly. If you encounter build errors related to the component methods, verify that the methods have been defined correctly and that you have imported the correct classes. A build error might be followed by a deployment error, in particular for WebAppOne_Standard and related to an unresolved ejb-link. The link cannot be resolved because the EJB JAR was not created. Fixing the build problem should resolve this deployment error.
If you encounter deployment errors that seem to be related to one of the web applications, make sure that you build the EJB project and not the entire application (because the application contains page flow applications that have different EJB dependencies from what you have created in this step).
Click one of the following arrows to navigate through the tutorial: