1 /*
2  * @(#) $Id: Client.java,v 1.2 2003/07/08 08:13:52 pankaj Exp $
3  *
4  * Copyright (c) 2002-03 by Pankaj Kumar (http://www.pankaj-k.net). 
5  * All rights reserved.
6  *
7  * The license governing the use of this file can be found in the 
8  * root directory of the containing software.
9  */
10package rbac;
11
12import java.rmi.RemoteException;
13import java.util.Properties;
14
15import javax.ejb.CreateException;
16import javax.ejb.RemoveException;
17import javax.naming.Context;
18import javax.naming.InitialContext;
19import javax.naming.NamingException;
20import javax.rmi.PortableRemoteObject;
21import rba.*;
22
23/**
24 * This class illustrates calling a stateless Session Bean and performing
25 * the following exercises:
26 * <ul>
27 * <li> Create a Trader
28 * <li> Buy some shares using the Trader
29 * <li> Sell some shares using the Trader
30 * <li> Remove the Trader
31 * </ul>
32 *
33 * @author Copyright (c) 1998-2002 by BEA Systems, Inc. All Rights Reserved.
34 */
35
36public class Client {
37
38  private static final String JNDI_NAME = "rba-TraderHome";
39
40  private String url;
41  String username = null;
42  String password = null;
43  private TraderHome home;
44
45  public Client(String url, String username, String password)
46    throws NamingException
47  {
48
49    this.url       = url;
50    this.username = username;
51    this.password = password;
52
53    home = lookupHome();
54  }
55
56  /**
57   * Runs this example from the command line. Example:
58   * <p>
59   * <tt>java examples.ejb20.basic.statelessSession.Client "t3://localhost:7001"</tt>
60   * <p>
61   * The parameters are optional, but if any are supplied,
62   * they are interpreted in this order:
63   * <p>
64   * @param url               URL such as "t3://localhost:7001" of Server
65   */
66  public static void main(String[] args) throws Exception {
67
68    log("\nBeginning statelessSession.Client...\n");
69
70    String url       = "t3://localhost:7001";
71    String username = null;
72    String password = null;
73
74    // Parse the argument list
75     if (args.length != 3) {
76      System.out.println("Usage: java rbac.Client t3://hostname:port username password");
77      return;
78    } else {
79      url = args[0];
80      username = args[1];
81      password = args[2];
82    }
83
84    Client client = null;
85    try {
86      client = new Client(url, username, password);
87    } catch (NamingException ne) {
88      System.exit(1);
89    }
90
91    try {
92      client.example();
93    } catch (Exception e) {
94      log("There was an exception while creating and using the Trader.");
95      log("This indicates that there was a problem communicating with the server: "+e);
96    }
97
98    log("\nEnd statelessSession.Client...\n");
99  }
00
01  /**
02   * Runs this example.
03   */
04  public void example()
05    throws CreateException, RemoteException, RemoveException
06  {
07
08    // create a Trader
09    log("Creating a trader");
10    Trader trader = (Trader) narrow(home.create(), Trader.class);
11
12    String [] stocks = {"BEAS", "MSFT", "AMZN", "HWP" };
13
14      // execute some buys
15    for (int i=0; i<stocks.length; i++) {
16      int shares = (i+1) * 100;
17      log("Buying "+shares+" shares of "+stocks[i]+".");
18      trader.buy(stocks[i], shares);
19    }
20
21    // execute some sells
22    for (int i=0; i<stocks.length; i++) {
23      int shares = (i+1) * 100;
24      log("Selling "+shares+" shares of "+stocks[i]+".");
25      trader.sell(stocks[i], shares);
26    }
27
28    // remove the Trader
29    log("Removing the trader");
30    trader.remove();
31
32  }
33
34  /**
35   * RMI/IIOP clients should use this narrow function
36   */
37  private Object narrow(Object ref, Class c) {
38    return PortableRemoteObject.narrow(ref, c);
39  }
40
41  /**
42   * Lookup the EJBs home in the JNDI tree
43   */
44  private TraderHome lookupHome()
45    throws NamingException
46  {
47    // Lookup the beans home using JNDI
48    Context ctx = getInitialContext();
49
50    try {
51      Object home = ctx.lookup(JNDI_NAME);
52      return (TraderHome) narrow(home, TraderHome.class);
53    } catch (NamingException ne) {
54      log("The client was unable to lookup the EJBHome.  Please make sure ");
55      log("that you have deployed the ejb with the JNDI name "+JNDI_NAME+" on the WebLogic server at "+url);
56      throw ne;
57    }
58  }
59
60  /**
61   * Using a Properties object will work on JDK 1.1.x and Java2
62   * clients
63   */
64  private Context getInitialContext() throws NamingException {
65
66    try {
67      // Get an InitialContext
68      Properties h = new Properties();
69      h.put(Context.INITIAL_CONTEXT_FACTORY,
70        "weblogic.jndi.WLInitialContextFactory");
71      h.put(Context.PROVIDER_URL, url);
72
73    h.put(Context.SECURITY_AUTHENTICATION, "simple");
74    h.put(Context.SECURITY_PRINCIPAL, username);
75    h.put(Context.SECURITY_CREDENTIALS, password);
76
77      return new InitialContext(h);
78    } catch (NamingException ne) {
79      log("We were unable to get a connection to the WebLogic server at "+url);
80      log("exception: " + ne);
81      log("Please make sure that the server is running.");
82      throw ne;
83    }
84  }
85
86  /**
87   * This is the Java2 version to get an InitialContext.
88   * This version relies on the existence of a jndi.properties file in
89   * the application's classpath.
90   *
91   */
92//    private static Context getInitialContext()
93//      throws NamingException
94//    {
95//      return new InitialContext();
96//    }
97
98  private static void log(String s) {
99    System.out.println(s);
00  }
01
02}
03