1 7 package org.jboss.tutorial.security.client; 8 9 import java.util.Properties ; 10 import javax.ejb.EJBAccessException ; 11 import javax.naming.Context ; 12 import javax.naming.InitialContext ; 13 import org.jboss.tutorial.security.bean.Calculator; 14 15 18 public class Client 19 { 20 public static void main(String [] args) throws Exception 21 { 22 Properties env = new Properties (); 24 env.setProperty(Context.SECURITY_PRINCIPAL, "kabir"); 25 env.setProperty(Context.SECURITY_CREDENTIALS, "invalidpassword"); 26 env.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.security.jndi.JndiLoginInitialContextFactory"); 27 InitialContext ctx = new InitialContext (env); 28 Calculator calculator = (Calculator) ctx.lookup(Calculator.class.getName()); 29 30 System.out.println("Kabir is a student."); 31 System.out.println("Kabir types in the wrong password"); 32 try 33 { 34 System.out.println("1 + 1 = " + calculator.add(1, 1)); 35 } 36 catch (EJBAccessException ex) 37 { 38 System.out.println("Saw expected SecurityException: " + ex.getMessage()); 39 } 40 41 System.out.println("Kabir types in correct password."); 42 System.out.println("Kabir does unchecked addition."); 43 44 env.setProperty(Context.SECURITY_CREDENTIALS, "validpassword"); 46 ctx = new InitialContext (env); 47 calculator = (Calculator) ctx.lookup(Calculator.class.getName()); 48 49 System.out.println("1 + 1 = " + calculator.add(1, 1)); 50 51 System.out.println("Kabir is not a teacher so he cannot do division"); 52 try 53 { 54 calculator.divide(16, 4); 55 } 56 catch (javax.ejb.EJBAccessException ex) 57 { 58 System.out.println(ex.getMessage()); 59 } 60 61 System.out.println("Students are allowed to do subtraction"); 62 System.out.println("1 - 1 = " + calculator.subtract(1, 1)); 63 } 64 } 65 | Popular Tags |