1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/**
* $RCSfile$
* $Revision$
* $Date$
*
* Copyright (C) 2004 Jive Software. All rights reserved.
*
* This software is published under the terms of the GNU Public License (GPL),
* a copy of which is included in this distribution.
*/
package org.jivesoftware.messenger.user;
import org.jivesoftware.messenger.roster.Roster;
import org.jivesoftware.messenger.XMPPServer;
import org.jivesoftware.util.Log;
import org.jivesoftware.util.Cacheable;
import org.jivesoftware.util.CacheSizes;
import org.jivesoftware.database.DbConnectionManager;
import java.util.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* Encapsulates information about a user. New users are created using
* {@link UserManager#createUser(String, String, String, String)}. All user
* properties are loaded on demand and are read from the <tt>jiveUserProp</tt>
* database table. The currently-installed {@link UserProvider} is used for
* setting all other user data and some operations may not be supported
* depending on the capabilities of the {@link UserProvider}.
*
* @author Matt Tucker
*/
public class User implements Cacheable {
private static final String LOAD_PROPERTIES =
"SELECT name, propValue FROM jiveUserProp WHERE username=?";
private static final String DELETE_PROPERTY =
"DELETE FROM jiveUserProp WHERE username=? AND name=?";
private static final String UPDATE_PROPERTY =
"UPDATE jiveUserProp SET propValue=? WHERE name=? AND username=?";
private static final String INSERT_PROPERTY =
"INSERT INTO jiveUserProp (username, name, propValue) VALUES (?, ?, ?)";
private String username;
private String name;
private String email;
private Date creationDate;
private Date modificationDate;
private Map<String,String> properties = null;
/**
* Constructs a new user. All arguments can be <tt>null</tt> except the username.
* Typically, User objects should not be constructed by end-users of the API.
* Instead, user objects should be retrieved using {@link UserManager#getUser(String)}.
*
* @param username the username.
* @param name the name.
* @param email the email address.
* @param creationDate the date the user was created.
* @param modificationDate the date the user was last modified.
*/
public User(String username, String name, String email, Date creationDate,
Date modificationDate)
{
if (username == null) {
throw new NullPointerException("Username cannot be null");
}
this.username = username;
this.name = name;
this.email = email;
this.creationDate = creationDate;
this.modificationDate = modificationDate;
}
/**
* Returns this user's username.
*
* @return the username..
*/
public String getUsername() {
return username;
}
/**
* Sets a new password for this user.
*
* @param password the new password for the user.
*/
public void setPassword(String password) {
try {
UserManager.getUserProvider().setPassword(username, password);
}
catch (UserNotFoundException unfe) {
Log.error(unfe);
}
}
public String getName() {
return name == null ? "" : name;
}
public void setName(String name) {
try {
UserManager.getUserProvider().setName(username, name);
this.name = name;
}
catch (UserNotFoundException unfe) {
Log.error(unfe);
}
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
try {
UserManager.getUserProvider().setEmail(username, email);
this.email = email;
}
catch (UserNotFoundException unfe) {
Log.error(unfe);
}
}
public Date getCreationDate() {
return creationDate;
}
public void setCreationDate(Date creationDate) {
try {
UserManager.getUserProvider().setCreationDate(username, creationDate);
this.creationDate = creationDate;
}
catch (UserNotFoundException unfe) {
Log.error(unfe);
}
}
public Date getModificationDate() {
return modificationDate;
}
public void setModificationDate(Date modificationDate) {
try {
UserManager.getUserProvider().setCreationDate(username, modificationDate);
this.modificationDate = modificationDate;
}
catch (UserNotFoundException unfe) {
Log.error(unfe);
}
}
/**
* Returns an extended property of the user. Each user can have an arbitrary number of extended
* properties. This lets particular skins or filters provide enhanced functionality that is not
* part of the base interface.
*
* @param name the name of the property to get.
* @return the value of the property
*/
public String getProperty(String name) {
if (properties == null) {
loadPropertiesFromDb();
}
return properties.get(name);
}
/**
* Sets an extended property of the user. Each user can have an arbitrary number of extended
* properties. This lets particular skins or filters provide enhanced functionality that is not
* part of the base interface. Property names and values must be valid Strings. If <tt>null</tt>
* or an empty length String is used, a NullPointerException will be thrown.
*
* @param name the name of the property to set.
* @param value the new value for the property.
*/
public void setProperty(String name, String value) {
if (properties == null) {
loadPropertiesFromDb();
}
// Make sure the property name and value aren't null.
if (name == null || value == null || "".equals(name) || "".equals(value)) {
throw new NullPointerException("Cannot set property with empty or null value.");
}
// See if we need to update a property value or insert a new one.
if (properties.containsKey(name)) {
// Only update the value in the database if the property value
// has changed.
if (!(value.equals(properties.get(name)))) {
properties.put(name, value);
updatePropertyInDb(name, value);
}
}
else {
properties.put(name, value);
insertPropertyIntoDb(name, value);
}
}
/**
* Deletes an extended property. If the property specified by <code>name</code> does not exist,
* this method will do nothing.
*
* @param name the name of the property to delete.
*/
public void deleteProperty(String name) {
if (properties == null) {
loadPropertiesFromDb();
}
properties.remove(name);
deletePropertyFromDb(name);
}
/**
* Returns a Collection of all the names of the extended user properties.
*
* @return a Collection of all the property names.
*/
public Collection<String> getPropertyNames() {
if (properties == null) {
loadPropertiesFromDb();
}
return Collections.unmodifiableCollection(properties.keySet());
}
/**
* Returns the user's roster. A roster is a list of users that the user wishes to know
* if they are online. Rosters are similar to buddy groups in popular IM clients.
*
* @return the user's roster.
*/
public Roster getRoster() {
try {
return XMPPServer.getInstance().getRosterManager().getRoster(username);
}
catch (UserNotFoundException unfe) {
Log.error(unfe);
return null;
}
}
public int getCachedSize() {
// Approximate the size of the object in bytes by calculating the size
// of each field.
int size = 0;
size += CacheSizes.sizeOfObject(); // overhead of object
size += CacheSizes.sizeOfLong(); // id
size += CacheSizes.sizeOfString(username); // username
size += CacheSizes.sizeOfString(email); // email
size += CacheSizes.sizeOfDate() * 2; // creationDate and modificationDate
size += CacheSizes.sizeOfMap(properties); // properties
return size;
}
public String toString() {
return username;
}
public int hashCode() {
return username.hashCode();
}
public boolean equals(Object object) {
if (this == object) {
return true;
}
if (object != null && object instanceof User) {
return username.equals(((User)object).getUsername());
}
else {
return false;
}
}
private synchronized void loadPropertiesFromDb() {
properties = new Hashtable<String,String>();
Connection con = null;
PreparedStatement pstmt = null;
try {
con = DbConnectionManager.getConnection();
pstmt = con.prepareStatement(LOAD_PROPERTIES);
pstmt.setString(1, username);
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
properties.put(rs.getString(1), rs.getString(2));
}
}
catch (SQLException e) {
Log.error(e);
}
finally {
try { if (pstmt != null) { pstmt.close(); } }
catch (Exception e) { Log.error(e); }
try { if (con != null) { con.close(); } }
catch (Exception e) { Log.error(e); }
}
}
private void insertPropertyIntoDb(String name, String value) {
Connection con = null;
PreparedStatement pstmt = null;
try {
con = DbConnectionManager.getConnection();
pstmt = con.prepareStatement(INSERT_PROPERTY);
pstmt.setString(1, username);
pstmt.setString(2, name);
pstmt.setString(3, value);
pstmt.executeUpdate();
}
catch (SQLException e) {
Log.error(e);
}
finally {
try { if (pstmt != null) { pstmt.close(); } }
catch (Exception e) { Log.error(e); }
try { if (con != null) { con.close(); } }
catch (Exception e) { Log.error(e); }
}
}
private void updatePropertyInDb(String name, String value) {
Connection con = null;
PreparedStatement pstmt = null;
try {
con = DbConnectionManager.getConnection();
pstmt = con.prepareStatement(UPDATE_PROPERTY);
pstmt.setString(1, value);
pstmt.setString(2, name);
pstmt.setString(3, username);
pstmt.executeUpdate();
}
catch (SQLException e) {
Log.error(e);
}
finally {
try { if (pstmt != null) { pstmt.close(); } }
catch (Exception e) { Log.error(e); }
try { if (con != null) { con.close(); } }
catch (Exception e) { Log.error(e); }
}
}
private void deletePropertyFromDb(String name) {
Connection con = null;
PreparedStatement pstmt = null;
try {
con = DbConnectionManager.getConnection();
pstmt = con.prepareStatement(DELETE_PROPERTY);
pstmt.setString(1, username);
pstmt.setString(2, name);
pstmt.executeUpdate();
}
catch (SQLException e) {
Log.error(e);
}
finally {
try { if (pstmt != null) { pstmt.close(); } }
catch (Exception e) { Log.error(e); }
try { if (con != null) { con.close(); } }
catch (Exception e) { Log.error(e); }
}
}
}