1 17 package org.apache.geronimo.console.securitymanager.realm; 18 19 import java.io.Serializable ; 20 import java.io.InputStream ; 21 import java.io.IOException ; 22 import java.util.List ; 23 import java.util.ArrayList ; 24 import java.util.Properties ; 25 import java.util.Iterator ; 26 import java.util.Map ; 27 import java.util.HashMap ; 28 import java.util.Collections ; 29 import java.util.Comparator ; 30 import org.apache.commons.logging.Log; 31 import org.apache.commons.logging.LogFactory; 32 33 39 public class MasterLoginModuleInfo implements Serializable { 40 private final static Log log = LogFactory.getLog(MasterLoginModuleInfo.class); 41 private static MasterLoginModuleInfo[] allModules; 42 private String name; 43 private String className; 44 private boolean testable = true; 45 private OptionInfo[] options = new OptionInfo[0]; 46 47 private MasterLoginModuleInfo(String name, String className) { 48 this.name = name; 49 this.className = className; 50 } 51 52 public OptionInfo[] getOptions() { 53 return options; 54 } 55 56 public Map getOptionMap() { 57 Map map = new HashMap (); 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 getName() { 66 return name; 67 } 68 69 public String 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 list = new ArrayList (); 94 Map map = new HashMap (), fieldMap = new HashMap (); 95 InputStream 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 props = new Properties (); 101 try { 102 props.load(in); 103 } catch (IOException e) { 104 log.error("Unable to read login module properties file", e); 105 } finally { 106 try { 107 in.close(); 108 } catch (java.io.IOException ignored) { 109 } 111 } 112 for (Iterator it = props.keySet().iterator(); it.hasNext();) { 113 String key = (String ) it.next(); 114 if(key.startsWith("module.")) { 115 String 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 test = props.getProperty("module."+name+".testable"); 121 if(test != null) { 122 info.setTestable(new Boolean (test.trim()).booleanValue()); 123 } 124 map.put(name, info); 125 list.add(info); 126 } 127 String prefix = "module."+name+".field."; 128 if(key.startsWith(prefix)) { 129 String fieldName = key.substring(prefix.length(), key.indexOf('.', prefix.length())); 130 List fields = (List ) fieldMap.get(name); 131 if(fields == null) { 132 fields = new ArrayList (); 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 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 it = map.keySet().iterator(); it.hasNext();) { 168 String name = (String ) it.next(); 169 MasterLoginModuleInfo info = (MasterLoginModuleInfo) map.get(name); 170 List fields = (List ) 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 () { 177 public int compare(Object o1, Object 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 , Comparable { 192 private final String name; 193 private final String displayName; 194 private final String 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 name, String displayName, String description) { 201 this.name = name; 202 this.displayName = displayName; 203 this.description = description; 204 } 205 206 public String getName() { 207 return name; 208 } 209 210 public String getDisplayName() { 211 return displayName; 212 } 213 214 public String 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 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 |