users@glassfish.java.net

Re: How do logon or set principle/roles/groups when using Embedded API

From: <glassfish_at_javadesktop.org>
Date: Wed, 10 Feb 2010 05:43:25 PST

One way to login is to create a file user for the embedded server. For that, the following API could be used to create an Embedded server, create an ejb container, deploy your app, create a file user , use ProgrammaticLogin and invoke it.

There is another way in which the installroot and configuration file for an existing GF instance could be set as an EmbeddedFileSystem for the embedded server. We are in the process of verifying that scenario.

HTH
Nithya

[i]import com.sun.appserv.security.ProgrammaticLogin;
import com.sun.embeded.ejb.GoodByeWorldBeanLocal;
import java.io.File;
import javax.naming.InitialContext;
import org.glassfish.api.ActionReport;
import org.glassfish.api.admin.CommandRunner;
import org.glassfish.api.admin.ParameterMap;
import org.glassfish.api.embedded.ContainerBuilder;
import org.glassfish.api.embedded.EmbeddedFileSystem;
import org.glassfish.api.embedded.ScatteredArchive;
import org.glassfish.api.embedded.Server;

public class GoodByeWorldBeanTest {

    static final String installRoot = "/Users/nithyasubramanian/gf-v3-image/glassfishv3/glassfish/";
    static final String archiveStr = "/Users/nithyasubramanian/NetBeansProjects/EmbeddedEjb/build/classes";

    public GoodByeWorldBeanTest() {
    }
    public static void main(String args[]) throws Exception {
        
        //Create a server, and the archive to be deployed.
        Server.Builder builder = new Server.Builder("test");
        Server server = builder.build();
        server.addContainer(ContainerBuilder.Type.ejb);
        File f = new File(archiveStr);
        ScatteredArchive archive = new ScatteredArchive.Builder("goodbyeWorld", f).buildJar();
        server.start();

        //Create a file user
        String command = "create-file-user";
        ParameterMap params = new ParameterMap();
        params.add("userpassword", "kadayam" );
        params.add("groups", "admin");
        params.add("username", "adminuser");
        CommandRunner runner = server.getHabitat().getComponent(CommandRunner.class);
        ActionReport report = server.getHabitat().getComponent(ActionReport.class);
        runner.getCommandInvocation(command, report).parameters(params).execute();

        String appName = null;
        appName = server.getDeployer().deploy(archive, null);

        System.out.println("Looking up EJB...");
        GoodByeWorldBeanLocal goodByeWorld = (GoodByeWorldBeanLocal) (new InitialContext()).lookup("java:global/goodbyeWorld/GoodByeWorldBean");
        if (goodByeWorld != null) {
            System.out.println("Invoking EJB...");

            ProgrammaticLogin pgLogin = new ProgrammaticLogin();
            pgLogin.login("adminuser", "kadayam", "file", true);
            goodByeWorld.sayGoodbye();
            
        }
        server.stop();

    }
}[i]
[Message sent by forum member 'nitkal' (nithya.subramanian_at_sun.com)]

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