Skip to content

Commit

Permalink
Merge pull request wildfly-extras#65 from jtymel/provider-loader
Browse files Browse the repository at this point in the history
provider-loader and aggregate-providers commands
  • Loading branch information
jtymel authored Feb 14, 2017
2 parents c7f938f + f7a1036 commit 4a06b8b
Show file tree
Hide file tree
Showing 5 changed files with 580 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package org.wildfly.extras.creaper.commands.elytron.providerloader;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.wildfly.extras.creaper.core.online.OnlineCommand;
import org.wildfly.extras.creaper.core.online.OnlineCommandContext;
import org.wildfly.extras.creaper.core.online.operations.Address;
import org.wildfly.extras.creaper.core.online.operations.Operations;
import org.wildfly.extras.creaper.core.online.operations.Values;
import org.wildfly.extras.creaper.core.online.operations.admin.Administration;

public class AddAggregateProviders implements OnlineCommand {

private final String name;
private final List<String> providers;
private final boolean replaceExisting;

private AddAggregateProviders(Builder builder) {
this.name = builder.name;
this.providers = builder.providers;
this.replaceExisting = builder.replaceExisting;
}

@Override
public void apply(OnlineCommandContext ctx) throws Exception {
Operations ops = new Operations(ctx.client);
Address aggregatepProvidersAddress = Address.subsystem("elytron").and("aggregate-providers", name);
if (replaceExisting) {
ops.removeIfExists(aggregatepProvidersAddress);
new Administration(ctx.client).reloadIfRequired();
}

ops.add(aggregatepProvidersAddress, Values.empty()
.andList(String.class, "providers", providers));
}

public static final class Builder {

private final String name;
private final List<String> providers = new ArrayList<String>();
private boolean replaceExisting;

public Builder(String name) {
if (name == null) {
throw new IllegalArgumentException("Name of the aggregate-providers must be specified as non null value");
}
if (name.isEmpty()) {
throw new IllegalArgumentException("Name of the aggregate-providers must not be empty value");
}

this.name = name;
}

public Builder providers(String... providers) {
if (providers == null) {
throw new IllegalArgumentException("Providers added to aggregate-providers must not be null");
}
Collections.addAll(this.providers, providers);
return this;
}

public Builder replaceExisting() {
this.replaceExisting = true;
return this;
}

public AddAggregateProviders build() {
if (providers.size() < 2) {
throw new IllegalArgumentException("There must be at least two providers");
}
return new AddAggregateProviders(this);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package org.wildfly.extras.creaper.commands.elytron.providerloader;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.wildfly.extras.creaper.core.online.OnlineCommand;
import org.wildfly.extras.creaper.core.online.OnlineCommandContext;
import org.wildfly.extras.creaper.core.online.operations.Address;
import org.wildfly.extras.creaper.core.online.operations.Operations;
import org.wildfly.extras.creaper.core.online.operations.Values;
import org.wildfly.extras.creaper.core.online.operations.admin.Administration;

public class AddProviderLoader implements OnlineCommand {

private final String name;
private final List<String> classNames;
private final Map<String, String> configuration;
private final String module;
private final String path;
private final String relativeTo;
private final boolean replaceExisting;

private AddProviderLoader(Builder builder) {
this.name = builder.name;
this.classNames = builder.classNames;
this.configuration = builder.configuration;
this.module = builder.module;
this.path = builder.path;
this.relativeTo = builder.relativeTo;
this.replaceExisting = builder.replaceExisting;
}

@Override
public void apply(OnlineCommandContext ctx) throws Exception {
Operations ops = new Operations(ctx.client);
Address providerLoaderAddress = Address.subsystem("elytron").and("provider-loader", name);
if (replaceExisting) {
ops.removeIfExists(providerLoaderAddress);
new Administration(ctx.client).reloadIfRequired();
}

ops.add(providerLoaderAddress, Values.empty()
.andListOptional(String.class, "class-names", classNames)
.andObjectOptional("configuration", Values.fromMap(configuration))
.andOptional("module", module)
.andOptional("path", path)
.andOptional("relative-to", relativeTo));
}

public static final class Builder {

private final String name;
private List<String> classNames;
private Map<String, String> configuration = new HashMap<String, String>();
private String module;
private String path;
private String relativeTo;
private boolean replaceExisting;

public Builder(String name) {
if (name == null) {
throw new IllegalArgumentException("Name of the provider-loader must be specified as non null value");
}
if (name.isEmpty()) {
throw new IllegalArgumentException("Name of the provider-loader must not be empty value");
}
this.name = name;
}

public Builder classNames(String... classNames) {
if (classNames == null) {
throw new IllegalArgumentException("Class-names added to provider-loader must not be null");
}
if (this.classNames == null) {
this.classNames = new ArrayList<String>();
}

Collections.addAll(this.classNames, classNames);
return this;
}

public Builder addConfiguration(String name, String value) {
if (name == null || name.isEmpty()) {
throw new IllegalArgumentException("Name of the configuration of the provider-loader must not be null");
}
if (value == null || value.isEmpty()) {
throw new IllegalArgumentException("Value of the configuration of the provider-loader must not be null");
}
configuration.put(name, value);
return this;
}

public Builder module(String module) {
this.module = module;
return this;
}

public Builder path(String path) {
this.path = path;
return this;
}

public Builder relativeTo(String relativeTo) {
this.relativeTo = relativeTo;
return this;
}

public Builder replaceExisting() {
this.replaceExisting = true;
return this;
}

public AddProviderLoader build() {
if (configuration != null && !configuration.isEmpty() && path != null && !path.isEmpty()) {
throw new IllegalArgumentException("Provider-loader must not have configured both path and configuration");
}

return new AddProviderLoader(this);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
package org.wildfly.extras.creaper.commands.elytron.providerloader;

import org.jboss.arquillian.junit.Arquillian;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.wildfly.extras.creaper.commands.elytron.AbstractElytronOnlineTest;
import org.wildfly.extras.creaper.core.CommandFailedException;
import org.wildfly.extras.creaper.core.online.operations.Address;

import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

@RunWith(Arquillian.class)
public class AddAggregateProvidersOnlineTest extends AbstractElytronOnlineTest {

private static final String TEST_AGGREGATE_PROVIDERS_NAME = "CreaperTestAggregateProviders";
private static final Address TEST_AGGREGATE_PROVIDERS_ADDRESS = SUBSYSTEM_ADDRESS
.and("aggregate-providers", TEST_AGGREGATE_PROVIDERS_NAME);
private static final String TEST_AGGREGATE_PROVIDERS_NAME2 = "CreaperTestAggregateProviders2";
private static final Address TEST_AGGREGATE_PROVIDERS_ADDRESS2 = SUBSYSTEM_ADDRESS
.and("aggregate-providers", TEST_AGGREGATE_PROVIDERS_NAME2);

protected static final String TEST_PROVIDER_LOADER_NAME = "CreaperTestProviderLoader";
protected static final Address TEST_PROVIDER_LOADER_ADDRESS = SUBSYSTEM_ADDRESS
.and("provider-loader", TEST_PROVIDER_LOADER_NAME);
protected static final String TEST_PROVIDER_LOADER_NAME2 = "CreaperTestProviderLoader2";
protected static final Address TEST_PROVIDER_LOADER_ADDRESS2 = SUBSYSTEM_ADDRESS
.and("provider-loader", TEST_PROVIDER_LOADER_NAME2);

@Before
public void createProviderLoaders() throws Exception {
AddProviderLoader addProviderLoader = new AddProviderLoader.Builder(TEST_PROVIDER_LOADER_NAME)
.build();
AddProviderLoader addProviderLoader2 = new AddProviderLoader.Builder(TEST_PROVIDER_LOADER_NAME2)
.build();
client.apply(addProviderLoader);
client.apply(addProviderLoader2);
}

@After
public void cleanup() throws Exception {
ops.removeIfExists(TEST_AGGREGATE_PROVIDERS_ADDRESS);
ops.removeIfExists(TEST_AGGREGATE_PROVIDERS_ADDRESS2);
ops.removeIfExists(TEST_PROVIDER_LOADER_ADDRESS);
ops.removeIfExists(TEST_PROVIDER_LOADER_ADDRESS2);
administration.reloadIfRequired();
}

@Test
public void addAggregateProviders() throws Exception {
AddAggregateProviders addAggregateProviders
= new AddAggregateProviders.Builder(TEST_AGGREGATE_PROVIDERS_NAME)
.providers(TEST_PROVIDER_LOADER_NAME, TEST_PROVIDER_LOADER_NAME2)
.build();

client.apply(addAggregateProviders);

assertTrue("Aggregate-providers should be created", ops.exists(TEST_AGGREGATE_PROVIDERS_ADDRESS));
checkAttribute(TEST_AGGREGATE_PROVIDERS_ADDRESS, "providers[0]", TEST_PROVIDER_LOADER_NAME);
checkAttribute(TEST_AGGREGATE_PROVIDERS_ADDRESS, "providers[1]", TEST_PROVIDER_LOADER_NAME2);
}

@Test
public void addTwoAggregateProviders() throws Exception {
AddAggregateProviders addAggregateProviders
= new AddAggregateProviders.Builder(TEST_AGGREGATE_PROVIDERS_NAME)
.providers(TEST_PROVIDER_LOADER_NAME, TEST_PROVIDER_LOADER_NAME2)
.build();

AddAggregateProviders addAggregateProviders2
= new AddAggregateProviders.Builder(TEST_AGGREGATE_PROVIDERS_NAME2)
.providers(TEST_PROVIDER_LOADER_NAME2, TEST_PROVIDER_LOADER_NAME2)
.build();

client.apply(addAggregateProviders);
client.apply(addAggregateProviders2);

assertTrue("Aggregate-providers should be created", ops.exists(TEST_AGGREGATE_PROVIDERS_ADDRESS));
assertTrue("Second aggregate-providers should be created", ops.exists(TEST_AGGREGATE_PROVIDERS_ADDRESS2));
}

@Test(expected = CommandFailedException.class)
public void addExistAggregateProvidersNotAllowed() throws Exception {
AddAggregateProviders addAggregateProviders
= new AddAggregateProviders.Builder(TEST_AGGREGATE_PROVIDERS_NAME)
.providers(TEST_PROVIDER_LOADER_NAME, TEST_PROVIDER_LOADER_NAME2)
.build();

AddAggregateProviders addAggregateProviders2
= new AddAggregateProviders.Builder(TEST_AGGREGATE_PROVIDERS_NAME)
.providers(TEST_PROVIDER_LOADER_NAME2, TEST_PROVIDER_LOADER_NAME2)
.build();

client.apply(addAggregateProviders);
assertTrue("Aggregate-providers should be created", ops.exists(TEST_AGGREGATE_PROVIDERS_ADDRESS));

client.apply(addAggregateProviders2);
fail("Aggregate-providers CreaperTestAggregateProviders already exists in configuration, exception should be thrown");
}

@Test
public void addExistAggregateProvidersAllowed() throws Exception {
AddAggregateProviders addAggregateProviders
= new AddAggregateProviders.Builder(TEST_AGGREGATE_PROVIDERS_NAME)
.providers(TEST_PROVIDER_LOADER_NAME, TEST_PROVIDER_LOADER_NAME2)
.build();

AddAggregateProviders addAggregateProviders2
= new AddAggregateProviders.Builder(TEST_AGGREGATE_PROVIDERS_NAME)
.providers(TEST_PROVIDER_LOADER_NAME2, TEST_PROVIDER_LOADER_NAME2)
.replaceExisting()
.build();

client.apply(addAggregateProviders);
assertTrue("Aggregate-providers should be created", ops.exists(TEST_AGGREGATE_PROVIDERS_ADDRESS));

client.apply(addAggregateProviders2);
assertTrue("Aggregate-providers should be created", ops.exists(TEST_AGGREGATE_PROVIDERS_ADDRESS));

// check whether it was really rewritten
checkAttribute(TEST_AGGREGATE_PROVIDERS_ADDRESS, "providers[1]", TEST_PROVIDER_LOADER_NAME2);
}

@Test(expected = CommandFailedException.class)
public void addAggregateProvidersWithoutConfiguredProviderLoader() throws Exception {
AddAggregateProviders addAggregateProviders
= new AddAggregateProviders.Builder(TEST_AGGREGATE_PROVIDERS_NAME)
.providers(TEST_PROVIDER_LOADER_NAME, "NotConfiguredProviderLoader")
.build();

client.apply(addAggregateProviders);
fail("Aggregate-providers shouldn't be added when using unconfigured provider-loader");
}

@Test(expected = IllegalArgumentException.class)
public void addAggregateProviders_nullName() throws Exception {
new AddAggregateProviders.Builder(null)
.providers(TEST_PROVIDER_LOADER_NAME, TEST_PROVIDER_LOADER_NAME2)
.build();
fail("Creating command with null name should throw exception");
}

@Test(expected = IllegalArgumentException.class)
public void addAggregateProviders_emptyName() throws Exception {
new AddAggregateProviders.Builder("")
.providers(TEST_PROVIDER_LOADER_NAME, TEST_PROVIDER_LOADER_NAME2)
.build();
fail("Creating command with empty name should throw exception");
}

@Test(expected = IllegalArgumentException.class)
public void addAggregateProviders_nullProviders() throws Exception {
new AddAggregateProviders.Builder(TEST_AGGREGATE_PROVIDERS_NAME)
.providers(null)
.build();
fail("Creating command with null providers should throw exception");
}

@Test(expected = IllegalArgumentException.class)
public void addAggregateProviders_emptyProviders() throws Exception {
new AddAggregateProviders.Builder(TEST_AGGREGATE_PROVIDERS_NAME)
.providers("")
.build();
fail("Creating command with empty providers should throw exception");
}

@Test(expected = IllegalArgumentException.class)
public void addAggregateProviders_oneProvider() throws Exception {
new AddAggregateProviders.Builder(TEST_AGGREGATE_PROVIDERS_NAME)
.providers(TEST_PROVIDER_LOADER_NAME)
.build();
fail("Creating command with only one provider should throw exception");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.wildfly.extras.creaper.commands.elytron.providerloader;

import java.security.Provider;

public class AddProviderLoaderImpl extends Provider {

public AddProviderLoaderImpl() {
super("name", 1.0, "some info");
}

}
Loading

0 comments on commit 4a06b8b

Please sign in to comment.