KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fulcrum > security > impl > db > entity > TurbineUser


1 package org.apache.fulcrum.security.impl.db.entity;
2
3 /* ====================================================================
4  * The Apache Software License, Version 1.1
5  *
6  * Copyright (c) 2001 The Apache Software Foundation. All rights
7  * reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution,
22  * if any, must include the following acknowledgment:
23  * "This product includes software developed by the
24  * Apache Software Foundation (http://www.apache.org/)."
25  * Alternately, this acknowledgment may appear in the software itself,
26  * if and wherever such third-party acknowledgments normally appear.
27  *
28  * 4. The names "Apache" and "Apache Software Foundation" and
29  * "Apache Turbine" must not be used to endorse or promote products
30  * derived from this software without prior written permission. For
31  * written permission, please contact apache@apache.org.
32  *
33  * 5. Products derived from this software may not be called "Apache",
34  * "Apache Turbine", nor may "Apache" appear in their name, without
35  * prior written permission of the Apache Software Foundation.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48  * SUCH DAMAGE.
49  * ====================================================================
50  *
51  * This software consists of voluntary contributions made by many
52  * individuals on behalf of the Apache Software Foundation. For more
53  * information on the Apache Software Foundation, please see
54  * <http://www.apache.org/>.
55  */

56
57 import java.io.ByteArrayOutputStream JavaDoc;
58 import java.io.PrintWriter JavaDoc;
59 import java.util.Date JavaDoc;
60 import java.util.Hashtable JavaDoc;
61 import org.apache.fulcrum.security.TurbineSecurity;
62 import org.apache.fulcrum.security.entity.User;
63 import org.apache.fulcrum.security.session.SessionBindingEvent;
64
65 /**
66  * A generic implementation of User interface.
67  *
68  * This basic implementation contains the functionality that is
69  * expected to be common among all User implementations.
70  *
71  * @author <a HREF="mailto:josh@stonecottage.com">Josh Lucas</a>
72  * @author <a HREF="mailto:jon@collab.net">Jon S. Stevens</a>
73  * @author <a HREF="mailto:jmcnally@collab.net">John D. McNally</a>
74  * @author <a HREF="mailto:frank.kim@clearink.com">Frank Y. Kim</a>
75  * @author <a HREF="mailto:cberry@gluecode.com">Craig D. Berry</a>
76  * @author <a HREF="mailto:mpoeschl@marmot.at">Martin Poeschl</a>
77  * @author <a HREF="mailto:dlr@collab.net">Daniel Rall</a>
78  * @version $Id: TurbineUser.java,v 1.1 2004/11/12 10:25:43 epugh Exp $
79  */

