com.tobedevoured.command.spring.SpringDependencyResolver Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of command-spring Show documentation
Show all versions of command-spring Show documentation
Make Java do your bidding by turning any code into an executable
The newest version!
package com.tobedevoured.command.spring;
import com.tobedevoured.command.*;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.*;
public class SpringDependencyResolver extends DefaultDependencyResolver {
ApplicationContext springContext;
Map beanLookUp = new HashMap();
public void init(Map commandsToRun) {
Set contexts = new HashSet();
for ( String command : commandsToRun.keySet() ) {
SpringCommandDependency dep = (SpringCommandDependency)manager.getCommands().get(command);
if (dep != null) {
contexts.addAll( dep.getContexts() );
if (StringUtils.isNotBlank( dep.getBeanName() ) ) {
beanLookUp.put( dep.getTarget(), dep.getBeanName() );
}
}
}
loadSpringContext(contexts);
}
public void loadSpringContext(Set contexts) {
springContext = new ClassPathXmlApplicationContext(contexts.toArray(new String[contexts.size()]));
}
public T getInstance(Class clazz) throws CommandException {
String beanName = beanLookUp.get(clazz);
try {
if ( StringUtils.isNotBlank(beanName) ) {
return (T)springContext.getBean(beanName);
} else {
return springContext.getBean(clazz);
}
} catch( NoSuchBeanDefinitionException e ) {
// Class not directly registered with Spring, manually construct it
return super.getInstance(clazz);
}
}
}