Implementation
This section describes how local interfaces are implemented
in the FBS 10g. The Design section describes how OTN developers
decided where to use local interfaces in the FBS 10g.
The figure below shows how various elements interact when
a client creates an EJB instance and calls methods through a local interface.
When a client invokes create (which returns an interfacein
this case, a local interface) on the Home Interface, the EJB Container calls
ejbCreate to instantiate the bean. Then the client can access the
bean through the interface returned by create.
In the FBS 10g, AlertsBean, PreferencesBean, TradeDetails, and
PortfolioBean are local EJBs with corresponding local interfaces. UserAccount
is a local Entity Object which has 1:N relationships with Alerts, Preferences,
TradeDetails, and Portfolio records. This section focuses on the PreferencesBean
implementation, but the same principles apply to the others.
FBS 10g users can set preferences to indicate what information
(news, a price quote, or both) they want for specified stock ticker symbol.For
example, a person who is thinking of buying Oracle stock might want a quote
for the symbol ORCL, while someone who already owns Oracle stock might want
to see news items to keep abreast of developments at the company. This functionality
is implemented in the following files:
| File |
Description |
PreferencesHomeLocal
|
Local Home interface, extends javax.ejb.EJBLocalHome,
declares two methods: create and findByPrimarykey. |
PreferencesLocal
|
Local interface, extends javax.ejb.EJBLocalObject,
declares methods to get and set account number, ticker symbol, and preference.
|
PreferencesBean
|
Bean implementation class, implements javax.ejb.EntityBean. |
PreferencesInfo
|
Encapsulates preference data for a particular
user account, implements java.io.Serializable. |
The following listings show how a client class works with
local interfaces. The process begins in the ejbCreate method with
a call to InitialContext.lookup. This is a standard EJB callthe
InitialContext class provides a context for performing naming operations.
However, with local interfaces, the client and the bean are in the same container,
so the client knows where to find the bean. As a result, the location can be
hard-coded into the lookup call.
public class UserManagementSessionFacadeBean implements javax.ejb.SessionBean { ...
/** Reference to Local Home Interface of UserAccountBean */ UserAccountHomeLocal userAccountHomeLocal = null; /** Reference to Local Home Interface of PreferencesBean */ PreferencesHomeLocal preferencesHomeLocal = null; ... public void ejbCreate() { try { InitialContext ic = new InitialContext(); ... preferencesHomeLocal = (PreferencesHomeLocal)ic.lookup("java:comp/env/ejb/PreferencesHomeLocal"); ... } }
...
}
Once the client has a reference to the bean's local Home interface,
it can call create to get a reference to the bean's local interface, and with
that, can call the bean's methods.
public class UserManagementSessionFacadeBean implements javax.ejb.SessionBean { ... public String addPreferences(Integer accountNumber, PreferencesInfo preferencesInfo) throws RemoteException { ... if (!symbolPresent && validSymbol) { PreferencesLocal preferencesLocal = preferencesHomeLocal.create(this.getNextID("PREFERENCES_SEQ"), accountNumber, preferencesInfo.getSymbol(), preferencesInfo.getPreferenceType()); allPrefIter.add(preferencesLocal); // allPrefIter is an ArrayList } ... } ... }
The code below shows how a client can call methods from a
bean's local interface. The call to userAccountLocal.getPreferences
returns a collection of PreferencesLocal interfaces, one interface
for each of the user's preferences. The code then iterates over this collection,
each time retrieving a reference to a PreferencesLocal interface
and using it to call getSymbol and getPrefType.
public class UserManagementSessionFacadeBean implements javax.ejb.SessionBean { ... public Collection getPreferences(Integer accountNumber) throws RemoteException { Collection allPreferences = new ArrayList(); try { UserAccountLocal userAccountLocal = userAccountHomeLocal.findByPrimaryKey(accountNumber);
// Get all the preferences and Loop through them
Iterator prefLocalIter = (userAccountLocal.getPreferences()).iterator();
while (prefLocalIter.hasNext()) {
// Get the next handle to PreferencesBean Local EJB Object
PreferencesLocal preferencesLocal = (PreferencesLocal) prefLocalIter.next();
// Add a new instance of PreferencesInfo Value Object to final list
allPreferences.add(new PreferencesInfo(preferencesLocal.getSymbol(),
preferencesLocal.getPrefType()));
}
} catch (FinderException ex) { // Trap Errors While Finding EJB Objects
... }
return allPreferences; // Return All Preferences
}
...
}
|