Added autodiscovery for command handlers.
This commit is contained in:
+29
-16
@@ -6,10 +6,14 @@
|
|||||||
package org.mifosplatform.commands.provider;
|
package org.mifosplatform.commands.provider;
|
||||||
|
|
||||||
import com.google.common.base.Preconditions;
|
import com.google.common.base.Preconditions;
|
||||||
|
import org.apache.commons.lang3.ArrayUtils;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.mifosplatform.commands.annotation.CommandType;
|
import org.mifosplatform.commands.annotation.CommandType;
|
||||||
import org.mifosplatform.commands.exception.UnsupportedCommandException;
|
import org.mifosplatform.commands.exception.UnsupportedCommandException;
|
||||||
import org.mifosplatform.commands.handler.NewCommandSourceHandler;
|
import org.mifosplatform.commands.handler.NewCommandSourceHandler;
|
||||||
|
import org.springframework.beans.BeansException;
|
||||||
|
import org.springframework.context.ApplicationContext;
|
||||||
|
import org.springframework.context.ApplicationContextAware;
|
||||||
import org.springframework.context.annotation.Scope;
|
import org.springframework.context.annotation.Scope;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
@@ -30,28 +34,17 @@ import java.util.HashMap;
|
|||||||
*/
|
*/
|
||||||
@Component
|
@Component
|
||||||
@Scope("singleton")
|
@Scope("singleton")
|
||||||
public class CommandHandlerProvider {
|
public class CommandHandlerProvider implements ApplicationContextAware {
|
||||||
|
|
||||||
private final HashMap<String, NewCommandSourceHandler> registeredHandlers = new HashMap<>();
|
private ApplicationContext applicationContext;
|
||||||
|
private HashMap<String, String> registeredHandlers;
|
||||||
|
|
||||||
CommandHandlerProvider() {
|
CommandHandlerProvider() {
|
||||||
super();
|
super();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Registers a {@link NewCommandSourceHandler} using the annotation {@link CommandType} to register the handler.<br/>
|
* Returns a handler for the given entity and action.<br/>
|
||||||
* <br/>
|
|
||||||
* @param handler the {@link NewCommandSourceHandler} to be added, must be given
|
|
||||||
*/
|
|
||||||
public <C extends NewCommandSourceHandler> void registerHandler(@Nonnull final C handler) {
|
|
||||||
Preconditions.checkArgument(handler != null, "A handler must be given!");
|
|
||||||
|
|
||||||
final CommandType commandType = handler.getClass().getAnnotation(CommandType.class);
|
|
||||||
this.registeredHandlers.put(commandType.entity() + "|" + commandType.action(), handler);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns a handler gor the given entity and action.<br/>
|
|
||||||
* <br/>
|
* <br/>
|
||||||
* Throws an {@link UnsupportedCommandException} if no handler
|
* Throws an {@link UnsupportedCommandException} if no handler
|
||||||
* for the given entity, action combination can be found.
|
* for the given entity, action combination can be found.
|
||||||
@@ -67,6 +60,26 @@ public class CommandHandlerProvider {
|
|||||||
if (!this.registeredHandlers.containsKey(key)) {
|
if (!this.registeredHandlers.containsKey(key)) {
|
||||||
throw new UnsupportedCommandException(key);
|
throw new UnsupportedCommandException(key);
|
||||||
}
|
}
|
||||||
return this.registeredHandlers.get(entity + "|" + action);
|
return (NewCommandSourceHandler)this.applicationContext.getBean(this.registeredHandlers.get(key));
|
||||||
|
}
|
||||||
|
|
||||||
|
private void initializeHandlerRegistry() {
|
||||||
|
if (this.registeredHandlers == null) {
|
||||||
|
this.registeredHandlers = new HashMap<>();
|
||||||
|
|
||||||
|
final String[] commandHandlerBeans = this.applicationContext.getBeanNamesForAnnotation(CommandType.class);
|
||||||
|
if (ArrayUtils.isNotEmpty(commandHandlerBeans)) {
|
||||||
|
for (final String commandHandlerName : commandHandlerBeans) {
|
||||||
|
final CommandType commandType = this.applicationContext.getType(commandHandlerName).getAnnotation(CommandType.class);
|
||||||
|
this.registeredHandlers.put(commandType.entity() + "|" + commandType.action(), commandHandlerName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
|
||||||
|
this.applicationContext = applicationContext;
|
||||||
|
this.initializeHandlerRegistry();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-3
@@ -22,10 +22,8 @@ public class CreateClientCommandHandler implements NewCommandSourceHandler {
|
|||||||
private final ClientWritePlatformService clientWritePlatformService;
|
private final ClientWritePlatformService clientWritePlatformService;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
public CreateClientCommandHandler(final ClientWritePlatformService clientWritePlatformService,
|
public CreateClientCommandHandler(final ClientWritePlatformService clientWritePlatformService) {
|
||||||
final CommandHandlerProvider commandHandlerProvider) {
|
|
||||||
this.clientWritePlatformService = clientWritePlatformService;
|
this.clientWritePlatformService = clientWritePlatformService;
|
||||||
commandHandlerProvider.registerHandler(this);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
|
|||||||
@@ -27,6 +27,7 @@
|
|||||||
|
|
||||||
<context:component-scan
|
<context:component-scan
|
||||||
base-package="org.mifosplatform.accounting.*,
|
base-package="org.mifosplatform.accounting.*,
|
||||||
|
org.mifosplatform.commands.provider.*,
|
||||||
org.mifosplatform.commands.handler.*,
|
org.mifosplatform.commands.handler.*,
|
||||||
org.mifosplatform.commands.service.*,
|
org.mifosplatform.commands.service.*,
|
||||||
org.mifosplatform.commands.*,
|
org.mifosplatform.commands.*,
|
||||||
|
|||||||
+14
-17
@@ -7,30 +7,35 @@ package org.mifosplatform.commands.provider;
|
|||||||
|
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
import org.mifosplatform.commands.exception.UnsupportedCommandException;
|
import org.mifosplatform.commands.exception.UnsupportedCommandException;
|
||||||
import org.mifosplatform.commands.handler.NewCommandSourceHandler;
|
import org.mifosplatform.commands.handler.NewCommandSourceHandler;
|
||||||
|
import org.mifosplatform.infrastructure.configuration.spring.TestsWithoutDatabaseAndNoJobsConfiguration;
|
||||||
import org.mifosplatform.infrastructure.core.api.JsonCommand;
|
import org.mifosplatform.infrastructure.core.api.JsonCommand;
|
||||||
import org.mifosplatform.infrastructure.core.data.CommandProcessingResult;
|
import org.mifosplatform.infrastructure.core.data.CommandProcessingResult;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.test.context.ContextConfiguration;
|
||||||
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||||
|
import org.springframework.test.context.web.WebAppConfiguration;
|
||||||
|
|
||||||
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
|
@WebAppConfiguration
|
||||||
|
@ContextConfiguration(classes = TestsWithoutDatabaseAndNoJobsConfiguration.class)
|
||||||
public class CommandHandlerProviderTest {
|
public class CommandHandlerProviderTest {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private CommandHandlerProvider commandHandlerProvider;
|
||||||
|
|
||||||
public CommandHandlerProviderTest() {
|
public CommandHandlerProviderTest() {
|
||||||
super();
|
super();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldRegisterHandler() {
|
public void shouldRegisterHandler() {
|
||||||
final CommandHandlerProvider provider = new CommandHandlerProvider();
|
|
||||||
try {
|
|
||||||
new ValidCommandHandler(provider);
|
|
||||||
} catch (NullPointerException pex) {
|
|
||||||
Assert.fail();
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
final Long testCommandId = 815L;
|
final Long testCommandId = 815L;
|
||||||
|
|
||||||
final NewCommandSourceHandler registeredHandler = provider.getHandler("HUMAN", "UPDATE");
|
final NewCommandSourceHandler registeredHandler = this.commandHandlerProvider.getHandler("HUMAN", "UPDATE");
|
||||||
|
|
||||||
final CommandProcessingResult result =
|
final CommandProcessingResult result =
|
||||||
registeredHandler.processCommand(
|
registeredHandler.processCommand(
|
||||||
@@ -41,16 +46,8 @@ public class CommandHandlerProviderTest {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected = NullPointerException.class)
|
|
||||||
public void shouldNotRegisterHandlerAndThrowNullPointerException() {
|
|
||||||
final CommandHandlerProvider provider = new CommandHandlerProvider();
|
|
||||||
new InvalidCommandHandler(provider);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test(expected = UnsupportedCommandException.class)
|
@Test(expected = UnsupportedCommandException.class)
|
||||||
public void shouldThrowUnsupportedCommandException() {
|
public void shouldThrowUnsupportedCommandException() {
|
||||||
final CommandHandlerProvider provider = new CommandHandlerProvider();
|
this.commandHandlerProvider.getHandler("WHATEVER", "DOSOMETHING");
|
||||||
provider.getHandler("WHATEVER", "DOSOMETHING");
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
-23
@@ -1,23 +0,0 @@
|
|||||||
/**
|
|
||||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
||||||
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
||||||
* You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
||||||
*/
|
|
||||||
package org.mifosplatform.commands.provider;
|
|
||||||
|
|
||||||
import org.mifosplatform.commands.handler.NewCommandSourceHandler;
|
|
||||||
import org.mifosplatform.infrastructure.core.api.JsonCommand;
|
|
||||||
import org.mifosplatform.infrastructure.core.data.CommandProcessingResult;
|
|
||||||
|
|
||||||
public class InvalidCommandHandler implements NewCommandSourceHandler {
|
|
||||||
|
|
||||||
public InvalidCommandHandler(final CommandHandlerProvider commandHandlerProvider) {
|
|
||||||
super();
|
|
||||||
commandHandlerProvider.registerHandler(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public CommandProcessingResult processCommand(JsonCommand command) {
|
|
||||||
return CommandProcessingResult.commandOnlyResult(command.commandId());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
+3
-2
@@ -9,13 +9,14 @@ import org.mifosplatform.commands.annotation.CommandType;
|
|||||||
import org.mifosplatform.commands.handler.NewCommandSourceHandler;
|
import org.mifosplatform.commands.handler.NewCommandSourceHandler;
|
||||||
import org.mifosplatform.infrastructure.core.api.JsonCommand;
|
import org.mifosplatform.infrastructure.core.api.JsonCommand;
|
||||||
import org.mifosplatform.infrastructure.core.data.CommandProcessingResult;
|
import org.mifosplatform.infrastructure.core.data.CommandProcessingResult;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
@Component
|
||||||
@CommandType(entity = "HUMAN", action = "UPDATE")
|
@CommandType(entity = "HUMAN", action = "UPDATE")
|
||||||
public class ValidCommandHandler implements NewCommandSourceHandler {
|
public class ValidCommandHandler implements NewCommandSourceHandler {
|
||||||
|
|
||||||
public ValidCommandHandler(final CommandHandlerProvider commandHandlerProvider) {
|
public ValidCommandHandler() {
|
||||||
super();
|
super();
|
||||||
commandHandlerProvider.registerHandler(this);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
Reference in New Issue
Block a user