SKU Enterprise Java Bean source code and helper files

  1. The following code is the implementation of this EJB.
    // SKUBean.java
    
    package server;
    
    import skucommon.*;
    import java.sql.*;
    import java.util.*;
    import java.rmi.RemoteException;
    import java.io.Serializable;
    import javax.ejb.*;
    
    public class SKUBean implements EntityBean {
      private transient EntityContext ctx;
      public String skunumber;
      public String description;
      public String price;
    
      public String getSKUNumber() throws RemoteException {
        return skunumber;
      }
    
      public void setSKUNumber(String number) throws RemoteException {
        this.skunumber = number;
      }
    
      public String getDescription() throws RemoteException {
        return description;
      }
    
      public void setDescription(String description) throws RemoteException {
        this.description = description;
      }
    
      public String getPrice() throws RemoteException {
        return price;
      }
    
      public void setPrice(String price) throws RemoteException {
        this.price = price;
      }
    
      public void setEntityContext(EntityContext ctx) {
        this.ctx = ctx;
        Properties props = ctx.getEnvironment();
      }
    
      public void unsetEntityContext() {
        this.ctx = null;
      }
    
      public String ejbCreate(String number, String desc, String p) 
                    throws CreateException, RemoteException {
        try {
          setSKUNumber(number);
          setDescription(desc);
          setPrice(p);
        } catch (java.rmi.RemoteException e) {
          throw new CreateException();
        }
    		return null;
      }
    
      public String ejbFindByPrimaryKey(String pk) 
                    throws RemoteException, FinderException {
        return null;
      }
    
      public void ejbPostCreate(String number, String desc, String p) 
                throws CreateException {
        // get primarykey
        String pk = (String)ctx.getPrimaryKey();
      }
    
      public void ejbLoad() {
        // You can get to the primary key
        String pk = (String)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.
    
    // SKUHome.java
    
    package skucommon;
    
    import java.rmi.RemoteException;
    import javax.ejb.*;
    
    public interface SKUHome extends EJBHome {
      public SKURemote findByPrimaryKey(String pk)
        throws RemoteException, FinderException;
      public SKURemote findByWhere(String whereString)
        throws RemoteException, FinderException;
      public java.util.Enumeration findAllSkus(String whereString)
        throws RemoteException, FinderException;
      public SKURemote create(String number, String desc, String p)
        throws RemoteException, CreateException;
    }
            
  3. The following code is the remote interface of this EJB.
    // SKURemote.java
    
    package skucommon;
    
    import java.rmi.RemoteException;
    import javax.ejb.EJBObject;
    
    public interface SKURemote extends EJBObject {
      public String getSKUNumber() throws RemoteException;
      public String getDescription() throws RemoteException;
      public void setDescription(String description) throws RemoteException;
      public String getPrice() throws RemoteException;
      public void setPrice(String p) throws RemoteException;
    }
            
  4. 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 skucommon.*;
    
    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);
          SKUHome ch = (SKUHome) ic.lookup(GIOP_SERVICE + ejbPubname);
    
          Enumeration e = ch.findAllSkus("");
          while(e.hasMoreElements()) {
            SKURemote sku = (SKURemote) e.nextElement();
            System.out.println (" skunumber: " + sku.getSKUNumber() + " Price: "  + 
              sku.getPrice() + " Description: " + sku.getDescription());
          }
        } 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();
        }
      }
    }
            
  5. There are two XML files that you need for deployment of this EJB. The first one is called SKU.xml, the other is SKUMap.xml. Here is the content of SKU.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>**SKU Bean**</description>
             <ejb-name>SKUBean</ejb-name>
             <home>skucommon.SKUHome</home>
             <remote>skucommon.SKURemote</remote>
             <ejb-class>server.SKUBean</ejb-class>
             <persistence-type>Container</persistence-type>
             <prim-key-class>java.lang.String</prim-key-class>
             <reentrant>False</reentrant>
             <cmp-field><field-name>skunumber</field-name></cmp-field>
             <cmp-field><field-name>description</field-name></cmp-field>
             <cmp-field><field-name>price</field-name></cmp-field>
             <primkey-field>skunumber</primkey-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>**SKU Role**</description>
             <role-name>PUBLIC</role-name>
          </security-role>
          <method-permission>
             <description>**SKU Permissions**</description>
             <role-name>PUBLIC</role-name>
             <method>
                <ejb-name>SKUBean</ejb-name>
                <method-name>*</method-name>
             </method>
          </method-permission>
          <container-transaction>
             <description>**SKU Transaction**</description>
             <method>
                <ejb-name>SKUBean</ejb-name>
                <method-name>*</method-name>
             </method>
             <trans-attribute>RequiresNew</trans-attribute>
          </container-transaction>
       </assembly-descriptor>
    </ejb-jar>
    
            
    And here is the content of SKUMap.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>SKUBean</ejb-name>
            <jndi-name>test/SKUBean</jndi-name>
          </ejb-mapping>
          <security-role-mapping>
             <security-role>
               <description>**SKU 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>SKUBean</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>SKUS</table>
            <attr-mapping>
              <field-name>skunumber</field-name>
              <column-name>SKUNUMBER</column-name>
            </attr-mapping>
            <attr-mapping>
              <field-name>description</field-name>
              <column-name>DESCRIPTION</column-name>
            </attr-mapping>
            <attr-mapping>
              <field-name>price</field-name>
              <column-name>PRICE</column-name>
            </attr-mapping>
          </psi-ri>
        </persistence-descriptor>
      </oracle-descriptor>
    </oracle-ejb-jar>
            
  6. 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% skucommon/*.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 skucommon/*.class
    
    REM client.jar
    jar cfM client.jar client/*.class
    
    REM deploy
    call deployejb -addclasspath %MAKE_CLASSPATH% -republish -temp temp -generated skucommon.jar -u %USER%/%PASS% -s %GIOP_SERVICE% -descriptor SKU.xml -oracledescriptor SKUMap.xml server.jar
    
    :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/SKUBean
    set PUBNAME=%EJB_PUBNAME%
    
    REM run
    java  -classpath "%MAKE_CLASSPATH:"=%;skucommon.jar" client.Client %USER% %PASS% %GIOP_SERVICE% %PUBNAME%
    
    :DOSEXIT
    @endlocal & popd & set RET=%RET%