Server-side code example

  

Chapter 27 introduced the Project Info App. That application is continued here with the addition of the ProjectBean code that the servlets access.

The Project EJB

The Project EJB has the standard home and remote interfaces shown in Listing 28-1 and Listing 28-2.

Listing 28-1: ProjectHome.java
start example
 package com.richware.projinfo;     import java.rmi.*; import javax.ejb.*; import java.util.*;     /**  * Interface ProjectHome  * Description: The Project EJB home interface  *  * Copyright:    Copyright (c) 2002  * Company:      HungryMinds  * @author Johennie Helton <jhelton@richware.com>  * @version 1.0   * DISCLAIMER: Please refer to the disclaimer at the beginning of this book. */     public interface ProjectHome extends EJBHome {     public Project create(String projID, String projName,             String projDescription,            double initQuote, double discount,            double actualRate)            throws CreateException, RemoteException;         public Project findByPrimaryKey(String id)         throws RemoteException, FinderException;    public Collection findAll ()          throws RemoteException, FinderException; } 
end example
 

The Project Info application only requires findByPrimaryKey and findAll methods , which are defined in the home interface and implemented by the container - since this is a container-managed bean.

Listing 28-2: Project.java
start example
 package com.richware.projinfo;     import java.rmi.*; import javax.ejb.*;     /**  * Interface Project  * Description: The Project EJB remote interface  *  * Copyright:    Copyright (c) 2002  * Company:      HungryMinds  * @author Johennie Helton <jhelton@richware.com>  * @version 1.0   * DISCLAIMER: Please refer to the disclaimer at the beginning of this book. */     public interface Project extends EJBObject {     public String getProjID() throws RemoteException;     public String getProjName() throws RemoteException;     public String getProjDescription() throws RemoteException;     public double getInitQuote() throws RemoteException;     public double getDiscount() throws RemoteException;     public double getActualRate() throws RemoteException;     public void setProjID(String val) throws RemoteException;     public void setProjName(String val) throws RemoteException;     public void setProjDescription(String val)            throws RemoteException;     public void setInitQuote(double val)            throws RemoteException;     public void setDiscount(double val)            throws RemoteException;     public void setActualRate(double val)           throws RemoteException;  } 
end example
 

The remote interface contains all the methods the bean needs to expose to its clients ; this includes any business methods required. Listing 28-3 shows the ProjectBean.java class.

