Index: kernel-impl/src/main/java/org/sakaiproject/event/impl/UsageSessionServiceSqlMySql.java =================================================================== --- kernel-impl/src/main/java/org/sakaiproject/event/impl/UsageSessionServiceSqlMySql.java (revision 77546) +++ kernel-impl/src/main/java/org/sakaiproject/event/impl/UsageSessionServiceSqlMySql.java (working copy) @@ -27,11 +27,4 @@ public class UsageSessionServiceSqlMySql extends UsageSessionServiceSqlDefault { - /** - * @return the SQL statement which retrieves the most recent active sakai session for a given userid - */ - @Override - public String getMostRecentOpenSakaiSessionForUserSql() { - return "select " + USAGE_SESSION_COLUMNS + " from SAKAI_SESSION where SESSION_ACTIVE=1 and SESSION_USER=? ORDER BY SESSION_START DESC LIMIT 1"; - } } Index: kernel-impl/src/main/java/org/sakaiproject/event/impl/UsageSessionServiceSqlOracle.java =================================================================== --- kernel-impl/src/main/java/org/sakaiproject/event/impl/UsageSessionServiceSqlOracle.java (revision 77546) +++ kernel-impl/src/main/java/org/sakaiproject/event/impl/UsageSessionServiceSqlOracle.java (working copy) @@ -27,11 +27,4 @@ public class UsageSessionServiceSqlOracle extends UsageSessionServiceSqlDefault { - /** - * @return the SQL statement which retrieves the most recent active sakai session for a given userid - */ - @Override - public String getMostRecentOpenSakaiSessionForUserSql() { - return "select " + USAGE_SESSION_COLUMNS + " from SAKAI_SESSION where SESSION_ACTIVE=1 and SESSION_USER=? and rownum=1 ORDER BY SESSION_START DESC"; - } } Index: kernel-impl/src/main/java/org/sakaiproject/event/impl/UsageSessionServiceAdaptor.java =================================================================== --- kernel-impl/src/main/java/org/sakaiproject/event/impl/UsageSessionServiceAdaptor.java (revision 77546) +++ kernel-impl/src/main/java/org/sakaiproject/event/impl/UsageSessionServiceAdaptor.java (working copy) @@ -25,8 +25,8 @@ import java.net.UnknownHostException; import java.sql.ResultSet; import java.sql.SQLException; +import java.util.ArrayList; import java.util.Enumeration; -import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; @@ -72,8 +72,11 @@ /** Storage manager for this service. */ protected Storage m_storage = null; - /** A Cache of recently refreshed users. This is to prevent frequent authentications refreshing user data */ + /** A Cache of recently refreshed users. This is to prevent frequent authentications refreshing user data */ protected Cache m_recentUserRefresh = null; + + /** A Cache of users that have active sessions */ + protected Cache activeUsers = null; /************************************************************************************************************************************************* * Abstractions, etc. @@ -214,7 +217,8 @@ m_storage.open(); m_recentUserRefresh = memoryService().newCache("org.sakaiproject.event.api.UsageSessionService.recentUserRefresh"); - + activeUsers = memoryService().newCache("org.sakaiproject.event.api.UsageSessionService.activeUsers"); + M_log.info("init()"); } catch (Exception t) @@ -507,6 +511,10 @@ // post the login event eventTrackingService().post(eventTrackingService().newEvent(event != null ? event : EVENT_LOGIN, null, true)); + // add to cache + activeUsers.put(uid, Boolean.TRUE); + M_log.debug("Added to active user cache: " + uid); + return true; } @@ -541,6 +549,12 @@ // generate a logout event (this session) eventTrackingService().post(eventTrackingService().newEvent(EVENT_LOGOUT, null, true), session); } + + // remove from cache + activeUsers.remove(session.getUserId()); + M_log.debug("Removed from active user cache: " + session.getUserId()); + + } /************************************************************************************************************************************************* @@ -1025,33 +1039,12 @@ * @param userId userId to check * @return true if active, false if not */ - @SuppressWarnings("unchecked") public boolean isUserActive(String userId) { - String statement = usageSessionServiceSql.getCountOpenSakaiSessionsForUserSql(); - if (M_log.isDebugEnabled()) { - M_log.debug("will get count of sessions with SQL=" + statement); + if(activeUsers.containsKey(userId)){ + return true; } - - List count = sqlService().dbRead(statement, new Object[] { userId }, new SqlReader() { - public Object readSqlResultRecord(ResultSet result) { - try { - //count column is a long - return (Long) result.getObject(1); - } catch (SQLException e) { - M_log.error("isUserActive: failed: " + e); - return null; - } - } - }); - - for(Long l: count) { - // if > 0, then we have an active sakai session - if(l > 0){ - return true; - } - } - return false; + return false; } /** @@ -1059,96 +1052,17 @@ * @param userIds userIds to check * @return List of userIds that have active Sakai sessions */ - @SuppressWarnings("unchecked") public List getActiveUsers(List userIds) { - String statement = usageSessionServiceSql.getUsersWithOpenSakaiSessionsSql(userIds); - if (M_log.isDebugEnabled()) { - M_log.debug("will get users with active sessions with SQL=" + statement); - } - - - List results = sqlService().dbRead(statement, null, new SqlReader() { - public Object readSqlResultRecord(ResultSet result) { - try { - return result.getString(1); - } - catch (SQLException e) { - M_log.error("getActiveUsers: failed: " + e); - return null; - } + List activeUsers = new ArrayList(); + for(String userId: userIds) { + if(isUserActive(userId)){ + activeUsers.add(userId); } - }); - - return results; - } - - - /** - * Get the most recent Sakai session that is active, for a given user - * @param userId userId to check - * @return most recent UsageSession or null if none - */ - @SuppressWarnings("unchecked") - public UsageSession getActiveUserSession(String userId) { - - String statement = usageSessionServiceSql.getMostRecentOpenSakaiSessionForUserSql(); - if (M_log.isDebugEnabled()) { - M_log.debug("will get session with SQL=" + statement); } - UsageSession session = null; - - List sessions = sqlService().dbRead(statement, new Object[] { userId }, new SqlReader() { - public Object readSqlResultRecord(ResultSet result) { - try { - return new BaseUsageSession(UsageSessionServiceAdaptor.this,result); - } - catch (SQLException e) { - M_log.error("getActiveUserSession: failed: " + e); - return null; - } - } - }); - - if (!sessions.isEmpty()) { - session = (UsageSession) sessions.get(0); - } - return session; + return activeUsers; } - - /** - * Get the most recent active UsageSessions for the given users. - * @param userIds userIds to check - * @return Map of userId and UsageSession. The returned map will not a record for the userId if there is no active session. - */ - @SuppressWarnings("unchecked") - public Map getActiveUserSessions(List userIds) { - - String statement = usageSessionServiceSql.getMostRecentOpenSakaiSessionForMultipleUsersSql(userIds); - if (M_log.isDebugEnabled()) { - M_log.debug("will get sessions with SQL=" + statement); - } - - Map map = new HashMap(); - - List sessions = sqlService().dbRead(statement, null, new SqlReader() { - public Object readSqlResultRecord(ResultSet result) { - try { - return new BaseUsageSession(UsageSessionServiceAdaptor.this,result); - } - catch (SQLException e) { - M_log.error("getActiveUserSessions: failed: " + e); - return null; - } - } - }); - - //create the map - for (UsageSession session : sessions) { - map.put(session.getUserId(), session); - } - return map; - } } + Index: kernel-impl/src/main/java/org/sakaiproject/event/impl/UsageSessionServiceSql.java =================================================================== --- kernel-impl/src/main/java/org/sakaiproject/event/impl/UsageSessionServiceSql.java (revision 77546) +++ kernel-impl/src/main/java/org/sakaiproject/event/impl/UsageSessionServiceSql.java (working copy) @@ -63,24 +63,4 @@ */ String getOpenSessionsOnInvalidServersSql(List validServerIds); - /** - * @return the SQL statement which retrieves the most recent active sakai session for a given userid - */ - String getMostRecentOpenSakaiSessionForUserSql(); - - /** - * @return the SQL statement which simply counts any active sakai sessions for a given userid - */ - String getCountOpenSakaiSessionsForUserSql(); - - /** - * @return the SQL statement which gets the list of users with active session, from the supplied list - */ - String getUsersWithOpenSakaiSessionsSql(List userIds); - - /** - * @return the SQL statement which retrieves the most recent active sakai session for each of the given userIds - */ - String getMostRecentOpenSakaiSessionForMultipleUsersSql(List userIds); - } Index: kernel-impl/src/main/java/org/sakaiproject/event/impl/UsageSessionServiceSqlDefault.java =================================================================== --- kernel-impl/src/main/java/org/sakaiproject/event/impl/UsageSessionServiceSqlDefault.java (revision 77546) +++ kernel-impl/src/main/java/org/sakaiproject/event/impl/UsageSessionServiceSqlDefault.java (working copy) @@ -96,55 +96,4 @@ return "update SAKAI_SESSION set SESSION_SERVER = ? where SESSION_ID = ?"; } - /** - * @return the SQL statement which retrieves the most recent active sakai session for a given userid - */ - public String getMostRecentOpenSakaiSessionForUserSql() { - return "select TOP 1 " + USAGE_SESSION_COLUMNS + " from SAKAI_SESSION where SESSION_ACTIVE=1 and SESSION_USER=? ORDER BY SESSION_START DESC"; - } - - /** - * @return the SQL statement which simply counts any active sakai sessions for a given userid - */ - public String getCountOpenSakaiSessionsForUserSql() { - return "select count(SESSION_ID) from SAKAI_SESSION where SESSION_ACTIVE=1 and SESSION_USER=?"; - } - - /** - * @return the SQL statement which gets the list of users with active session, from the supplied list - */ - public String getUsersWithOpenSakaiSessionsSql(List userIds) { - - StringBuilder sql = new StringBuilder("select SESSION_USER from SAKAI_SESSION where SESSION_ACTIVE=1 and SESSION_USER in ("); - for (int i = 0; i < userIds.size(); i++) - { - String userId = userIds.get(i); - if (i > 0) sql.append(","); - sql.append("'").append(userId).append("'"); - } - sql.append(")"); - sql.append(" GROUP BY SESSION_USER"); - - return sql.toString(); - } - - /** - * @return the SQL statement which retrieves the most recent active sakai session for the given userIds - */ - public String getMostRecentOpenSakaiSessionForMultipleUsersSql(List userIds) { - - StringBuilder sql = new StringBuilder("select " + MOST_RECENT_USAGE_SESSION_COLUMNS + " from SAKAI_SESSION where SESSION_ACTIVE=1 and SESSION_USER in ("); - for (int i = 0; i < userIds.size(); i++) - { - String userId = userIds.get(i); - if (i > 0) sql.append(","); - sql.append("'").append(userId).append("'"); - } - sql.append(")"); - sql.append(" GROUP BY SESSION_USER"); - - return sql.toString(); - } - - } Index: api/src/main/java/org/sakaiproject/event/api/UsageSessionService.java =================================================================== --- api/src/main/java/org/sakaiproject/event/api/UsageSessionService.java (revision 77546) +++ api/src/main/java/org/sakaiproject/event/api/UsageSessionService.java (working copy) @@ -226,18 +226,4 @@ */ public List getActiveUsers(List userIds); - /** - * Get the most recent Sakai session that is active, for a given user - * @param userId userId to check - * @return most recent UsageSession or null if none - */ - public UsageSession getActiveUserSession(String userId); - - - /** - * Get the most recent active UsageSessions for the given users. - * @param userIds userIds to check - * @return Map of userId and UsageSession. The returned map will not contain a record for the userId if there is no active session. - */ - public Map getActiveUserSessions(List userIds); }