summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--dozentenmodul/src/main/java/org/openslx/dozmod/thrift/UserCache.java71
1 files changed, 71 insertions, 0 deletions
diff --git a/dozentenmodul/src/main/java/org/openslx/dozmod/thrift/UserCache.java b/dozentenmodul/src/main/java/org/openslx/dozmod/thrift/UserCache.java
new file mode 100644
index 00000000..50c02f08
--- /dev/null
+++ b/dozentenmodul/src/main/java/org/openslx/dozmod/thrift/UserCache.java
@@ -0,0 +1,71 @@
+package org.openslx.dozmod.thrift;
+
+import java.util.List;
+
+import org.apache.log4j.Logger;
+import org.openslx.bwlp.thrift.iface.UserInfo;
+import org.openslx.thrifthelper.ThriftManager;
+import org.openslx.util.GenericDataCache;
+
+public class UserCache {
+
+ private static final Logger LOGGER = Logger.getLogger(UserCache.class);
+
+ /**
+ * How long should the list be cached?
+ */
+ private static final int CACHE_TIME_MS = 5 * 60 * 1000;
+
+ private static final GenericDataCache<List<UserInfo>> cache = new GenericDataCache<List<UserInfo>>(
+ CACHE_TIME_MS) {
+
+ @Override
+ protected List<UserInfo> update() throws Exception {
+ List<UserInfo> result = null;
+ int pageSize = ThriftManager.getSatClient().getPageSize();
+ for (int i = 0;; ++i) {
+ List<UserInfo> page = ThriftManager.getSatClient()
+ .getUserList(Session.getSatelliteToken(), i);
+ if (result == null) {
+ result = page;
+ } else {
+ result.addAll(page);
+ }
+ if (page.size() < pageSize)
+ break;
+ }
+ return result;
+ }
+ };
+
+ private UserCache() {
+ // No instancing
+ }
+
+ /**
+ * Get all known users
+ *
+ * @return list of users
+ */
+ public static List<UserInfo> getAll() {
+ return cache.get();
+ }
+
+ /**
+ * Find the user with the given id
+ *
+ * @param user id
+ * @return matching user, or <code>null</code> if not found
+ */
+ public static UserInfo find(String userId) {
+ List<UserInfo> list = cache.get();
+ if (list == null)
+ return null;
+ for (UserInfo user : list) {
+ if (user.userId.equals(userId))
+ return user;
+ }
+ return null;
+ }
+
+}