All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.springframework.social.partnercenter.connect.MultiTenantConnectionFactoryRegistry Maven / Gradle / Ivy

Go to download

A provider extension for Spring Social to enable connectivity with Microsoft and an API binding for Microsoft's Partner Center API.

There is a newer version: 10.11.0
Show newest version
package org.springframework.social.partnercenter.connect;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import org.springframework.core.GenericTypeResolver;
import org.springframework.social.connect.ConnectionFactory;

public class MultiTenantConnectionFactoryRegistry {
	private final Map> connectionFactories = new HashMap>();

	private final Map, String> apiTypeIndex = new HashMap, String>();

	/**
	 * Add a {@link ConnectionFactory} to this registry.
	 * @param connectionFactory the connection factory
	 * @param tenant the tenant to register the connection under
	 */
	public void addConnectionFactory(String tenant, ConnectionFactory connectionFactory) {
		if (connectionFactories.containsKey(buildKey(tenant, connectionFactory.getProviderId()))) {
			throw new IllegalArgumentException("A ConnectionFactory for provider '" + connectionFactory.getProviderId() + "' with tenantId '"+tenant+"' has already been registered");
		}
		Class apiType = GenericTypeResolver.resolveTypeArgument(connectionFactory.getClass(), ConnectionFactory.class);
		if (!apiTypeIndex.containsKey(apiType)) {
			apiTypeIndex.put(apiType, connectionFactory.getProviderId());
		}
		connectionFactories.put(buildKey(tenant, connectionFactory.getProviderId()), connectionFactory);
	}

	public void setConnectionFactories(Map> connectionFactories) {
		for(String key : connectionFactories.keySet()){
			addConnectionFactory(key, connectionFactories.get(key));
		}
	}

	// implementing ConnectionFactoryLocator

	public ConnectionFactory getConnectionFactory(String tenantId, String providerId) {
		ConnectionFactory connectionFactory = connectionFactories.get(buildKey(tenantId, providerId));
		if (connectionFactory == null) {
			throw new IllegalArgumentException("No connection factory for service provider '" + providerId + "' with tenantId '"+tenantId+"'is registered");
		}
		return connectionFactory;
	}

	@SuppressWarnings("unchecked")
	public  ConnectionFactory getConnectionFactory(String tenantId, Class apiType) {
		String providerId = apiTypeIndex.get(apiType);
		if (providerId == null) {
			throw new IllegalArgumentException("No connection factory for API [" + apiType.getName() + "] with tenantId '"+tenantId+"' is registered");
		}
		return (ConnectionFactory) getConnectionFactory(tenantId, providerId);
	}

	public boolean isRegistered(String tenantId, String providerId){
		return connectionFactories.containsKey(buildKey(tenantId, providerId));
	}

	public boolean isRegistered(String tenantId, Class apiType){
		String providerId = apiTypeIndex.get(apiType);
		return providerId != null && connectionFactories.containsKey(buildKey(tenantId, providerId));
	}

	public Set registeredTenantIds() {
		return connectionFactories.keySet()
				.stream().map(s -> s.split(":")[0])
				.collect(Collectors.toSet());
	}

	public Set registeredProviderIds() {
		return new HashSet<>(apiTypeIndex.values());
	}

	private String buildKey(String tenantId, String providerId){
		return tenantId.concat(":").concat(providerId);
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy