KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > freecs > auth > AbstractAuthenticator


1 package freecs.auth;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.Enumeration JavaDoc;
5 import java.util.List JavaDoc;
6 import java.util.Properties JavaDoc;
7 import freecs.Server;
8
9 /**
10  * base class for all Authenticators, provides methods for reading config files
11  */

12 public abstract class AbstractAuthenticator implements IAuthenticator {
13     String JavaDoc additionalPrefix = null;
14     Properties JavaDoc props = null;
15     String JavaDoc toStringValue = null;
16
17     public void init (Properties JavaDoc allProps, String JavaDoc additionalPrefix) {
18         this.additionalPrefix = additionalPrefix;
19         props = filterProperties(allProps);
20     }
21
22     /***************************************************************************
23      * helpers
24      **************************************************************************/

25     
26     /**
27      * parses a comma-separated uselist (e.g. friendslist)
28      * @param string like this: "friend1,friend2,friend3"
29      * @return array of valid usernames, empty array if no users are valid
30      */

31     public List JavaDoc parseUserList(String JavaDoc friendslist) {
32         List JavaDoc users = new ArrayList JavaDoc();
33         if (friendslist != null && friendslist.length() > 0) {
34             friendslist = friendslist.toLowerCase();
35             String JavaDoc friendsArray[] = friendslist.split (",");
36             for (int i = 0; i < friendsArray.length; i++) {
37                 String JavaDoc 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 JavaDoc value) {
49         if (value == null) {
50             return false;
51         } else if (value instanceof Boolean JavaDoc) {
52             return ((Boolean JavaDoc)value).booleanValue();
53         } else if (value instanceof String JavaDoc) {
54             String JavaDoc stringVal = (String JavaDoc)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 JavaDoc) {
61             int i = ((Number JavaDoc)value).intValue();
62             return (i==1) ? true : false;
63         }
64         return false;
65     }
66     
67
68     /**
69      * parses properties so that only keys that start with the
70      * name of this class remain
71      */

72     public Properties JavaDoc filterProperties(Properties JavaDoc props) {
73         Class JavaDoc clazz = getClass();
74         String JavaDoc packageName = clazz.getPackage().getName();
75         String JavaDoc simpleClassname = clazz.getName().substring(packageName.length() + 1);
76         String JavaDoc filter = simpleClassname + ".";
77         if (additionalPrefix != null)
78             filter += additionalPrefix + ".";
79         return filterProperties(props, filter);
80     }
81         
82     /**
83      * parses properties so that only keys that start with the
84      * given prefix remain
85      */

86     public Properties JavaDoc filterProperties(Properties JavaDoc props, String JavaDoc prefix) {
87         Properties JavaDoc newProps = new Properties JavaDoc();
88         for (Enumeration JavaDoc e = props.keys(); e.hasMoreElements();) {
89             String JavaDoc key = (String JavaDoc) e.nextElement();
90             if (key.startsWith(prefix)) {
91                 String JavaDoc propName = key.substring(prefix.length());
92                 String JavaDoc propVal = props.getProperty(key);
93                 newProps.put(propName, propVal);
94             }
95         }
96         
97         return newProps;
98     }
99     
100     public String JavaDoc toString() {
101         if (toStringValue != null)
102             return toStringValue;
103         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
104         Class JavaDoc clazz = getClass();
105         String JavaDoc 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