package org.openslx.dozmod.thrift.cache; import java.util.ArrayList; import java.util.List; import org.apache.log4j.Logger; import org.apache.thrift.TApplicationException; import org.apache.thrift.TException; import org.openslx.bwlp.thrift.iface.LdapFilter; import org.openslx.bwlp.thrift.iface.Location; import org.openslx.bwlp.thrift.iface.NetShare; import org.openslx.bwlp.thrift.iface.OperatingSystem; import org.openslx.bwlp.thrift.iface.PredefinedData; import org.openslx.bwlp.thrift.iface.PresetRunScript; import org.openslx.bwlp.thrift.iface.Virtualizer; import org.openslx.dozmod.thrift.Session; import org.openslx.thrifthelper.ThriftManager; import org.openslx.util.GenericDataCache; import org.openslx.util.GenericDataCache.CacheMode; public class MetaDataCache { private static final Logger LOGGER = Logger.getLogger(MetaDataCache.class); /** * How long to cache data. */ private static final int CACHE_TIME_LONG_MS = 60 * 60 * 1000; private static final int CACHE_TIME_SHORT_MS = 10 * 60 * 1000; private static final GenericDataCache> osCache = new GenericDataCache>( CACHE_TIME_LONG_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 GenericDataCache> virtualizerCache = new GenericDataCache>( CACHE_TIME_LONG_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(); } }; private static int locationFails = 0; private static final GenericDataCache> locationCache = new GenericDataCache>( CACHE_TIME_SHORT_MS) { @Override protected List update() throws TException { if (locationFails > 2) return null; try { List ret = ThriftManager.getSatClient().getLocations(); locationFails = 0; return ret; } catch (TApplicationException e) { locationFails++; if (locationFails == 1) { LOGGER.info("Satellite doesn't seem to support locations"); } } catch (TException e) { LOGGER.warn("Could not get locations list from the satellite...", e); } // querying masterserver does not make sense for locations return null; } }; /** * Get all known/valid operating systems an image can be marked as. * * @return */ public static List getOperatingSystems() { return osCache.get(); } public static OperatingSystem getOsById(int id) { return getOsById(id, false); } public static OperatingSystem getOsById(int id, boolean forceCache) { // First, try in "always cached" mode List list = osCache.get(CacheMode.FORCE_CACHED); OperatingSystem os = getOsById(id, list); if (os != null || forceCache) return os; // Try again with a potential refresh List newList = osCache.get(CacheMode.DEFAULT); if (list == newList) // Returned list from cache as it was still recent enough return null; return getOsById(id, newList); } private static OperatingSystem getOsById(int id, List list) { if (list != 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 getVirtualizers() { return virtualizerCache.get(); } public static Virtualizer getVirtualizerById(String virtId) { return getVirtualizerById(virtId, false); } public static Virtualizer getVirtualizerById(String virtId, boolean forceCache) { // First, try in "always cached" mode List list = virtualizerCache.get(CacheMode.FORCE_CACHED); Virtualizer virt = getVirtualizerById(virtId, list); if (virt != null || forceCache) return virt; // Try again with a potential refresh List newList = virtualizerCache.get(CacheMode.DEFAULT); if (list == newList) // Returned list from cache as it was still recent enough return null; return getVirtualizerById(virtId, newList); } private static Virtualizer getVirtualizerById(String virtId, List list) { if (list != null) { for (Virtualizer virt : list) { if (virt.getVirtId().equals(virtId)) return virt; } } return null; } /** * Get all known/valid locations a lecture can be assigned to. * * @return */ public static List getLocations() { return locationCache.get(); } public static Location getLocationById(int id) { return getLocationById(id, false); } public static Location getLocationById(int id, boolean forceCache) { // First, try in "always cached" mode List list = locationCache.get(CacheMode.FORCE_CACHED); Location location = getLocationById(id, list); if (location != null || forceCache) return location; // Try again with a potential refresh List newList = locationCache.get(CacheMode.DEFAULT); if (list == newList) // Returned list from cache as it was still recent enough return null; return getLocationById(id, newList); } private static Location getLocationById(int id, List list) { if (list != null) { for (Location loc : list) { if (loc.getLocationId() == id) return loc; } } return null; } /* * LDAP filters and network shares */ private static final GenericDataCache predefinedData = new GenericDataCache( CACHE_TIME_SHORT_MS) { @Override protected PredefinedData update() throws TException { try { return ThriftManager.getSatClient().getPredefinedData(Session.getSatelliteToken()); } catch (TException e) { LOGGER.warn("Could not get predefined Data from sat...", e); } return null; } }; public static List getPredefinedLdapFilters() { PredefinedData pd = predefinedData.get(); if (pd == null || pd.ldapFilter == null) return new ArrayList(0); return pd.ldapFilter; } public static List getPredefinedNetshares() { PredefinedData pd = predefinedData.get(); if (pd == null || pd.ldapFilter == null) return new ArrayList(0); return pd.netShares; } public static List getPredefinedRunScripts() { PredefinedData pd = predefinedData.get(); if (pd == null || pd.ldapFilter == null) return new ArrayList(0); return pd.runScripts; } }