KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > security > plugins > DefaultLoginConfig


1 /*
2 * JBoss, the OpenSource J2EE webOS
3 *
4 * Distributable under LGPL license.
5 * See terms of license at gnu.org.
6 */

7 package org.jboss.security.plugins;
8
9 import java.lang.reflect.Constructor JavaDoc;
10 import java.lang.reflect.Method JavaDoc;
11 import java.net.MalformedURLException JavaDoc;
12 import java.net.URL JavaDoc;
13 import javax.management.Attribute JavaDoc;
14 import javax.management.AttributeList JavaDoc;
15 import javax.management.AttributeNotFoundException JavaDoc;
16 import javax.management.DynamicMBean JavaDoc;
17 import javax.management.InvalidAttributeValueException JavaDoc;
18 import javax.management.MBeanAttributeInfo JavaDoc;
19 import javax.management.MBeanConstructorInfo JavaDoc;
20 import javax.management.MBeanException JavaDoc;
21 import javax.management.MBeanInfo JavaDoc;
22 import javax.management.MBeanOperationInfo JavaDoc;
23 import javax.management.ReflectionException JavaDoc;
24 import javax.security.auth.login.Configuration JavaDoc;
25 import javax.security.auth.login.AppConfigurationEntry JavaDoc;
26
27 import org.jboss.logging.Logger;
28
29 /** An mbean that uses the default JAAS login configuration file based
30  implementation.
31
32 @author Scott.Stark@jboss.org
33 @version $Revision: 1.1.24.1 $
34  */

35 public class DefaultLoginConfig implements DynamicMBean JavaDoc
36 {
37    private static Logger log = Logger.getLogger(DefaultLoginConfig.class);
38    private String JavaDoc authConfig = "auth.conf";
39    private Configuration JavaDoc theConfig;
40
41    /** Creates a new instance of DefaultLoginConfig */
42    public DefaultLoginConfig()
43    {
44    }
45
46    /** Get the resource path to the JAAS login configuration file to use.
47     */

48    public String JavaDoc getAuthConfig()
49    {
50       return authConfig;
51    }
52
53    /** Set the resource path or URL to the JAAS login configuration file to use.
54     The default is "auth.conf".
55     */

56    public void setAuthConfig(String JavaDoc authConfURL) throws MalformedURLException JavaDoc
57    {
58       this.authConfig = authConfURL;
59       // Set the JAAS login config file if not already set
60
ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
61       URL JavaDoc loginConfig = loader.getResource(authConfig);
62       if( loginConfig != null )
63       {
64          System.setProperty("java.security.auth.login.config", loginConfig.toExternalForm());
65          if (log.isInfoEnabled())
66             log.info("Using JAAS LoginConfig: "+loginConfig.toExternalForm());
67       }
68       else
69       {
70          log.warn("Resource: "+authConfig+" not found");
71       }
72    }
73
74    /** Return the Configuration instance managed by this mbean. This simply
75     obtains the default Configuration by calling Configuration.getConfiguration.
76     Note that this means this mbean must be the first pushed onto the config
77     stack if it is used.
78     @see javax.security.auth.login.Configuration
79     */

80    public Configuration JavaDoc getConfiguration(Configuration JavaDoc currentConfig)
81    {
82       if( theConfig == null )
83       {
84          theConfig = Configuration.getConfiguration();
85          log.debug("theConfig set to: "+theConfig);
86       }
87       return theConfig;
88    }
89
90 // Begin DynamicMBean interfaces
91
public Object JavaDoc getAttribute(String JavaDoc name)
92       throws AttributeNotFoundException JavaDoc, MBeanException JavaDoc, ReflectionException JavaDoc
93    {
94       if( name.equals("AuthConfig") )
95          return getAuthConfig();
96       throw new AttributeNotFoundException JavaDoc(name+": is not an attribute");
97    }
98
99    public AttributeList JavaDoc getAttributes(String JavaDoc[] names)
100    {
101       AttributeList JavaDoc list = new AttributeList JavaDoc();
102       for(int n = 0; n < names.length; n ++)
103       {
104          String JavaDoc name = names[n];
105          try
106          {
107             Object JavaDoc value = getAttribute(name);
108             Attribute JavaDoc attr = new Attribute JavaDoc(name, value);
109             list.add(attr);
110          }
111          catch(Exception JavaDoc e)
112          {
113          }
114       }
115       return list;
116    }
117
118    public MBeanInfo JavaDoc getMBeanInfo()
119    {
120       Class JavaDoc c = getClass();
121       MBeanAttributeInfo JavaDoc[] attrInfo = {
122          new MBeanAttributeInfo JavaDoc("AuthConfig", "java.lang.String",
123              "", true, true, false)
124       };
125       Constructor JavaDoc ctor = null;
126       try
127       {
128          Class JavaDoc[] sig = {};
129          ctor = c.getDeclaredConstructor(sig);
130       }
131       catch(Exception JavaDoc e)
132       {
133       }
134       MBeanConstructorInfo JavaDoc[] ctorInfo = {
135          new MBeanConstructorInfo JavaDoc("Default ctor", ctor)
136       };
137       Method JavaDoc getConfiguration = null;
138       try
139       {
140          Class JavaDoc[] sig = {Configuration JavaDoc.class};
141          getConfiguration = c.getDeclaredMethod("getConfiguration", sig);
142       }
143       catch(Exception JavaDoc e)
144       {
145       }
146       MBeanOperationInfo JavaDoc[] opInfo = {
147          new MBeanOperationInfo JavaDoc("Access the LoginConfiguration", getConfiguration)
148       };
149       MBeanInfo JavaDoc info = new MBeanInfo JavaDoc(c.getName(), "Default JAAS LoginConfig",
150          attrInfo, ctorInfo, opInfo, null);
151       return info;
152    }
153
154    public Object JavaDoc invoke(String JavaDoc method, Object JavaDoc[] args, String JavaDoc[] signature)
155       throws MBeanException JavaDoc, ReflectionException JavaDoc
156    {
157       Object JavaDoc value = null;
158       if( method.equals("getConfiguration") )
159       {
160          Configuration JavaDoc currentConfig = (Configuration JavaDoc) args[0];
161          value = this.getConfiguration(currentConfig);
162       }
163       return value;
164    }
165
166    public void setAttribute(Attribute JavaDoc attribute)
167       throws AttributeNotFoundException JavaDoc, InvalidAttributeValueException JavaDoc, MBeanException JavaDoc, ReflectionException JavaDoc
168    {
169       String JavaDoc name = attribute.getName();
170       String JavaDoc value = (String JavaDoc) attribute.getValue();
171       if( name.equals("AuthConfig") )
172       {
173          try
174          {
175             setAuthConfig(value);
176          }
177          catch(Exception JavaDoc e)
178          {
179             throw new MBeanException JavaDoc(e);
180          }
181       }
182       else
183          throw new AttributeNotFoundException JavaDoc(name+": is not an attribute");
184    }
185
186    public AttributeList JavaDoc setAttributes(AttributeList JavaDoc attributeList)
187    {
188       AttributeList JavaDoc list = new AttributeList JavaDoc();
189       for(int n = 0; n < attributeList.size(); n ++)
190       {
191          Attribute JavaDoc attr = (Attribute JavaDoc) attributeList.get(n);
192          try
193          {
194             setAttribute(attr);
195             list.add(attr);
196          }
197          catch(Exception JavaDoc e)
198          {
199          }
200       }
201       return list;
202    }
203 // End DynamicMBean interfaces
204

205 }
206
Popular Tags