summaryrefslogblamecommitdiffstats
path: root/dozentenmodul/src/main/java/org/openslx/dozmod/thrift/cache/MetaDataCache.java
blob: 340d95e8d1cb3b7cff42619e89b7b549e6fcda04 (plain) (tree)
1
2
3
4
5
6
7
8
9
                                        







                                                     
                                         

                            
 
                                                                                   
 



                                                                


                                                                                                                           









                                                                                                                    


                                                                                                                            









                                                                                                                             
 

                                                                           
           




                                                                   

                                                         



                                                                   

                                               


                            
 

                                                                                
           




                                                           











                                                                     
 
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;
	}

}