【Java开源代码栏目提醒】:网学会员在Java开源代码频道为大家收集整理了BrowseSessionEJBean.java提供大家参考,希望对大家有所帮助!
package ejava.ejbch36.browse;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.ejb.CreateException;
import javax.ejb.EJBException;
import java.rmi.RemoteException;
import javax.ejb.FinderException;
import javax.rmi.PortableRemoteObject;
import java.util.Enumeration;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.naming.Context;
import ejava.ejbch36.EJBHelper;
import ejava.ejbch36.tshirt.*;
import ejava.servletsch32.TShirt;
import java.util.Vector;
public class BrowseSessionEJBean implements SessionBean
{
private SessionContext sessionContext;
private TShirtHome tshirtHome;
// Standard interfaces
public void setSessionContext(SessionContext aCtx)
{
sessionContext = aCtx;
}
public void ejbCreate(){ /* no creation logic */ }
public void ejbRemove(){ /* no removal logic */ }
public void ejbPassivate(){ /* no state persistence */ }
public void ejbActivate(){ /* no state activation */ }
/** Finds the TShirt Home Object */
private void lookupTShirtHome()
throws NamingException
{
if(tshirtHome == null){
InitialContext context = new InitialContext();
Object tshirtHomeRef = context.lookup(EJBHelper.TSHIRT_REFNAME);
tshirtHome = (TShirtHome)PortableRemoteObject.narrow(tshirtHomeRef,
ejava.ejbch36.tshirt.TShirtHome.class);
context.close();
}
}
/** Find TShirt entity beans and create Serialized versions */
public Vector queryShirts(String size, String color)
throws RemoteException
{
// Create collections for entity beans and serialized objects
Enumeration queriedShirts = null;
Vector returnedShirts = new Vector();
try{
// First create TShirt Home object handle
lookupTShirtHome();
// Find a collection of TShirt entity beans in the database
queriedShirts = tshirtHome.findBySizeAndColor(size, color);
// While cycling through TShirt entity object collection...
while(queriedShirts.hasMoreElements()){
// Get TShirt entity bean
ejava.ejbch36.tshirt.TShirt beanShirt =
(ejava.ejbch36.tshirt.TShirt)queriedShirts.nextElement();
// Create Serialized TShirt object from entity bean data
ejava.servletsch32.TShirt clientShirt =
new ejava.servletsch32.TShirt(
beanShirt.getID().intValue(),
beanShirt.getSize(),beanShirt.getColor(),
beanShirt.getDesignFront(),beanShirt.getDesignBack(),
beanShirt.getPictureFront(),beanShirt.getPictureBack(),
beanShirt.getUnitPrice());
// Add the TShirt object to a return vector
returnedShirts.addElement(clientShirt);
}
}
catch(NamingException namingException){
namingException.printStackTrace();
}
catch(FinderException finderException){
finderException.printStackTrace();
}
// Return the Vector of Serialized TShirt objects
return returnedShirts;
}
}