summaryrefslogtreecommitdiffstats
path: root/dozentenmodul/src/main/java/org/openslx/dozmod/thrift/cache/OrganizationCache.java
blob: f4e0a4301998d9ace10c5c27cfcad275371e81a1 (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
package org.openslx.dozmod.thrift.cache;

import java.util.Collections;
import java.util.List;

import org.apache.log4j.Logger;
import org.openslx.bwlp.thrift.iface.Organization;
import org.openslx.bwlp.thrift.iface.SatelliteServer;
import org.openslx.bwlp.thrift.iface.UserInfo;
import org.openslx.dozmod.thrift.Sorters;
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 {
			List<Organization> orgs = null;
			try {
				orgs = ThriftManager.getMasterClient().getOrganizations();
			} catch (Exception e) {
				LOGGER.warn(
						"Failed to get organization list from master server, trying satellite for backup..",
						e);
			}
			if (orgs == null) {
				SatelliteServer.Iface client = ThriftManager.getSatClient();
				if (client == null) {
					LOGGER.warn("Satellite server not known yet.");
				} else {
					try {
						orgs = client.getAllOrganizations();
					} catch (Exception e) {
						LOGGER.error("Failed to get organization list from satellite server. ", e);
						// both failed, can this ever happen? TODO if it does, what do we do?
					}
				}
			}
			if (orgs != null)
				Collections.sort(orgs, Sorters.organization);
			return orgs;
		}
	};

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

}