KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > console > securitymanager > realm > MasterLoginModuleInfo


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.geronimo.console.securitymanager.realm;
18
19 import java.io.Serializable JavaDoc;
20 import java.io.InputStream JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.util.List JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Properties JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.Map JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.Collections JavaDoc;
29 import java.util.Comparator JavaDoc;
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32
33 /**
34  * Describes an available login module, including how to create and configure it.
35  * Reads the list of available login modules from a properties file on the class path.
36  *
37  * @version $Rev: 476061 $ $Date: 2006-11-17 01:36:50 -0500 (Fri, 17 Nov 2006) $
38  */

39 public class MasterLoginModuleInfo implements Serializable JavaDoc {
40     private final static Log log = LogFactory.getLog(MasterLoginModuleInfo.class);
41     private static MasterLoginModuleInfo[] allModules;
42     private String JavaDoc name;
43     private String JavaDoc className;
44     private boolean testable = true;
45     private OptionInfo[] options = new OptionInfo[0];
46
47     private MasterLoginModuleInfo(String JavaDoc name, String JavaDoc className) {
48         this.name = name;
49         this.className = className;
50     }
51
52     public OptionInfo[] getOptions() {
53         return options;
54     }
55
56     public Map JavaDoc getOptionMap() {
57         Map JavaDoc map = new HashMap JavaDoc();
58         for (int i = 0; i < options.length; i++) {
59             OptionInfo info = options[i];
60             map.put(info.getName(), info);
61         }
62         return map;
63     }
64
65     public String JavaDoc getName() {
66         return name;
67     }
68
69     public String JavaDoc getClassName() {
70         return className;
71     }
72
73     public boolean isTestable() {
74         return testable;
75     }
76
77     private void setTestable(boolean testable) {
78         this.testable = testable;
79     }
80
81     private void setOptions(OptionInfo[] options) {
82         this.options = options;
83     }
84
85     public static MasterLoginModuleInfo[] getAllModules() {
86         if(allModules == null) {
87             allModules = loadModules();
88         }
89         return allModules;
90     }
91
92     private static MasterLoginModuleInfo[] loadModules() {
93         List JavaDoc list = new ArrayList JavaDoc();
94         Map JavaDoc map = new HashMap JavaDoc(), fieldMap = new HashMap JavaDoc();
95         InputStream JavaDoc in = MasterLoginModuleInfo.class.getResourceAsStream("/login-modules.properties");
96         if(in == null) {
97             log.error("Unable to locate login module properties file");
98             return null;
99         }
100         Properties JavaDoc props = new Properties JavaDoc();
101         try {
102             props.load(in);
103         } catch (IOException JavaDoc e) {
104             log.error("Unable to read login module properties file", e);
105         } finally {
106             try {
107                 in.close();
108             } catch (java.io.IOException JavaDoc ignored) {
109                 // ignore
110
}
111         }
112         for (Iterator JavaDoc it = props.keySet().iterator(); it.hasNext();) {
113             String JavaDoc key = (String JavaDoc) it.next();
114             if(key.startsWith("module.")) {
115                 String JavaDoc name = key.substring(7, key.indexOf('.', 7));
116                 MasterLoginModuleInfo info = (MasterLoginModuleInfo) map.get(name);
117                 if(info == null) {
118                     info = new MasterLoginModuleInfo(props.getProperty("module."+name+".name"),
119                             props.getProperty("module."+name+".class"));
120                     String JavaDoc test = props.getProperty("module."+name+".testable");
121                     if(test != null) {
122                         info.setTestable(new Boolean JavaDoc(test.trim()).booleanValue());
123                     }
124                     map.put(name, info);
125                     list.add(info);
126                 }
127                 String JavaDoc prefix = "module."+name+".field.";
128                 if(key.startsWith(prefix)) {
129                     String JavaDoc fieldName = key.substring(prefix.length(), key.indexOf('.', prefix.length()));
130                     List JavaDoc fields = (List JavaDoc) fieldMap.get(name);
131                     if(fields == null) {
132                         fields = new ArrayList JavaDoc();
133                         fieldMap.put(name, fields);
134                     }
135                     OptionInfo option = null;
136                     for (int i = 0; i < fields.size(); i++) {
137                         OptionInfo opt = (OptionInfo) fields.get(i);
138                         if(opt.getName().equals(fieldName)) {
139                             option = opt;
140                             break;
141                         }
142                     }
143                     if(option == null) {
144                         option = new OptionInfo(fieldName, props.getProperty(prefix+fieldName+".displayName"),
145                                 props.getProperty(prefix+fieldName+".description"));
146                         String JavaDoc test = props.getProperty(prefix+fieldName+".password");
147                         if(test != null) {
148                             option.setPassword(true);
149                         }
150                         test = props.getProperty(prefix+fieldName+".length");
151                         if(test != null) {
152                             option.setLength(Integer.parseInt(test.trim()));
153                         }
154                         test = props.getProperty(prefix+fieldName+".displayOrder");
155                         if(test != null) {
156                             option.setDisplayOrder(Integer.parseInt(test.trim()));
157                         }
158                         test = props.getProperty(prefix+fieldName+".blankAllowed");
159                         if(test != null) {
160                             option.setBlankAllowed("true".equalsIgnoreCase(test.trim()));
161                         }
162                         fields.add(option);
163                     }
164                 }
165             }
166         }
167         for (Iterator JavaDoc it = map.keySet().iterator(); it.hasNext();) {
168             String JavaDoc name = (String JavaDoc) it.next();
169             MasterLoginModuleInfo info = (MasterLoginModuleInfo) map.get(name);
170             List JavaDoc fields = (List JavaDoc) fieldMap.get(name);
171             if(fields != null) {
172                 Collections.sort(fields);
173                 info.setOptions((OptionInfo[]) fields.toArray(new OptionInfo[fields.size()]));
174             }
175         }
176         Collections.sort(list, new Comparator JavaDoc() {
177             public int compare(Object JavaDoc o1, Object JavaDoc o2) {
178                 MasterLoginModuleInfo m1 = (MasterLoginModuleInfo) o1, m2 = (MasterLoginModuleInfo) o2;
179                 if(m1.getName().equals("Other")) {
180                     return 1;
181                 } else if(m2.getName().equals("Other")) {
182                     return -1;
183                 } else {
184                     return m1.getName().compareTo(m2.getName());
185                 }
186             }
187         });
188         return (MasterLoginModuleInfo[]) list.toArray(new MasterLoginModuleInfo[list.size()]);
189     }
190
191     public final static class OptionInfo implements Serializable JavaDoc, Comparable JavaDoc {
192         private final String JavaDoc name;
193         private final String JavaDoc displayName;
194         private final String JavaDoc description;
195         private boolean password = false;
196         private int length = 30;
197         private int displayOrder = 1;
198         private boolean blankAllowed = false;
199
200         public OptionInfo(String JavaDoc name, String JavaDoc displayName, String JavaDoc description) {
201             this.name = name;
202             this.displayName = displayName;
203             this.description = description;
204         }
205
206         public String JavaDoc getName() {
207             return name;
208         }
209
210         public String JavaDoc getDisplayName() {
211             return displayName;
212         }
213
214         public String JavaDoc getDescription() {
215             return description;
216         }
217
218         public boolean isPassword() {
219             return password;
220         }
221
222         public void setPassword(boolean password) {
223             this.password = password;
224         }
225
226         public int getLength() {
227             return length;
228         }
229
230         public void setLength(int length) {
231             this.length = length;
232         }
233
234         public int getDisplayOrder() {
235             return displayOrder;
236         }
237
238         public void setDisplayOrder(int displayOrder) {
239             this.displayOrder = displayOrder;
240         }
241
242         public int compareTo(Object JavaDoc o) {
243             return displayOrder - ((OptionInfo)o).displayOrder;
244         }
245         
246         public boolean isBlankAllowed() {
247             return this.blankAllowed;
248         }
249         
250         public void setBlankAllowed(boolean blankAllowed) {
251             this.blankAllowed = blankAllowed;
252         }
253     }
254 }
255
Popular Tags