Listing 28-3: ProjectBean.java
start example
 package com.richware.projinfo;      import java.io.Serializable; import java.util.Collection; import java.util.Vector; import javax.ejb.*; import javax.naming.InitialContext; import javax.naming.NamingException; import javax.sql.DataSource; import java.rmi.*; import java.util.*;     /**  * Class ProjectBean  * Description: The Project EJB class  *  * Copyright:    Copyright (c) 2002  * Company:      HungryMinds  * @author Johennie Helton <jhelton@richware.com>  * @version 1.0   * DISCLAIMER: Please refer to the disclaimer at the beginning of this book. */     public class ProjectBean implements EntityBean  {    protected EntityContext   context;         // flag to determine whether or not the     // bean needs to be written to storage.     private transient boolean isDirty;             // container managed fields     public String projID = null;     public String projName = null;     public String projDescription = null;     public double initQuote = 0.0;     public double discount = 0.0;     public double actualRate = 0.0;         //     // constructor     public ProjectBean() {       System.out.println(        "ProjectBean is created by EJB container.");     }         // EJB required methods     // called after activation by the EJB container         public void ejbActivate() throws RemoteException {         System.out.println("ProjectBean:ejbActivate()");     }         // When the Home Object is called, the Home Object calls     // this method.  Populate the attributes so that the      // container can     // create the database rows needed.         public String ejbCreate(String projID, String projName, String  projDescription,                            double initQuote, double discount,                            double actualRate)                 throws RemoteException {             System.out.println("ProjectBean: ejbCreate ()");             this.projID = projID;          this.projName   = projName;         this.projDescription = projDescription;         this.initQuote = initQuote;         this.discount = discount;         this.actualRate = actualRate;         return this.projID;     }         // updates the object with data from database but      // we are using CMP so we do not need it.     // Just do post-processing needed.     public void ejbLoad() throws RemoteException {         System.out.println("ProjectBean:ejbLoad()");     }         // called before passivation by the EJB container     public void ejbPassivate() throws RemoteException {         System.out.println("ProjectBean:ejbPassivate()");     }         // called after ejbCreate.  The instance has been      // associated with an EJB Object and we can get a      // reference via the context if we     // need to.     public void ejbPostCreate(String projID, String projName,          String projDescription,         double initQuote, double discount,         double actualRate)         throws RemoteException {       System.out.println("ProjectBean:ejbPostCreate()");     }         // this method is called by the container right before     // removing entity information from the database.         public void ejbRemove() throws RemoteException {         System.out.println("ProjectBean:ejbRemove()");     }         // updates the database but we are using CMP so we do not      // need it. Just do preprocessing needed.     public void ejbStore() throws RemoteException {      System.out.println(           "ProjectBean:ejbStore( " + projID + ")");      setModified(false);     }         // keep the context so we can access it later if needed     public void setEntityContext(EntityContext ecxt)         throws RemoteException {      System.out.println("ProjectBean:setEntityContext()");      context = ecxt;     }         // disassociate the context     public void unsetEntityContext() throws RemoteException {         System.out.println("ProjectBean:unsetEntityContext()");         context = null;     }         //     // business methods     public String getProjID() throws RemoteException{         System.out.println("ProjectBean:getID()");        return projID;     }     public void setProjID(String id) throws RemoteException {         System.out.println("ProjectBean:setID()");        this.projID = id;     }     public String getProjName() throws RemoteException {         System.out.println("ProjectBean:getName()");        return projName;      }     public String getProjDescription() throws RemoteException {         System.out.println("ProjectBean:getDescription()");        return projDescription;     }     public double getDiscount() throws RemoteException {         System.out.println("ProjectBean:getDiscount()");        return discount;     }     public double getInitQuote() throws RemoteException {         System.out.println("ProjectBean:getInitQuote()");        return initQuote;     }     public double getActualRate() throws RemoteException {         System.out.println("ProjectBean:getActual()");        return actualRate;     }     public void setProjName(String name) throws RemoteException {         System.out.println("ProjectBean:setProjName()");         setModified(true);         this.projName = name;     }         public void setProjDescription(String name)          throws RemoteException {         System.out.println("ProjectBean:setProjDescription()");         setModified(true);         this.projDescription = name;     }     public void setInitQuote(double initQuote)          throws RemoteException {         System.out.println("ProjectBean:setInitQuote()");         setModified(true);         this.initQuote = initQuote;     }     public void setDiscount(double discount)         throws RemoteException {         System.out.println("ProjectBean:setDisc()");         setModified(true);         this.discount = discount;     }     public void setActualRate(double actualRate)         throws RemoteException {         System.out.println("ProjectBean:setActual()");         setModified(true);         this.actualRate = actualRate;     }     // Returns whether the EJBean has been modified or not.     // This method must be public for the container to be      // able to invoke it.    public boolean isModified() {        return isDirty;    }     // Sets the EJBean's modified flag.     public void setModified(boolean flag) {         isDirty = flag;         System.out.println("setModified(): " + projID + (String) (flag ?  ": requires saving"                   : ": saving not required"));     }  } 
end example
 

The ProjectBean class implements any business methods and any necessary support methods. You identify any security restrictions to these methods at deployment time. The Project Info application does not require these methods to be restricted to different users. In this example, I entrust the Web container to vouch for the identity for users, and the access to the Project EJB is being accessed via secured Web components .

Listing 28-4 provides the Project-cmp-rdbms-jar.xml file that is the deployment descriptor for the User EJB; I used BEA's Weblogic Server 7.0 (WLS) to deploy this application; therefore, there are some WLS-specific tags.

Listing 28-4: Project-cmp-rdbms-jar.xml
start example
 <!DOCTYPE weblogic-rdbms-bean PUBLIC '-//BEA Systems, Inc.//DTD WebLogic  5.1.0 EJB RDBMS      Persistence//EN'  'http://www.bea.com/servers/wls510/dtd/weblogic-rdbms-persistence.dtd'> <weblogic-rdbms-bean>    <pool-name>demoPool</pool-name>    <table-name>EAPROJ</table-name>    <attribute-map>       <object-link>          <bean-field>projID</bean-field>          <dbms-column>projid</dbms-column>       </object-link>       <object-link>          <bean-field>projName</bean-field>          <dbms-column>name</dbms-column>       </object-link>       <object-link>          <bean-field>projDescription</bean-field>          <dbms-column>DESCRIPTION</dbms-column>       </object-link>           <object-link>          <bean-field>initQuote</bean-field>          <dbms-column>initquote</dbms-column>       </object-link>       <object-link>          <bean-field>discount</bean-field>          <dbms-column>discount</dbms-column>       </object-link>       <object-link>          <bean-field>actualRate</bean-field>          <dbms-column>actualrate</dbms-column>       </object-link>    </attribute-map>    <finder-list>       <finder>          <method-name>findByPrimaryKey</method-name>          <method-params>             <method-param>java.lang.String</method-param>          </method-params>          <finder-query><![CDATA[(= projid)]]></finder-query>       </finder>       <finder>          <method-name>findAll</method-name>          <finder-query></finder-query>       </finder>    </finder-list>    <options>       <use-quoted-names>false</use-quoted-names>    </options> </weblogic-rdbms-bean> 
end example
 

Using RMI to access the Project EJB

Listing 28-5 shows a client using RMI to use the Project EJB. This client is very simple and prints the number of projects found in the database via the findAll method. It uses JNDI to look up the home object and JTA to demarcate the transaction. Note that the RMI-IIOP javax.rmi.PortableRemoteObject.narrow method is used to cast returned remote objects.

Listing 28-5: RMIClient.java
start example
 package com.richware.projinfo;     import javax.ejb.*; import javax.naming.*; import javax.rmi.*; import javax.util.Properties; import javax.transaction.UserTransaction;     /**  * Class RMIClient  * Description: The RMI client class code that invokes methods on the  Project EJB   *  * Copyright:    Copyright (c) 2002  * Company:      HungryMinds  * @author Johennie Helton <jhelton@richware.com>  * @version 1.0   * DISCLAIMER: Please refer to the disclaimer at the beginning of this book. */     public class RMIClient {   public static void main(String[] args) {    try {     // Get system properties for JNDI      Properties prop = System.getProperties();     Context ctx = new InitialContext(prop);     ProjectHome home = (ProjectHome)          javax.rmi.PortableRemoteObject.narrow(          ctx.lookup("ProjectHome"),ProjectHome.class);      // now use JNDI to find the JTA UserTransaction interface     // and start the transaction     UserTransaction utr = (UserTransaction)          ctx.lookup("javax.transaction.UserTransaction");      utr.begin();     // create the Project object and use it     Project projs = home.create();     Collection prjcol = projectHome.findAll();     System.out.println("there are " + prjcol.size()            + " projects.");      // now remove the object and commit the transaction     projs.remove();     utr.commit();   } catch (Exception e) {      e.printStackTrace();   }   } } 
end example
 

Using CORBA to access the Project EJB

Listing 28-6 shows a Java CORBA client that accomplishes the same function as the preceding RMI example. It is somewhat more complex and uses COS Naming to look up the home object, and OTS to demarcate the transaction.

Listing 28-6: CORBAClient.java
start example
 package com.richware.projinfo;     import java.util.*; import org.omg.CosNaming.*; import org.omg.CosTransactions.*; import org.omg.CORBA.ORB; import org.omg.CORBA.Object;     /**  * Class CORBAClient  * Description: The CORBA client class code that invokes methods on the  Project EJB   *  * Copyright:    Copyright (c) 2002  * Company:      HungryMinds  * @author Johennie Helton <jhelton@richware.com>  * @version 1.0   * DISCLAIMER: Please refer to the disclaimer at the beginning of this book. */     public class CORBAClient {   public static main(String[] args) throws Exception {     // init the ORB     org.omg.CORBA.ORB orb = org.imprise.ejb.Global.orb();     // obtain naming context      NamingContext nctx = NamingContextHelper.narrow(         orb.resolve_initial_reference("NameService"));     // look up home object     NameComponent[] components = {         new NameComponent(ProjectHome","")}     ProjectHome home = ProjectHomeHelper.narrow(nctx.resolve(         components));     // get the OTS Current interface     Current cTrn = CurrentHelper.narrow (         orb.resolve_initial_references("TransactionCurrent"));     cTrn.begin();     // create the Project object and use it     Project projs = home.create();         Collection prjcol = projectHome.findAll();     System.out.println("there are " + prjcol.size()            + " projects.");      // now remove the object and commit the transaction     projs.remove();     // commit the transaction     cTrn.commit(true);  } } 
end example
 
  


Java Security Solutions
Java Security Solutions
ISBN: 0764549286
EAN: 2147483647
Year: 2001
Pages: 222

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net