package examples.ejb.extensions.isModified; import java.io.Serializable; import java.rmi.RemoteException; import java.rmi.Remote; import javax.ejb.*; import java.util.*; /** * AccountBean is an EntityBean. This EJBean illustrates: * * * @author Copyright (c) 1998 by WebLogic, Inc. All Rights Reserved. * @author Copyright (c) 1998-1999 by BEA WebXpress. All Rights Reserved. */ public class AccountBean implements EntityBean { final static boolean VERBOSE = true; // ----------------------------------------------------------------- // private variables private transient EntityContext ctx; // ----------------------------------------------------------------- // public container managed variables public String accountId; // also the primary Key public double balance; public String type; // "Checking" private transient boolean isDirty; // ----------------------------------------------------------------- // EntityBean implementation /** * Returns whether the EJBean has been modified or not. * * @return boolean isDirty */ public boolean isModified() { if (VERBOSE) System.out.println ("isModified(): isDirty = " + (isDirty ? "true" : "false")); return isDirty; } /** * Sets the EJBean's modified flag. * * @param flag Modified Flag */ public void setModified(boolean flag) { isDirty = flag; if (VERBOSE) System.out.println("setModified(): " + id() + (String) (flag ? ": requires saving" : ": saving not required")); } /** * Returns the Primary Key identifying this EJBean. * * @return String Identification */ private String id() { return "" + System.identityHashCode(this) + ", PK = " + (String) ((ctx == null) ? "nullctx" : ((ctx.getPrimaryKey() == null ? "null" : ctx.getPrimaryKey().toString()))) + "; isDirty = " + isDirty; } /** * This method is required by the EJB Specification, * but is not used by this example. * */ public void ejbActivate() { if (VERBOSE) System.out.println("ejbActivate(): " + id()); } /** * This method is required by the EJB Specification, * but is not used by this example. * */ public void ejbPassivate() { if (VERBOSE) System.out.println("ejbPassivate(): " + id()); } /** * Sets the EntityContext for the EJBean. * * @param ctx EntityContext */ public void setEntityContext(EntityContext ctx) { if (VERBOSE) System.out.println("setEntityContext(): " + id()); this.ctx = ctx; } /** * Unsets the EntityContext for the EJBean. * */ public void unsetEntityContext() { if (VERBOSE) System.out.println("unsetEntityContext(): " + id()); this.ctx = null; } /** * Sets the EJBean's modified flag to false. * */ public void ejbLoad() { if (VERBOSE) System.out.println("ejbLoad(): " + id()); setModified(false); // to avoid writing } /** * Sets the EJBean's modified flag to false. * */ public void ejbStore() { if (VERBOSE) System.out.println("ejbStore(): " + id()); setModified(false); // to avoid writing } /** * This method is required by the EJB Specification, * but is not used by this example. * * @exception javax.ejb.RemoveException * if the EJBean does not allow removing the EJBean */ public void ejbRemove() throws RemoveException { if (VERBOSE) System.out.println("AccountBean.ejbRemove (" + id() + ")"); } /** * This method corresponds to the create method in the home interface * "AccountHome.java". * The parameter sets of the two methods are identical. When the client calls * AccountHome.create(), the container (which in WebLogic EJB is * also the home) allocates an instance of this EJBean and * calls AccountBean.ejbCreate(). *

* For container-managed persistence, ejbCreate() returns * a void, unlike the case of bean-managed * persistence, where it returns a primary key. * * @param accountID String Account ID * @param initialBalance double Initial Balance * @param type String Account type * @exception javax.ejb.CreateException * if there is a problem creating the bean */ public void ejbCreate(String accountId, double initialBalance, String type) throws CreateException { if (VERBOSE) System.out.println("AccountBean.ejbCreate( id = " + System.identityHashCode(this) + ", PK = " + accountId + ", " + "initial balance = $ " + initialBalance + ", type = " + type + ")"); this.accountId = accountId; this.balance = initialBalance; this.type = type; } /** * This method is required by the EJB Specification, * but is not used by this example. * * @param accountID String Account Identification * @param initialBalance double Initial Balance * @param type String Account type */ public void ejbPostCreate(String accountId, double initialBalance, String type) { if (VERBOSE) System.out.println("ejbPostCreate(): " + id()); } // Application defined methods /** * Adds amount to balance. * * @param amount double Amount * @return double balance */ public double deposit(double amount) { if (VERBOSE) System.out.println("deposit(): Depositing $" + amount + " into '" + accountId + "'"); balance += amount; setModified(true); // to force writing return balance; } /** * Subtracts amount from balance. * * @param amount double Amount * @return double Balance * @exception ProcessingErrorException * if Amount > Balance */ public double withdraw(double amount) throws ProcessingErrorException { if (VERBOSE) System.out.println("withdraw(): Withdrawing $" + amount + " from '" + accountId + "'"); if (amount > balance) { throw new ProcessingErrorException("Request to withdraw $" + amount + "; is more than balance $" + balance + " in account " + accountId); } balance -= amount; setModified(true); // to force writing return balance; } /** * Returns current balance. * * @return double Balance */ public double balance() { if (VERBOSE) System.out.println("balance(): balance $" + balance); return balance; } /** * Returns account type. * * @return String Type */ public String type() { return type; } }