blob: 69e48552af8b011237e3bf3c30502e96ad876a79 (
plain) (
blame)
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
|
package org.openslx.dozmod.thrift.cache;
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;
import org.openslx.util.GenericDataCache;
import org.openslx.util.GenericDataCache.CacheMode;
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 GenericDataCache<List<Organization>> cache = new GenericDataCache<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?
}
};
private OrganizationCache() {
// No instancing
}
/**
* Get all known organizations
*
* @return list of organizations
*/
public static List<Organization> getAll() {
return cache.get();
}
/**
* Find the organization the given user belongs to
*
* @param user
* @return matching organization, or <code>null</code> 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 <code>null</code> if not found
*/
public static Organization find(String organizationId) {
// First, try in "always cached" mode
List<Organization> list = cache.get(CacheMode.PREFER_CACHED);
Organization org = find(organizationId, list);
if (org != null)
return org;
// Try again with a potential refresh
List<Organization> newList = cache.get(CacheMode.DEFAULT);
if (list == newList) // Returned list from cache as it was still recent enough
return null;
return find(organizationId, newList);
}
private static Organization find(String organizationId, List<Organization> list) {
if (list != null) {
for (Organization org : list) {
if (org.organizationId.equals(organizationId))
return org;
}
}
return null;
}
}
|