KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jcorporate > expresso > core > misc > CurrentLogin


1 /* ====================================================================
2  * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
3  *
4  * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution,
19  * if any, must include the following acknowledgment:
20  * "This product includes software developed by Jcorporate Ltd.
21  * (http://www.jcorporate.com/)."
22  * Alternately, this acknowledgment may appear in the software itself,
23  * if and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. "Jcorporate" and product names such as "Expresso" must
26  * not be used to endorse or promote products derived from this
27  * software without prior written permission. For written permission,
28  * please contact info@jcorporate.com.
29  *
30  * 5. Products derived from this software may not be called "Expresso",
31  * or other Jcorporate product names; nor may "Expresso" or other
32  * Jcorporate product names appear in their name, without prior
33  * written permission of Jcorporate Ltd.
34  *
35  * 6. No product derived from this software may compete in the same
36  * market space, i.e. framework, without prior written permission
37  * of Jcorporate Ltd. For written permission, please contact
38  * partners@jcorporate.com.
39  *
40  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
41  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
42  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
43  * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
44  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
45  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
46  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
47  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
48  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
49  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
50  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  * ====================================================================
53  *
54  * This software consists of voluntary contributions made by many
55  * individuals on behalf of the Jcorporate Ltd. Contributions back
56  * to the project(s) are encouraged when you make modifications.
57  * Please send them to support@jcorporate.com. For more information
58  * on Jcorporate Ltd. and its products, please see
59  * <http://www.jcorporate.com/>.
60  *
61  * Portions of this software are based upon other open source
62  * products and are subject to their respective licenses.
63  */

64
65 package com.jcorporate.expresso.core.misc;
66
67 import com.jcorporate.expresso.core.security.User;
68 import org.apache.log4j.Logger;
69
70 import javax.servlet.http.HttpSessionBindingEvent JavaDoc;
71 import javax.servlet.http.HttpSessionBindingListener JavaDoc;
72 import java.io.Serializable JavaDoc;
73 import java.util.ArrayList JavaDoc;
74 import java.util.Hashtable JavaDoc;
75
76 /**
77  * Bean that stores current login info in session; implements
78  * session binding listener
79  * so that it can hear when session is terminated.
80  * This class be overriden and replaced with subclass by adding a
81  * line to expresso-config.xml under
82  * 'class-handlers', like
83  * class-handler name="CurrentLogin"
84  * classHandler="my.subclass.of.CurrentLogin"
85  * This override will allow custom handling in the session bind/unbind events.
86  *
87  * @author Michael Nash; Larry Hamel (CodeGuild)
88  * @since Expresso 4.0
89  */

