1 22 package org.jboss.etutorial.security.client; 23 24 import java.util.Properties ; 25 import javax.ejb.EJBAccessException ; 26 import javax.naming.Context ; 27 import javax.naming.InitialContext ; 28 import org.jboss.etutorial.security.bean.Calculator; 29 import org.jboss.ejb3.embedded.EJB3StandaloneBootstrap; 30 31 34 public class Main 35 { 36 public static void main(String [] args) throws Exception 37 { 38 EJB3StandaloneBootstrap.boot(null); 39 EJB3StandaloneBootstrap.deployXmlResource("security-beans.xml"); 40 EJB3StandaloneBootstrap.scanClasspath(); 41 42 Properties env = new Properties (); 44 env.setProperty(Context.SECURITY_PRINCIPAL, "kabir"); 45 env.setProperty(Context.SECURITY_CREDENTIALS, "invalidpassword"); 46 env.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.security.jndi.JndiLoginInitialContextFactory"); 47 InitialContext ctx = new InitialContext (env); 48 Calculator calculator = (Calculator) ctx.lookup("CalculatorBean/local"); 49 50 System.out.println("Kabir is a student."); 51 System.out.println("Kabir types in the wrong password"); 52 try 53 { 54 System.out.println("1 + 1 = " + calculator.add(1, 1)); 55 } 56 catch (EJBAccessException ex) 57 { 58 System.out.println("Saw expected SecurityException: " + ex.getMessage()); 59 } 60 61 System.out.println("Kabir types in correct password."); 62 System.out.println("Kabir does unchecked addition."); 63 64 env.setProperty(Context.SECURITY_CREDENTIALS, "validpassword"); 66 ctx = new InitialContext (env); 67 calculator = (Calculator) ctx.lookup("CalculatorBean/local"); 68 69 System.out.println("1 + 1 = " + calculator.add(1, 1)); 70 71 System.out.println("Kabir is not a teacher so he cannot do division"); 72 try 73 { 74 calculator.divide(16, 4); 75 } 76 catch (javax.ejb.EJBAccessException ex) 77 { 78 System.out.println(ex.getMessage()); 79 } 80 81 System.out.println("Students are allowed to do subtraction"); 82 System.out.println("1 - 1 = " + calculator.subtract(1, 1)); 83 } 84 } 85 | Popular Tags |