Web Services
  Home arrow Web Services arrow Page 3 - Using EJBs with Axis
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
WEB SERVICES

Using EJBs with Axis
By: Sams Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 5
    2006-05-25

    Table of Contents:
  • Using EJBs with Axis
  • Using EJBs from Axis
  • The Session Bean
  • The Deployment Unit
  • Exposing the EJBs via Axis

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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.

    More Web Services Articles
    More By Sams Publishing


       · This article is an excerpt from the book "Building Web Services with Java: Making...
     

    Buy this book now. 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.

    WEB SERVICES ARTICLES

    - Safety, Idempotence, and the Resource-Orient...
    - The Resource-Oriented Architecture in Action
    - Features of the Resource-Oriented Architectu...
    - The Resource-Oriented Architecture
    - Getting Started with Flex
    - Automated Billing and Faxing for the Web
    - An Introduction to Web Services
    - The Foundations of Web Services: From Novice...
    - Web Services Reengineering: Finishing Touches
    - Fault Handling with Web Services
    - Flow and Web Services
    - Process Lifecycles and Web Services
    - Business Processes and Web Services
    - Orchestrating Web Services
    - Notifications and Resources in the WS-Resour...







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek