From 73f5c149482b4f2053e279181a90fa0f9ef47c4b Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Mon, 6 Jul 2015 15:40:32 +0200 Subject: [client] Continued work on caching --- .../org/openslx/bwlp/dozmod/thrift/ACache.java | 2 +- .../org/openslx/bwlp/dozmod/thrift/ImageCache.java | 38 +++++++++++ .../openslx/bwlp/dozmod/thrift/MetaDataCache.java | 62 ++++++++++++++++++ .../bwlp/dozmod/thrift/OrganizationCache.java | 73 ++++++++++++++++++++++ .../bwlp/dozmod/thrift/OrganizationList.java | 69 -------------------- .../org/openslx/bwlp/dozmod/thrift/Session.java | 48 ++++++++++++++ 6 files changed, 222 insertions(+), 70 deletions(-) create mode 100644 dozentenmodul/src/main/java/org/openslx/bwlp/dozmod/thrift/ImageCache.java create mode 100644 dozentenmodul/src/main/java/org/openslx/bwlp/dozmod/thrift/MetaDataCache.java create mode 100644 dozentenmodul/src/main/java/org/openslx/bwlp/dozmod/thrift/OrganizationCache.java delete mode 100644 dozentenmodul/src/main/java/org/openslx/bwlp/dozmod/thrift/OrganizationList.java create mode 100644 dozentenmodul/src/main/java/org/openslx/bwlp/dozmod/thrift/Session.java (limited to 'dozentenmodul/src/main/java') 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 { 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> listCache = new ACache>(CACHE_TIME_LIST_MS) { + @Override + protected List update() throws TAuthorizationException, TInternalServerError, TException { + List result = null; + for (int i = 0;; ++i) { + List 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 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> osCache = new ACache>(CACHE_TIME_MS) { + @Override + protected List 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> virtualizerCache = new ACache>(CACHE_TIME_MS) { + @Override + protected List 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 getOperatingSystems() { + return osCache.get(); + } + + /** + * Get all supported virtualizers an image can be declared to be run as. + * + * @return + */ + public static List getVirtualizers() { + return virtualizerCache.get(); + } + +} \ No newline at end of file diff --git a/dozentenmodul/src/main/java/org/openslx/bwlp/dozmod/thrift/OrganizationCache.java b/dozentenmodul/src/main/java/org/openslx/bwlp/dozmod/thrift/OrganizationCache.java new file mode 100644 index 00000000..30a0cb3e --- /dev/null +++ b/dozentenmodul/src/main/java/org/openslx/bwlp/dozmod/thrift/OrganizationCache.java @@ -0,0 +1,73 @@ +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 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 ACache> cache = new ACache>(CACHE_TIME_MS) { + + @Override + protected List 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 getAll() { + return cache.get(); + } + + /** + * Find the organization the given user belongs to + * + * @param user + * @return matching organization, or null 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 null if not found + */ + public static Organization find(String organizationId) { + List 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/bwlp/dozmod/thrift/OrganizationList.java b/dozentenmodul/src/main/java/org/openslx/bwlp/dozmod/thrift/OrganizationList.java deleted file mode 100644 index d13da1b3..00000000 --- a/dozentenmodul/src/main/java/org/openslx/bwlp/dozmod/thrift/OrganizationList.java +++ /dev/null @@ -1,69 +0,0 @@ -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> { - - /** - * 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 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 getAll() { - return instance.get(); - } - - /** - * Find the organization the given user belongs to - * - * @param user - * @return matching organization, or null 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 null if not found - */ - public static Organization find(String organizationId) { - List list = instance.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/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; + } + +} -- cgit v1.2.3-55-g7522