KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * JBoss, Home of Professional Open Source
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.tutorial.security.client;
8
9 import java.util.Properties JavaDoc;
10 import javax.ejb.EJBAccessException JavaDoc;
11 import javax.naming.Context JavaDoc;
12 import javax.naming.InitialContext JavaDoc;
13 import org.jboss.tutorial.security.bean.Calculator;
14
15 /**
16  * @version $Revision: 1.1.6.6 $
17  */

18 public class Client
19 {
20    public static void main(String JavaDoc[] args) throws Exception JavaDoc
21    {
22       // Establish the proxy with an incorrect security identity
23
Properties JavaDoc env = new Properties JavaDoc();
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 JavaDoc ctx = new InitialContext JavaDoc(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 JavaDoc 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       // Re-establish the proxy with the correct security identity
45
env.setProperty(Context.SECURITY_CREDENTIALS, "validpassword");
46       ctx = new InitialContext JavaDoc(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 JavaDoc 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