-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding the View Logged in Users functionality to core
- Loading branch information
1 parent
51e3485
commit 89473ac
Showing
1 changed file
with
69 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package org.openmrs.util; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.TreeMap; | ||
|
||
import javax.servlet.http.HttpSession; | ||
|
||
public class CurrentUsers { | ||
|
||
|
||
/** | ||
* Initialize the current users list. | ||
* | ||
* @param servletContext | ||
*/ | ||
|
||
public static Map<String,String> init(ServletContext servletContext){ | ||
Map<String,String> currentUserMap = Collections.synchronizedMap(new TreeMap<String,String>()); | ||
servletContext.setAtrribute(WebConstants.CURRENT_USERS,currentUserMap); | ||
return currentUserMap; | ||
} | ||
|
||
/** | ||
* Get the current list of map of users stored in the session | ||
* | ||
* @param httpSession the current session | ||
* @return map of users logged in | ||
*/ | ||
private static Map<String,String> getCurrentUsers(HttpSession httpSession){ | ||
|
||
Map<String,String> currentUsers = (Map <String,String>) httpSession.getServletContext().getAttribute(WebConstants.CURRENT_USERS); | ||
if(currentUsers == null) { | ||
|
||
currentUsers = init(httpSession.getServletContext()); | ||
} | ||
|
||
return currentUsers; | ||
} | ||
|
||
/** | ||
* Get sorted user names list. | ||
* | ||
* @param httpSession | ||
* @return sorted user names | ||
*/ | ||
|
||
public static List<String> getCurrentUsernames(HttpSession httpSession){ | ||
|
||
Map<String,String> currentUsers = getCurrentUsers(httpSession); | ||
|
||
List<String> userNames = new ArrayList<String>(); | ||
synchronized(currentUsers){ | ||
|
||
for(String value:currentUsers.values()) { | ||
|
||
userNames.add(value); | ||
} | ||
|
||
} | ||
|
||
Collections.sort(userNames); | ||
return userNames; | ||
} | ||
|
||
|
||
} |