Java Tutorial - Java Scipt : Creating the Home and LocalHome Interfaces

Java Tutorial - Java Scipt :

Creating the Home and LocalHome Interfaces


Normally, an entity bean should never be referenced from outside of the EJB container. Instead, session beans residing in the same container should use the entity bean and provide a facade layer for the outside world. So for entity beans, we want to create a LocalHome interface that session beans can use to create and retrieve instances of the entity bean. If we were going to expose this bean outside of the container, we would also create a home interface. In our case, we will create a home interface just so that we have a convenient means of testing the bean.
GUIDS AND JBOSS
The JBoss GUID( ) function does not create a standard GUID. A standard GUID is a hex representation of a 128-bit unsigned integer. This 128-bit number can be represented as 16 bytes. Normally, each byte is translated into two hexadecimal digits and represented as a string with dashes inserted to aid in the reading. By convention, the 128 bits are constructed from a combination of the network card Media Access Control (MAC) address, the system clock down to the current tenth of a millisecond, and a random number. This combination is supposed to guarantee uniqueness for the next 100 years or so. Although there are a number of Java-based GUID generators, most true GUID generators are unsuitable for use in an EJB environment because a native library is needed to read the MAC address from the network card. Some GUID generators get around this by having the MAC address optionally provided as a configuration parameter. Other programs use different values to replace the MAC address. Floyd Marinescu provides a good example of an alternative GUID generator in the book EJB Design Patterns (Wiley Publishing, Inc. 2002). The JBoss GUID generator, although not standard, does generate keys that are the correct length and are unique. But notice that the strings are not hexadecimal digits. Still, this is good enough for our current needs.

The following code shows the LocalHome interface:

package com.oldfriends.ejb;
import javax.ejb.FinderException;
public interface UserLocalHome extends javax.ejb.EJBLocalHome
{
public LocalUser create(
String firstName,
String lastName,
String mi,
String maidenName,
String loginId,
String email,
String password
);
public LocalUser create( com.oldfriends.user.UserDTO userBean );
public LocalUser findByPrimaryKey(int id) throws FinderException;
public LocalUser findByLoginId(String id) throws FinderException;
public LocalUser findByEmail(String email) throws FinderException;
}
The LocalHome interface provides two create methods matching the ejbCreate methods on our bean. We also expose three finder methods:

findByPrimaryKey: This retrieves an instance based on the userid or primary key of the record.

findByLoginId: This retrieves an instance based on the login_id column.

findByEmai:. This retrieves an instance based on the email address.

The container will implement these methods based on values in our deployment descriptor when we deploy the EJB.
The following code shows the Home interface:
package com.oldfriends.entity;
import com.oldfriends.user.*;
import java.rmi.RemoteException;
import javax.ejb.*;
public interface UserRemoteHome extends javax.ejb.EJBHome
{
public UserRemote create(
String firstName,
String lastName,
String mi,
String maidenName,
int gradYear,
String loginId,
String email,
String password
) throws CreateException, RemoteException;
public com.oldfriends.entity.UserRemote create( UserDTO userBean )
throws CreateException, RemoteException;
public com.oldfriends.entity.UserRemote findByPrimaryKey(String id)
throws FinderException, RemoteException;
public com.oldfriends.entity.UserRemote findByLoginId(String id)
throws FinderException, RemoteException;
public com.oldfriends.entity.UserRemote findByEmail(String email)
throws FinderException, RemoteException;
}
The primary difference here is that the methods on the RemoteHome interface must also throw RemoteException. It is possible to expose different functions in the home and local home interfaces. But remember that in this case we
would ordinarily only provide the LocalHome interface; we are providing a home interface just to facilitate testing.