1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
|
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;
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_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) {
return getOsById(id, false);
}
public static OperatingSystem getOsById(int id, boolean forceCache) {
// First, try in "always cached" mode
List<OperatingSystem> list = osCache.get(CacheMode.FORCE_CACHED);
OperatingSystem os = getOsById(id, list);
if (os != null || forceCache)
return os;
// Try again with a potential refresh
List<OperatingSystem> 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<OperatingSystem> 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<Virtualizer> 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<Virtualizer> list = virtualizerCache.get(CacheMode.FORCE_CACHED);
Virtualizer virt = getVirtualizerById(virtId, list);
if (virt != null || forceCache)
return virt;
// Try again with a potential refresh
List<Virtualizer> 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<Virtualizer> list) {
if (list != null) {
for (Virtualizer virt : list) {
if (virt.getVirtId().equals(virtId))
return virt;
}
}
return null;
}
}
|