KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > security > auth > login > XMLLoginConfigImpl


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.auth.login;
8
9 // $Id: XMLLoginConfigImpl.java,v 1.13.2.3 2005/06/19 10:07:09 bill Exp $
10

11 import java.io.File JavaDoc;
12 import java.io.IOException JavaDoc;
13 import java.io.InputStream JavaDoc;
14 import java.io.InputStreamReader JavaDoc;
15 import java.net.MalformedURLException JavaDoc;
16 import java.net.URL JavaDoc;
17 import java.security.AccessController JavaDoc;
18 import java.security.PrivilegedAction JavaDoc;
19 import java.util.ArrayList JavaDoc;
20 import javax.security.auth.AuthPermission JavaDoc;
21 import javax.security.auth.login.AppConfigurationEntry JavaDoc;
22 import javax.security.auth.login.Configuration JavaDoc;
23
24 import org.jboss.logging.Logger;
25 import org.jboss.security.auth.spi.UsersObjectModelFactory;
26 import org.jboss.xb.binding.JBossXBException;
27 import org.jboss.xb.binding.Unmarshaller;
28 import org.jboss.xb.binding.UnmarshallerFactory;
29
30 /** An concrete implementation of the javax.security.auth.login.Configuration
31  class that parses an xml configuration of the form:
32
33  <policy>
34  <application-policy name = "test-domain">
35  <authentication>
36  <login-module code = "org.jboss.security.plugins.samples.IdentityLoginModule"
37  flag = "required">
38  <module-option name = "principal">starksm</module-option>
39  </login-module>
40  </authentication>
41  </application-policy>
42  </policy>
43
44  @see javax.security.auth.login.Configuration
45
46  @author Scott.Stark@jboss.org
47  @version $Revision: 1.13.2.3 $
48  */

