KickJava   Java API By Example, From Geeks To Geeks.

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


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.HashMap JavaDoc;
10 import javax.security.auth.login.AppConfigurationEntry JavaDoc;
11
12 import org.jboss.xb.binding.ObjectModelFactory;
13 import org.jboss.xb.binding.UnmarshallingContext;
14 import org.jboss.util.StringPropertyReplacer;
15 import org.xml.sax.Attributes JavaDoc;
16
17 /** A JBossXB object factory for parsing the login-config.xml object model.
18  *
19  * @author Scott.Stark@jboss.org
20  * @version $Revision: 1.1.2.5 $
21  */

22 public class LoginConfigObjectModelFactory implements ObjectModelFactory
23 {
24    public Object JavaDoc completeRoot(Object JavaDoc root, UnmarshallingContext ctx,
25          String JavaDoc uri, String JavaDoc name)
26    {
27       return root;
28    }
29
30    public Object JavaDoc newRoot(Object JavaDoc root, UnmarshallingContext navigator,
31       String JavaDoc namespaceURI, String JavaDoc localName, Attributes JavaDoc attrs)
32    {
33       if (!localName.equals("policy"))
34       {
35          throw new IllegalStateException JavaDoc("Unexpected root element: was expecting 'policy' but got '" + localName + "'");
36       }
37       return new PolicyConfig();
38    }
39
40    
41    public Object JavaDoc newChild(PolicyConfig config, UnmarshallingContext navigator,
42       String JavaDoc namespaceUri, String JavaDoc localName, Attributes JavaDoc attrs)
43    {
44       Object JavaDoc child = null;
45       if("application-policy".equals(localName))
46       {
47          String JavaDoc name = attrs.getValue("name");
48          name = StringPropertyReplacer.replaceProperties(name);
49          child = new AuthenticationInfo(name);
50       }
51       return child;
52    }
53    public Object JavaDoc newChild(AuthenticationInfo info, UnmarshallingContext navigator,
54       String JavaDoc namespaceUri, String JavaDoc localName, Attributes JavaDoc attrs)
55    {
56       Object JavaDoc child = null;
57       if("login-module".equals(localName))
58       {
59          String JavaDoc code = attrs.getValue("code");
60          code = StringPropertyReplacer.replaceProperties(code.trim());
61          String JavaDoc flag = attrs.getValue("flag");
62          flag = StringPropertyReplacer.replaceProperties(flag.trim());
63          AppConfigurationEntryHolder holder = new AppConfigurationEntryHolder(code, flag);
64          child = holder;
65       }
66
67       return child;
68    }
69    public Object JavaDoc newChild(AppConfigurationEntryHolder entry, UnmarshallingContext navigator,
70       String JavaDoc namespaceUri, String JavaDoc localName, Attributes JavaDoc attrs)
71    {
72       Object JavaDoc child = null;
73       if("module-option".equals(localName))
74       {
75          String JavaDoc name = attrs.getValue("name");
76          child = new ModuleOption(name);
77       }
78
79       return child;
80    }
81
82    public void setValue(ModuleOption option, UnmarshallingContext navigator,
83       String JavaDoc namespaceUri, String JavaDoc localName, String JavaDoc value)
84    {
85       if("module-option".equals(localName))
86       {
87          String JavaDoc valueWithReplacement = StringPropertyReplacer.replaceProperties(value.trim());
88          option.setValue(valueWithReplacement);
89       }
90    }
91
92    public void addChild(PolicyConfig config, AuthenticationInfo authInfo,
93       UnmarshallingContext navigator, String JavaDoc namespaceURI, String JavaDoc localName)
94    {
95       config.add(authInfo);
96    }
97    public void addChild(AuthenticationInfo authInfo, AppConfigurationEntryHolder entryInfo,
98       UnmarshallingContext navigator, String JavaDoc namespaceURI, String JavaDoc localName)
99    {
100       AppConfigurationEntry JavaDoc entry = entryInfo.getEntry();
101       authInfo.addAppConfigurationEntry(entry);
102    }
103    public void addChild(AppConfigurationEntryHolder entryInfo, ModuleOption option,
104       UnmarshallingContext navigator, String JavaDoc namespaceURI, String JavaDoc localName)
105    {
106       entryInfo.addOption(option);
107    }
108    public void addChild(ModuleOption option, Object JavaDoc value,
109       UnmarshallingContext navigator, String JavaDoc namespaceURI, String JavaDoc localName)
110    {
111       option.setValue(value);
112    }
113
114    private static class AppConfigurationEntryHolder
115    {
116       String JavaDoc code;
117       AppConfigurationEntry.LoginModuleControlFlag JavaDoc controlFlag;
118       HashMap JavaDoc options = new HashMap JavaDoc();
119       AppConfigurationEntryHolder(String JavaDoc code, String JavaDoc flag)
120       {
121          this.code = code;
122          controlFlag = AppConfigurationEntry.LoginModuleControlFlag.REQUIRED;
123          if (flag != null)
124          {
125             // Lower case is what is used by the jdk1.4.1 implementation
126
flag = flag.toLowerCase();
127             if (AppConfigurationEntry.LoginModuleControlFlag.REQUIRED.toString().indexOf(flag) > 0)
128                controlFlag = AppConfigurationEntry.LoginModuleControlFlag.REQUIRED;
129             else if (AppConfigurationEntry.LoginModuleControlFlag.REQUISITE.toString().indexOf(flag) > 0)
130                controlFlag = AppConfigurationEntry.LoginModuleControlFlag.REQUISITE;
131             else if (AppConfigurationEntry.LoginModuleControlFlag.SUFFICIENT.toString().indexOf(flag) > 0)
132                controlFlag = AppConfigurationEntry.LoginModuleControlFlag.SUFFICIENT;
133             else if (AppConfigurationEntry.LoginModuleControlFlag.OPTIONAL.toString().indexOf(flag) > 0)
134                controlFlag = AppConfigurationEntry.LoginModuleControlFlag.OPTIONAL;
135          }
136       }
137
138       public AppConfigurationEntry JavaDoc getEntry()
139       {
140          AppConfigurationEntry JavaDoc entry = new AppConfigurationEntry JavaDoc(code, controlFlag, options);
141          return entry;
142       }
143
144       public void addOption(ModuleOption option)
145       {
146          options.put(option.name, option.value);
147       }
148    }
149    private static class ModuleOption
150    {
151       String JavaDoc name;
152       Object JavaDoc value = "";
153       ModuleOption(String JavaDoc name)
154       {
155          this.name = name;
156       }
157       void setValue(Object JavaDoc value)
158       {
159          this.value = value;
160       }
161    }
162 }
163
Popular Tags