org.picketlink.idm.config.IdentityStoresConfigurationBuilder Maven / Gradle / Ivy
/*
* JBoss, Home of Professional Open Source.
* Copyright 2012, Red Hat, Inc., and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.picketlink.idm.config;
import org.picketlink.idm.config.annotation.MethodConfigID;
import org.picketlink.idm.config.annotation.ParameterConfigID;
import org.picketlink.idm.model.Relationship;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import static org.picketlink.idm.IDMMessages.MESSAGES;
/**
* A class used to build the configuration for identity stores. Only a single configuration can exists for a given
* identity store.
*
* @author Pedro Igor
*/
public class IdentityStoresConfigurationBuilder
extends AbstractIdentityConfigurationChildBuilder>
implements IdentityStoreConfigurationChildBuilder {
private final List> identityStoresConfiguration;
private final Map, Class extends IdentityStoreConfigurationBuilder, ?>>> supportedStoreBuilders;
private final Set> globalRelationships;
private final Set> selfRelationships;
protected IdentityStoresConfigurationBuilder(NamedIdentityConfigurationBuilder builder) {
super(builder);
this.identityStoresConfiguration = new ArrayList>();
this.supportedStoreBuilders = new HashMap, Class extends IdentityStoreConfigurationBuilder, ?>>>();
this.globalRelationships = new HashSet>();
this.selfRelationships = new HashSet>();
this.supportedStoreBuilders.put(FileIdentityStoreConfiguration.class, FileStoreConfigurationBuilder.class);
this.supportedStoreBuilders.put(JPAIdentityStoreConfiguration.class, JPAStoreConfigurationBuilder.class);
this.supportedStoreBuilders.put(LDAPIdentityStoreConfiguration.class, LDAPStoreConfigurationBuilder.class);
this.supportedStoreBuilders.put(JDBCIdentityStoreConfiguration.class, JDBCStoreConfigurationBuilder.class);
this.supportedStoreBuilders.put(TokenStoreConfiguration.class, TokenStoreConfigurationBuilder.class);
}
/**
* Configures a file-based identity store for this configuration.
*
* @return
*/
@Override
public FileStoreConfigurationBuilder file() {
return forIdentityStoreConfig(FileIdentityStoreConfiguration.class, true);
}
/**
* Configures a JDBC based Identity Store
* @return
*/
public JDBCStoreConfigurationBuilder jdbc(){
return forIdentityStoreConfig(JDBCIdentityStoreConfiguration.class, true);
}
/**
* Configures a jpa-based identity store for this configuration.
*
* @return
*/
@Override
public JPAStoreConfigurationBuilder jpa() {
return forIdentityStoreConfig(JPAIdentityStoreConfiguration.class, true);
}
/**
* Configures a ldap-based identity store for this configuration.
*
* @return
*/
@Override
public LDAPStoreConfigurationBuilder ldap() {
return forIdentityStoreConfig(LDAPIdentityStoreConfiguration.class, true);
}
/**
* Configures a token-based identity store for this configuration.
*
* This identity store does not persist data, but only extracts information from tokens in order to perform some basic
* identity management operations. In this case, a token acts as a temporary identity store from where all identity data will
* be retrieved.
*
* Usually, tokens are a self-contained repository for the identities or claims for a particular subject.
*
* @return
*/
@Override
public TokenStoreConfigurationBuilder token() {
return forIdentityStoreConfig(TokenStoreConfiguration.class, true);
}
/**
* Adds support for a custom {@link org.picketlink.idm.spi.IdentityStore}.
*
* @param identityStoreConfiguration
* @param builder
*
* @return
*/
@MethodConfigID(name = "customIdentityStore")
public > T add(
@ParameterConfigID(name = "identityStoreConfigurationClass") Class extends IdentityStoreConfiguration> identityStoreConfiguration,
@ParameterConfigID(name = "builderClass") Class builder) {
this.supportedStoreBuilders.put(identityStoreConfiguration, builder);
return forIdentityStoreConfig(identityStoreConfiguration, true);
}
public List> getIdentityStoresConfigurationBuilder() {
return this.identityStoresConfiguration;
}
@Override
protected List extends IdentityStoreConfiguration> create() {
List configurations = new ArrayList();
IdentityStoreConfiguration partitionStoreConfig = null;
for (IdentityStoreConfigurationBuilder, ?> storeConfigurationBuilder : this.identityStoresConfiguration) {
IdentityStoreConfiguration storeConfiguration = storeConfigurationBuilder.create();
if (storeConfiguration.supportsPartition()) {
if (partitionStoreConfig != null) {
throw MESSAGES.configStoreMultiplePartitionConfigExists(partitionStoreConfig, storeConfiguration);
}
partitionStoreConfig = storeConfiguration;
}
for (Class extends Relationship> relType : storeConfigurationBuilder.getGlobalRelationshipTypes()) {
this.globalRelationships.add(relType);
}
for (Class extends Relationship> relType : storeConfigurationBuilder.getSelfRelationshipTypes()) {
this.selfRelationships.add(relType);
}
configurations.add(storeConfiguration);
}
return configurations;
}
@Override
protected void validate() {
if (this.identityStoresConfiguration.isEmpty()) {
throw MESSAGES.configStoreNoIdentityStoreConfigProvided();
}
for (IdentityStoreConfigurationBuilder, ?> currentConfiguration : this.identityStoresConfiguration) {
currentConfiguration.validate();
for (Class> type: currentConfiguration.getSupportedTypes().keySet()) {
for (IdentityStoreConfigurationBuilder, ?> storeConfiguration: this.identityStoresConfiguration) {
if (!storeConfiguration.equals(currentConfiguration)) {
for (Class> storeType: storeConfiguration.getSupportedTypes().keySet()) {
if (storeType.isAssignableFrom(type)) {
throw MESSAGES.configStoreDuplicatedSupportedType(type);
}
}
}
}
}
}
}
@Override
protected IdentityStoresConfigurationBuilder readFrom(List extends IdentityStoreConfiguration> fromConfiguration) {
if (fromConfiguration == null) {
throw MESSAGES.nullArgument("Configurations to read");
}
for (IdentityStoreConfiguration identityStoreConfiguration : fromConfiguration) {
IdentityStoreConfigurationBuilder storeConfigBuilder = forIdentityStoreConfig(
identityStoreConfiguration.getClass(), true);
storeConfigBuilder.readFrom(identityStoreConfiguration);
}
return this;
}
@SuppressWarnings("unchecked")
private > S forIdentityStoreConfig(
Class extends IdentityStoreConfiguration> configurationType, boolean createIfNotExists) {
Class builderType = (Class) this.supportedStoreBuilders.get(configurationType);
for (IdentityStoreConfigurationBuilder, ?> registeredStoreConfig : this.identityStoresConfiguration) {
if (registeredStoreConfig.getClass().equals(builderType)) {
return (S) registeredStoreConfig;
}
}
if (!createIfNotExists) {
return null;
}
S instance;
try {
instance = builderType.getConstructor(IdentityStoresConfigurationBuilder.class).newInstance(this);
} catch (Exception e) {
throw MESSAGES.instantiationError(builderType, e);
}
this.identityStoresConfiguration.add(instance);
return instance;
}
protected Set> getGlobalRelationships() {
return this.globalRelationships;
}
protected Set> getSelfRelationships() {
return this.selfRelationships;
}
}