summaryrefslogtreecommitdiffstats
path: root/dozentenmodul/src/main/java/org/openslx/dozmod/thrift/cache/UserCache.java
blob: d3774d5822a12c6099f2dcda6619f6aaca721ab0 (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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package org.openslx.dozmod.thrift.cache;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.apache.thrift.TException;
import org.openslx.bwlp.thrift.iface.UserInfo;
import org.openslx.dozmod.thrift.Session;
import org.openslx.thrifthelper.ThriftManager;
import org.openslx.util.GenericDataCache;
import org.openslx.util.GenericDataCache.CacheMode;

public class UserCache {

	private static final Logger LOGGER = Logger.getLogger(UserCache.class);

	/**
	 * How long should the list be cached?
	 */
	private static final int CACHE_TIME_MS = 5 * 60 * 1000;

	private static final GenericDataCache<List<UserInfo>> cache = new GenericDataCache<List<UserInfo>>(
			CACHE_TIME_MS) {

		@Override
		protected List<UserInfo> update() throws Exception {
			List<UserInfo> result = null;
			int pageSize = Session.getSatelliteConfig().pageSize;
			for (int i = 0;; ++i) {
				List<UserInfo> page = ThriftManager.getSatClient()
						.getUserList(Session.getSatelliteToken(), i);
				if (result == null) {
					result = page;
				} else {
					result.addAll(page);
				}
				if (page.size() < pageSize)
					break;
			}
			if (result != null) {
				result = Collections.unmodifiableList(result);
			}
			return result;
		}
	};

	// map for caching users found on the masterserver
	private static final Map<String, UserInfo> cachedUserFromMaster = new HashMap<String, UserInfo>();

	private UserCache() {
		// No instancing
	}

	/**
	 * Get all known users
	 * 
	 * @return list of users
	 */
	public static List<UserInfo> getAll() {
		return cache.get();
	}

	/**
	 * Find the user with the given id
	 * 
	 * @param user id
	 * @return matching user, or <code>null</code> if not found
	 */
	public static UserInfo find(String userId) {
		// First, try in "always cached" mode
		List<UserInfo> list = cache.get(CacheMode.PREFER_CACHED);
		UserInfo user = find(userId, list);
		if (user != null)
			return user;
		// still no user, look in local cache for master user
		// TODO check validity of the map?
		if (cachedUserFromMaster.containsKey(userId))
			return cachedUserFromMaster.get(userId);
		// Try again with a potential refresh
		List<UserInfo> newList = cache.get(CacheMode.DEFAULT);
		if (list != newList) { // Skip second searh if lists are identical
			user = find(userId, newList);
		}
		if (user == null) {
			// not in cached map either, query master
			try {
				user = ThriftManager.getMasterClient().getUser(Session.getMasterToken(), userId);
			} catch (TException e) {
				LOGGER.error("User with ID '" + userId + "' not found on the masterserver.", e);
				return null;
			}
			// remember it
			if (user != null)
				cachedUserFromMaster.put(userId, user);
		}
		return user;
	}

	private static UserInfo find(String userId, List<UserInfo> list) {
		if (list != null) {
			for (UserInfo user : list) {
				if (user.userId.equals(userId))
					return user;
			}
		}
		return null;
	}

}