80 public class TurbineUser
81     extends org.apache.fulcrum.security.impl.db.entity.BaseTurbineUser
82     implements User
83 {
84     /** The date on which the user last accessed the application. */
85     private Date JavaDoc lastAccessDate = null;
86
87     /** This is data that will survive a servlet engine restart. */
88     private Hashtable JavaDoc permStorage = null;
89
90     /** This is data that will not survive a servlet engine restart. */
91     private Hashtable JavaDoc tempStorage = null;
92
93     /**
94      * Constructor.
95      * Create a new User and set the createDate.
96      */

97     public TurbineUser()
98     {
99         setCreateDate(new Date JavaDoc());
100         tempStorage = new Hashtable JavaDoc(10);
101         setHasLoggedIn(Boolean.FALSE);
102     }
103
104     /**
105      * Gets the access counter for a user from perm storage.
106      *
107      * @return The access counter for the user.
108      */

109     public int getAccessCounter()
110     {
111         try
112         {
113             return ((Integer JavaDoc) getPerm(User.ACCESS_COUNTER)).intValue();
114         }
115         catch (Exception JavaDoc e)
116         {
117             return 0;
118         }
119     }
120
121     /**
122      * Gets the access counter for a user during a session.
123      *
124      * @return The access counter for the user for the session.
125      */

126     public int getAccessCounterForSession()
127     {
128         try
129         {
130             return ((Integer JavaDoc) getTemp(User.SESSION_ACCESS_COUNTER)).intValue();
131         }
132         catch (Exception JavaDoc e)
133         {
134             return 0;
135         }
136     }
137
138     /**
139      * Increments the permanent hit counter for the user.
140      */

141     public void incrementAccessCounter()
142     {
143         setAccessCounter(getAccessCounter() + 1);
144     }
145
146     /**
147      * Increments the session hit counter for the user.
148      */

149     public void incrementAccessCounterForSession()
150     {
151         setAccessCounterForSession(getAccessCounterForSession() + 1);
152     }
153
154     /**
155      * Sets the access counter for a user, saved in perm storage.
156      *
157      * @param cnt The new count.
158      */

159     public void setAccessCounter(int cnt)
160     {
161         setPerm(User.ACCESS_COUNTER, new Integer JavaDoc(cnt));
162     }
163
164     /**
165      * Sets the session access counter for a user, saved in temp
166      * storage.
167      *
168      * @param cnt The new count.
169      */

170     public void setAccessCounterForSession(int cnt)
171     {
172         setTemp(User.SESSION_ACCESS_COUNTER, new Integer JavaDoc(cnt));
173     }
174
175     /**
176      * This method reports whether or not the user has been confirmed
177      * in the system by checking the User.CONFIRM_VALUE
178      * column in the users record to see if it is equal to
179      * User.CONFIRM_DATA.
180      *
181      * @return True if the user has been confirmed.
182      */

183     public boolean isConfirmed()
184     {
185         String JavaDoc value = getConfirmed();
186         return (value != null && value.equals(User.CONFIRM_DATA));
187     }
188
189     /**
190      * The user is considered logged in if they have not timed out.
191      *
192      * @return Whether the user has logged in.
193      */

194     public boolean hasLoggedIn()
195     {
196         Boolean JavaDoc loggedIn = getHasLoggedIn();
197         return (loggedIn != null && loggedIn.booleanValue());
198     }
199
200     /**
201      * This sets whether or not someone has logged in. hasLoggedIn()
202      * returns this value.
203      *
204      * @param value Whether someone has logged in or not.
205      */

206     public void setHasLoggedIn(Boolean JavaDoc value)
207     {
208         setTemp(User.HAS_LOGGED_IN, value);
209     }
210
211     /**
212      * This gets whether or not someone has logged in. hasLoggedIn()
213      * returns this value as a boolean. This is private because you
214      * should use hasLoggedIn() instead.
215      *
216      * @return True if someone has logged in.
217      */

218     private Boolean JavaDoc getHasLoggedIn()
219     {
220         return (Boolean JavaDoc) getTemp(User.HAS_LOGGED_IN);
221     }
222
223     /**
224      * Gets the last access date for this User. This is the last time
225      * that the user object was referenced.
226      *
227      * @return A Java Date with the last access date for the user.
228      */

229     public java.util.Date JavaDoc getLastAccessDate()
230     {
231         if (lastAccessDate == null)
232         {
233             setLastAccessDate();
234         }
235         return lastAccessDate;
236     }
237
238     /**
239      * Sets the last access date for this User. This is the last time
240      * that the user object was referenced.
241      */

242     public void setLastAccessDate()
243     {
244         lastAccessDate = new java.util.Date JavaDoc();
245     }
246
247     /**
248      * Get an object from permanent storage.
249      *
250      * @param name The object's name.
251      * @return An Object with the given name.
252      */

253     public Object JavaDoc getPerm(String JavaDoc name)
254     {
255         return permStorage.get(name);
256     }
257
258     /**
259      * Get an object from permanent storage; return default if value
260      * is null.
261      *
262      * @param name The object's name.
263      * @param def A default value to return.
264      * @return An Object with the given name.
265      */

266     public Object JavaDoc getPerm(String JavaDoc name, Object JavaDoc def)
267     {
268         try
269         {
270             Object JavaDoc val = permStorage.get(name);
271             return (val == null ? def : val);
272         }
273         catch (Exception JavaDoc e)
274         {
275             return def;
276         }
277     }
278
279     /**
280      * This should only be used in the case where we want to save the
281      * data to the database.
282      *
283      * @return A Hashtable.
284      */

285     public Hashtable JavaDoc getPermStorage()
286     {
287         if (this.permStorage == null)
288         {
289             this.permStorage = new Hashtable JavaDoc();
290         }
291
292         return this.permStorage;
293     }
294
295     /**
296      * Put an object into permanent storage. If the value is null,
297      * it will convert that to a "" because the underlying storage
298      * mechanism within TurbineUser is currently a Hashtable and
299      * null is not a valid value.
300      *
301      * @param name The object's name.
302      * @param value The object.
303      */

304     public void setPerm(String JavaDoc name, Object JavaDoc value)
305     {
306         safeAddToHashtable(getPermStorage(), name, value);
307     }
308
309     /**
310      * This should only be used in the case where we want to save the
311      * data to the database.
312      *
313      * @param stuff A Hashtable.
314      */

315     public void setPermStorage(Hashtable JavaDoc stuff)
316     {
317         this.permStorage = stuff;
318     }
319
320     /**
321      * Get an object from temporary storage.
322      *
323      * @param name The object's name.
324      * @return An Object with the given name.
325      */

326     public Object JavaDoc getTemp(String JavaDoc name)
327     {
328         return tempStorage.get(name);
329     }
330
331     /**
332      * Get an object from temporary storage; return default if value
333      * is null.
334      *
335      * @param name The object's name.
336      * @param def A default value to return.
337      * @return An Object with the given name.
338      */

339     public Object JavaDoc getTemp(String JavaDoc name, Object JavaDoc def)
340     {
341         Object JavaDoc val;
342         try
343         {
344             val = tempStorage.get(name);
345             if (val == null)
346             {
347                 val = def;
348             }
349         }
350         catch (Exception JavaDoc e)
351         {
352             val = def;
353         }
354         return val;
355     }
356
357     /**
358      * This should only be used in the case where we want to save the
359      * data to the database.
360      *
361      * @return A Hashtable.
362      */

363     public Hashtable JavaDoc getTempStorage()
364     {
365         if (this.tempStorage == null)
366         {
367             this.tempStorage = new Hashtable JavaDoc();
368         }
369         return this.tempStorage;
370     }
371
372     /**
373      * Remove an object from temporary storage and return the object.
374      *
375      * @param name The name of the object to remove.
376      * @return An Object.
377      */

378     public Object JavaDoc removeTemp(String JavaDoc name)
379     {
380         return tempStorage.remove(name);
381     }
382
383     /**
384      * Put an object into temporary storage. If the value is null,
385      * it will convert that to a "" because the underlying storage
386      * mechanism within TurbineUser is currently a Hashtable and
387      * null is not a valid value.
388      *
389      * @param name The object's name.
390      * @param value The object.
391      */

392     public void setTemp(String JavaDoc name, Object JavaDoc value)
393     {
394         safeAddToHashtable(tempStorage, name, value);
395     }
396
397     /**
398      * This should only be used in the case where we want to save the
399      * data to the database.
400      *
401      * @param storage A Hashtable.
402      */

403     public void setTempStorage(Hashtable JavaDoc storage)
404     {
405         this.tempStorage = storage;
406     }
407
408     /**
409      * Updates the last login date in the database.
410      *
411      * @exception Exception, a generic exception.
412      */

413     public void updateLastLogin()
414         throws Exception JavaDoc
415     {
416         setPerm( User.LAST_LOGIN, new java.util.Date JavaDoc() );
417     }
418
419     /**
420      * Implement this method if you wish to be notified when the User
421      * has been Bound to the session.
422      *
423      * @param event Inidication of value/session binding.
424      */

425     public void valueBound(SessionBindingEvent event)
426     {
427         // Currently we have no need for this method.
428
}
429
430     /**
431      * Implement this method if you wish to be notified when the User
432      * has been Unbound from the session.
433      *
434      * @param event Inidication of value/session unbinding.
435      */

436     public void valueUnbound(SessionBindingEvent event)
437     {
438         try
439         {
440             if (hasLoggedIn())
441             {
442                 TurbineSecurity.saveUser(this);
443             }
444         }
445         catch (Exception JavaDoc e)
446         {
447             //Log.error("TurbineUser.valueUnbobund(): " + e.getMessage(), e);
448

449             // To prevent messages being lost in case the logging system
450
// goes away before sessions get unbound on servlet container
451
// shutdown, print the stcktrace to the container's console.
452
ByteArrayOutputStream JavaDoc ostr = new ByteArrayOutputStream JavaDoc();
453             e.printStackTrace(new PrintWriter JavaDoc(ostr,true));
454             String JavaDoc stackTrace = ostr.toString();
455             System.out.println(stackTrace);
456         }
457     }
458
459     /**
460      * Set the value of GroupName
461      */

462     public void setName(String JavaDoc v)
463     {
464         setUserName(v);
465     }
466
467     /**
468      * Set the value of GroupName
469      */

470     public String JavaDoc getName()
471     {
472         return getUserName();
473     }
474
475     /**
476      * Nice method for adding data to a Hashtable in such a way
477      * as to not get NPE's. The point being that if the
478      * value is null, Hashtable.put() will throw an exception.
479      * That blows in the case of this class cause you may want to
480      * essentially treat put("Not Null", null ) == put("Not Null", "")
481      * We will still throw a NPE if the key is null cause that should
482      * never happen.
483      *
484      * !! Maybe a hashtable isn't the best option here and we
485      * should use a Map. This was taken from ObjectUtils.
486      */

487     public static final void safeAddToHashtable(Hashtable JavaDoc hash, Object JavaDoc key, Object JavaDoc value)
488         throws NullPointerException JavaDoc
489     {
490         if (value == null)
491         {
492             hash.put ( key, "" );
493         }
494         else
495         {
496            hash.put ( key, value );
497         }
498     }
499 }
500
Popular Tags