package ejb.demo4; /** * DemoBean -- This is implemented by the EnterPrise`Bean. * This class must extend`javax.ejb.SessionBean and implement * the methods in this interface as well as providing * the implementation of the business methods. * */ import ejb.demo2.Demo2; import ejb.demo2.Demo2Home; import javax.ejb.*; import javax.naming.*; import java.rmi.*; import javax.rmi.PortableRemoteObject; import java.util.*; public class Demo4Bean implements javax.ejb.SessionBean { //Instance member variables. // // Session context, can be used to obtain handles etc // javax.ejb.SessionContext ejbSessionContext = null; //Getter/setter methods // // TBD: Add implementations for any additional remote method interfaces // // The default ejbCreate method. // public void ejbCreate( ) throws javax.ejb.CreateException,java.rmi.RemoteException { } // TBD: If any other ejbCreate's are added manually to the home interface, define them. // // Other methods required in a session bean // public void setSessionContext( javax.ejb.SessionContext ejbSessionContext ) throws RemoteException { this.ejbSessionContext = ejbSessionContext; } public void unsetSessionContext() throws RemoteException { this.ejbSessionContext = null; } public void ejbRemove() throws java.rmi.RemoteException, javax.ejb.EJBException { // TBD: Do any processing here when instance is being removed } public void ejbActivate() throws java.rmi.RemoteException { // TBD: Restore any saved resources } public void ejbPassivate() throws java.rmi.RemoteException { // TBD: Save any resources here before bean is passivated } /** * **** HERE IS THE BUSINESS LOGIC ***** * Do the demoSelect() but don't even go to * the database in this eg but instead just * return a String. * The really BIG thing to notice here is that * this is the only code we have invented at all * the rest of the code has been declarations * or simply implementing methods which are * part of the EJB interfaces and in this example * are not even used. */ public String demoSelect() throws RemoteException { Context ctx = null; Demo2Home demo2homeObj = null; Demo2 demo2 = null; Properties env = new Properties(); env.put( "java.naming.factory.initial", "desisoft.ejb.client.JRMPFactory" ); env.put( "desisoft.ejb.nameServer1", "localhost:2050" ); try { ctx = new InitialContext(env); } catch (Exception e) { System.out.println("can't get initial context."); return("no initial context"); } try { Object ref = ctx.lookup("ejb.demo2.Demo2" ); Demo2Home home = (Demo2Home) PortableRemoteObject.narrow(ref, Demo2Home.class ); } catch (Exception e) { System.out.println("error look up Demo2Home."); return ("can't look up Demo2"); } return("hello"); } }