/*
* Copyright 2011-2014 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 pl.com.bottega.cqrs.command.handler.spring;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;
import javax.inject.Inject;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;
import pl.com.bottega.cqrs.command.handler.CommandHandler;
import pl.com.bottega.cqrs.command.impl.RunEnvironment.HandlersProvider;
@Component
public class SpringHandlersProvider implements HandlersProvider, ApplicationListener<ContextRefreshedEvent> {
@Inject
private ConfigurableListableBeanFactory beanFactory;
private Map<Class<?>, String> handlers = new HashMap<Class<?>, String>();
@SuppressWarnings("unchecked")
@Override
public CommandHandler<Object, Object> getHandler(Object command) {
String beanName = handlers.get(command.getClass());
if (beanName == null) {
throw new RuntimeException("command handler not found. Command class is " + command.getClass());
}
CommandHandler<Object, Object> handler = beanFactory.getBean(beanName, CommandHandler.class);
return handler;
}
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
handlers.clear();
String[] commandHandlersNames = beanFactory.getBeanNamesForType(CommandHandler.class);
for (String beanName : commandHandlersNames) {
BeanDefinition commandHandler = beanFactory.getBeanDefinition(beanName);
try {
Class<?> handlerClass = Class.forName(commandHandler.getBeanClassName());
handlers.put(getHandledCommandType(handlerClass), beanName);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
}
private Class<?> getHandledCommandType(Class<?> clazz) {
Type[] genericInterfaces = clazz.getGenericInterfaces();
ParameterizedType type = findByRawType(genericInterfaces, CommandHandler.class);
return (Class<?>) type.getActualTypeArguments()[0];
}
private ParameterizedType findByRawType(Type[] genericInterfaces, Class<?> expectedRawType) {
for (Type type : genericInterfaces) {
if (type instanceof ParameterizedType) {
ParameterizedType parametrized = (ParameterizedType) type;
if (expectedRawType.equals(parametrized.getRawType())) {
return parametrized;
}
}
}
throw new RuntimeException();
}
}