diff options
author | Jonathan Bauer | 2015-08-19 13:40:53 +0200 |
---|---|---|
committer | Jonathan Bauer | 2015-08-19 13:40:53 +0200 |
commit | 5d94f0ba69a68a3fb733fc0617bc6a414dae8a52 (patch) | |
tree | a065cfe4e6d4ac593a8143e3c55e3624a02e0e54 /dozentenmodul/src/main/java/org/openslx/dozmod/thrift/cache | |
parent | [client] delete version through popupmenu with confirmation and refresh vers... (diff) | |
download | tutor-module-5d94f0ba69a68a3fb733fc0617bc6a414dae8a52.tar.gz tutor-module-5d94f0ba69a68a3fb733fc0617bc6a414dae8a52.tar.xz tutor-module-5d94f0ba69a68a3fb733fc0617bc6a414dae8a52.zip |
[client] move thrift cache to its own package
Diffstat (limited to 'dozentenmodul/src/main/java/org/openslx/dozmod/thrift/cache')
5 files changed, 315 insertions, 0 deletions
diff --git a/dozentenmodul/src/main/java/org/openslx/dozmod/thrift/cache/ImageCache.java b/dozentenmodul/src/main/java/org/openslx/dozmod/thrift/cache/ImageCache.java new file mode 100644 index 00000000..8e6f8566 --- /dev/null +++ b/dozentenmodul/src/main/java/org/openslx/dozmod/thrift/cache/ImageCache.java @@ -0,0 +1,41 @@ +package org.openslx.dozmod.thrift.cache; + +import java.util.List; + +import org.apache.thrift.TException; +import org.openslx.bwlp.thrift.iface.ImageSummaryRead; +import org.openslx.bwlp.thrift.iface.TAuthorizationException; +import org.openslx.bwlp.thrift.iface.TInternalServerError; +import org.openslx.dozmod.thrift.Session; +import org.openslx.thrifthelper.ThriftManager; +import org.openslx.util.GenericDataCache; +import org.openslx.util.GenericDataCache.CacheMode; + +public class ImageCache { + + private static final int CACHE_TIME_LIST_MS = 2 * 60 * 1000; + + private static final GenericDataCache<List<ImageSummaryRead>> listCache = new GenericDataCache<List<ImageSummaryRead>>(CACHE_TIME_LIST_MS) { + @Override + protected List<ImageSummaryRead> update() throws TAuthorizationException, TInternalServerError, TException { + List<ImageSummaryRead> result = null; + int pageSize = Session.getSatelliteConfig().pageSize; + for (int i = 0;; ++i) { + List<ImageSummaryRead> page = ThriftManager.getSatClient().getImageList(Session.getSatelliteToken(), null, i); + if (result == null) { + result = page; + } else { + result.addAll(page); + } + if (page.size() < pageSize) + break; + } + return result; + } + }; + + public static List<ImageSummaryRead> get(boolean forceRefresh) { + return listCache.get(forceRefresh ? CacheMode.NEVER_CACHED : CacheMode.DEFAULT); + } + +} diff --git a/dozentenmodul/src/main/java/org/openslx/dozmod/thrift/cache/LectureCache.java b/dozentenmodul/src/main/java/org/openslx/dozmod/thrift/cache/LectureCache.java new file mode 100644 index 00000000..6b4466c8 --- /dev/null +++ b/dozentenmodul/src/main/java/org/openslx/dozmod/thrift/cache/LectureCache.java @@ -0,0 +1,41 @@ +package org.openslx.dozmod.thrift.cache; + +import java.util.List; + +import org.apache.thrift.TException; +import org.openslx.bwlp.thrift.iface.LectureSummary; +import org.openslx.bwlp.thrift.iface.TAuthorizationException; +import org.openslx.bwlp.thrift.iface.TInternalServerError; +import org.openslx.dozmod.thrift.Session; +import org.openslx.thrifthelper.ThriftManager; +import org.openslx.util.GenericDataCache; +import org.openslx.util.GenericDataCache.CacheMode; + +public class LectureCache { + + private static final int CACHE_TIME_LIST_MS = 2 * 60 * 1000; + + private static final GenericDataCache<List<LectureSummary>> listCache = new GenericDataCache<List<LectureSummary>>(CACHE_TIME_LIST_MS) { + @Override + protected List<LectureSummary> update() throws TAuthorizationException, TInternalServerError, TException { + List<LectureSummary> result = null; + int pageSize = Session.getSatelliteConfig().pageSize; + for (int i = 0;; ++i) { + List<LectureSummary> page = ThriftManager.getSatClient().getLectureList(Session.getSatelliteToken(), i); + if (result == null) { + result = page; + } else { + result.addAll(page); + } + if (page.size() < pageSize) + break; + } + return result; + } + }; + + public static List<LectureSummary> get(boolean forceRefresh) { + return listCache.get(forceRefresh ? CacheMode.NEVER_CACHED : CacheMode.DEFAULT); + } + +} diff --git a/dozentenmodul/src/main/java/org/openslx/dozmod/thrift/cache/MetaDataCache.java b/dozentenmodul/src/main/java/org/openslx/dozmod/thrift/cache/MetaDataCache.java new file mode 100644 index 00000000..340d95e8 --- /dev/null +++ b/dozentenmodul/src/main/java/org/openslx/dozmod/thrift/cache/MetaDataCache.java @@ -0,0 +1,87 @@ +package org.openslx.dozmod.thrift.cache; + +import java.util.List; + +import org.apache.log4j.Logger; +import org.apache.thrift.TException; +import org.openslx.bwlp.thrift.iface.OperatingSystem; +import org.openslx.bwlp.thrift.iface.Virtualizer; +import org.openslx.thrifthelper.ThriftManager; +import org.openslx.util.GenericDataCache; + +public class MetaDataCache { + + private static final Logger LOGGER = Logger.getLogger(MetaDataCache.class); + + /** + * How long to cache data. + */ + private static final int CACHE_TIME_MS = 60 * 60 * 1000; + + private static final GenericDataCache<List<OperatingSystem>> osCache = new GenericDataCache<List<OperatingSystem>>( + CACHE_TIME_MS) { + @Override + protected List<OperatingSystem> update() throws TException { + try { + return ThriftManager.getSatClient().getOperatingSystems(); + } catch (Exception e) { + LOGGER.warn("Could not get OS list from satellite, trying master for backup...", e); + } + return ThriftManager.getMasterClient().getOperatingSystems(); + } + }; + + private static final GenericDataCache<List<Virtualizer>> virtualizerCache = new GenericDataCache<List<Virtualizer>>( + CACHE_TIME_MS) { + @Override + protected List<Virtualizer> update() throws TException { + try { + return ThriftManager.getSatClient().getVirtualizers(); + } catch (TException e) { + LOGGER.warn("Could not get virtualizer list from satellite, trying master for backup...", e); + } + return ThriftManager.getMasterClient().getVirtualizers(); + } + }; + + /** + * Get all known/valid operating systems an image can be marked as. + * + * @return + */ + public static List<OperatingSystem> getOperatingSystems() { + return osCache.get(); + } + + public static OperatingSystem getOsById(int id) { + List<OperatingSystem> list = getOperatingSystems(); + if (list == null) + return null; + for (OperatingSystem os : list) { + if (os.getOsId() == id) + return os; + } + return null; + } + + /** + * Get all supported virtualizers an image can be declared to be run as. + * + * @return + */ + public static List<Virtualizer> getVirtualizers() { + return virtualizerCache.get(); + } + + public static Virtualizer getVirtualizerById(String virtId) { + List<Virtualizer> list = getVirtualizers(); + if (list == null) + return null; + for (Virtualizer virt : list) { + if (virt.getVirtId().equals(virtId)) + return virt; + } + return null; + } + +}
\ No newline at end of file diff --git a/dozentenmodul/src/main/java/org/openslx/dozmod/thrift/cache/OrganizationCache.java b/dozentenmodul/src/main/java/org/openslx/dozmod/thrift/cache/OrganizationCache.java new file mode 100644 index 00000000..4d1c1225 --- /dev/null +++ b/dozentenmodul/src/main/java/org/openslx/dozmod/thrift/cache/OrganizationCache.java @@ -0,0 +1,74 @@ +package org.openslx.dozmod.thrift.cache; + +import java.util.List; + +import org.apache.log4j.Logger; +import org.openslx.bwlp.thrift.iface.Organization; +import org.openslx.bwlp.thrift.iface.UserInfo; +import org.openslx.thrifthelper.ThriftManager; +import org.openslx.util.GenericDataCache; + +public class OrganizationCache { + + private static final Logger LOGGER = Logger.getLogger(OrganizationCache.class); + + /** + * How long should the list be cached? + */ + private static final int CACHE_TIME_MS = 20 * 60 * 1000; + + private static final GenericDataCache<List<Organization>> cache = new GenericDataCache<List<Organization>>(CACHE_TIME_MS) { + + @Override + protected List<Organization> update() throws Exception { + try { + return ThriftManager.getMasterClient().getOrganizations(); + } catch (Exception e) { + LOGGER.warn("Failed to get organization list from master server, trying satellite for backup..", e); + } + return ThriftManager.getSatClient().getAllOrganizations(); + // TODO: Sort list by display name? + } + }; + + private OrganizationCache() { + // No instancing + } + + /** + * Get all known organizations + * + * @return list of organizations + */ + public static List<Organization> getAll() { + return cache.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 = cache.get(); + if (list == null) + return null; + for (Organization org : list) { + if (org.organizationId.equals(organizationId)) + return org; + } + return null; + } + +} diff --git a/dozentenmodul/src/main/java/org/openslx/dozmod/thrift/cache/UserCache.java b/dozentenmodul/src/main/java/org/openslx/dozmod/thrift/cache/UserCache.java new file mode 100644 index 00000000..4b51e2c3 --- /dev/null +++ b/dozentenmodul/src/main/java/org/openslx/dozmod/thrift/cache/UserCache.java @@ -0,0 +1,72 @@ +package org.openslx.dozmod.thrift.cache; + +import java.util.List; + +import org.apache.log4j.Logger; +import org.openslx.bwlp.thrift.iface.UserInfo; +import org.openslx.dozmod.thrift.Session; +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 = Session.getSatelliteConfig().pageSize; + 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; + } + +} |