001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.test.security.test;
023:
024: import java.io.IOException;
025: import java.rmi.RemoteException;
026: import java.util.Properties;
027: import javax.ejb.CreateException;
028: import javax.naming.Context;
029: import javax.naming.InitialContext;
030: import javax.naming.NamingException;
031: import javax.security.auth.callback.*;
032: import javax.security.auth.login.*;
033:
034: import org.jboss.test.security.interfaces.StatelessSession;
035: import org.jboss.test.security.interfaces.StatelessSessionHome;
036: import org.jboss.test.util.AppCallbackHandler;
037:
038: /** Run with -Djava.security.auth.login.config=url_to_jaas_login_conf
039:
040: @author Scott.Stark@jboss.org
041: @version $Revision: 57211 $
042: */
043: public class StatelessSessionClient {
044: static org.apache.log4j.Category log = org.apache.log4j.Category
045: .getInstance(StatelessSessionClient.class);
046:
047: static void runMT() {
048: Thread t0 = new Thread() {
049: public void run() {
050: try {
051: runAs("scott", "echoman".toCharArray());
052: } catch (Exception e) {
053: }
054: }
055: };
056: Thread t1 = new Thread() {
057: public void run() {
058: try {
059: runAs("stark", "javaman".toCharArray());
060: } catch (Exception e) {
061: }
062: }
063: };
064: t0.start();
065: t1.start();
066: }
067:
068: static void runAs(String username, char[] password)
069: throws Exception {
070: LoginContext lc = null;
071: String confName = System.getProperty("conf.name", "other");
072: boolean loggedIn = false;
073: try {
074: AppCallbackHandler handler = new AppCallbackHandler(
075: username, password);
076: log.debug("Creating LoginContext(" + confName + ")");
077: lc = new LoginContext(confName, handler);
078: lc.login();
079: log.debug("Created LoginContext, subject="
080: + lc.getSubject());
081: loggedIn = true;
082:
083: /* The properties are for passing credentials to j2ee-ri which
084: are not used with jboss */
085: Properties props = new Properties();
086: props.setProperty(Context.SECURITY_PRINCIPAL, username);
087: props.setProperty(Context.SECURITY_CREDENTIALS, new String(
088: password));
089: props.setProperty(Context.SECURITY_PROTOCOL, "simple");
090: InitialContext jndiContext = new InitialContext(props);
091: Object obj = jndiContext.lookup("StatelessSession2");
092: Class[] ifaces = obj.getClass().getInterfaces();
093: StatelessSessionHome home = (StatelessSessionHome) obj;
094: log.debug("Found StatelessSessionHome");
095: StatelessSession bean = home.create();
096: log.debug("Created StatelessSession");
097: log.debug("Bean.echo('Hello') -> " + bean.echo("Hello"));
098: } finally {
099: if (lc != null && loggedIn)
100: lc.logout();
101: }
102: }
103:
104: public static void main(String args[]) throws Exception {
105: if (args.length == 1 && args[0].equals("-mt")) {
106: log
107: .debug("Running multi-threaded with simultaneous logins");
108: runMT();
109: } else {
110: log.debug("Running single-threaded with sequential logins");
111: runAs("scott", "echoman".toCharArray());
112: runAs("stark", "javaman".toCharArray());
113: }
114: }
115: }
|