Commit b5f4139e authored by csh's avatar csh

OF-193 Last logouts are not recorded when server is shut down

git-svn-id: http://svn.igniterealtime.org/svn/repos/openfire/trunk@13984 b35dd754-fafc-0310-a699-88a17e54d16e
parent 3993e7f8
...@@ -25,22 +25,14 @@ import java.sql.PreparedStatement; ...@@ -25,22 +25,14 @@ import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Types; import java.sql.Types;
import java.util.ArrayList; import java.util.*;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.Lock;
import org.dom4j.Document; import org.dom4j.Document;
import org.dom4j.DocumentException; import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper; import org.dom4j.DocumentHelper;
import org.jivesoftware.database.DbConnectionManager; import org.jivesoftware.database.DbConnectionManager;
import org.jivesoftware.openfire.PacketDeliverer; import org.jivesoftware.openfire.*;
import org.jivesoftware.openfire.PresenceManager;
import org.jivesoftware.openfire.RoutingTable;
import org.jivesoftware.openfire.SessionManager;
import org.jivesoftware.openfire.XMPPServer;
import org.jivesoftware.openfire.auth.UnauthorizedException; import org.jivesoftware.openfire.auth.UnauthorizedException;
import org.jivesoftware.openfire.component.InternalComponentManager; import org.jivesoftware.openfire.component.InternalComponentManager;
import org.jivesoftware.openfire.container.BasicModule; import org.jivesoftware.openfire.container.BasicModule;
...@@ -71,7 +63,7 @@ import org.xmpp.packet.Presence; ...@@ -71,7 +63,7 @@ import org.xmpp.packet.Presence;
* *
* @author Iain Shigeoka * @author Iain Shigeoka
*/ */
public class PresenceManagerImpl extends BasicModule implements PresenceManager, UserEventListener { public class PresenceManagerImpl extends BasicModule implements PresenceManager, UserEventListener, XMPPServerListener {
private static final Logger Log = LoggerFactory.getLogger(PresenceManagerImpl.class); private static final Logger Log = LoggerFactory.getLogger(PresenceManagerImpl.class);
...@@ -273,31 +265,32 @@ public class PresenceManagerImpl extends BasicModule implements PresenceManager, ...@@ -273,31 +265,32 @@ public class PresenceManagerImpl extends BasicModule implements PresenceManager,
} }
lastActivityCache.put(username, offlinePresenceDate.getTime()); lastActivityCache.put(username, offlinePresenceDate.getTime());
// delete existing offline presence (if any) writeToDatabase(username, offlinePresence, offlinePresenceDate);
deleteOfflinePresenceFromDB(username); }
}
// Insert data into the database.
Connection con = null; private void writeToDatabase(String username, String offlinePresence, Date offlinePresenceDate) {
PreparedStatement pstmt = null; // delete existing offline presence (if any)
try { deleteOfflinePresenceFromDB(username);
con = DbConnectionManager.getConnection();
pstmt = con.prepareStatement(INSERT_OFFLINE_PRESENCE); // Insert data into the database.
pstmt.setString(1, username); Connection con = null;
if (offlinePresence != null) { PreparedStatement pstmt = null;
DbConnectionManager.setLargeTextField(pstmt, 2, offlinePresence); try {
} con = DbConnectionManager.getConnection();
else { pstmt = con.prepareStatement(INSERT_OFFLINE_PRESENCE);
pstmt.setNull(2, Types.VARCHAR); pstmt.setString(1, username);
} if (offlinePresence != null) {
pstmt.setString(3, StringUtils.dateToMillis(offlinePresenceDate)); DbConnectionManager.setLargeTextField(pstmt, 2, offlinePresence);
pstmt.execute(); } else {
} pstmt.setNull(2, Types.VARCHAR);
catch (SQLException sqle) {
Log.error("Error storing offline presence of user: " + username, sqle);
}
finally {
DbConnectionManager.closeConnection(pstmt, con);
} }
pstmt.setString(3, StringUtils.dateToMillis(offlinePresenceDate));
pstmt.execute();
} catch (SQLException sqle) {
Log.error("Error storing offline presence of user: " + username, sqle);
} finally {
DbConnectionManager.closeConnection(pstmt, con);
} }
} }
...@@ -582,4 +575,21 @@ public class PresenceManagerImpl extends BasicModule implements PresenceManager, ...@@ -582,4 +575,21 @@ public class PresenceManagerImpl extends BasicModule implements PresenceManager,
lock.unlock(); lock.unlock();
} }
} }
@Override
public void serverStarted() {
}
@Override
public void serverStopping() {
for (ClientSession session : XMPPServer.getInstance().getSessionManager().getSessions()) {
if (!session.isAnonymousUser()) {
try {
writeToDatabase(session.getUsername(), null, new Date());
} catch (UserNotFoundException e) {
Log.error(e.getMessage(), e);
}
}
}
}
} }
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