KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snipsnap > user > DefaultUserManager


1 /* Compent
2  * This file is part of "SnipSnap Wiki/Weblog".
3  *
4  * Copyright (c) 2002 Stephan J. Schmidt, Matthias L. Jugel
5  * All Rights Reserved.
6  *
7  * Please visit http://snipsnap.org/ for updates and contact.
8  *
9  * --LICENSE NOTICE--
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  * --LICENSE NOTICE--
24  */

25 package org.snipsnap.user;
26
27 import org.snipsnap.container.Components;
28 import org.snipsnap.snip.storage.UserStorage;
29 import org.snipsnap.util.ApplicationAwareMap;
30 import org.snipsnap.util.ApplicationAwareIntegerMap;
31 import org.snipsnap.jdbc.IntHolder;
32
33 import java.sql.Timestamp JavaDoc;
34 import java.util.*;
35
36 /**
37  * User manager handles all register, creation and authentication of users.
38  * Default user manager uses a UserStorage component for storage
39  *
40  * @author Stephan J. Schmidt
41  * @version $Id: DefaultUserManager.java 1606 2004-05-17 10:56:18Z leo $
42  */

43
44 public class DefaultUserManager implements UserManager {
45   // Number of users
46
private ApplicationAwareIntegerMap countCache;
47   private ApplicationAwareMap missingCache;
48
49   // List of users that should be stored later
50
private List delayed;
51   // Storage from where to load users
52
private UserStorage storage;
53   public static final int MILLISECS_PER_MINUTE = 60 * 1000;
54
55   public DefaultUserManager(UserStorage storage) {
56     this.storage = storage;
57
58     delayed = new LinkedList();
59
60     countCache = new ApplicationAwareIntegerMap(HashMap.class);
61     missingCache = new ApplicationAwareMap(HashMap.class, ArrayList.class);
62
63     Timer timer = new Timer();
64     timer.schedule(new TimerTask() {
65       public void run() {
66         synchronized (delayed) {
67           ListIterator iterator = delayed.listIterator();
68           while (iterator.hasNext()) {
69             User user = (User) iterator.next();
70             systemStore(user);
71             iterator.remove();
72           }
73         }
74       }
75       // execute after 5 minutes and then
76
// every 5 minutes
77
}, 5 * MILLISECS_PER_MINUTE, 5 * MILLISECS_PER_MINUTE);
78   }
79
80   /**
81    * Get all users in the system
82    *
83    * @return
84    */

85   public List getAll() {
86     return storage.storageAll();
87   }
88
89   /**
90    * Get the number of users in the system
91    * @return
92    */

93   public int getUserCount() {
94     IntHolder holder = countCache.getIntHolder();
95     int count = holder.getValue();
96     if (-1 == count) {
97       count = storage.storageUserCount();
98       holder.setValue(count);
99     }
100     return count;
101   }
102
103   /**
104    * Create a new user
105    *
106    * @param login Login for the user
107    * @param passwd Password for the user
108    * @param email Email for the user
109    * @return
110    */

111   public User create(String JavaDoc login, String JavaDoc passwd, String JavaDoc email) {
112     User user = storage.storageCreate(login, passwd, email);
113     IntHolder holder = countCache.getIntHolder();
114     if(holder.getValue() == -1) {
115       holder.setValue(storage.storageUserCount());
116     }
117     holder.inc();
118     // Remove from missing list
119
return user;
120   }
121
122   /**
123    * Store user
124    *
125    * @param user User to store
126    */

127   public void store(User user) {
128     user.setMTime(new Timestamp JavaDoc(new Date().getTime()));
129     storage.storageStore(user);
130     return;
131   }
132
133   /**
134    * Store user delayed. Some unimportant
135    * changes to users are done every login etc.
136    * This does not to be persistet every time,
137    * only after some time the user is stored
138    *
139    * @param user User to store
140    */

141   public void delayedStore(User user) {
142     synchronized (delayed) {
143       if (!delayed.contains(user)) {
144         delayed.add(user);
145       }
146     }
147   }
148
149   /**
150    * Stores the user but does not change
151    * user data (like last modified time)
152    *
153    * @param user User to store
154    */

155   public void systemStore(User user) {
156     storage.storageStore(user);
157     return;
158   }
159
160   /**
161    * Remove user from system
162    *
163    * @param user User to remove
164    */

165   public void remove(User user) {
166     // Add to missing list
167
storage.storageRemove(user);
168     IntHolder holder = countCache.getIntHolder();
169     holder.dec();
170     return;
171   }
172
173   /**
174    * Test if an user in the system exists
175    *
176    * @param login Login of the user to test
177    * @return
178    */

179   public boolean exists(String JavaDoc login) {
180     // Cache this with an interceptor
181
// Read from missing list
182
return (null != storage.storageLoad(login));
183   }
184
185   /**
186    * Load user from backend
187    *
188    * @param login Login of the user to load
189    * @return
190    */

191   public User load(String JavaDoc login) {
192     return storage.storageLoad(login);
193   }
194 }
195
Popular Tags