90 public class CurrentLogin
91         implements HttpSessionBindingListener JavaDoc,
92         Serializable JavaDoc {
93
94     /**
95      * The sessionID assigned to this login.
96      */

97     private String JavaDoc sessionId = "";
98
99     /**
100      * User name assigned to this login.
101      */

102     private String JavaDoc userName = User.UNKNOWN_USER;
103
104     /**
105      * This login's remote ip address.
106      */

107     private String JavaDoc ipAddress = "";
108
109     /**
110      * Timestamp for when the person logged in.
111      */

112     private long loggedInAt = 0;
113
114     /**
115      * The integer uid for this login.
116      */

117     private int uid = 0;
118
119     /**
120      * The database context that this login belongs to.
121      */

122     private String JavaDoc dbName = "default";
123
124     /**
125      * The messages for the user.
126      */

127     private ArrayList JavaDoc messages = null;
128
129     /**
130      * Table of attributes.
131      */

132     private Hashtable JavaDoc attribHash = null;
133
134     /**
135      * The session key that we can use to get the currentlogin from
136      * a person's session.
137      */

138     public static final String JavaDoc LOGIN_KEY = "CurrentLogin";
139
140     /**
141      * The log4j Logger.
142      */

143     private transient static Logger slog = null;
144
145     /**
146      * Loads the Log4j Logger if necessary.
147      *
148      * @return Logger instance.
149      */

150     public static Logger getLogger() {
151         synchronized (CurrentLogin.class) {
152             if (slog == null) {
153                 slog = Logger.getLogger(CurrentLogin.class);
154             }
155             return slog;
156         }
157     }
158
159     /**
160      * Old constructor
161      *
162      * @param newSessionId the session id as returned by the servlet container
163      * @param newUserName the user login name
164      * @param newIPAddress the user's ip address
165      * @param newDataContext the data context for the login
166      * @param newLoggedInAt the time the user logged in
167      * @param newUid the new user id
168      * @deprecated use newInstance() instead; 9/04 v5.5+
169      */

170     public CurrentLogin(final String JavaDoc newSessionId, final String JavaDoc newUserName,
171                         final String JavaDoc newIPAddress, final String JavaDoc newDataContext,
172                         final long newLoggedInAt, final int newUid) {
173         sessionId = newSessionId;
174         userName = newUserName;
175         ipAddress = newIPAddress;
176         loggedInAt = newLoggedInAt;
177         dbName = newDataContext;
178         uid = newUid;
179     }
180
181     /**
182      * Main constructor.
183      *
184      * @param newUserName the user login name
185      * @param newIPAddress the user's ip address
186      * @param newDataContext the data context for the login
187      * @param newUid the new user id
188      */

189     private CurrentLogin(final String JavaDoc newUserName,
190                          final String JavaDoc newIPAddress,
191                          final String JavaDoc newDataContext,
192                          final int newUid) {
193         userName = newUserName;
194         ipAddress = newIPAddress;
195         loggedInAt = System.currentTimeMillis();
196         dbName = newDataContext;
197         uid = newUid;
198     }
199
200     /**
201      * Default constructor.
202      */

203     public CurrentLogin() {
204     }
205
206     /**
207      * Add message to the current login.
208      *
209      * @param newMessage The New Message.
210      */

211     public synchronized void addMessage(final String JavaDoc newMessage) {
212         if (messages == null) {
213             messages = new ArrayList JavaDoc();
214         }
215
216         messages.add(newMessage);
217     }
218
219     /**
220      * Gets the current messages associated with this current login and
221      * then automatically removes them.
222      *
223      * @return ArrayList of messages.
224      */

225     public synchronized ArrayList JavaDoc getAndClearMessages() {
226         if (messages != null) {
227             ArrayList JavaDoc msgToReturn = messages;
228             messages = null;
229
230             return msgToReturn;
231         }
232
233         return null;
234     }
235
236     /**
237      * Bean method that sets the user name for the login.
238      *
239      * @param newUserName the new user name.
240      */

241     public void setUserName(final String JavaDoc newUserName) {
242         userName = newUserName;
243     }
244
245     /**
246      * Bean method that setse the database context name for the login.
247      *
248      * @param newDBName the database context.
249      */

250     public void setDBName(final String JavaDoc newDBName) {
251         dbName = newDBName;
252     }
253
254     /**
255      * Bean method that gets the session id.
256      *
257      * @return java.lang.String the server's session id
258      */

259     public String JavaDoc getSessionId() {
260         return sessionId;
261     }
262
263     /**
264      * Bean method that gets the currently logged in user name.
265      *
266      * @return java.lang.String
267      */

268     public String JavaDoc getUserName() {
269         return userName;
270     }
271
272     /**
273      * Bean method that gets the current uid.
274      *
275      * @return integer
276      */

277     public int getUid() {
278         return uid;
279     }
280
281     /**
282      * Bean method that gets the current database name.
283      *
284      * @return java.lang.String
285      */

286     public String JavaDoc getDBName() {
287         return dbName;
288     }
289
290     public String JavaDoc getIPAddress() {
291         return ipAddress;
292     }
293
294     public long getLogInTime() {
295         return loggedInAt;
296     }
297
298     /**
299      * This class listens to the binding of this object to the session.
300      * Can be overriden, but be sure to call this super method.
301      *
302      * @param evt The HttpSession Binding Event.
303      */

304     public void valueBound(final HttpSessionBindingEvent JavaDoc evt) {
305         sessionId = evt.getSession().getId();
306         ConfigManager.addSession(this);
307     }
308
309     /**
310      * This class listens to the unbinding of this object to the session.
311      * Clears this login object from the config system. Can be overriden,
312      * but be sure to call this super method.
313      *
314      * @param evt The HttpSessionBindingEvent that is the source.
315      */

316     public void valueUnbound(final HttpSessionBindingEvent JavaDoc evt) {
317         ConfigManager.removeSession(sessionId);
318         sessionId = null;
319     }
320
321     /**
322      * Object construction. Constructs a new instance of a Current login.
323      * This function will allow pluggable CurrentLogins
324      *
325      * @param newUserName the user name of the person logging in
326      * @param newIPAddress the client ip address
327      * @param dataContext the data context to log into.
328      * @param newUid The new user id.
329      * @return The constructed CurrentLogin instance
330      */

331     public static CurrentLogin newInstance(final String JavaDoc newUserName,
332                                            final String JavaDoc newIPAddress,
333                                            final String JavaDoc dataContext,
334                                            final int newUid) {
335
336         // check for override
337
String JavaDoc className = ConfigManager.getClassHandler(LOGIN_KEY);
338         CurrentLogin loginInstance = null;
339         if (className != null) {
340             try {
341                 loginInstance = (CurrentLogin) Class.forName(className).
342                         newInstance();
343             } catch (Exception JavaDoc e) {
344                 getLogger().error(e);
345             }
346         }
347
348         if (loginInstance == null) {
349             // use default--this class
350
loginInstance = new CurrentLogin(newUserName, newIPAddress,
351                     dataContext, newUid);
352         } else {
353             loginInstance.setUserName(newUserName);
354             loginInstance.setIpAddress(newIPAddress);
355             loginInstance.setDBName(dataContext);
356             loginInstance.setUid(newUid);
357             loginInstance.setLoggedInAt(System.currentTimeMillis());
358         }
359
360         return loginInstance;
361     }
362
363     /**
364      * Object construction. Constructs a new instance of a Current login.
365      * This function will allow pluggable CurrentLogins
366      *
367      * @param newSessionId the servlet session id
368      * @param newUserName the user name of the person logging in
369      * @param newIPAddress the client ip address
370      * @param dataContext the data context to log into.
371      * @param newLoggedInAt system time that the person logged in.
372      * @param newUid The new user id.
373      * @return The constructed CurrentLogin instance
374      * @deprecated use other newInstance instead; 9/04 v.5.5+
375      */

376     public static CurrentLogin newInstance(final String JavaDoc newSessionId,
377                                            final String JavaDoc newUserName,
378                                            final String JavaDoc newIPAddress,
379                                            final String JavaDoc dataContext,
380                                            final long newLoggedInAt,
381                                            final int newUid) {
382         CurrentLogin alogin = new CurrentLogin(newUserName, newIPAddress,
383                 dataContext, newUid);
384         alogin.loggedInAt = newLoggedInAt;
385         alogin.sessionId = newSessionId;
386         return alogin;
387     }
388
389     /**
390      * Convenience setter.
391      *
392      * @param loggedInAt timestamp in milliseconds
393      */

394     public void setLoggedInAt(final long loggedInAt) {
395         this.loggedInAt = loggedInAt;
396     }
397
398     /**
399      * Get an attribute stored in hashtable in session.
400      *
401      * @param key hashkey
402      * @return Object or null if the attribute isn't set.
403      */

404     public Object JavaDoc getAttribute(final String JavaDoc key) {
405         Object JavaDoc result = null;
406         if (attribHash != null && key != null) {
407             result = attribHash.get(key);
408         }
409         return result;
410     }
411
412     /**
413      * Set an attribute stored in hashtable in session.
414      *
415      * @param key hashkey
416      * @param attribute to be stored
417      */

418     public void setAttribute(final String JavaDoc key, final Object JavaDoc attribute) {
419         if (attribute == null) {
420             removeAttribute(key);
421             return;
422         }
423
424         if (attribHash == null) {
425             attribHash = new Hashtable JavaDoc();
426         }
427
428         if (key != null) {
429             attribHash.put(key, attribute);
430         }
431     }
432
433     /**
434      * Remove an attribute stored in hashtable in session.
435      *
436      * @param key hashkey
437      */

438     public void removeAttribute(final String JavaDoc key) {
439         if (attribHash != null && key != null) {
440             attribHash.remove(key);
441         }
442     }
443
444     /**
445      * Convenience setter.
446      *
447      * @param theIpAddress address of this login
448      */

449     public void setIpAddress(final String JavaDoc theIpAddress) {
450         this.ipAddress = theIpAddress;
451     }
452
453     /**
454      * Convenience setter.
455      *
456      * @param theUid user ID of this login
457      */

458     public void setUid(final int theUid) {
459         this.uid = theUid;
460     }
461
462     /**
463      * Convenience setter.
464      *
465      * @param theSessionId ID of this session
466      */

467     public void setSessionId(final String JavaDoc theSessionId) {
468         this.sessionId = theSessionId;
469     }
470 }
471
472 /* CurrentLogin */
473
Popular Tags