Using EJBs with Axis - The Session Bean
(Page 3 of 5 )
The session bean has similar interface and implementation classes (see Listings 7.2 and 7.3).
Listing 7.2 SkatesProducts.java Session Bean Interface
public interface SkatesProducts extends
javax.ejb.EJBObject {
public void addProduct(Product prod)
throws java.rmi.RemoteException;
public Product viewProduct(String prodcode)
throws java.rmi.RemoteException;
}
Listing 7.3 SkatesProductsBean.java Session Bean Implementation
import javax.ejb.CreateException;
import javax.ejb.FinderException;
import javax.naming.InitialContext;
import javax.naming.NamingException;
public class SkatesProductsBean
implements javax.ejb.SessionBean {
private javax.ejb.SessionContext mySessionCtx;
public javax.ejb.SessionContext
getSessionContext() {
return mySessionCtx; }
public void setSessionContext
(javax.ejb.SessionContext ctx) {
mySessionCtx = ctx; }
public void ejbCreate() throws
javax.ejb.CreateException {}
public void ejbActivate() {}
public void ejbPassivate() {}
public void ejbRemove() {}
public void addProduct(Product prod) {
try {
InitialContext ic = new InitialContext();
SkatesEntityLocalHome selh =
(SkatesEntityLocalHome)
ic.lookup("java:comp/env/skateEnt");
SkatesEntityLocal skent =
selh.create(prod.getProductCode());
skent.setDescription(prod.getDescription());
skent.setPrice(prod.getPrice());
} catch (NamingException ne) { // handle naming
} catch (CreateException ce) { // handle create
}
}
// viewProduct goes here
}
Because the entity is simply a view on the persistence layer, it utilizes the underlying semantics of a data element (create, read, update, delete). The session bean has the interface we wish to offer to the service requestor. We want to expose two "verbs" add and view to the service requestor. Because the service interface we're designing is stateless, we need to map one (or more) of the parameters of the service request into the unique primary key the entity bean needs.
The implementation code shows some standard code—that is, it's the same in most EJBs. The ejbXXXXXX() methods, such as ejbCreate(), let you override methods that are called by the container during the component's event lifecycle. A more intelligently designed bean would get a reference to the entity at component creation time, reuse it, and therefore be more efficient. However, for this simple example it's easier to get the reference as needed.
In the addProduct() method shown, the logic gets a reference to the entity bean using a local ejb-ref (local name for the entity bean) "java:comp/env/skateEnt". The "java:comp/env/" piece is predefined (it's the J2EE way of saying environment). "skateEnt" is a name we chose to identify the entity bean. This reference is then defined in the DD to point to the real internal name of the entity bean. This capability allows EJBs to be wired together, so a different entity bean (perhaps with a different persistence method) that implemented the same interface contract could be used without recoding our session bean.
The code then calls the create() method on the entity to create a new product instance, using the product code as the primary key. At this point, if there is already a product in the database that has the same key, a createException will be thrown, which will cause a fault in the Web service response.
There is similar code in the viewProduct() method shown here:
public Product viewProduct(String prodcode) {
Product prod = new ProductImpl();
try {
InitialContext ic = new InitialContext();
SkatesEntityHome home =
(SkatesEntityHome)
ic.lookup("java:comp/env/skatesEntity");
SkatesEntity skent =
home.findByPrimaryKey(prodcode);
prod.setProductCode(prodcode);
prod.setPrice(skent.getPrice());
prod.setDescription(skent.getDescription());
}
catch (NamingException ne) { /* deal with exc */}
catch (FinderException fe) { /* deal with exc */}
return prod;
}
This code looks up the existing product instance and then returns the information as a new Product object.
Next: The Deployment Unit >>
More Web Services Articles
More By Sams Publishing
|
This article is excerpted from chapter 7 of the book Building Web Services with Java: Making sense of XML, SOAP, WSDL, and UDDI, written by Steve Graham et al. (Sams; ISBN: 0672326418). Check it out today at your favorite bookstore. Buy this book now.
|
|