1 package freecs.auth; 2 3 import java.util.ArrayList ; 4 import java.util.Enumeration ; 5 import java.util.List ; 6 import java.util.Properties ; 7 import freecs.Server; 8 9 12 public abstract class AbstractAuthenticator implements IAuthenticator { 13 String additionalPrefix = null; 14 Properties props = null; 15 String toStringValue = null; 16 17 public void init (Properties allProps, String additionalPrefix) { 18 this.additionalPrefix = additionalPrefix; 19 props = filterProperties(allProps); 20 } 21 22 25 26 31 public List parseUserList(String friendslist) { 32 List users = new ArrayList (); 33 if (friendslist != null && friendslist.length() > 0) { 34 friendslist = friendslist.toLowerCase(); 35 String friendsArray[] = friendslist.split (","); 36 for (int i = 0; i < friendsArray.length; i++) { 37 String friend = friendsArray[i].trim(); 38 if (friend.length() < 1) 39 continue; 40 if (!Server.srv.auth.isValidName(friend)) 41 continue; 42 users.add(friend); 43 } 44 } 45 return users; 46 } 47 48 public boolean parseBoolean(Object value) { 49 if (value == null) { 50 return false; 51 } else if (value instanceof Boolean ) { 52 return ((Boolean )value).booleanValue(); 53 } else if (value instanceof String ) { 54 String stringVal = (String )value; 55 if ("true".equalsIgnoreCase(stringVal) || "1".equalsIgnoreCase(stringVal) || "yes".equalsIgnoreCase(stringVal)) { 56 return true; 57 } else { 58 return false; 59 } 60 } else if (value instanceof Number ) { 61 int i = ((Number )value).intValue(); 62 return (i==1) ? true : false; 63 } 64 return false; 65 } 66 67 68 72 public Properties filterProperties(Properties props) { 73 Class clazz = getClass(); 74 String packageName = clazz.getPackage().getName(); 75 String simpleClassname = clazz.getName().substring(packageName.length() + 1); 76 String filter = simpleClassname + "."; 77 if (additionalPrefix != null) 78 filter += additionalPrefix + "."; 79 return filterProperties(props, filter); 80 } 81 82 86 public Properties filterProperties(Properties props, String prefix) { 87 Properties newProps = new Properties (); 88 for (Enumeration e = props.keys(); e.hasMoreElements();) { 89 String key = (String ) e.nextElement(); 90 if (key.startsWith(prefix)) { 91 String propName = key.substring(prefix.length()); 92 String propVal = props.getProperty(key); 93 newProps.put(propName, propVal); 94 } 95 } 96 97 return newProps; 98 } 99 100 public String toString() { 101 if (toStringValue != null) 102 return toStringValue; 103 StringBuffer sb = new StringBuffer (); 104 Class clazz = getClass(); 105 String packageName = clazz.getPackage().getName(); 106 sb.append ("["); 107 sb.append (clazz.getName().substring(packageName.length() + 1)); 108 if (additionalPrefix!=null) 109 sb.append (" ").append(additionalPrefix); 110 sb.append ("]"); 111 toStringValue = sb.toString(); 112 return toStringValue; 113 } 114 } 115 | Popular Tags |