KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > security > test > StatelessSessionClient


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.test.security.test;
23
24 import java.io.IOException JavaDoc;
25 import java.rmi.RemoteException JavaDoc;
26 import java.util.Properties JavaDoc;
27 import javax.ejb.CreateException JavaDoc;
28 import javax.naming.Context JavaDoc;
29 import javax.naming.InitialContext JavaDoc;
30 import javax.naming.NamingException JavaDoc;
31 import javax.security.auth.callback.*;
32 import javax.security.auth.login.*;
33
34 import org.jboss.test.security.interfaces.StatelessSession;
35 import org.jboss.test.security.interfaces.StatelessSessionHome;
36 import org.jboss.test.util.AppCallbackHandler;
37
38 /** Run with -Djava.security.auth.login.config=url_to_jaas_login_conf
39
40 @author Scott.Stark@jboss.org
41 @version $Revision: 58115 $
42 */

43 public class StatelessSessionClient
44 {
45    static org.jboss.logging.Logger log = org.jboss.logging.Logger.getLogger(StatelessSessionClient.class);
46    
47     static void runMT()
48     {
49         Thread JavaDoc t0 = new Thread JavaDoc()
50         {
51             public void run()
52             {
53                 try
54                 {
55                     runAs("scott", "echoman".toCharArray());
56                 }
57                 catch(Exception JavaDoc e)
58                 {
59                 }
60             }
61         };
62         Thread JavaDoc t1 = new Thread JavaDoc()
63         {
64             public void run()
65             {
66                 try
67                 {
68                     runAs("stark", "javaman".toCharArray());
69                 }
70                 catch(Exception JavaDoc e)
71                 {
72                 }
73             }
74         };
75         t0.start();
76         t1.start();
77     }
78
79     static void runAs(String JavaDoc username, char[] password) throws Exception JavaDoc
80     {
81         LoginContext lc = null;
82         String JavaDoc confName = System.getProperty("conf.name", "other");
83         boolean loggedIn = false;
84         try
85         {
86             AppCallbackHandler handler = new AppCallbackHandler(username, password);
87             log.debug("Creating LoginContext("+confName+")");
88             lc = new LoginContext(confName, handler);
89             lc.login();
90             log.debug("Created LoginContext, subject="+lc.getSubject());
91             loggedIn = true;
92
93             /* The properties are for passing credentials to j2ee-ri which
94                are not used with jboss */

95             Properties JavaDoc props = new Properties JavaDoc();
96             props.setProperty(Context.SECURITY_PRINCIPAL, username);
97             props.setProperty(Context.SECURITY_CREDENTIALS, new String JavaDoc(password));
98             props.setProperty(Context.SECURITY_PROTOCOL, "simple");
99             InitialContext JavaDoc jndiContext = new InitialContext JavaDoc(props);
100             Object JavaDoc obj = jndiContext.lookup("StatelessSession2");
101             Class JavaDoc[] ifaces = obj.getClass().getInterfaces();
102             StatelessSessionHome home = (StatelessSessionHome) obj;
103             log.debug("Found StatelessSessionHome");
104             StatelessSession bean = home.create();
105             log.debug("Created StatelessSession");
106             log.debug("Bean.echo('Hello') -> "+bean.echo("Hello"));
107         }
108         finally
109         {
110             if( lc != null && loggedIn )
111                 lc.logout();
112         }
113     }
114
115     public static void main(String JavaDoc args[]) throws Exception JavaDoc
116     {
117         if( args.length == 1 && args[0].equals("-mt") )
118         {
119             log.debug("Running multi-threaded with simultaneous logins");
120             runMT();
121         }
122         else
123         {
124             log.debug("Running single-threaded with sequential logins");
125             runAs("scott", "echoman".toCharArray());
126             runAs("stark", "javaman".toCharArray());
127         }
128     }
129 }
130
Popular Tags