From 06c57000c7fd3408d2974d38f7f61f4f602547ae Mon Sep 17 00:00:00 2001 From: Markus Geiss Date: Sun, 26 Apr 2015 17:08:07 +0200 Subject: [PATCH] Added autodiscovery for command handlers. --- .../provider/CommandHandlerProvider.java | 45 ++++++++++++------- .../handler/CreateClientCommandHandler.java | 4 +- .../resources/META-INF/spring/appContext.xml | 1 + .../provider/CommandHandlerProviderTest.java | 31 ++++++------- .../provider/InvalidCommandHandler.java | 23 ---------- .../provider/ValidCommandHandler.java | 5 ++- 6 files changed, 48 insertions(+), 61 deletions(-) delete mode 100644 mifosng-provider/src/test/java/org/mifosplatform/commands/provider/InvalidCommandHandler.java diff --git a/mifosng-provider/src/main/java/org/mifosplatform/commands/provider/CommandHandlerProvider.java b/mifosng-provider/src/main/java/org/mifosplatform/commands/provider/CommandHandlerProvider.java index ce6375248..850e38a81 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/commands/provider/CommandHandlerProvider.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/commands/provider/CommandHandlerProvider.java @@ -6,10 +6,14 @@ package org.mifosplatform.commands.provider; import com.google.common.base.Preconditions; +import org.apache.commons.lang3.ArrayUtils; import org.apache.commons.lang3.StringUtils; import org.mifosplatform.commands.annotation.CommandType; import org.mifosplatform.commands.exception.UnsupportedCommandException; 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.stereotype.Component; @@ -30,28 +34,17 @@ import java.util.HashMap; */ @Component @Scope("singleton") -public class CommandHandlerProvider { +public class CommandHandlerProvider implements ApplicationContextAware { - private final HashMap registeredHandlers = new HashMap<>(); + private ApplicationContext applicationContext; + private HashMap registeredHandlers; CommandHandlerProvider() { super(); } /** - * Registers a {@link NewCommandSourceHandler} using the annotation {@link CommandType} to register the handler.
- *
- * @param handler the {@link NewCommandSourceHandler} to be added, must be given - */ - public 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.
+ * Returns a handler for the given entity and action.
*
* Throws an {@link UnsupportedCommandException} if no handler * for the given entity, action combination can be found. @@ -67,6 +60,26 @@ public class CommandHandlerProvider { if (!this.registeredHandlers.containsKey(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(); } } diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/client/handler/CreateClientCommandHandler.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/client/handler/CreateClientCommandHandler.java index 17a864746..3b4526175 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/client/handler/CreateClientCommandHandler.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/client/handler/CreateClientCommandHandler.java @@ -22,10 +22,8 @@ public class CreateClientCommandHandler implements NewCommandSourceHandler { private final ClientWritePlatformService clientWritePlatformService; @Autowired - public CreateClientCommandHandler(final ClientWritePlatformService clientWritePlatformService, - final CommandHandlerProvider commandHandlerProvider) { + public CreateClientCommandHandler(final ClientWritePlatformService clientWritePlatformService) { this.clientWritePlatformService = clientWritePlatformService; - commandHandlerProvider.registerHandler(this); } @Transactional diff --git a/mifosng-provider/src/main/resources/META-INF/spring/appContext.xml b/mifosng-provider/src/main/resources/META-INF/spring/appContext.xml index 422d368b4..26e8abead 100644 --- a/mifosng-provider/src/main/resources/META-INF/spring/appContext.xml +++ b/mifosng-provider/src/main/resources/META-INF/spring/appContext.xml @@ -27,6 +27,7 @@