Example usage for org.springframework.context ApplicationContext getAutowireCapableBeanFactory

List of usage examples for org.springframework.context ApplicationContext getAutowireCapableBeanFactory

Introduction

In this page you can find the example usage for org.springframework.context ApplicationContext getAutowireCapableBeanFactory.

Prototype

AutowireCapableBeanFactory getAutowireCapableBeanFactory() throws IllegalStateException;

Source Link

Document

Expose AutowireCapableBeanFactory functionality for this context.

Usage

From source file:org.mitre.mpf.app.ComponentRegistrationApp.java

/**
 * Register one or more components. Updates the Algorithms.xml, Actions.xml, Tasks.xml, Pipelines.xml,
 * nodeServicesPalette.json, and nodeManagerConfig.xml files.
 *
 * @param args args[0] contains the path to the cpp component list;
 *             args[1] contains the number of services per component that should be configured
 *             args[2] contains the node manager hostname to use in nodeManagerConfig.xml
 *             args[3] (optional) contains the string of user-specified components
 *//* www . ja  v  a2 s  . c om*/
public static void main(String[] args) {

    // NOTE: "-DcppComponents=<blank>" is the same as not providing the option

    if (args.length != 3 && args.length != 4) {
        System.err.println("Usage: java " + ComponentRegistrationApp.class.getSimpleName()
                + " component-list-file num-services-per-component node-manager-hostname [\"componentA,componentB,componentC,...\"]");
        System.exit(-1);
    }

    String componentListPath = args[0];
    if (!Files.exists(Paths.get(componentListPath))) {
        System.err.println("Cannot read: " + componentListPath);
        System.exit(-1);
    }

    List<String> componentPaths = null;
    try {
        componentPaths = Files.readAllLines(Paths.get(componentListPath), StandardCharsets.UTF_8);
    } catch (IOException e) {
        e.printStackTrace();
        System.exit(-1);
    }

    int numServicesPerComponent = 1;
    try {
        numServicesPerComponent = Integer.parseInt(args[1]);
    } catch (NumberFormatException e) {
        e.printStackTrace();
        System.exit(-1);
    }

    String nodeManagerHostname = args[2];

    if (args.length == 4) {
        String componentsSpecified = args[3];
        List<String> componentsSpecifiedList = Arrays.asList(componentsSpecified.split(","));

        List<String> componentsSpecifiedInFileList = componentPaths.stream()
                .map(c -> FilenameUtils.getBaseName(c)).collect(Collectors.toList());

        // sanity check
        if (!componentsSpecifiedInFileList.containsAll(componentsSpecifiedList)) {
            System.err.println(
                    "The specified components " + componentsSpecifiedList + " are not a subset of those in "
                            + componentListPath + " " + componentsSpecifiedInFileList + ".");
            System.err.println("Do a full MPF clean and build.");
            System.exit(-1);
        }

        // filter out components that were built, but should not be registered
        componentPaths = componentPaths.stream()
                .filter(c -> componentsSpecifiedList.contains(FilenameUtils.getBaseName(c)))
                .collect(Collectors.toList());
    }

    // performance optimization: load the application context once
    ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext-minimal.xml");
    AutowireCapableBeanFactory beanFactory = context.getAutowireCapableBeanFactory();

    AddComponentService addComponentService = context.getBean(AddComponentService.class);
    beanFactory.autowireBean(addComponentService);

    NodeManagerService nodeManagerService = context.getBean(NodeManagerService.class);
    beanFactory.autowireBean(nodeManagerService);

    for (String componentPath : componentPaths) {

        // TODO: Handle caffe in the same way as the other components, then remove this check.
        // TODO: Ansible should prompt the user to install each and every component in the deployment package.
        String componentName = FilenameUtils.getBaseName(componentPath);
        if (componentName.equals("caffeComponent")) {
            continue;
        }

        String descriptorPath = componentPath + "/descriptor.json";
        System.out.println("Registering: " + descriptorPath);

        try {
            // update Algorithms.xml, Actions.xml, Tasks.xml, Pipelines.xml, and nodeServicesPalette.json
            addComponentService.registerDeployedComponent(descriptorPath);
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(-1); // kill the build if anything goes wrong
        }
    }

    if (numServicesPerComponent > 0) {
        Map<String, ServiceModel> nodeServiceModels = nodeManagerService.getServiceModels();

        for (ServiceModel serviceModel : nodeServiceModels.values()) {
            serviceModel.setServiceCount(numServicesPerComponent);
        }

        NodeManagerModel nodeManagerModel = new NodeManagerModel(nodeManagerHostname);
        nodeManagerModel.setServices(new ArrayList(nodeServiceModels.values()));

        List<NodeManagerModel> nodeManagerModels = new ArrayList<NodeManagerModel>();
        nodeManagerModels.add(nodeManagerModel);

        try {
            // update nodeManagerConfig.xml
            nodeManagerService.saveNodeManagerConfig(nodeManagerModels, false); // don't reload NodeManagerStatus
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(-1); // kill the build if anything goes wrong
        }
    }
}

