KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > module > builders > Users


1 /* -*- tab-width: 4; -*-
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

10 package org.mmbase.module.builders;
11
12 import java.util.*;
13 import org.mmbase.module.*;
14 import org.mmbase.module.core.*;
15 import org.mmbase.util.*;
16
17 import org.mmbase.util.logging.Logger;
18 import org.mmbase.util.logging.Logging;
19
20 /**
21  * @author Daniel Ockeloen
22  * @version $Id: Users.java,v 1.9 2004/09/20 09:43:58 marcel Exp $
23  */

24 public class Users extends MMObjectBuilder {
25
26     private static Logger log = Logging.getLoggerInstance(Users.class.getName());
27
28     // cache the 100 most active users, enh. is to allow
29
// people to set it in users.xml
30
LRUHashtable cache = new LRUHashtable(100);
31
32     // rico's funkie password generator
33
protected PasswordGeneratorInterface pwgen = new PasswordGenerator ();
34     
35     /**
36     * replace call, when called in format MMBASE-BUILDER-users-xxxxx
37     */

38     public String JavaDoc replace(scanpage sp, StringTokenizer tok) {
39         if (tok.hasMoreTokens()) {
40             String JavaDoc cmd=tok.nextToken();
41             if (cmd.equals("number")) {
42                 int i=getNumber(sp.getSessionName());
43                 if (i!=-1) {
44                     return(""+i);
45                 } else {
46                     return("");
47                 }
48
49             } else if (cmd.equals("email")) {
50                 // send email field of this user
51
return(getEmail(sp.getSessionName()));
52
53             } else if (cmd.equals("password")) {
54                 // send password field of this user
55
return(getPassword(sp.getSessionName()));
56
57             } else if (cmd.equals("account")) {
58                 // send account field of this user
59
return(getAccount(sp.getSessionName()));
60
61             } else if (cmd.equals("newpassword")) {
62                 // send a _new_ generated password
63
return(pwgen.getPassword());
64             }
65         }
66             return("");
67         }
68
69     /**
70     * get the number of the user object connected to this cookie.
71     * @param key The value of the browser cookie.
72     * @return the object number of the user object.
73     */

74     public int getNumber(String JavaDoc key) {
75
76         // check if we have this key allready in cache
77
Integer JavaDoc n=(Integer JavaDoc)cache.get(key);
78         if (n!=null) {
79
80             // we have it in the cache so return that
81
if (log.isDebugEnabled()) {
82                 log.debug("user positive cache");
83             }
84             return(n.intValue());
85         }
86
87         // it is not in the cache so lets check since the current
88
// way is only by cookies ask the cookies builder
89
// in the future more ways can be added here
90
Cookies bul=(Cookies)mmb.getMMObject("cookies");
91         if (bul!=null) {
92
93             // ask the cookie builder if he knows this cookie
94
int i=bul.getNumber(key);
95
96             if (i!=-1) {
97                 // lets find a related user, since
98
// the logic is that an user has a relation
99
// to a cookie object
100
MMObjectNode node=getNode(i);
101                 Enumeration e=node.getRelatedNodes("users").elements();
102                 if (e.hasMoreElements()) {
103                     MMObjectNode node2=(MMObjectNode)e.nextElement();
104                     if (node2!=null) {
105                         // found a related user so put it in
106
// cache and return it
107
int number=node2.getIntValue("number");
108                         cache.put(key,new Integer JavaDoc(number));
109                         if (log.isDebugEnabled()) {
110                             log.debug("users positive");
111                         }
112                         return(number);
113                     }
114                 }
115             }
116         }
117         // no user found send -1 to signal this
118
return(-1);
119     }
120
121
122     /**
123     * get account name of user (indicated by its cookie).
124     * @param key the value of the browser cookie.
125     * @return the account name.
126     */

127     protected String JavaDoc getAccount(String JavaDoc key) {
128         int number=getNumber(key);
129         if (number!=-1) {
130             MMObjectNode node=getNode(number);
131             String JavaDoc value=node.getStringValue("account");
132             return(value);
133         }
134         return("");
135     }
136
137
138     /**
139     * get email address of user (indicated by its cookie).
140     * @param key the value of the browser cookie.
141     * @return the email address.
142     */

143     protected String JavaDoc getEmail(String JavaDoc key) {
144         int number=getNumber(key);
145         if (number!=-1) {
146             MMObjectNode node=getNode(number);
147             String JavaDoc value=node.getStringValue("email");
148             return(value);
149         }
150         return("");
151     }
152
153
154     /**
155     * get password of user (indicated by its cookie).
156     * @param key the value of the browser cookie.
157     * @return the password.
158     */

159     protected String JavaDoc getPassword(String JavaDoc key) {
160         int number=getNumber(key);
161         if (number!=-1) {
162             MMObjectNode node=getNode(number);
163             String JavaDoc value=node.getStringValue("password");
164             return(value);
165         }
166         return("");
167     }
168
169     /**
170     * flush caches of the (cookie defined) user
171     * also signals the session module
172     * @param key the value of the browser cookie.
173     */

174     public void flushCache(String JavaDoc key) {
175         // bug #6583, code does not check key == null
176
if(key != null) {
177             // remove from cache
178
cache.remove(key);
179             // not get module sessions and forget the session
180
sessions s=(sessions)Module.getModule("SESSION");
181             if (s!=null) {
182                 // session module found ask it to forget
183
// this sessions
184
s.forgetSession(key);
185             }
186         }
187     }
188 }
189
Popular Tags