users@glassfish.java.net

GFv3 ACC and LoginContext

From: <glassfish_at_javadesktop.org>
Date: Wed, 19 May 2010 07:23:49 PDT

/*
Hi,

I would like to use LoginContext.login from ACC main class, but

-- if I use @EJB annotation then
   the login mechanism start automatically before the "main" started !!!

-- if I do not use @EJB annotation then
   the login mechanism start automatically when InitialContext.lookup called
   and my LoginContext.login unguessed !!!

How can I resolve this situation ?

Thanks, Attila.

*/
package enterprise.hello_stateless_client;

import javax.ejb.EJB;
import javax.naming.InitialContext;
import javax.security.auth.login.LoginContext;
import enterprise.hello_stateless_ejb.StatelessSession;

public class StatelessJavaClient {

    @EJB
    public static StatelessSession sless;

    public static void main(String args[]) {
        new StatelessJavaClient();
    }

    public StatelessJavaClient() {
        System.out.println("Start");
        System.setProperty("java.security.auth.login.config", "/java/sges-v3/glassfish/lib/appclient/appclientlogin.conf");
        StatelessJavaClientPassiveCallbackHandler ch = new StatelessJavaClientPassiveCallbackHandler("programmer", "hello");
        try {
            InitialContext ic = new InitialContext();
            LoginContext lc = new LoginContext("helloRealm",ch);
            System.out.println("Login elott !!!");
            lc.login();
            try {
                if (sless == null) {
                    System.out.println("@EJB sets the 'StatelessSession' to null !");
                    sless = (StatelessSession)ic.lookup("enterprise.hello_stateless_ejb.StatelessSession");
                    if (sless == null) {
                        System.out.println("The InitialContext().lookup() sets the 'StatelessSession' to null !");
                    }
                }
                if (sless != null) {
                    for (int c=0; c<4; c++) {
                        System.out.println("StatelessSession bean says : " + returnMessage());
                    }
                    sless = null;
                }
            } finally {
                lc.logout();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("Stop");
    }

    public String returnMessage() {

        try {
            return (sless.hello());
        } catch(Exception e) {
            e.printStackTrace();
        }

        return null;
    }

}
[Message sent by forum member 'aszomor']

http://forums.java.net/jive/thread.jspa?messageID=470475