KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > tutorial > jboss_deployment_descriptor > client > Client


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.tutorial.jboss_deployment_descriptor.client;
8
9 import java.util.HashMap JavaDoc;
10 import java.util.Properties JavaDoc;
11 import javax.ejb.EJBNoSuchObjectException;
12 import javax.naming.Context JavaDoc;
13 import javax.naming.InitialContext JavaDoc;
14 import org.jboss.tutorial.jboss_deployment_descriptor.bean.ShoppingCart;
15
16 /**
17  * Comment
18  *
19  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
20  * @version $Revision: 1.3.2.3 $
21  */

22 public class Client
23 {
24    public static void main(String JavaDoc[] args) throws Exception JavaDoc
25    {
26       // Establish the proxy with an incorrect security identity
27
Properties JavaDoc env = new Properties JavaDoc();
28       env.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.security.jndi.JndiLoginInitialContextFactory");
29       env.setProperty(Context.SECURITY_PRINCIPAL, "bill");
30       env.setProperty(Context.SECURITY_CREDENTIALS, "invalidpassword");
31       InitialContext JavaDoc ctx = new InitialContext JavaDoc(env);
32       ShoppingCart cart = (ShoppingCart) ctx.lookup("ShoppingCart");
33
34
35       System.out.println("Attempting to buy 1 memory stick with incorrect password");
36       try
37       {
38          cart.buy("Memory stick", 1);
39       }
40       catch (javax.ejb.EJBAccessException JavaDoc e)
41       {
42          System.out.println("Caught javax.ejb.EJBAccessException as expected");
43       }
44
45       System.out.println("Setting user/password");
46       env.setProperty(Context.SECURITY_CREDENTIALS, "password");
47
48       ctx = new InitialContext JavaDoc(env);
49
50       System.out.println("bill is a shopper, so is allowed to buy");
51       System.out.println("Buying 1 memory stick");
52       cart.buy("Memory stick", 1);
53       System.out.println("Buying another memory stick");
54       cart.buy("Memory stick", 1);
55
56       System.out.println("Buying a laptop");
57       cart.buy("Laptop", 1);
58
59       System.out.println("Print cart:");
60       HashMap JavaDoc<String JavaDoc, Integer JavaDoc> fullCart = cart.getCartContents();
61       for (String JavaDoc product : fullCart.keySet())
62       {
63          System.out.println(fullCart.get(product) + " " + product);
64       }
65
66       System.out.println("bill is not a clerk, so is not allowed to price check");
67       try
68       {
69          cart.priceCheck("Laptop");
70       }
71       catch (javax.ejb.EJBAccessException JavaDoc ex)
72       {
73          System.out.println("Caught SecurityException as expected");
74       }
75
76       System.out.println("Checkout");
77       cart.checkout();
78
79       System.out.println("Should throw an object not found exception by invoking on cart after @Remove method");
80       try
81       {
82          cart.getCartContents();
83       }
84       catch (EJBNoSuchObjectException e)
85       {
86          System.out.println("Successfully caught no such object exception.");
87       }
88
89
90    }
91 }
92
Popular Tags