49 public class XMLLoginConfigImpl extends Configuration JavaDoc
50 {
51    private static final String JavaDoc DEFAULT_APP_CONFIG_NAME = "other";
52    private static final AuthPermission JavaDoc REFRESH_PERM = new AuthPermission JavaDoc("refreshLoginConfiguration");
53    private static Logger log = Logger.getLogger(XMLLoginConfigImpl.class);
54    /** A mapping of application name to AppConfigurationEntry[]
55    protected Map appConfigs = Collections.synchronizedMap(new HashMap());
56    */

57    PolicyConfig appConfigs = new PolicyConfig();
58    /** The URL to the XML or Sun login configuration */
59    protected URL JavaDoc loginConfigURL;
60    /** The inherited configuration we delegate to */
61    protected Configuration JavaDoc parentConfig;
62    /** A flag indicating if XML configs should be validated */
63    private boolean validateDTD = true;
64
65    // --- Begin Configuration method overrrides
66
public void refresh()
67    {
68       SecurityManager JavaDoc sm = System.getSecurityManager();
69       if (sm != null)
70          sm.checkPermission(REFRESH_PERM);
71       if (log.isTraceEnabled())
72          log.trace("Begin refresh");
73       appConfigs.clear();
74       loadConfig();
75       if (log.isTraceEnabled())
76          log.trace("End refresh");
77    }
78
79    public AppConfigurationEntry JavaDoc[] getAppConfigurationEntry(String JavaDoc appName)
80    {
81       if (log.isTraceEnabled())
82          log.trace("Begin getAppConfigurationEntry("+appName+"), size="+appConfigs.size());
83       // If the config has not been loaded try to do so
84
if (loginConfigURL == null)
85       {
86          loadConfig();
87       }
88
89       AppConfigurationEntry JavaDoc[] entry = null;
90       AuthenticationInfo authInfo = (AuthenticationInfo) appConfigs.get(appName);
91       if (authInfo == null)
92       {
93          if (log.isTraceEnabled())
94             log.trace("getAppConfigurationEntry("+appName+"), no entry in appConfigs, tyring parentCont: "+parentConfig);
95          if (parentConfig != null)
96             entry = parentConfig.getAppConfigurationEntry(appName);
97          if (entry == null)
98          {
99             if (log.isTraceEnabled())
100                log.trace("getAppConfigurationEntry("+appName+"), no entry in parentConfig, trying: "+DEFAULT_APP_CONFIG_NAME);
101          }
102          authInfo = (AuthenticationInfo) appConfigs.get(DEFAULT_APP_CONFIG_NAME);
103       }
104
105       if (authInfo != null)
106       {
107          if (log.isTraceEnabled())
108             log.trace("End getAppConfigurationEntry("+appName+"), authInfo=" + authInfo);
109          // Make a copy of the authInfo object
110
final AuthenticationInfo theAuthInfo = authInfo;
111          PrivilegedAction JavaDoc action = new PrivilegedAction JavaDoc()
112          {
113             public Object JavaDoc run()
114             {
115                return theAuthInfo.copyAppConfigurationEntry();
116             }
117          };
118          entry = (AppConfigurationEntry JavaDoc[]) AccessController.doPrivileged(action);
119       }
120       else
121       {
122          if (log.isTraceEnabled())
123             log.trace("End getAppConfigurationEntry("+appName+"), failed to find entry");
124       }
125
126       return entry;
127    }
128    // --- End Configuration method overrrides
129

130    /** Set the URL of the XML login configuration file that should
131     be loaded by this mbean on startup.
132     */

133    public URL JavaDoc getConfigURL()
134    {
135       return loginConfigURL;
136    }
137
138    /** Set the URL of the XML login configuration file that should
139     be loaded by this mbean on startup.
140     */

141    public void setConfigURL(URL JavaDoc loginConfigURL)
142    {
143       this.loginConfigURL = loginConfigURL;
144    }
145
146    public void setConfigResource(String JavaDoc resourceName)
147       throws IOException JavaDoc
148    {
149       ClassLoader JavaDoc tcl = Thread.currentThread().getContextClassLoader();
150       loginConfigURL = tcl.getResource(resourceName);
151       if (loginConfigURL == null)
152          throw new IOException JavaDoc("Failed to find resource: " + resourceName);
153    }
154
155    public void setParentConfig(Configuration JavaDoc parentConfig)
156    {
157       this.parentConfig = parentConfig;
158    }
159
160    /** Get whether the login config xml document is validated againsts its DTD
161     */

162    public boolean getValidateDTD()
163    {
164       return this.validateDTD;
165    }
166
167    /** Set whether the login config xml document is validated againsts its DTD
168     */

169    public void setValidateDTD(boolean flag)
170    {
171       this.validateDTD = flag;
172    }
173
174    /** Add an application configuration
175     */

176    public void addAppConfig(String JavaDoc appName, AppConfigurationEntry JavaDoc[] entries)
177    {
178       SecurityManager JavaDoc sm = System.getSecurityManager();
179       if (sm != null)
180          sm.checkPermission(REFRESH_PERM);
181       AuthenticationInfo authInfo = new AuthenticationInfo(appName);
182       authInfo.setAppConfigurationEntry(entries);
183       if (log.isTraceEnabled())
184          log.trace("addAppConfig("+appName+"), authInfo=" + authInfo);
185       appConfigs.add(authInfo);
186    }
187
188    public void removeAppConfig(String JavaDoc appName)
189    {
190       SecurityManager JavaDoc sm = System.getSecurityManager();
191       if (sm != null)
192          sm.checkPermission(REFRESH_PERM);
193       if (log.isTraceEnabled())
194          log.trace("removeAppConfig, appName="+appName);
195       appConfigs.remove(appName);
196    }
197
198    public void clear()
199    {
200
201    }
202
203    /** Called to try to load the config from the java.security.auth.login.config
204     * property value when there is no loginConfigURL.
205     */

206    public void loadConfig()
207    {
208       // Try to load the java.security.auth.login.config property
209
String JavaDoc loginConfig = System.getProperty("java.security.auth.login.config");
210       if (loginConfig == null)
211          loginConfig = "login-config.xml";
212
213       // If there is no loginConfigURL build it from the loginConfig
214
if (loginConfigURL == null)
215       {
216          try
217          {
218             // Try as a URL
219
loginConfigURL = new URL JavaDoc(loginConfig);
220          }
221          catch (MalformedURLException JavaDoc e)
222          {
223             // Try as a resource
224
try
225             {
226                setConfigResource(loginConfig);
227             }
228             catch (IOException JavaDoc ignore)
229             {
230                // Try as a file
231
File JavaDoc configFile = new File JavaDoc(loginConfig);
232                try
233                {
234                   setConfigURL(configFile.toURL());
235                }
236                catch (MalformedURLException JavaDoc ignore2)
237                {
238                }
239             }
240          }
241       }
242
243       if (loginConfigURL == null)
244       {
245          log.warn("Failed to find config: " + loginConfig);
246          return;
247       }
248
249       if (log.isTraceEnabled())
250          log.trace("Begin loadConfig, loginConfigURL="+loginConfigURL);
251       // Try to load the config if found
252
try
253       {
254          loadConfig(loginConfigURL);
255          if (log.isTraceEnabled())
256             log.trace("End loadConfig, loginConfigURL="+loginConfigURL);
257       }
258       catch (Exception JavaDoc e)
259       {
260          log.warn("End loadConfig, failed to load config: " + loginConfigURL, e);
261       }
262    }
263
264    protected String JavaDoc[] loadConfig(URL JavaDoc config) throws Exception JavaDoc
265    {
266       SecurityManager JavaDoc sm = System.getSecurityManager();
267       if (sm != null)
268          sm.checkPermission(REFRESH_PERM);
269
270       ArrayList JavaDoc configNames = new ArrayList JavaDoc();
271       log.debug("Try loading config as XML, url=" + config);
272       try
273       {
274          loadXMLConfig(config, configNames);
275       }
276       catch(Throwable JavaDoc e)
277       {
278          log.debug("Failed to load config as XML", e);
279          log.debug("Try loading config as Sun format, url=" + config);
280          loadSunConfig(config, configNames);
281       }
282       String JavaDoc[] names = new String JavaDoc[configNames.size()];
283       configNames.toArray(names);
284       return names;
285    }
286
287    private void loadSunConfig(URL JavaDoc sunConfig, ArrayList JavaDoc configNames)
288       throws Exception JavaDoc
289    {
290       InputStream JavaDoc is = sunConfig.openStream();
291       if (is == null)
292          throw new IOException JavaDoc("InputStream is null for: " + sunConfig);
293
294       InputStreamReader JavaDoc configFile = new InputStreamReader JavaDoc(is);
295       boolean trace = log.isTraceEnabled();
296       SunConfigParser.doParse(configFile, this, trace);
297    }
298
299    private void loadXMLConfig(URL JavaDoc loginConfigURL, ArrayList JavaDoc configNames)
300       throws IOException JavaDoc, JBossXBException
301    {
302       LoginConfigObjectModelFactory lcomf = new LoginConfigObjectModelFactory();
303       UsersObjectModelFactory uomf = new UsersObjectModelFactory();
304
305       InputStreamReader JavaDoc xmlReader = loadURL(loginConfigURL);
306       Unmarshaller unmarshaller = UnmarshallerFactory.newInstance()
307             .newUnmarshaller();
308       unmarshaller.mapFactoryToNamespace(uomf, "http://www.jboss.org/j2ee/schemas/XMLLoginModule");
309       PolicyConfig config = (PolicyConfig) unmarshaller.unmarshal(xmlReader, lcomf, null);
310       configNames.addAll(config.getConfigNames());
311       appConfigs.copy(config);
312    }
313
314    private InputStreamReader JavaDoc loadURL(URL JavaDoc configURL)
315       throws IOException JavaDoc
316    {
317       InputStream JavaDoc is = configURL.openStream();
318       if (is == null)
319          throw new IOException JavaDoc("Failed to obtain InputStream from url: " + configURL);
320       InputStreamReader JavaDoc xmlReader = new InputStreamReader JavaDoc(is);
321       return xmlReader;
322    }
323
324 }
325
Popular Tags