From source file:org.sparkcommerce.core.web.catalog.taglib.GoogleAnalyticsTag.java

@Override
public void doTag() throws JspException, IOException {
    JspWriter out = getJspContext().getOut();
    String webPropertyId = getWebPropertyId();

    if (webPropertyId == null) {
        ServletContext sc = ((PageContext) getJspContext()).getServletContext();
        ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(sc);
        context.getAutowireCapableBeanFactory().autowireBeanProperties(this,
                AutowireCapableBeanFactory.AUTOWIRE_BY_NAME, false);
    }//  w  ww .  ja  v  a2s .c o m

    if (webPropertyId.equals("UA-XXXXXXX-X")) {
        LOG.warn(
                "googleAnalytics.webPropertyId has not been overridden in a custom property file. Please set this in order to properly use the Google Analytics tag");
    }

    out.println(analytics(webPropertyId, order));
    super.doTag();
}

From source file:org.springframework.batch.core.launch.support.JobRegistryBackgroundJobRunner.java

private void run() {
    final ApplicationContext parent = new ClassPathXmlApplicationContext(parentContextPath);
    parent.getAutowireCapableBeanFactory().autowireBeanProperties(this,
            AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, false);
    parent.getAutowireCapableBeanFactory().initializeBean(this, getClass().getSimpleName());
    this.parentContext = parent;
}

From source file:org.springframework.messaging.handler.invocation.AbstractMethodMessageHandler.java

/**
 * Create a HandlerMethod instance from an Object handler that is either a handler
 * instance or a String-based bean name.
 *///from   w w  w . j a  va 2s . c  o m
protected HandlerMethod createHandlerMethod(Object handler, Method method) {
    HandlerMethod handlerMethod;
    if (handler instanceof String) {
        ApplicationContext context = getApplicationContext();
        Assert.state(context != null, "ApplicationContext is required for resolving handler bean names");
        String beanName = (String) handler;
        handlerMethod = new HandlerMethod(beanName, context.getAutowireCapableBeanFactory(), method);
    } else {
        handlerMethod = new HandlerMethod(handler, method);
    }
    return handlerMethod;
}

From source file:org.springframework.messaging.handler.invocation.reactive.AbstractMethodMessageHandler.java

/**
 * Create a HandlerMethod instance from an Object handler that is either a handler
 * instance or a String-based bean name.
 *///from   www  . j a va  2 s  .c o m
private HandlerMethod createHandlerMethod(Object handler, Method method) {
    HandlerMethod handlerMethod;
    if (handler instanceof String) {
        ApplicationContext context = getApplicationContext();
        Assert.state(context != null, "ApplicationContext is required for resolving handler bean names");
        String beanName = (String) handler;
        handlerMethod = new HandlerMethod(beanName, context.getAutowireCapableBeanFactory(), method);
    } else {
        handlerMethod = new HandlerMethod(handler, method);
    }
    return handlerMethod;
}

From source file:org.usergrid.tools.ToolBase.java

public void startSpring() {

    // copy("/testApplicationContext.xml", TMP);

    String[] locations = { "toolsApplicationContext.xml" };
    ApplicationContext ac = new ClassPathXmlApplicationContext(locations);

    AutowireCapableBeanFactory acbf = ac.getAutowireCapableBeanFactory();
    acbf.autowireBeanProperties(this, AutowireCapableBeanFactory.AUTOWIRE_BY_NAME, false);
    acbf.initializeBean(this, "testClient");

    assertNotNull(emf);/*from   w  ww.j a  v a  2  s  .  c om*/
    assertTrue("EntityManagerFactory is instance of EntityManagerFactoryImpl",
            emf instanceof EntityManagerFactoryImpl);

}

From source file:ru.runa.wfe.execution.ExecutionContext.java

protected ExecutionContext(ApplicationContext applicationContext, ProcessDefinition processDefinition,
        Token token, Map<Process, Map<String, Variable<?>>> loadedVariables,
        boolean disableVariableDaoLoading) {
    this.processDefinition = processDefinition;
    this.token = token;
    Preconditions.checkNotNull(token, "token");
    applicationContext.getAutowireCapableBeanFactory().autowireBean(this);
    if (disableVariableDaoLoading) {
        this.variableLoader = new VariableLoaderFromMap(loadedVariables);
    } else {//from w  w  w .  j  ava 2  s  .c  om
        this.variableLoader = new VariableLoaderDaoFallback(variableDao, loadedVariables);
    }
    this.baseProcessVariableLoader = new BaseProcessVariableLoader(variableLoader, getProcessDefinition(),
            getProcess());
}