Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 1b649d8e6a73805d15b0bace7c593451fc78487a (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
/*******************************************************************************
 * Copyright (c) 2005, 2018 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.equinox.internal.security.auth;

import java.util.Hashtable;
import javax.security.auth.login.AppConfigurationEntry;
import javax.security.auth.login.Configuration;
import org.eclipse.equinox.internal.security.auth.nls.SecAuthMessages;
import org.eclipse.osgi.util.NLS;

public class ConfigurationFederator extends Configuration {

	// TODO this usage needs to be changed. We should retrieve federatedConfigs
	// from the "ConfigurationFactory" every time we are asked; the "ConfigurationFactory"
	// shoudl keep a cache that corresponds to what's in the registry and update it on registry 
	// events
	private Configuration[] federatedConfigs = null;

	private Hashtable<String, AppConfigurationEntry[]> configCache = new Hashtable<>(5);
	private Hashtable<String, String> configToProviderMap = new Hashtable<>(5);

	final private Configuration defaultConfiguration;

	public ConfigurationFederator(Configuration defaultConfiguration) {
		this.defaultConfiguration = defaultConfiguration;
	}

	@Override
	public synchronized AppConfigurationEntry[] getAppConfigurationEntry(String name) {
		AppConfigurationEntry[] returnValue = configCache.get(name);
		if (returnValue != null)
			return returnValue;

		// Note: adding default config provider last; extension-point based configs are queried first
		Configuration[] configs = getFederatedConfigs();
		Configuration[] allConfigs = configs;
		if (defaultConfiguration != null) {
			allConfigs = new Configuration[configs.length + 1];
			System.arraycopy(configs, 0, allConfigs, 0, configs.length);
			allConfigs[configs.length] = defaultConfiguration;
		}
		for (int i = 0; i < allConfigs.length; i++) {
			boolean found = false;
			AppConfigurationEntry[] config = allConfigs[i].getAppConfigurationEntry(name);
			if (config == null)
				continue;
			String cachedProviderName = configToProviderMap.get(name);
			if (cachedProviderName != null && !cachedProviderName.equals(allConfigs[i].getClass().getName())) {
				String message = NLS.bind(SecAuthMessages.duplicateJaasConfig1, name, cachedProviderName);
				AuthPlugin.getDefault().logError(message, null);
			} else {
				if (found) {
					String message = NLS.bind(SecAuthMessages.duplicateJaasConfig2, name, cachedProviderName);
					AuthPlugin.getDefault().logError(message, null);
				} else if ((config != null) && (config.length != 0)) {
					returnValue = config;
					configToProviderMap.put(name, allConfigs[i].getClass().getName());
					configCache.put(name, returnValue);
					found = true;
				}
			}
		}

		if (returnValue == null || returnValue.length == 0) {
			String message = NLS.bind(SecAuthMessages.nonExistantJaasConfig, name);
			AuthPlugin.getDefault().logError(message, null);
		}
		return returnValue;
	}

	@Override
	public synchronized void refresh() {
		for (int i = 0; i < federatedConfigs.length; i++)
			federatedConfigs[i].refresh();
		if (defaultConfiguration != null)
			defaultConfiguration.refresh();

		configCache.clear();
		configToProviderMap.clear();
	}

	private Configuration[] getFederatedConfigs() {
		if (federatedConfigs == null)
			federatedConfigs = ConfigurationFactory.getInstance().getConfigurations();
		return federatedConfigs;
	}
}

Back to the top