001 package examples.ejb.ejb20.basic.statelessSession;
002
003 import java.rmi.RemoteException;
004 import java.util.Hashtable;
005 import javax.ejb.CreateException;
006 import javax.ejb.RemoveException;
007 import javax.naming.Context;
008 import javax.naming.InitialContext;
009 import javax.naming.NamingException;
010 import javax.rmi.PortableRemoteObject;
011
012 /**
013 * This class illustrates calling a stateless Session Bean and performing
014 * the following exercises:
015 * <ul>
016 * <li> Create a Trader
017 * <li> Buy some shares using the Trader
018 * <li> Sell some shares using the Trader
019 * <li> Remove the Trader
020 * </ul>
021 *
022 * @author Copyright (c) 1998,2009, Oracle and/or its affiliates. All Rights Reserved.
023 */
024 public class Client
025 {
026 private static final String JNDI_NAME = "ejb20-statelessSession-TraderHome";
027 private String url;
028 private TraderHome home;
029
030 public Client(String url) throws NamingException {
031 this.url = url;
032 home = lookupHome();
033 }
034
035 /**
036 * Runs this example from the command line. Example:
037 * <p>
038 * <tt>java examples.ejb.ejb20.basic.statelessSession.Client "t3://localhost:7001"</tt>
039 * <p>
040 * The parameters are optional, but if any are supplied,
041 * they are interpreted in this order:
042 * <p>
043 * @param args URL such as "t3://localhost:7001" of Server
044 */
045 public static void main(String[] args) throws Exception {
046 log("\nBeginning statelessSession.Client...\n");
047 String url = "t3://localhost:7001";
048 Client client = null;
049
050 // Parse the argument list
051 if (args.length > 1) {
052 log("Usage: java examples.ejb.ejb20.basic.statelessSession.Client t3://hostname:port");
053 return;
054 } else if (args.length == 1) {
055 url = args[0];
056 }
057
058 try {
059 client = new Client(url);
060 client.example();
061 } catch (NamingException ne) {
062 log("Unable to look up the beans home: " + ne.getMessage());
063 throw ne;
064 } catch (Exception e) {
065 log("There was an exception while creating and using the Trader.");
066 log("This indicates that there was a problem communicating with the server: "+e);
067 throw e;
068 }
069
070 log("\nEnd statelessSession.Client...\n");
071 }
072
073 /**
074 * Runs this example.
075 */
076 public void example()
077 throws CreateException, RemoteException, RemoveException
078 {
079 // create a Trader
080 log("Creating a trader");
081 Trader trader = (Trader) narrow(home.create(), Trader.class);
082
083 String [] stocks = {"BEAS", "MSFT", "AMZN", "HWP" };
084
085 // execute some buys
086 for (int i=0; i<stocks.length; i++) {
087 int shares = (i+1) * 100;
088 log("Buying "+shares+" shares of "+stocks[i]+".");
089 trader.buy(stocks[i], shares);
090 }
091
092 // execute some sells
093 for (int i=0; i<stocks.length; i++) {
094 int shares = (i+1) * 100;
095 log("Selling "+shares+" shares of "+stocks[i]+".");
096 trader.sell(stocks[i], shares);
097 }
098
099 // remove the Trader
100 log("Removing the trader");
101 trader.remove();
102 }
103
104 /**
105 * RMI/IIOP clients should use this narrow function
106 */
107 private Object narrow(Object ref, Class c) {
108 return PortableRemoteObject.narrow(ref, c);
109 }
110
111 /**
112 * Lookup the EJBs home in the JNDI tree
113 */
114 private TraderHome lookupHome() throws NamingException {
115 // Lookup the beans home using JNDI
116 Context ctx = getInitialContext();
117 try {
118 Object home = ctx.lookup(JNDI_NAME);
119 return (TraderHome) narrow(home, TraderHome.class);
120 } catch (NamingException ne) {
121 log("The client was unable to lookup the EJBHome. Please make sure ");
122 log("that you have deployed the ejb with the JNDI name "+
123 JNDI_NAME+" on the WebLogic server at "+url);
124 throw ne;
125 }
126 }
127
128 /**
129 * Using a Properties object will work on JDK 1.1.x and Java2
130 * clients
131 */
132 private Context getInitialContext() throws NamingException {
133
134 try {
135 // Get an InitialContext
136 Hashtable<String,String> h = new Hashtable<String,String>();
137 h.put(Context.INITIAL_CONTEXT_FACTORY,
138 "weblogic.jndi.WLInitialContextFactory");
139 h.put(Context.PROVIDER_URL, url);
140 return new InitialContext(h);
141 } catch (NamingException ne) {
142 log("We were unable to get a connection to the WebLogic server at "+url);
143 log("Please make sure that the server is running.");
144 throw ne;
145 }
146 }
147
148 private static void log(String s) { System.out.println(s); }
149
150 }
|