1 /**
2  * @author janaudy
3  * @date Jun 3, 2003
4  */
5 package org.jyperion.j2ee.sample.sfsb.client;
6 
7 import java.util.Iterator;
8 import java.util.List;
9 
10import javax.naming.InitialContext;
11import javax.security.auth.login.LoginContext;
12import javax.security.auth.login.LoginException;
13
14import org.jyperion.j2ee.sample.sfsb.Cart;
15import org.jyperion.j2ee.sample.sfsb.CartHome;
16
17/**
18 * @author janaudy
19 * @version 1.0
20 */
21public class JbossCartClient {
22    /**
23     * java JbossCartClient <username> <password>
24     * @param args
25     * @throws Exception
26     */
27    public static void main(String[] args) throws Exception {
28        String username = args[0];
29        String password = args[1];
30        
31        // doLogin
32        doLogin(username, password);
33        
34//      Hashtable ht = new Hashtable();
35//      ht.put(Context.SECURITY_PRINCIPAL, "claire");
36//      ht.put(Context.SECURITY_CREDENTIALS, "c");
37        
38        InitialContext lContext = new InitialContext();
39        System.out.println("Got initial context: " + lContext);
40        
41        CartHome lHome =
42            (CartHome) lContext.lookup(
43                "org.jyperion.sample.sessionbean.CartBean");
44        System.out.println("Got CartHome: " + lHome);       
45        
46        Cart cart = null;
47        
48        try {
49            cart = lHome.create();
50        } catch (Exception e) {
51            Throwable t = e.getCause();
52            System.err.println("Exception lors du create sur CartHome: " + t.getLocalizedMessage());
53        }
54
55        //      Add some items to the Cart.
56        cart.addBook("Apres l'Empire");
57        cart.addBook("L'Illusion economique");
58        cart.addBook("Le langage silencieux");
59        cart.addBook("Hyperion");
60        // Remove an item.
61        List livres = cart.getContent();
62
63        Iterator it = livres.iterator();
64        int i = 1;
65        while (it.hasNext()) {
66            String book = (String) it.next();
67            System.out.println("Livre numero " + (i++) + ": " + book);
68        }
69
70        cart.remove();
71    }
72    
73    private static void doLogin(String username, String password) {
74        System.out.println("--> doLogin");
75        
76        UPCallbackHandler upCallbackHandler = new UPCallbackHandler(username, password);
77        try {
78            LoginContext loginContext = new LoginContext("HyperionSec", upCallbackHandler);
79            loginContext.login();
80            System.out.println("Successful login");
81        } catch (LoginException e) {
82            e.printStackTrace();
83            System.err.println("Login failed!");
84        }
85    }
86}
87