Prevent double registration of commands
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
/*
|
||||
* Copyright 2017 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.shell;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.TreeMap;
|
||||
|
||||
/**
|
||||
* A {@link CommandRegistry} that supports registration of new commands.
|
||||
*
|
||||
* <p>Makes sure that no two commands are registered with the same name.</p>
|
||||
*
|
||||
* @author Eric Bottard
|
||||
*/
|
||||
public class ConfigurableCommandRegistry implements CommandRegistry {
|
||||
|
||||
private Map<String, MethodTarget> commands = new HashMap<>();
|
||||
|
||||
@Override
|
||||
public Map<String, MethodTarget> listCommands() {
|
||||
return new TreeMap<>(commands);
|
||||
}
|
||||
|
||||
public void register(String name, MethodTarget target) {
|
||||
MethodTarget previous = commands.get(name);
|
||||
if (previous != null) {
|
||||
throw new IllegalArgumentException(
|
||||
String.format("Illegal registration for command '%s': Attempt to register both '%s' and '%s'", name, target, previous));
|
||||
}
|
||||
commands.put(name, target);
|
||||
}
|
||||
}
|
||||
@@ -16,19 +16,17 @@
|
||||
|
||||
package org.springframework.shell;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Strategy interface for discovering commands.
|
||||
* Strategy interface for registering commands.
|
||||
*
|
||||
* @author Eric Bottard
|
||||
* @author Camilo Gonzalez
|
||||
*/
|
||||
public interface MethodTargetResolver {
|
||||
public interface MethodTargetRegistrar {
|
||||
|
||||
/**
|
||||
* Return a mapping from {@literal <command keyword(s)>} to actual behavior.
|
||||
* Register mappings from {@literal <command keyword(s)>} to actual behavior.
|
||||
*/
|
||||
public Map<String, MethodTarget> resolve();
|
||||
void register(ConfigurableCommandRegistry registry);
|
||||
|
||||
}
|
||||
@@ -33,8 +33,6 @@ import javax.validation.Validation;
|
||||
import javax.validation.executable.ExecutableValidator;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.ApplicationArguments;
|
||||
import org.springframework.boot.ApplicationRunner;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
@@ -80,9 +78,11 @@ public class Shell implements CommandRegistry {
|
||||
|
||||
@PostConstruct
|
||||
public void gatherMethodTargets() throws Exception {
|
||||
for (MethodTargetResolver resolver : applicationContext.getBeansOfType(MethodTargetResolver.class).values()) {
|
||||
methodTargets.putAll(resolver.resolve());
|
||||
ConfigurableCommandRegistry registry = new ConfigurableCommandRegistry();
|
||||
for (MethodTargetRegistrar resolver : applicationContext.getBeansOfType(MethodTargetRegistrar.class).values()) {
|
||||
resolver.register(registry);
|
||||
}
|
||||
methodTargets = registry.listCommands();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
/*
|
||||
* Copyright 2017 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.shell;
|
||||
|
||||
import static org.hamcrest.collection.IsMapContaining.hasEntry;
|
||||
import static org.hamcrest.collection.IsMapContaining.hasKey;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.hamcrest.collection.IsMapContaining;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link ConfigurableCommandRegistry}.
|
||||
*
|
||||
* @author Eric Bottard
|
||||
*/
|
||||
public class ConfigurableCommandRegistryTest {
|
||||
|
||||
@Rule
|
||||
public ExpectedException thrown= ExpectedException.none();
|
||||
|
||||
@Test
|
||||
public void testRegistration() {
|
||||
ConfigurableCommandRegistry registry = new ConfigurableCommandRegistry();
|
||||
registry.register("foo", MethodTarget.of("toString", this, "some command"));
|
||||
|
||||
assertThat(registry.listCommands(), hasKey("foo"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDoubleRegistration() {
|
||||
ConfigurableCommandRegistry registry = new ConfigurableCommandRegistry();
|
||||
registry.register("foo", MethodTarget.of("toString", this, "some command"));
|
||||
|
||||
thrown.expect(IllegalArgumentException.class);
|
||||
thrown.expectMessage("foo");
|
||||
thrown.expectMessage("toString");
|
||||
thrown.expectMessage("hashCode");
|
||||
|
||||
registry.register("foo", MethodTarget.of("hashCode", this, "some command"));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user