1 7 package org.jboss.tutorial.jboss_deployment_descriptor.client; 8 9 import java.util.HashMap ; 10 import java.util.Properties ; 11 import javax.ejb.EJBNoSuchObjectException; 12 import javax.naming.Context ; 13 import javax.naming.InitialContext ; 14 import org.jboss.tutorial.jboss_deployment_descriptor.bean.ShoppingCart; 15 16 22 public class Client 23 { 24 public static void main(String [] args) throws Exception 25 { 26 Properties env = new Properties (); 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 ctx = new InitialContext (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 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 (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 <String , Integer > fullCart = cart.getCartContents(); 61 for (String 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 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 |