Java Tutorial - Java Scipt : Creating a Session Façade

Java Tutorial - Java Scipt :

Creating a Session Façade


The session facade is a primary design pattern for enterprise Java development. In general, the facade pattern presents a simpler interface to a complex set of underlying interfaces. The session facade can improve performance in distributed systems. Facades are normally course grained; for example, instead of making many small network round trips to read each field of an entity bean a session facade bundles the fields up into a value bean or DTO for single network transfer. The facade can also represent a single entry point for accessing all of the data related to a particular domain object. In our case, all of the information that can be tied to a user record can be made accessible via the facade. As the underlying application becomes more complex, the facade can remain fairly simple from the client’s perspective. The facade also acts as an adapter layer between the client programs and the underlying business components. As the underlying infrastructure changes the clients are only dependent on the facade remaining the same. Our facade is a stateless session bean that accesses and uses the entity bean we have just created to provide clients with access to the information about users. This information is not necessarily limited to data in the user table. As the system is expanded, we should be able to use this session bean to retrieve information that is stored throughout the database. Following is the source code for our session bean: package com.oldfriends.session;

import com.oldfriends.entity.UserLocal;
import com.oldfriends.entity.UserLocalHome;
import com.oldfriends.user.UserDTO;
import com.oldfriends.utils.*;
import java.lang.ClassCastException;
import javax.ejb.*;
import javax.naming.Context;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
public class UserSessionEJB implements javax.ejb.SessionBean
{
protected UserLocalHome userHome = null;
protected SessionContext sessionCtx = null;
public UserSessionEJB() throws EJBException
{
}
public String getHello() throws EJBException
{
return “Hello from UserSessionEJB”;
}
public UserDTO getUserByUserid(String uid)
throws EJBException
{
UserLocal lu;
UserDTO user;
try
{
UserLocalHome lh = lookupUserLocalHome();
lu = lh.findByPrimaryKey(uid);
return buildUserBean( lu );
}
catch(FinderException fe )
{
throw new EJBException( “getUser: Could not findkey(“+uid+”)”,fe);
}
catch(Exception e)
{
throw new EJBException( “getUserByUserid: UID=”+uid ,e);
}
// return buildUserBean( lu );
}
public UserDTO getUserByLogin( String login )
throws EJBException
{
UserLocal lu;
try
{
UserLocalHome lh = lookupUserLocalHome();
lu = lh.findByLoginId(login);
}
catch(FinderException fe )
{
throw new EJBException( “getUser: Could not findkey(“
+ login
+ “)”,fe);
}
return buildUserBean( lu );
}
public UserDTO getUserByEmail( String email )
throws EJBException
{
UserLocal lu;
try
{
UserLocalHome lh = lookupUserLocalHome();
lu = lh.findByEmail(email);
}
catch(FinderException fe )
{
throw new EJBException( “getUser: Could not findkey(“
+ email
+ “)”,fe);
}
return buildUserBean( lu );
}
private UserDTO buildUserBean( UserLocal lu )
{
UserDTO u = new UserDTO();
u.setFirstName(lu.getFirstName());
u.setLastName(lu.getLastName());
u.setMi(lu.getMi());
u.setMaidenName(lu.getMaidenName());
u.setGradYear(lu.getGradYear());
u.setLoginId(lu.getLoginId());
u.setPassword(lu.getPassword());
u.setEmail(lu.getEmail());
u.setUserid(lu.getUserid());
return u;
}
public void updateUser( UserDTO user )
{
UserLocal lu;
try
{
UserLocalHome lh = lookupUserLocalHome();
lu = lh.findByPrimaryKey(user.getUserid());
setUser( user, lu );
}
catch(FinderException fe )
{
throw new EJBException(
“setUser: Could not findkey(“+user.getUserid()+”)”,fe);
}
catch( Exception ex )
{
throw new EJBException(
“updateUser: Unexpected Exception “,ex);
}
}
public UserDTO addUser( UserDTO user )
{
UserLocal lu;
try
{
UserLocalHome lh = lookupUserLocalHome();
lu = lh.create(user);
}
catch(CreateException cx )
{
throw new EJBException(
“addUser: could not create user”, cx);
}
return buildUserBean( lu );
}
private void setUser( UserDTO u, UserLocal lu)
throws EJBException
{
lu.setFirstName(u.getFirstName());
lu.setLastName(u.getLastName());
lu.setMi(lu.getMi());
lu.setMaidenName(u.getMaidenName());
lu.setGradYear(u.getGradYear());
lu.setLoginId(u.getLoginId());
lu.setPassword(u.getPassword());
lu.setEmail(u.getEmail());
}
private UserLocalHome lookupUserLocalHome()
throws EJBException
{
UserLocalHome home;
if( userHome != null)
return userHome;
try
{
Context ctx = ResourceLocator.getInitialContext();
home = (UserLocalHome)ctx.lookup(“ejb/LocalUserEJB”);
userHome = home;
}
catch( NamingException ne )
{
throw(
new EJBException(“UserSessionBean: Problem finding User EJB”, ne)
);
}
catch( ClassCastException cce )
{
throw(new EJBException(“UserSessionBean: Class Cast Fail”, cce));
}
catch( Exception cce )
{
throw new EJBException(
“lookupUserLocalHome: unexpected exception:”, cce);
}
return home;
}
public UserDTO[] getUsersByYear( int year )
throws EJBException
{
return null;
}
public UserDTO[] getAllUsers()
throws EJBException
{
return null;
}
public void ejbCreate()
{
}
public void ejbActivate()
throws javax.ejb.EJBException, java.rmi.RemoteException
{
}
public void ejbPassivate()
throws javax.ejb.EJBException, java.rmi.RemoteException
{
}
public void ejbRemove()
throws javax.ejb.EJBException, java.rmi.RemoteException
{
}
public void setSessionContext(SessionContext sc)
throws javax.ejb.EJBException, java.rmi.RemoteException
{
sessionCtx = sc;
}
}

This should be placed in the com.oldfriends.session directory of our project. The first interesting method here is getHello(). getHello() is used here as an aid to troubleshooting. This method should always work unless there is something wrong with your deployment. There are several private methods here that perform useful work for the bean. The private setUser() method sets the fields of a UserEJB entity bean from a User DTO object. The buildUserBean method does the opposite: it builds the UserDTO based on the contents of a UserEJB instance. The lookup UserLocalHome() method does a context lookup once and then stores the home interface for subsequent use. It uses a utility class to provide it with the InitialContext. We will look at this class after the current discussion.
The three methods are


·         getUserByUserid
·         getUserByLogin
·         getUserByEmail

These methods are all used to retrieve individual records from the user table. The record is copied into a UserDTO instance and returned to the client. The attributes of this UserDTO object can be modified, and then the object can be sent to the update user method, which retrieves the matching user record and updates it in the database. The addUser method accepts a UserDTO object and creates a new record in the database. The method returns the new record, which makes the primary key available to the client. Two methods are provided to return arrays of UserDTO objects. The first, getUsersByYear() returns an array of all of the users that share the same graduating class as passed as an argument. This method does not use the user entity bean. Instead, it performs a query directly against the MySql DataSource and converts the records in the resulting recordset to an array of UserDTO objects. This array of object is returned. getAllUsers is similar, except that it returns the entire user table as an array of UserDTO objects. The reason for doing direct queries is that for this type of data the direct query is normally more efficient than a set of individual queries on the entity beans. The rest of the methods are standard EJB life-cycle methods.