Oracle8i Enterprise JavaBeans Developer's Guide and Reference Release 3 (8.1.7) Part Number A83725-01 |
|
import purchase.PurchaseOrder; import purchase.PurchaseOrderHome; import purchase.LineItem; import oracle.aurora.jndi.sess_iiop.ServiceCtx; import javax.naming.Context; import javax.naming.InitialContext; import javax.transaction.UserTransaction; import java.util.*; public class Client { public static void main (String [] args) throws Exception { System.out.println("Running client"); if (args.length != 4) { System.out.println("usage: Client serviceURL objectName user password"); System.exit(1); } String serviceURL = args [0]; String objectName = args [1]; String user = args [2]; String password = args [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); PurchaseOrderHome home = (PurchaseOrderHome)ic.lookup (serviceURL + objectName); // Begin a transaction to create a new PO UserTransaction ut; ut = (UserTransaction)ic.lookup ("local://corbaut"); ut.begin(); // Create a new PO and add items to it PurchaseOrder po = home.create(); po.addItem (111111, 2); po.addItem (333333, 4); // Price the PO System.out.println ("PO price $" + po.price ()); // Get the po number for future reference String ponumber = (String)po.getPrimaryKey (); // Commit the transaction ut.commit(); // This is now the future: // Start another transaction ut.begin (); // Retrieve the PO from its primary key PurchaseOrder po2 = home.findByPrimaryKey(ponumber); // Add another item po.addItem (222222, 1); // Check the PO contents System.out.println ("Contents of the PO:"); Vector items = po.getContents (); Enumeration e = items.elements (); while (e.hasMoreElements ()) { LineItem item = (LineItem)e.nextElement (); System.out.println (item.quantity + " " + item.description + " at $" + (int)item.price + " each"); } // Compute the price again System.out.println ("PO price $" + po.price ()); // Rollback the change ut.rollback (); } }
import javax.ejb.*; import java.rmi.RemoteException; import java.sql.SQLException; public interface PurchaseOrderHome extends EJBHome { // Create a new PO public PurchaseOrder create() throws CreateException, RemoteException; // Find an existing one public PurchaseOrder findByPrimaryKey (String POnumber) throws FinderException, RemoteException; }
package purchase; /* PurchaseOrder is an entity bean. It is a remote interface to a purchasing application. The remote interface lets you manage PO contents (getContents, addItem) and execute complex logic on the server side (price). */ import javax.ejb.EJBObject; import java.rmi.RemoteException; import java.sql.SQLException; import java.util.Vector; public interface PurchaseOrder extends EJBObject { // Price the PO public float price() throws RemoteException; // Manage contents // getContents returns a Vector of LineItem objects public Vector getContents() throws RemoteException; public void addItem (int sku, int count) throws RemoteException; }
package purchaseServer; /* This is the PurchaseOrder Bean. The bean manages its own persistence. The PO line items are stored in the LINEITEMS table. This table references the SKUS table that contain individual pricing information. */ import purchase.*; import java.sql.*; import java.rmi.RemoteException; import javax.ejb.*; import java.util.*; #sql iterator ItemsIter (int skunumber, int count, String description, float price); public class PurchaseOrderBean implements EntityBean { EntityContext ctx; Vector items; // The items in the PO (instances of LineItem) public void PurchaseOrderBean() {} // Bean Managed Persistence methods // The create methods takes care of generating a new PO and returns // its primary key public String ejbCreate () throws CreateException, RemoteException { String ponumber = null; try { #sql { select ponumber.nextval into :ponumber from dual }; #sql { insert into pos (ponumber, status) values (:ponumber, 'OPEN') }; } catch (SQLException e) { throw new PurchaseException (this, "create", e); } return ponumber; } // Nothing to do here public void ejbPostCreate () { items = new Vector (); } // The remove method deletes all line items belonging to the PO public void ejbRemove() throws RemoteException { // Get the PO number and delete String ponumber = (String)ctx.getPrimaryKey(); try { #sql { delete from lineitems where ponumber = :ponumber }; #sql { delete from pos where ponumber = :ponumber }; } catch (SQLException e) { throw new PurchaseException (this, "remove", e); } } // The load method populates the items array with all the existing // line items public void ejbLoad() throws RemoteException { // Get the PO number String ponumber = (String)ctx.getPrimaryKey(); // Load all line items. try { items = new Vector (); ItemsIter iter = null; try { #sql iter = { select lineitems.skunumber, lineitems.count, skus.description, skus.price from lineitems, skus where ponumber = :ponumber and lineitems.skunumber = skus.skunumber }; while (iter.next ()) { LineItem item = new LineItem (iter.skunumber(), iter.count(), iter.description(), iter.price()); items.addElement (item); } } finally { if (iter != null) iter.close (); } } catch (SQLException e) { throw new PurchaseException (this, "load", e); } } // The store method replaces all entries in the lineitems table with the // new entries from the bean public void ejbStore() throws RemoteException { // Get the PO number String ponumber = (String)ctx.getPrimaryKey(); try { // Delete old entries #sql { delete from lineitems where ponumber = :ponumber }; // Insert new entries Enumeration e = items.elements (); while (e.hasMoreElements ()) { LineItem item = (LineItem)e.nextElement (); #sql { insert into lineitems (ponumber, skunumber, count) values (:ponumber, :(item.sku), :(item.quantity)) }; } } catch (SQLException e) { throw new PurchaseException (this, "store", e); } } // The findByPrimaryKey method verifies that the POnumber exists public String ejbFindByPrimaryKey (String ponumber) throws FinderException, RemoteException { try { int count; #sql { select count (ponumber) into :count from pos where ponumber = :ponumber }; // There has to be one if (count != 1) throw new FinderException ("Inexistent PO: " + ponumber); } catch (SQLException e) { throw new PurchaseException (this, "findByPrimaryKey", e); } // The ponumber is the primary key return ponumber; } // Business Methods // Price the PO public float price() throws RemoteException { float price = 0; Enumeration e = items.elements (); while (e.hasMoreElements ()) { LineItem item = (LineItem)e.nextElement (); price += item.quantity * item.price; } // 5% discount if buying more than 10 items if (items.size () > 10) price -= price * 0.05; // Shipping is a constant plus function of the number of items price += 10 + (items.size () * 2); return price; } // The getContents methods has to load the descriptions public Vector getContents() throws RemoteException { return items; } // The add Item method gets the price and description public void addItem (int sku, int count) throws RemoteException { try { String description; float price; #sql { select price, description into :price, :description from skus where skunumber = :sku }; items.addElement (new LineItem (sku, count, description, price)); } catch (SQLException e) { throw new PurchaseException (this, "addItem", e); } } // EntityBean Methods public void setEntityContext(EntityContext ctx) { this.ctx = ctx; } public void unsetEntityContext() {} public void ejbActivate() {} public void ejbPassivate() {} }
package purchase; /* This class represents one line item */ public class LineItem implements java.io.Serializable { public int sku; public int quantity; public String description; public float price; public LineItem (int sku, int quantity, String description, float price) { this.sku = sku; this.quantity = quantity; this.description = description; this.price = price; } }
<?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>no description</description> <ejb-name>test/purchase</ejb-name> <home>purchase.PurchaseOrderHome</home> <remote>purchase.PurchaseOrder</remote> <ejb-class>purchaseServer.PurchaseOrderBean</ejb-class> <persistence-type>Bean</persistence-type> <prim-key-class>java.lang.String</prim-key-class> <reentrant>False</reentrant> </entity> </enterprise-beans> <assembly-descriptor> <security-role> <description>no description</description> <role-name>PUBLIC</role-name> </security-role> <method-permission> <description>no description</description> <role-name>PUBLIC</role-name> <method> <ejb-name>test/purchase</ejb-name> <method-name>*</method-name> </method> </method-permission> <container-transaction> <description>no description</description> <method> <ejb-name>test/purchase</ejb-name> <method-name>*</method-name> </method> <trans-attribute>Required</trans-attribute> </container-transaction> </assembly-descriptor> </ejb-jar>
|
![]() Copyright © 1996-2000, Oracle Corporation. All Rights Reserved. |
|