diff options
| author | Simon Rettberg | 2015-07-06 15:40:32 +0200 |
|---|---|---|
| committer | Simon Rettberg | 2015-07-06 15:40:32 +0200 |
| commit | 73f5c149482b4f2053e279181a90fa0f9ef47c4b (patch) | |
| tree | 6daa92769e87da9bfd9a22ef19dcdeb0abbad38a /dozentenmodul/src/main/java | |
| parent | [client] reworked exceptions handling of BWIDM auth (diff) | |
| download | tutor-module-73f5c149482b4f2053e279181a90fa0f9ef47c4b.tar.gz tutor-module-73f5c149482b4f2053e279181a90fa0f9ef47c4b.tar.xz tutor-module-73f5c149482b4f2053e279181a90fa0f9ef47c4b.zip | |
[client] Continued work on caching
Diffstat (limited to 'dozentenmodul/src/main/java')
| -rw-r--r-- | dozentenmodul/src/main/java/org/openslx/bwlp/dozmod/thrift/ACache.java | 2 | ||||
| -rw-r--r-- | dozentenmodul/src/main/java/org/openslx/bwlp/dozmod/thrift/ImageCache.java | 38 | ||||
| -rw-r--r-- | dozentenmodul/src/main/java/org/openslx/bwlp/dozmod/thrift/MetaDataCache.java | 62 | ||||
| -rw-r--r-- | dozentenmodul/src/main/java/org/openslx/bwlp/dozmod/thrift/OrganizationCache.java (renamed from dozentenmodul/src/main/java/org/openslx/bwlp/dozmod/thrift/OrganizationList.java) | 40 | ||||
| -rw-r--r-- | dozentenmodul/src/main/java/org/openslx/bwlp/dozmod/thrift/Session.java | 48 |
5 files changed, 171 insertions, 19 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 index 7c7d927e..68e8433d 100644 --- a/dozentenmodul/src/main/java/org/openslx/bwlp/dozmod/thrift/ACache.java +++ b/dozentenmodul/src/main/java/org/openslx/bwlp/dozmod/thrift/ACache.java @@ -81,7 +81,7 @@ public abstract class ACache<T> { return true; } - public abstract T update() throws Exception; + protected abstract T update() throws Exception; // diff --git a/dozentenmodul/src/main/java/org/openslx/bwlp/dozmod/thrift/ImageCache.java b/dozentenmodul/src/main/java/org/openslx/bwlp/dozmod/thrift/ImageCache.java new file mode 100644 index 00000000..2628807a --- /dev/null +++ b/dozentenmodul/src/main/java/org/openslx/bwlp/dozmod/thrift/ImageCache.java @@ -0,0 +1,38 @@ +package org.openslx.bwlp.dozmod.thrift; + +import java.util.List; + +import org.apache.thrift.TException; +import org.openslx.bwlp.dozmod.thrift.ACache.CacheMode; +import org.openslx.bwlp.thrift.iface.ImageSummaryRead; +import org.openslx.bwlp.thrift.iface.TAuthorizationException; +import org.openslx.bwlp.thrift.iface.TInternalServerError; +import org.openslx.thrifthelper.ThriftManager; + +public class ImageCache { + + private static final int CACHE_TIME_LIST_MS = 2 * 60 * 1000; + + private static final ACache<List<ImageSummaryRead>> listCache = new ACache<List<ImageSummaryRead>>(CACHE_TIME_LIST_MS) { + @Override + protected List<ImageSummaryRead> update() throws TAuthorizationException, TInternalServerError, TException { + List<ImageSummaryRead> result = null; + 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() < ThriftManager.getSatClient().getPageSize()) + 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/bwlp/dozmod/thrift/MetaDataCache.java b/dozentenmodul/src/main/java/org/openslx/bwlp/dozmod/thrift/MetaDataCache.java new file mode 100644 index 00000000..fd4b7172 --- /dev/null +++ b/dozentenmodul/src/main/java/org/openslx/bwlp/dozmod/thrift/MetaDataCache.java @@ -0,0 +1,62 @@ +package org.openslx.bwlp.dozmod.thrift; + +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; + +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 ACache<List<OperatingSystem>> osCache = new ACache<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 ACache<List<Virtualizer>> virtualizerCache = new ACache<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(); + } + + /** + * Get all supported virtualizers an image can be declared to be run as. + * + * @return + */ + public static List<Virtualizer> getVirtualizers() { + return virtualizerCache.get(); + } + +}
\ No newline at end of file diff --git a/dozentenmodul/src/main/java/org/openslx/bwlp/dozmod/thrift/OrganizationList.java b/dozentenmodul/src/main/java/org/openslx/bwlp/dozmod/thrift/OrganizationCache.java index d13da1b3..30a0cb3e 100644 --- a/dozentenmodul/src/main/java/org/openslx/bwlp/dozmod/thrift/OrganizationList.java +++ b/dozentenmodul/src/main/java/org/openslx/bwlp/dozmod/thrift/OrganizationCache.java @@ -2,32 +2,36 @@ package org.openslx.bwlp.dozmod.thrift; 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; -public class OrganizationList extends ACache<List<Organization>> { - +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 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 + private static final ACache<List<Organization>> cache = new ACache<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? } - return ThriftManager.getSatClient().getAllOrganizations(); - // TODO: Sort list by display name? + }; + + private OrganizationCache() { + // No instancing } /** @@ -36,7 +40,7 @@ public class OrganizationList extends ACache<List<Organization>> { * @return list of organizations */ public static List<Organization> getAll() { - return instance.get(); + return cache.get(); } /** @@ -56,7 +60,7 @@ public class OrganizationList extends ACache<List<Organization>> { * @return matching organization, or <code>null</code> if not found */ public static Organization find(String organizationId) { - List<Organization> list = instance.get(); + List<Organization> list = cache.get(); if (list == null) return null; for (Organization org : list) { diff --git a/dozentenmodul/src/main/java/org/openslx/bwlp/dozmod/thrift/Session.java b/dozentenmodul/src/main/java/org/openslx/bwlp/dozmod/thrift/Session.java new file mode 100644 index 00000000..0cdce3fd --- /dev/null +++ b/dozentenmodul/src/main/java/org/openslx/bwlp/dozmod/thrift/Session.java @@ -0,0 +1,48 @@ +package org.openslx.bwlp.dozmod.thrift; + +import org.openslx.bwlp.thrift.iface.ClientSessionData; + +import util.ServiceProviderResponse; + +public class Session { + + private static String firstName = null; + + private static String lastName = null; + + private static String eMail = null; + + private static String userId = null; + + private static String satelliteToken = null; + + private static String masterToken = null; + + public static void fromClientSessionData(ClientSessionData session) { + if (userId != null && !userId.equals(session.userInfo.userId)) + throw new IllegalArgumentException("Cannot set new session data with different user id!"); + firstName = session.userInfo.firstName; + lastName = session.userInfo.lastName; + eMail = session.userInfo.eMail; + userId = session.userInfo.userId; + masterToken = session.sessionId; + satelliteToken = session.authToken; + } + + public static void fromEcpLogin(ServiceProviderResponse response) { + // TODO + //if (userId != null && !userId.equals(response.userId)) + // throw new IllegalArgumentException("Cannot set new session data with different user id!"); + firstName = response.firstName; + lastName = response.lastName; + eMail = response.mail; + //userId = response.userId; TODO + masterToken = response.token; + satelliteToken = response.sessionId; + } + + public static String getSatelliteToken() { + return satelliteToken; + } + +} |
