All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.tobedevoured.command.spring.SpringDependencyResolver Maven / Gradle / Ivy

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);
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy