📄 user.java
字号:
public void setModificationDate(Date modificationDate) {
if (UserManager.getUserProvider().isReadOnly()) {
throw new UnsupportedOperationException("User provider is read-only.");
}
try {
Date originalModificationDate = this.modificationDate;
UserManager.getUserProvider().setCreationDate(username, modificationDate);
this.modificationDate = modificationDate;
// Fire event.
Map<String, Object> params = new HashMap<String, Object>();
params.put("type", "nameModified");
params.put("originalValue", originalModificationDate);
UserEventDispatcher.dispatchEvent(this, UserEventDispatcher.EventType.user_modified,
params);
}
catch (UserNotFoundException unfe) {
Log.error(unfe);
}
}
/**
* Returns all extended properties of the user. Users have an arbitrary
* number of extended properties. The returned collection can be modified
* to add new properties or remove existing ones.
*
* @return the extended properties.
*/
public Map<String, String> getProperties() {
synchronized (this) {
if (properties == null) {
properties = new ConcurrentHashMap<String, String>();
loadProperties();
}
}
// Return a wrapper that will intercept add and remove commands.
return new PropertiesMap();
}
/**
* 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(name); // name
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;
}
}
/**
* Map implementation that updates the database when properties are modified.
*/
private class PropertiesMap extends AbstractMap {
public Object put(Object key, Object value) {
Map<String, Object> eventParams = new HashMap<String, Object>();
Object answer;
String keyString = (String) key;
synchronized (keyString.intern()) {
if (properties.containsKey(keyString)) {
String originalValue = properties.get(keyString);
answer = properties.put(keyString, (String) value);
updateProperty(keyString, (String) value);
// Configure event.
eventParams.put("type", "propertyModified");
eventParams.put("propertyKey", key);
eventParams.put("originalValue", originalValue);
} else {
answer = properties.put(keyString, (String) value);
insertProperty(keyString, (String) value);
// Configure event.
eventParams.put("type", "propertyAdded");
eventParams.put("propertyKey", key);
}
}
// Fire event.
UserEventDispatcher.dispatchEvent(User.this,
UserEventDispatcher.EventType.user_modified, eventParams);
return answer;
}
public Set<Entry> entrySet() {
return new PropertiesEntrySet();
}
}
/**
* Set implementation that updates the database when properties are deleted.
*/
private class PropertiesEntrySet extends AbstractSet {
public int size() {
return properties.entrySet().size();
}
public Iterator iterator() {
return new Iterator() {
Iterator iter = properties.entrySet().iterator();
Map.Entry current = null;
public boolean hasNext() {
return iter.hasNext();
}
public Object next() {
current = (Map.Entry) iter.next();
return current;
}
public void remove() {
if (current == null) {
throw new IllegalStateException();
}
String key = (String) current.getKey();
deleteProperty(key);
iter.remove();
// Fire event.
Map<String, Object> params = new HashMap<String, Object>();
params.put("type", "propertyDeleted");
params.put("propertyKey", key);
UserEventDispatcher.dispatchEvent(User.this,
UserEventDispatcher.EventType.user_modified, params);
}
};
}
}
private void loadProperties() {
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));
}
rs.close();
}
catch (SQLException sqle) {
Log.error(sqle);
}
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 insertProperty(String propName, String propValue) {
Connection con = null;
PreparedStatement pstmt = null;
try {
con = DbConnectionManager.getConnection();
pstmt = con.prepareStatement(INSERT_PROPERTY);
pstmt.setString(1, username);
pstmt.setString(2, propName);
pstmt.setString(3, propValue);
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 updateProperty(String propName, String propValue) {
Connection con = null;
PreparedStatement pstmt = null;
try {
con = DbConnectionManager.getConnection();
pstmt = con.prepareStatement(UPDATE_PROPERTY);
pstmt.setString(1, propValue);
pstmt.setString(2, propName);
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 deleteProperty(String propName) {
Connection con = null;
PreparedStatement pstmt = null;
try {
con = DbConnectionManager.getConnection();
pstmt = con.prepareStatement(DELETE_PROPERTY);
pstmt.setString(1, username);
pstmt.setString(2, propName);
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);
}
}
}
public void writeExternal(ObjectOutput out) throws IOException {
ExternalizableUtil.getInstance().writeSafeUTF(out, username);
ExternalizableUtil.getInstance().writeSafeUTF(out, getName());
ExternalizableUtil.getInstance().writeBoolean(out, email != null);
if (email != null) {
ExternalizableUtil.getInstance().writeSafeUTF(out, email);
}
ExternalizableUtil.getInstance().writeLong(out, creationDate.getTime());
ExternalizableUtil.getInstance().writeLong(out, modificationDate.getTime());
}
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
username = ExternalizableUtil.getInstance().readSafeUTF(in);
name = ExternalizableUtil.getInstance().readSafeUTF(in);
if (ExternalizableUtil.getInstance().readBoolean(in)) {
email = ExternalizableUtil.getInstance().readSafeUTF(in);
}
creationDate = new Date(ExternalizableUtil.getInstance().readLong(in));
modificationDate = new Date(ExternalizableUtil.getInstance().readLong(in));
}
/*
* (non-Javadoc)
* @see org.jivesoftware.util.resultsetmanager.Result#getUID()
*/
public String getUID() {
return username;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -