KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > security > auth > SystemAuthenticator


1 /***************************************
2  * *
3  * JBoss: The OpenSource J2EE WebOS *
4  * *
5  * Distributable under LGPL license. *
6  * See terms of license at gnu.org. *
7  * *
8  ***************************************/

9
10 package org.jboss.security.auth;
11
12 import javax.security.auth.Subject JavaDoc;
13 import javax.security.auth.callback.CallbackHandler JavaDoc;
14 import javax.security.auth.login.LoginContext JavaDoc;
15 import javax.security.auth.login.LoginException JavaDoc;
16
17 import org.jboss.deployment.DeploymentException;
18 import org.jboss.system.ServiceMBeanSupport;
19
20 /** An MBean that requires a JAAS login in order for it to startup. This is
21  * used to require authentication to startup a JBoss instance.
22  *
23  * @version $Revision: 1.4 $
24  * @author Scott.Stark@jboss.org
25  */

26 public class SystemAuthenticator extends ServiceMBeanSupport
27    implements SystemAuthenticatorMBean
28 {
29    /** The Subject that results from the login. Not used currently */
30    private Subject JavaDoc systemSubject;
31    /** The name of the security domain to authenticate under */
32    private String JavaDoc securityDomain;
33    /** The CallbackHandler that knows how to provide Callbacks for the
34       security domain login modules
35     */

36    private CallbackHandler JavaDoc callbackHandler;
37
38    /** Get the name of the security domain used for authentication
39     */

40    public String JavaDoc getSecurityDomain()
41    {
42       return this.securityDomain;
43    }
44
45    /** Set the name of the security domain used for authentication
46     */

47    public void setSecurityDomain(String JavaDoc name)
48    {
49       this.securityDomain = name;
50    }
51
52    /** Get the CallbackHandler to use to obtain the authentication
53     information.
54     @see javax.security.auth.callback.CallbackHandler
55     */

56    public Class JavaDoc getCallbackHandler()
57    {
58       Class JavaDoc clazz = null;
59       if( callbackHandler != null )
60          clazz = callbackHandler.getClass();
61       return clazz;
62    }
63    /** Specify the CallbackHandler to use to obtain the authentication
64     information.
65     @see javax.security.auth.callback.CallbackHandler
66     */

67    public void setCallbackHandler(Class JavaDoc callbackHandlerClass)
68       throws InstantiationException JavaDoc, IllegalAccessException JavaDoc
69    {
70       callbackHandler = (CallbackHandler JavaDoc) callbackHandlerClass.newInstance();
71    }
72
73    protected void startService() throws Exception JavaDoc
74    {
75       try
76       {
77          LoginContext JavaDoc lc = new LoginContext JavaDoc(securityDomain, callbackHandler);
78          lc.login();
79          this.systemSubject = lc.getSubject();
80       }
81       catch(Throwable JavaDoc t)
82       {
83          log.fatal("SystemAuthenticator failed, server will shutdown NOW!", t);
84          LoginException JavaDoc le = new LoginException JavaDoc("SystemAuthenticator failed, msg="+t.getMessage());
85          Thread JavaDoc shutdownThread = new Thread JavaDoc("SystemAuthenticatorExitThread")
86          {
87             public void run()
88             {
89                System.exit(1);
90             }
91          };
92          shutdownThread.start();
93       }
94    }
95
96    protected void stopService() throws Exception JavaDoc
97    {
98       if( systemSubject != null )
99       {
100          LoginContext JavaDoc lc = new LoginContext JavaDoc(securityDomain, systemSubject, callbackHandler);
101          lc.logout();
102       }
103    }
104 }
105
Popular Tags