1 Reply Latest reply on May 22, 2003 9:24 AM by adrian.brock

    Principal/Role from Scheduler?

    jox72

      I'm running a Scheduler task as a maintenance nighly job. I need to access an EJB from the Scheduler task, but how do I setup the Principal/Role from here? Can someone please point me in the right direction on where I can read about how I do this?

      -Joacim

        • 1. Re: Principal/Role from Scheduler?

          You have to do a JAAS login using the client
          login module.

          Use something like the following class
          with code that looks something like this:

          LoginContext context = ApplicationCallbackHandler.login("client-login", "user", "pass");
          try
          {
          doStuff();
          }
          finally
          {
          context.logout();
          }

          /*
          * JBoss, the OpenSource J2EE webOS
          *
          * Distributable under LGPL license.
          * See terms of license at gnu.org.
          *
          */
          package org.jboss.example.security.test;

          import javax.security.auth.callback.Callback;
          import javax.security.auth.callback.CallbackHandler;
          import javax.security.auth.callback.NameCallback;
          import javax.security.auth.callback.PasswordCallback;
          import javax.security.auth.callback.UnsupportedCallbackException;
          import javax.security.auth.login.LoginContext;
          import javax.security.auth.login.LoginException;

          /**
          * A callback handler for login with user and password.
          *
          * @author Adrian Brock
          * @version $Revision: 1.1 $
          */
          public class ApplicationCallbackHandler
          implements CallbackHandler
          {
          // Attributes ----------------------------------------------------

          private String user;
          private char[] password;

          // Constructor ---------------------------------------------------

          private ApplicationCallbackHandler(String user, String password)
          {
          this.user = user;
          this.password = password.toCharArray();
          }

          // Public --------------------------------------------------------

          public void handle(Callback[] callbacks)
          throws UnsupportedCallbackException
          {
          for (int i = 0; i < callbacks.length; i++)
          {
          if (callbacks instanceof NameCallback)
          {
          NameCallback nameCallback = (NameCallback) callbacks
          ;
          nameCallback.setName(user);
          }
          else if (callbacks instanceof PasswordCallback)
          {
          PasswordCallback passwordCallback = (PasswordCallback) callbacks
          ;
          passwordCallback.setPassword(password);
          }
          else
          {
          throw new UnsupportedCallbackException(callbacks, "Unsupported callback");
          }
          }
          }

          // Static --------------------------------------------------------

          /**
          * Login
          */
          public static LoginContext login(String context, String user, String password)
          throws LoginException
          {
          ApplicationCallbackHandler handler = new ApplicationCallbackHandler(user, password);
          LoginContext result = new LoginContext(context, handler);
          result.login();
          return result;
          }
          }