KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.util.ArrayList JavaDoc;
10 import java.util.HashMap JavaDoc;
11
12 import javax.security.auth.login.AppConfigurationEntry JavaDoc;
13
14 import org.w3c.dom.Element JavaDoc;
15 import org.w3c.dom.NodeList JavaDoc;
16 import org.jboss.metadata.MetaData;
17
18 /** Utility methods for parsing the XMlLoginConfig elements into
19  * AuthenticationInfo instances.
20  *
21  * @author Scott.Stark@jboss.org
22  * @version $Revision: 1.5.2.1 $
23  */

24 public class ConfigUtil
25 {
26    /** Parse the application-policy/authentication element
27     @param policy , the application-policy/authentication element
28     @return the AuthenticationInfo object for the xml policy fragment
29     */

30    static public AuthenticationInfo parseAuthentication(Element JavaDoc policy)
31       throws Exception JavaDoc
32    {
33       // Parse the permissions
34
NodeList JavaDoc authentication = policy.getElementsByTagName("authentication");
35       if (authentication.getLength() == 0)
36       {
37          return null;
38       }
39
40       Element JavaDoc auth = (Element JavaDoc) authentication.item(0);
41       NodeList JavaDoc modules = auth.getElementsByTagName("login-module");
42       ArrayList JavaDoc tmp = new ArrayList JavaDoc();
43       for (int n = 0; n < modules.getLength(); n++)
44       {
45          Element JavaDoc module = (Element JavaDoc) modules.item(n);
46          parseModule(module, tmp);
47       }
48       AppConfigurationEntry JavaDoc[] entries = new AppConfigurationEntry JavaDoc[tmp.size()];
49       tmp.toArray(entries);
50       AuthenticationInfo info = new AuthenticationInfo();
51       info.setAppConfigurationEntry(entries);
52       return info;
53    }
54
55    static void parseModule(Element JavaDoc module, ArrayList JavaDoc entries)
56       throws Exception JavaDoc
57    {
58       AppConfigurationEntry.LoginModuleControlFlag JavaDoc controlFlag = AppConfigurationEntry.LoginModuleControlFlag.REQUIRED;
59       String JavaDoc className = MetaData.getElementAttribute(module, "code");
60       String JavaDoc flag = MetaData.getElementAttribute(module, "flag");
61       if (flag != null)
62       {
63          // Lower case is what is used by the jdk1.4.1 implementation
64
flag = flag.toLowerCase();
65          if (AppConfigurationEntry.LoginModuleControlFlag.REQUIRED.toString().indexOf(flag) > 0)
66             controlFlag = AppConfigurationEntry.LoginModuleControlFlag.REQUIRED;
67          else if (AppConfigurationEntry.LoginModuleControlFlag.REQUISITE.toString().indexOf(flag) > 0)
68             controlFlag = AppConfigurationEntry.LoginModuleControlFlag.REQUISITE;
69          else if (AppConfigurationEntry.LoginModuleControlFlag.SUFFICIENT.toString().indexOf(flag) > 0)
70             controlFlag = AppConfigurationEntry.LoginModuleControlFlag.SUFFICIENT;
71          else if (AppConfigurationEntry.LoginModuleControlFlag.OPTIONAL.toString().indexOf(flag) > 0)
72             controlFlag = AppConfigurationEntry.LoginModuleControlFlag.OPTIONAL;
73       }
74       NodeList JavaDoc opts = module.getElementsByTagName("module-option");
75       HashMap JavaDoc options = new HashMap JavaDoc();
76       for (int n = 0; n < opts.getLength(); n++)
77       {
78          Element JavaDoc opt = (Element JavaDoc) opts.item(n);
79          String JavaDoc name = opt.getAttribute("name");
80          String JavaDoc value = MetaData.getElementContent(opt, "");
81          options.put(name, value);
82       }
83       AppConfigurationEntry JavaDoc entry = new AppConfigurationEntry JavaDoc(className, controlFlag, options);
84       entries.add(entry);
85    }
86 }
87
Popular Tags