Commit 9c425dd1 authored by Derek DeMoro's avatar Derek DeMoro Committed by derek

Refactoring work.


git-svn-id: http://svn.igniterealtime.org/svn/repos/messenger/trunk@584 b35dd754-fafc-0310-a699-88a17e54d16e
parent e928da83
...@@ -160,6 +160,26 @@ public class DbConnectionManager { ...@@ -160,6 +160,26 @@ public class DbConnectionManager {
} }
/**
* Close the prepared statement and connection
* @param pstmt the <code>PreparedStatement</code> to close.
* @param con the <code>Connection</code> to close.
*/
public static void close(PreparedStatement pstmt, Connection con) {
try {
if (pstmt != null) pstmt.close();
}
catch (Exception e) {
Log.error(e);
}
try {
if (con != null) con.close();
}
catch (Exception e) {
Log.error(e);
}
}
/** /**
* Creates a scroll insensitive Statement if the JDBC driver supports it, or a normal * Creates a scroll insensitive Statement if the JDBC driver supports it, or a normal
* Statement otherwise. * Statement otherwise.
......
...@@ -11,7 +11,6 @@ ...@@ -11,7 +11,6 @@
package org.jivesoftware.messenger.user; package org.jivesoftware.messenger.user;
import org.jivesoftware.messenger.Presence;
import org.jivesoftware.messenger.auth.UnauthorizedException; import org.jivesoftware.messenger.auth.UnauthorizedException;
import org.jivesoftware.util.Cacheable; import org.jivesoftware.util.Cacheable;
import org.xmpp.packet.Presence; import org.xmpp.packet.Presence;
......
...@@ -11,14 +11,12 @@ ...@@ -11,14 +11,12 @@
package org.jivesoftware.messenger.user.spi; package org.jivesoftware.messenger.user.spi;
import org.jivesoftware.util.CacheSizes; import java.util.List;
import org.jivesoftware.messenger.XMPPAddress;
import org.jivesoftware.messenger.user.BasicRosterItem; import org.jivesoftware.messenger.user.BasicRosterItem;
import org.jivesoftware.messenger.user.CachedRosterItem; import org.jivesoftware.messenger.user.CachedRosterItem;
import org.jivesoftware.messenger.user.RosterItem; import org.jivesoftware.messenger.user.RosterItem;
import org.jivesoftware.util.CacheSizes; import org.jivesoftware.util.CacheSizes;
import org.xmpp.packet.JID;
import java.util.List;
/** /**
* In-memory implementation of a roster item. The ID of the roster item is it's roster ID. * In-memory implementation of a roster item. The ID of the roster item is it's roster ID.
...@@ -28,7 +26,7 @@ import java.util.List; ...@@ -28,7 +26,7 @@ import java.util.List;
public class CachedRosterItemImpl extends BasicRosterItem implements CachedRosterItem { public class CachedRosterItemImpl extends BasicRosterItem implements CachedRosterItem {
public CachedRosterItemImpl(long id, public CachedRosterItemImpl(long id,
XMPPAddress jid, JID jid,
SubType subStatus, SubType subStatus,
AskType askStatus, AskType askStatus,
RecvType recvStatus, RecvType recvStatus,
...@@ -38,7 +36,7 @@ public class CachedRosterItemImpl extends BasicRosterItem implements CachedRoste ...@@ -38,7 +36,7 @@ public class CachedRosterItemImpl extends BasicRosterItem implements CachedRoste
this.rosterID = id; this.rosterID = id;
} }
public CachedRosterItemImpl(long id, XMPPAddress jid) { public CachedRosterItemImpl(long id, JID jid) {
this(id, this(id,
jid, jid,
RosterItem.SUB_NONE, RosterItem.SUB_NONE,
...@@ -48,7 +46,7 @@ public class CachedRosterItemImpl extends BasicRosterItem implements CachedRoste ...@@ -48,7 +46,7 @@ public class CachedRosterItemImpl extends BasicRosterItem implements CachedRoste
null); null);
} }
public CachedRosterItemImpl(long id, XMPPAddress jid, String nickname, List groups) { public CachedRosterItemImpl(long id, JID jid, String nickname, List groups) {
this(id, this(id,
jid, jid,
RosterItem.SUB_NONE, RosterItem.SUB_NONE,
...@@ -90,7 +88,7 @@ public class CachedRosterItemImpl extends BasicRosterItem implements CachedRoste ...@@ -90,7 +88,7 @@ public class CachedRosterItemImpl extends BasicRosterItem implements CachedRoste
} }
public int getCachedSize() { public int getCachedSize() {
int size = jid.getCachedSize(); int size = jid.toBareJID().length();
size += CacheSizes.sizeOfString(nickname); size += CacheSizes.sizeOfString(nickname);
size += CacheSizes.sizeOfList(groups); size += CacheSizes.sizeOfList(groups);
size += CacheSizes.sizeOfInt(); // subStatus size += CacheSizes.sizeOfInt(); // subStatus
......
...@@ -11,22 +11,25 @@ ...@@ -11,22 +11,25 @@
package org.jivesoftware.messenger.user.spi; package org.jivesoftware.messenger.user.spi;
import org.jivesoftware.database.DbConnectionManager;
import org.jivesoftware.database.SequenceManager;
import org.jivesoftware.util.JiveConstants;
import org.jivesoftware.util.LocaleUtils;
import org.jivesoftware.util.Log;
import org.jivesoftware.messenger.XMPPAddress;
import org.jivesoftware.messenger.user.*;
import java.sql.Connection; import java.sql.Connection;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator; import java.util.Iterator;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.jivesoftware.database.DbConnectionManager;
import org.jivesoftware.database.SequenceManager;
import org.jivesoftware.messenger.user.CachedRosterItem;
import org.jivesoftware.messenger.user.RosterItem;
import org.jivesoftware.messenger.user.RosterItemProvider;
import org.jivesoftware.messenger.user.UserAlreadyExistsException;
import org.jivesoftware.messenger.user.UserNotFoundException;
import org.jivesoftware.util.JiveConstants;
import org.jivesoftware.util.LocaleUtils;
import org.jivesoftware.util.Log;
import org.xmpp.packet.JID;
/** /**
* <p>Implements the roster item provider against the jiveRoster table * <p>Implements the roster item provider against the jiveRoster table
...@@ -52,7 +55,7 @@ public class DbRosterItemProvider implements RosterItemProvider { ...@@ -52,7 +55,7 @@ public class DbRosterItemProvider implements RosterItemProvider {
pstmt = con.prepareStatement(CREATE_ROSTER_ITEM); pstmt = con.prepareStatement(CREATE_ROSTER_ITEM);
pstmt.setString(1, username); pstmt.setString(1, username);
pstmt.setLong(2, rosterID); pstmt.setLong(2, rosterID);
pstmt.setString(3, item.getJid().toBareString()); pstmt.setString(3, item.getJid().toBareJID());
pstmt.setInt(4, item.getSubStatus().getValue()); pstmt.setInt(4, item.getSubStatus().getValue());
pstmt.setInt(5, item.getAskStatus().getValue()); pstmt.setInt(5, item.getAskStatus().getValue());
pstmt.setInt(6, item.getRecvStatus().getValue()); pstmt.setInt(6, item.getRecvStatus().getValue());
...@@ -71,13 +74,10 @@ public class DbRosterItemProvider implements RosterItemProvider { ...@@ -71,13 +74,10 @@ public class DbRosterItemProvider implements RosterItemProvider {
insertGroups(rosterID, item.getGroups().iterator(), pstmt, con); insertGroups(rosterID, item.getGroups().iterator(), pstmt, con);
} }
catch (SQLException e) { catch (SQLException e) {
throw new UserAlreadyExistsException(item.getJid().toStringPrep()); throw new UserAlreadyExistsException(item.getJid().toBareJID());
} }
finally { finally {
try { if (pstmt != null) { pstmt.close(); } } DbConnectionManager.close(pstmt, con);
catch (Exception e) { Log.error(e); }
try { if (con != null) { con.close(); } }
catch (Exception e) { Log.error(e); }
} }
return cachedItem; return cachedItem;
} }
...@@ -115,10 +115,7 @@ public class DbRosterItemProvider implements RosterItemProvider { ...@@ -115,10 +115,7 @@ public class DbRosterItemProvider implements RosterItemProvider {
Log.error(LocaleUtils.getLocalizedString("admin.error"), e); Log.error(LocaleUtils.getLocalizedString("admin.error"), e);
} }
finally { finally {
try { if (pstmt != null) { pstmt.close(); } } DbConnectionManager.close(pstmt, con);
catch (Exception e) { Log.error(e); }
try { if (con != null) { con.close(); } }
catch (Exception e) { Log.error(e); }
} }
} }
...@@ -183,15 +180,27 @@ public class DbRosterItemProvider implements RosterItemProvider { ...@@ -183,15 +180,27 @@ public class DbRosterItemProvider implements RosterItemProvider {
Log.error(LocaleUtils.getLocalizedString("admin.error"), e); Log.error(LocaleUtils.getLocalizedString("admin.error"), e);
} }
finally { finally {
try { if (pstmt != null) { pstmt.close(); } } try {
catch (Exception e) { Log.error(e); } if (pstmt != null) {
try { if (con != null) { con.close(); } } pstmt.close();
catch (Exception e) { Log.error(e); } }
}
catch (Exception e) {
Log.error(e);
}
try {
if (con != null) {
con.close();
}
}
catch (Exception e) {
Log.error(e);
}
} }
} }
private static final String LOAD_USERNAMES = private static final String LOAD_USERNAMES =
"SELECT DISTINCT username from jiveRoster WHERE jid=?"; "SELECT DISTINCT username from jiveRoster WHERE jid=?";
public Iterator<String> getUsernames(String jid) { public Iterator<String> getUsernames(String jid) {
List<String> answer = new ArrayList<String>(); List<String> answer = new ArrayList<String>();
...@@ -210,16 +219,28 @@ public class DbRosterItemProvider implements RosterItemProvider { ...@@ -210,16 +219,28 @@ public class DbRosterItemProvider implements RosterItemProvider {
Log.error(LocaleUtils.getLocalizedString("admin.error"), e); Log.error(LocaleUtils.getLocalizedString("admin.error"), e);
} }
finally { finally {
try { if (pstmt != null) { pstmt.close(); } } try {
catch (Exception e) { Log.error(e); } if (pstmt != null) {
try { if (con != null) { con.close(); } } pstmt.close();
catch (Exception e) { Log.error(e); } }
}
catch (Exception e) {
Log.error(e);
}
try {
if (con != null) {
con.close();
}
}
catch (Exception e) {
Log.error(e);
}
} }
return answer.iterator(); return answer.iterator();
} }
private static final String COUNT_ROSTER_ITEMS = private static final String COUNT_ROSTER_ITEMS =
"SELECT COUNT(rosterID) FROM jiveRoster WHERE username=?"; "SELECT COUNT(rosterID) FROM jiveRoster WHERE username=?";
public int getItemCount(String username) { public int getItemCount(String username) {
int count = 0; int count = 0;
...@@ -238,18 +259,30 @@ public class DbRosterItemProvider implements RosterItemProvider { ...@@ -238,18 +259,30 @@ public class DbRosterItemProvider implements RosterItemProvider {
Log.error(LocaleUtils.getLocalizedString("admin.error"), e); Log.error(LocaleUtils.getLocalizedString("admin.error"), e);
} }
finally { finally {
try { if (pstmt != null) { pstmt.close(); } } try {
catch (Exception e) { Log.error(e); } if (pstmt != null) {
try { if (con != null) { con.close(); } } pstmt.close();
catch (Exception e) { Log.error(e); } }
}
catch (Exception e) {
Log.error(e);
}
try {
if (con != null) {
con.close();
}
}
catch (Exception e) {
Log.error(e);
}
} }
return count; return count;
} }
private static final String LOAD_ROSTER = private static final String LOAD_ROSTER =
"SELECT jid, rosterID, sub, ask, recv, nick FROM jiveRoster WHERE username=?"; "SELECT jid, rosterID, sub, ask, recv, nick FROM jiveRoster WHERE username=?";
private static final String LOAD_ROSTER_ITEM_GROUPS = private static final String LOAD_ROSTER_ITEM_GROUPS =
"SELECT groupName FROM jiveRosterGroups WHERE rosterID=? ORDER BY rank"; "SELECT groupName FROM jiveRosterGroups WHERE rosterID=? ORDER BY rank";
public Iterator getItems(String username) { public Iterator getItems(String username) {
LinkedList itemList = new LinkedList(); LinkedList itemList = new LinkedList();
...@@ -262,7 +295,7 @@ public class DbRosterItemProvider implements RosterItemProvider { ...@@ -262,7 +295,7 @@ public class DbRosterItemProvider implements RosterItemProvider {
ResultSet rs = pstmt.executeQuery(); ResultSet rs = pstmt.executeQuery();
while (rs.next()) { while (rs.next()) {
CachedRosterItem item = new CachedRosterItemImpl(rs.getLong(2), CachedRosterItem item = new CachedRosterItemImpl(rs.getLong(2),
XMPPAddress.parseJID(rs.getString(1)), new JID(rs.getString(1)),
RosterItem.SubType.getTypeFromInt(rs.getInt(3)), RosterItem.SubType.getTypeFromInt(rs.getInt(3)),
RosterItem.AskType.getTypeFromInt(rs.getInt(4)), RosterItem.AskType.getTypeFromInt(rs.getInt(4)),
RosterItem.RecvType.getTypeFromInt(rs.getInt(5)), RosterItem.RecvType.getTypeFromInt(rs.getInt(5)),
...@@ -280,10 +313,22 @@ public class DbRosterItemProvider implements RosterItemProvider { ...@@ -280,10 +313,22 @@ public class DbRosterItemProvider implements RosterItemProvider {
itemList.add(item); itemList.add(item);
} }
finally { finally {
try {if (gs != null) { gs.close(); } } try {
catch (Exception e) { Log.error(e); } if (gs != null) {
try {if (gstmt != null) { gstmt.close(); } } gs.close();
catch (Exception e) { Log.error(e); } }
}
catch (Exception e) {
Log.error(e);
}
try {
if (gstmt != null) {
gstmt.close();
}
}
catch (Exception e) {
Log.error(e);
}
} }
} }
} }
...@@ -291,10 +336,22 @@ public class DbRosterItemProvider implements RosterItemProvider { ...@@ -291,10 +336,22 @@ public class DbRosterItemProvider implements RosterItemProvider {
Log.error(LocaleUtils.getLocalizedString("admin.error"), e); Log.error(LocaleUtils.getLocalizedString("admin.error"), e);
} }
finally { finally {
try { if (pstmt != null) { pstmt.close(); } } try {
catch (Exception e) { Log.error(e); } if (pstmt != null) {
try { if (con != null) { con.close(); } } pstmt.close();
catch (Exception e) { Log.error(e); } }
}
catch (Exception e) {
Log.error(e);
}
try {
if (con != null) {
con.close();
}
}
catch (Exception e) {
Log.error(e);
}
} }
return itemList.iterator(); return itemList.iterator();
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment