summaryrefslogtreecommitdiffstats
path: root/dozentenmodul/src/main/java
diff options
context:
space:
mode:
authorSimon Rettberg2015-07-03 19:13:12 +0200
committerSimon Rettberg2015-07-03 19:13:12 +0200
commit808c9b4abf714697011895e093c20938520abb18 (patch)
tree140b2991bd5f733f9ef2e7caa21fb26f6f10a18f /dozentenmodul/src/main/java
parent[client] bwIDM Authentication implemented, yet to be finalized. (diff)
downloadtutor-module-808c9b4abf714697011895e093c20938520abb18.tar.gz
tutor-module-808c9b4abf714697011895e093c20938520abb18.tar.xz
tutor-module-808c9b4abf714697011895e093c20938520abb18.zip
[client] Add classes for caching of data fetched via thrift
(could be used for other things too, code is fairly generic)
Diffstat (limited to 'dozentenmodul/src/main/java')
-rw-r--r--dozentenmodul/src/main/java/org/openslx/bwlp/dozmod/thrift/ACache.java94
-rw-r--r--dozentenmodul/src/main/java/org/openslx/bwlp/dozmod/thrift/OrganizationList.java69
2 files changed, 163 insertions, 0 deletions
diff --git a/dozentenmodul/src/main/java/org/openslx/bwlp/dozmod/thrift/ACache.java b/dozentenmodul/src/main/java/org/openslx/bwlp/dozmod/thrift/ACache.java
new file mode 100644
index 00000000..7c7d927e
--- /dev/null
+++ b/dozentenmodul/src/main/java/org/openslx/bwlp/dozmod/thrift/ACache.java
@@ -0,0 +1,94 @@
+package org.openslx.bwlp.dozmod.thrift;
+
+import java.util.concurrent.atomic.AtomicReference;
+
+import org.apache.log4j.Logger;
+
+public abstract class ACache<T> {
+
+ private static final Logger LOGGER = Logger.getLogger(ACache.class);
+
+ /**
+ * How long the cached data is valid after fetching
+ */
+ private final int validMs;
+
+ /**
+ * Deadline when the cache goes invalid
+ */
+ private long validUntil = 0;
+
+ /**
+ * The data being held
+ */
+ private final AtomicReference<T> item = new AtomicReference<>();
+
+ public ACache(int validMs) {
+ this.validMs = validMs;
+ }
+
+ /**
+ * Get the cached object, but refresh the cache first if
+ * the cached instance is too old.
+ *
+ * @return
+ */
+ public T get() {
+ return get(CacheMode.DEFAULT);
+ }
+
+ /**
+ * Get the cached object, using the given cache access strategy.
+ * ALWAYS_CACHED: Never refresh the cache, except if it has never been fetched before
+ * DEFAULT: Only fetch from remote if the cached value is too old
+ * NEVER_CACHED: Always fetch from remote. If it fails, return null
+ *
+ * @param mode Cache access strategy as described above
+ * @return T
+ */
+ public T get(CacheMode mode) {
+ switch (mode) {
+ case ALWAYS_CACHED:
+ if (validUntil == 0)
+ ensureUpToDate(true);
+ break;
+ case DEFAULT:
+ ensureUpToDate(false);
+ break;
+ case NEVER_CACHED:
+ if (!ensureUpToDate(true))
+ return null;
+ break;
+ }
+ return item.get();
+ }
+
+ private synchronized boolean ensureUpToDate(boolean force) {
+ final long now = System.currentTimeMillis();
+ if (!force && now < validUntil)
+ return true;
+ T fetched;
+ try {
+ fetched = update();
+ if (fetched == null)
+ return false;
+ } catch (Exception e) {
+ LOGGER.warn("Could not fetch fresh data", e);
+ return false;
+ }
+ item.set(fetched);
+ validUntil = now + validMs;
+ return true;
+ }
+
+ public abstract T update() throws Exception;
+
+ //
+
+ public static enum CacheMode {
+ ALWAYS_CACHED,
+ DEFAULT,
+ NEVER_CACHED
+ }
+
+}
diff --git a/dozentenmodul/src/main/java/org/openslx/bwlp/dozmod/thrift/OrganizationList.java b/dozentenmodul/src/main/java/org/openslx/bwlp/dozmod/thrift/OrganizationList.java
new file mode 100644
index 00000000..d13da1b3
--- /dev/null
+++ b/dozentenmodul/src/main/java/org/openslx/bwlp/dozmod/thrift/OrganizationList.java
@@ -0,0 +1,69 @@
+package org.openslx.bwlp.dozmod.thrift;
+
+import java.util.List;
+
+import org.openslx.bwlp.thrift.iface.Organization;
+import org.openslx.bwlp.thrift.iface.UserInfo;
+import org.openslx.thrifthelper.ThriftManager;
+
+public class OrganizationList extends ACache<List<Organization>> {
+
+ /**
+ * How long should the list be cached?
+ */
+ private static final int CACHE_TIME_MS = 20 * 60 * 1000;
+
+ private static final OrganizationList instance = new OrganizationList();
+
+ private OrganizationList() {
+ super(CACHE_TIME_MS);
+ }
+
+ @Override
+ public List<Organization> update() throws Exception {
+ try {
+ return ThriftManager.getMasterClient().getOrganizations();
+ } catch (Exception e) {
+ // Swallow exception, then try satellite
+ }
+ return ThriftManager.getSatClient().getAllOrganizations();
+ // TODO: Sort list by display name?
+ }
+
+ /**
+ * Get all known organizations
+ *
+ * @return list of organizations
+ */
+ public static List<Organization> getAll() {
+ return instance.get();
+ }
+
+ /**
+ * Find the organization the given user belongs to
+ *
+ * @param user
+ * @return matching organization, or <code>null</code> if not found
+ */
+ public static Organization find(UserInfo user) {
+ return find(user.organizationId);
+ }
+
+ /**
+ * Find the organization with the given id
+ *
+ * @param organization id
+ * @return matching organization, or <code>null</code> if not found
+ */
+ public static Organization find(String organizationId) {
+ List<Organization> list = instance.get();
+ if (list == null)
+ return null;
+ for (Organization org : list) {
+ if (org.organizationId.equals(organizationId))
+ return org;
+ }
+ return null;
+ }
+
+}