package sqetests.ejb.stateful.passivate.ejb.stateful; import javax.ejb.*; import javax.transaction.*; import javax.naming.*; import javax.rmi.*; import java.util.HashMap; import sqetests.ejb.stateful.passivate.util.*; import javax.jms.*; /** * Created Oct 17, 2003 4:06:59 PM * Code generated by the Sun ONE Studio EJB Builder * @author dsingh */ public class SessionBean implements javax.ejb.SessionBean { private String message; private int lastCalculation; private int count = 0; private javax.ejb.SessionContext sessionContext; Integer serializedInteger=new Integer(10000); UserTransaction userTx=null; public Context context=null; public InitialContext initialContext=null; public Object objref; java.net.URL urlSource=null; Integer nullInt=null; HashMap beanResult=new HashMap(); EJBRecorderHome ejbRecorder=EJBRecorderHome.getInstance(); /** * @see javax.ejb.SessionBean#setSessionContext(javax.ejb.SessionContext) */ public void setSessionContext(javax.ejb.SessionContext aContext) { sessionContext=aContext; try{ /* *Calling the InitialContext method with no parameters results in a search of the current classpath for a vendor-specific file named jndi.properties. This file indicates which JNDI implementation to use and which namespace to use. */ initialContext = new InitialContext(); }catch(Throwable e){ e.printStackTrace(); } } /** * @see javax.ejb.SessionBean#ejbActivate() */ public void ejbActivate() { //key is method name and value is true/false beanResult.put(new String("activate"),new Boolean(true)); System.out.println("---------SFSB Bean "+message+" activated-------"); } /** * @see javax.ejb.SessionBean#ejbPassivate() */ public void ejbPassivate() { //key is method name and value is true/false beanResult.put(new String("passivate"),new Boolean(true)); System.out.println("--------SFSB Bean " +message+" passivated--------"); } /** * @see javax.ejb.SessionBean#ejbRemove() */ public void ejbRemove() { System.out.println("----------SFSB Bean " +message+" removed--------"); } /** * See section 7.10.3 of the EJB 2.0 specification */ public void ejbCreate() { message = "Hello World From Stateful EJB"; } public void ejbCreate(String initialMessage) throws javax.ejb.CreateException { try{ beanResult.put(new String("passivate"),new Boolean(false)); beanResult.put(new String("activate"),new Boolean(false)); message = initialMessage; setInstanceFields(); System.out.println("<STATEFUL>CREATE==========="+message+"=================="); }catch(Exception e){ throw new CreateException(e.getMessage()); } } /** * @param * @see Called from ejbCreate.Sets all instance members * */ public void setInstanceFields() throws Exception { try{ // JNDI lookup to obtain resource manager connection factory reference userTx=sessionContext.getUserTransaction(); System.out.println("<STATEFUL>"+message+"UT"); urlSource=(java.net.URL)initialContext.lookup("java:comp/env/url/myURL"); nullInt=null; System.out.println("<STATEFUL>setInstanceFields==========="+message+"=================="); }catch(Throwable e){ e.printStackTrace(); throw new Exception(e.getMessage()); } } /*public void badMethod() throws RuntimeException{ throw new RuntimeException("<STATEFUL> ####Expected### Exception!!! Runtime exception"); }*/ public void badMethod(){ System.out.println("modified method"); } public String getMessage() { String returnValue=message; System.out.println("<STATEFUL> BUSINESS METHOD"+returnValue); return returnValue; } /** * @throws Exception * @return *tx is delibrately left in uncommitted state to verify a SFSB instance with this method *invoked can not be passivated */ public String txMethod() throws Exception{ System.out.println("<STATEFUL> txMethod "+message); userTx.begin(); return new String("Transaction started but not committed"); } /** * @return boolean.true if all of instance variables are set correctly * false if any of instance variable is incorrect * */ public boolean afterActivationBusinessMethod(){ ejbRecorder.setBeanMap(message,beanResult); boolean retResult=true; try{ int txStatus=userTx.getStatus(); System.out.println("Tx Status"+txStatus); if (txStatus == javax.transaction.Status.STATUS_ACTIVE) { System.out.println("committing tx that was left active"); userTx.commit(); } System.out.println("URL Path is "+urlSource.toString()); System.out.println("SFSB <"+message+">..Activation SUCCESSFUL"); }catch(Throwable e){ System.out.println("SFSB <"+message+">..Activation FAILED"); e.printStackTrace(); retResult=false; } return retResult; } /** *EJBRecorder is a Singleton class so we can't return it from SFSB.Appclient gets a * NPE.Instead, HashMap result is extracted and returned to calling client.This is *SFSB independent (Any instance of SFSB returns same HashMap) **/ public HashMap getEJBRecorder(){ if(ejbRecorder!=null){ //Now extract all bean results. HashMap ejbRecorderResult=new HashMap(); ejbRecorderResult=ejbRecorder.getAllBeanResults(); return ejbRecorderResult; } else return null; } }