org.picketlink.idm.config.IdentityConfigurationBuilder 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 java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import static org.picketlink.idm.IDMMessages.MESSAGES;
/**
* A class used to build {@link IdentityConfiguration} instances, providing a fluent API with some meaningful
* methods.
It can be initialized in two ways:
- Using the default constructor. In this case
* all the configuration must be done before invoking one of the build methods.
- Passing a {@link List} of
* {@link IdentityConfiguration}. In this case the builder will be initialized with all the configuration read from the
* provided configurations.
Multiple configurations are supported and each one must have a unique
* name. At least one configuration must be provided, otherwise the build methods will fail when invoked.
*
* @author Pedro Igor
*/
public class IdentityConfigurationBuilder extends Builder> implements IdentityConfigurationChildBuilder {
private final Map namedIdentityConfigurationBuilders;
public IdentityConfigurationBuilder() {
this.namedIdentityConfigurationBuilders = new LinkedHashMap();
}
/**
* Creates a new instance reading all the configuration from a previously created list of {@link
* IdentityConfiguration}.
*
* @param configurations
* @throws SecurityConfigurationException if any error occurs or for any invalid configuration
*/
public IdentityConfigurationBuilder(List configurations) throws SecurityConfigurationException {
this();
readFrom(configurations);
}
/**
* Creates a new configuration.
If a configuration with the given configurationName
already
* exists, this method will return the same instance instead of creating a new one.
*
* @param configurationName
* @return
*/
public NamedIdentityConfigurationBuilder named(String configurationName) {
// Check if config with this name is already here
if (this.namedIdentityConfigurationBuilders.containsKey(configurationName)) {
return this.namedIdentityConfigurationBuilders.get(configurationName);
}
NamedIdentityConfigurationBuilder namedIdentityConfiguration = new NamedIdentityConfigurationBuilder(configurationName, this);
this.namedIdentityConfigurationBuilders.put(configurationName, namedIdentityConfiguration);
return namedIdentityConfiguration;
}
/**
* Builds a single {@link IdentityConfiguration}.
This method should be called when only a single
* configuration was provided. Otherwise an exception will be thrown.
For building multiple
* configurations use the buildAll
method instead.
*
* @return
* @throws SecurityConfigurationException if multiple configurations was defined, or if any validation check fails
* or if any error occurs when building the configuration.
*/
@Override
public IdentityConfiguration build() throws SecurityConfigurationException {
if (this.namedIdentityConfigurationBuilders.size() > 1) {
throw MESSAGES.configBuildMultipleConfigurationExists();
}
List identityConfigurations = create();
return identityConfigurations.get(0);
}
/**
* Builds a {@link List} of {@link IdentityConfiguration}.
This method should be used when multiple
* configurations exists.
*
* @return
* @throws SecurityConfigurationException if any validation check fails or if any error occurs when building the
* configuration.
*/
@Override
public List buildAll() throws SecurityConfigurationException {
return create();
}
/**
* Indicates if any configuration was already provided for this instance.
*
* @return
*/
public boolean isConfigured() {
return !this.namedIdentityConfigurationBuilders.isEmpty();
}
/**
* Returns all {@link org.picketlink.idm.config.NamedIdentityConfigurationBuilder}.
*
* @return
*/
public Collection getNamedIdentityConfigurationBuilders() {
return this.namedIdentityConfigurationBuilders.values();
}
@Override
protected void validate() throws SecurityConfigurationException {
if (this.namedIdentityConfigurationBuilders.isEmpty()) {
throw MESSAGES.configNoConfigurationProvided();
}
for (NamedIdentityConfigurationBuilder identityConfigBuilder : this.namedIdentityConfigurationBuilders.values()) {
try {
identityConfigBuilder.validate();
} catch (Exception e) {
throw MESSAGES.configInvalidConfiguration(identityConfigBuilder.getName(), e);
}
}
}
@Override
protected List create() throws SecurityConfigurationException {
List configurations = new ArrayList();
try {
validate();
for (NamedIdentityConfigurationBuilder identityConfigBuilder : this.namedIdentityConfigurationBuilders.values()) {
IdentityConfiguration configuration = identityConfigBuilder.create();
if (configurations.contains(configuration)) {
throw MESSAGES.configMultipleConfigurationsFoundWithSameName(configuration.getName());
}
boolean supportCredentials = false;
for (IdentityStoreConfiguration storeConfiguration : configuration.getStoreConfiguration()) {
if (storeConfiguration.supportsCredential()) {
if (supportCredentials) {
throw MESSAGES.configMultipleConfigurationsFoundWithCredentialSupport();
}
supportCredentials = true;
}
}
configurations.add(configuration);
}
} catch (Exception sce) {
throw MESSAGES.configCouldNotCreateConfiguration(sce);
}
return configurations;
}
@Override
protected Builder> readFrom(List fromConfiguration) throws SecurityConfigurationException {
if (fromConfiguration == null || fromConfiguration.isEmpty()) {
throw MESSAGES.nullArgument("Configuration to read from.");
}
for (IdentityConfiguration identityConfiguration : fromConfiguration) {
named(identityConfiguration.getName()).readFrom(identityConfiguration);
}
return this;
}
}