LineItems Enterprise Java Bean source code and helper files

  1. The following code is the implementation of this EJB.
    // LineItemsBean.java
    
    package server;
    
    import licommon.*;
    import java.sql.*;
    import java.util.*;
    import java.rmi.RemoteException;
    import java.io.Serializable;
    import javax.ejb.*;
    
    public class LineItemsBean implements EntityBean {
      private transient EntityContext ctx;
      public String ponumber;
      public String skunumber;
      public String count;
    
      public String getPONumber() throws RemoteException {
        return ponumber;
      }
    
      public void setPONumber(String number) throws RemoteException {
        this.ponumber = number;
      }
    
      public String getSKUNumber() throws RemoteException {
        return skunumber;
      }
    
      public void setSKUNumber(String number) throws RemoteException {
        this.skunumber = number;
      }
    
      public String getCount() throws RemoteException {
        return count;
      }
    
      public void setCount(String count) throws RemoteException {
        this.count = count;
      }
    
      public void setEntityContext(EntityContext ctx) {
        this.ctx = ctx;
        Properties props = ctx.getEnvironment();
      }
    
      public void unsetEntityContext() {
        this.ctx = null;
      }
    
      public LineItemsPK ejbCreate(String pnumber, String snumber, String cou) 
                  throws CreateException, RemoteException {
        try {
          setPONumber(pnumber);
          setSKUNumber(snumber);
          setCount(cou);
        } catch (java.rmi.RemoteException e) {
          throw new CreateException();
        }
        return null;
      }
    
      public LineItemsPK ejbFindByPrimaryKey(LineItemsPK pk) 
                        throws RemoteException, FinderException {
        return null;
      }
    
    
      public void ejbPostCreate(String pnumber, String snumber, String cou) 
                                throws CreateException {
        // get primarykey
        LineItemsPK pk = (LineItemsPK)ctx.getPrimaryKey();
      }
    
      public void ejbLoad() {
        // You can get to the primary key
        LineItemsPK pk = (LineItemsPK)ctx.getPrimaryKey();
      }
    
      public void ejbActivate() {}
      public void ejbPassivate() {}
      public void ejbRemove() {}
      public void ejbStore() {}
    }
            
  2. The following code is the home interface of this EJB.
    
    // LineItemsHome.java
    
    package licommon;
    
    import java.rmi.RemoteException;
    import javax.ejb.*;
    
    public interface LineItemsHome extends EJBHome {
      public LineItemsRemote findByPrimaryKey(LineItemsPK pk)
        throws RemoteException, FinderException;
      public LineItemsRemote findByWhere(String whereString)
        throws RemoteException, FinderException;
      public java.util.Enumeration findAllLineItems(String whereString)
        throws RemoteException, FinderException;
      public LineItemsRemote create(String pnumber, String snumber, String cou)
        throws RemoteException, CreateException;
    }
            
  3. The following code is the remote interface of this EJB.
    // LineItemsRemote.java
    
    package licommon;
    
    import java.rmi.RemoteException;
    import javax.ejb.EJBObject;
    
    public interface LineItemsRemote extends EJBObject {
      public String getPONumber() throws RemoteException;
      public String getSKUNumber() throws RemoteException;
      public String getCount() throws RemoteException;
      public void setCount(String p) throws RemoteException;
    }
            
  4. As mentioned earlier LineItems EJB needs a special class that buils a primary-key for its table. The following code is the source of this special class.
    //LineItemsPK.java
    package licommon;
    
    public class LineItemsPK implements java.io.Serializable {
      
      //purchaseorder number
      public String ponumber;
    
      //skunumber
      public String skunumber;
    
      //empty constructor
      public LineItemsPK ( ) { }
    
      public boolean equals(Object obj) {
        if ((obj instanceof LineItemsPK) &&
            (((LineItemsPK)obj).ponumber == this.ponumber) &&
            (((LineItemsPK)obj).skunumber == this.skunumber))
          return true;
        return false;
      }
    
      public int hashCode() {
        return ((ponumber + skunumber).hashCode());
      }
    }
            
  5. We have also created a simple Java client for this EJB so after deploying the EJB you can run the client to make sure it has been deployed properly. The following code is the Java client of this EJB.
    // Client.java
    
    package client;
    
    import licommon.*;
    
    import java.util.Hashtable;
    import java.util.Enumeration;
    import java.rmi.RemoteException;
    import javax.naming.InitialContext;
    import javax.naming.Context;
    import javax.naming.NamingException;
    import javax.ejb.RemoveException;
    import javax.ejb.CreateException;
    import javax.ejb.FinderException;
    import oracle.aurora.jndi.sess_iiop.ServiceCtx;
    
    public class Client {
      public static void main(String[] argv) {                
        System.out.println("client is running");
        try {
          if (argv.length != 4) {
            System.out.println("usage: Client user password GIOP_SERVICE ejbPubname");
            System.exit(1);
          }
          String user = argv[0];
          String password = argv[1];
          String GIOP_SERVICE = argv[2];
          String ejbPubname = argv[3];
    
          Hashtable env = new Hashtable();
          env.put(Context.URL_PKG_PREFIXES, "oracle.aurora.jndi");
          env.put(Context.SECURITY_PRINCIPAL, user);
          env.put(Context.SECURITY_CREDENTIALS, password);
          env.put(Context.SECURITY_AUTHENTICATION, ServiceCtx.NON_SSL_LOGIN);
          Context ic = new InitialContext(env);
          LineItemsHome ch = (LineItemsHome) ic.lookup(GIOP_SERVICE + ejbPubname);
    
          Enumeration e = ch.findAllLineItems("");
          while(e.hasMoreElements()) {
            LineItemsRemote items = (LineItemsRemote) e.nextElement();
            System.out.println ("ponumber: " + items.getPONumber() + 
              " skunumber: " + items.getSKUNumber() + 
              " Count: " + items.getCount());
          }
        } catch (RemoteException e) {
          System.out.println("RemoveException caught:" + e);
          e.printStackTrace();
        } catch (NamingException e) {
          System.out.println("NamingException caught:" + e);
          e.printStackTrace();
        } catch (FinderException e) {
          System.out.println("FinderException caught:" + e);
          e.printStackTrace();
        }
      }
    }
            
  6. There are two XML files that you need for deployment of this EJB. The first one is called LineItems.xml, the other is LineItemsMap.xml. Here is the content of LineItems.xml:
    <?xml version="1.0"?>
    <!DOCTYPE ejb-jar PUBLIC "-//Sun Microsystems Inc.//DTD Enterprise JavaBeans 1.1//EN" "ejb-jar.dtd">
    <ejb-jar>
       <enterprise-beans>
          <entity>
             <description>**LineItems Bean**</description>
             <ejb-name>LineItemsBean</ejb-name>
             <home>licommon.LineItemsHome</home>
             <remote>licommon.LineItemsRemote</remote>
             <ejb-class>server.LineItemsBean</ejb-class>
             <persistence-type>Container</persistence-type>
             <prim-key-class>licommon.LineItemsPK</prim-key-class>
             <reentrant>False</reentrant>
             <cmp-field><field-name>ponumber</field-name></cmp-field>
             <cmp-field><field-name>skunumber</field-name></cmp-field>
             <cmp-field><field-name>count</field-name></cmp-field>
             <resource-ref>
               <res-ref-name>DataSource</res-ref-name>
               <res-type>javax.sql.DataSource</res-type>
               <res-auth>Application</res-auth>
             </resource-ref>
          </entity>
       </enterprise-beans>
       <assembly-descriptor>
          <security-role>
             <description>**LineItems Role**</description>
             <role-name>PUBLIC</role-name>
          </security-role>
          <method-permission>
             <description>**LineItems Permissions**</description>
             <role-name>PUBLIC</role-name>
             <method>
                <ejb-name>LineItemsBean</ejb-name>
                <method-name>*</method-name>
             </method>
          </method-permission>
          <container-transaction>
             <description>**LineItems Transaction**</description>
             <method>
                <ejb-name>LineItemsBean</ejb-name>
                <method-name>*</method-name>
             </method>
             <trans-attribute>RequiresNew</trans-attribute>
          </container-transaction>
       </assembly-descriptor>
    </ejb-jar>
            
    And here is the content of LineItemsMap.xml:
    <?xml version="1.0"?>
    <!DOCTYPE oracle-ejb-jar PUBLIC "-//Sun Microsystems Inc.//DTD Enterprise JavaBeans 1.1//EN" "oracle-ejb-jar.dtd">
    <oracle-ejb-jar>
      <oracle-descriptor>
        <mappings>
          <ejb-mapping>
            <ejb-name>LineItemsBean</ejb-name>
            <jndi-name>test/LineItemsBean</jndi-name>
          </ejb-mapping>
          <security-role-mapping>
             <security-role>
               <description>**LineItems Role**</description>
               <role-name>OraclePublicRole</role-name>
             </security-role>
             <oracle-role>PUBLIC</oracle-role>
          </security-role-mapping>
          <resource-ref-mapping>
            <res-ref-name>DataSource</res-ref-name>
            <jndi-name>test/DataSource/testds</jndi-name>
          </resource-ref-mapping>
          <transaction-manager>
            <default-enlist>True</default-enlist>
          </transaction-manager>
        </mappings>
        <persistence-provider>
          <description>**Persistence Provider**</description>
          <persistence-name>psi-ri</persistence-name>
          <persistence-deployer>oracle.aurora.ejb.persistence.ocmp.OcmpEntityDeployer</persistence-deployer>
        </persistence-provider>
        <persistence-descriptor>
          <description>**Persistence Descriptor**</description>
          <ejb-name>LineItemsBean</ejb-name>
          <persistence-name>psi-ri</persistence-name>
          <persistence-param>test param 1</persistence-param>
          <persistence-param>test param 2</persistence-param>
          <psi-ri>
            <schema>ICS</schema>
            <table>LINEITEMS</table>
            <attr-mapping>
              <field-name>ponumber</field-name>
              <column-name>PONUMBER</column-name>
            </attr-mapping>
            <attr-mapping>
              <field-name>skunumber</field-name>
              <column-name>SKUNUMBER</column-name>
            </attr-mapping>
            <attr-mapping>
              <field-name>count</field-name>
              <column-name>COUNT</column-name>
            </attr-mapping>
          </psi-ri>
        </persistence-descriptor>
      </oracle-descriptor>
    </oracle-ejb-jar>
            
  7. Also you need two batch files to build and test this EJB. For building the EJB we have created a batch file called "makeit.bat, for testing the EJB there is a batch file called "runit.bat". Here is the content of makeit.bat:
    REM makeit.bat
    @echo off
    @if not "%ECHO%"=="" echo %ECHO%
    @setlocal & pushd & set RET=
    set ORACLE_HOME=C:\oracle\ora90
    call %ORACLE_HOME%\javavm\demo\common.bat
    if not "%RET%"=="1" goto :DOSEXIT
    
    REM set classpath local to this script
    set MAKE_CLASSPATH=%JDK12_CLASSPATH%
    
    REM  published JNDI name of remote object
    set EJB_PUBNAME=/test/SKUBean
    set PUBNAME=%EJB_PUBNAME%
    
    REM COMMON_CLASS
    javac -g -classpath %MAKE_CLASSPATH% licommon/*.java
    
    REM SERVER_CLASS
    javac -g -classpath %MAKE_CLASSPATH% server/*.java
    
    REM CLIENT_CLASS
    javac -g -classpath %MAKE_CLASSPATH% client/*.java
    
    REM server.jar
    jar cf server.jar server/*.class licommon/*.class
    
    REM client.jar
    jar cfM client.jar client/*.class
    
    REM deploy
    call deployejb -addclasspath %MAKE_CLASSPATH% -republish -temp temp -generated licommon.jar -u %USER%/%PASS% -s %GIOP_SERVICE% -descriptor LineItems.xml -oracledescriptor LineItemsMap.xml server.jar
    
    jar uf licommon.jar licommon/LineItemsPK.class
    
    :DOSEXIT
    @endlocal & popd & set RET=%RET%
            
    And here is the content of runit.bat:
    REM runit.bat
    @echo off
    @if not "%ECHO%"=="" echo %ECHO%
    @setlocal & pushd & set RET=
    set ORACLE_HOME=C:\oracle\ora90
    call %ORACLE_HOME%\javavm\demo\common.bat
    if not "%RET%"=="1" goto :DOSEXIT
    
    REM set classpath local to this script
    set MAKE_CLASSPATH=%JDK12_CLASSPATH%
    
    REM  published JNDI name of remote object
    set EJB_PUBNAME=/test/LineItemsBean
    set PUBNAME=%EJB_PUBNAME%
    
    REM run
    java  -classpath "%MAKE_CLASSPATH:"=%;licommon.jar" client.Client %USER% %PASS% %GIOP_SERVICE% %PUBNAME%
    
    :DOSEXIT
    @endlocal & popd & set RET=%RET%