import java.util.Properties; import javax.ejb.CreateException; import javax.ejb.RemoveException; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; import javax.rmi.PortableRemoteObject; import mypackage1.*; /** * This class illustrates calling the stateless session bean MySessionEJB * that was created with JDeveloperthe following exercises: */ public class Client { private static final String JNDI_NAME = "MySessionEJB"; private String url; private MySessionEJBHome home; public Client(String url) throws NamingException { this.url = url; home = lookupHome(); } public static void main(String[] args) { log("\nBeginning statefulSession.Client...\n"); String url = "t3://localhost:7001"; // Parse the argument list if (args.length != 1) { System.out.println("Usage: Client t3://hostname:port"); return; } else { url = args[0]; } Client client = null; try { client = new Client(url); } catch (NamingException ne) { System.exit(1); } try { client.example(); } catch (Exception e) { log("There was an exception while creating and using the MySessionEJB."); log("This indicates that there was a problem communicating with the server: "+e); } log("\nEnd statefulSession.Client...\n"); } /** * Runs the example. */ public void example() throws CreateException, RemoteException, RemoveException { // Create a MySessionEJB log("Creating MySessionEJB\n"); MySessionEJB mysession = (MySessionEJB) narrow(home.create(), MySessionEJB.class); String igot = mysession.getInfo(); System.out.println("Here's what MySessionEJB returns: "+igot); mysession.remove(); } /** * RMI/IIOP clients should use this narrow function */ private Object narrow(Object ref, Class c) { return PortableRemoteObject.narrow(ref, c); } /** * Lookup the EJBs home in the JNDI tree */ private MySessionEJBHome lookupHome() throws NamingException { // Lookup the beans home using JNDI Context ctx = getInitialContext(); try { Object home = ctx.lookup(JNDI_NAME); return (MySessionEJBHome) narrow(home, MySessionEJBHome.class); } catch (NamingException ne) { log("The client was unable to lookup the EJBHome. Please make sure "); log("that you have deployed the ejb with the JNDI name "+JNDI_NAME+" on the WebLogic server at "+url); throw ne; } } /** * Using a Properties object will work on JDK 1.1.x and Java2 * clients */ private Context getInitialContext() throws NamingException { try { // Get an InitialContext Properties h = new Properties(); h.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory"); h.put(Context.PROVIDER_URL, url); return new InitialContext(h); } catch (NamingException ne) { log("We were unable to get a connection to the WebLogic server at "+url); log("Please make sure that the server is running."); throw ne; } } /** * This is the Java2 version to get an InitialContext. * This version relies on the existence of a jndi.properties file in * the application's classpath. * */ // private static Context getInitialContext() // throws NamingException // { // return new InitialContext(); // } private static void log(String s) { System.out.println(s); } }