Example usage for org.springframework.context.support GenericXmlApplicationContext getBeansOfType

List of usage examples for org.springframework.context.support GenericXmlApplicationContext getBeansOfType

Introduction

In this page you can find the example usage for org.springframework.context.support GenericXmlApplicationContext getBeansOfType.

Prototype

@Override
    public <T> Map<String, T> getBeansOfType(@Nullable Class<T> type) throws BeansException 

Source Link

Usage

From source file:org.opentestsystem.shared.test.standalone.LoadTestPackageRunner.java

private void main_(String[] args) {
    // Bridge the JUL logging into SLF4J
    SLF4JBridgeHandler.removeHandlersForRootLogger(); // (since SLF4J 1.6.5)
    SLF4JBridgeHandler.install();//from  w w  w  .  j  ava2  s  . co  m
    MDC.put("interestingThreadId", "main");

    // Parse arguments
    Options options = new Options();
    options.addOption("h", OPTION_NAME_HELP, false, "Print this message");
    options.addOption("p", OPTION_NAME_CONTEXT_CONFIG, true,
            "URL of Spring context-configuration file that defines the test to run");
    options.addOption("c", OPTION_NAME_CONTEXT_PROPERTIES, true,
            "URL of a file defining properties for the test");
    options.addOption("d", OPTION_NAME_SPRING_PROFILES, true,
            "Names of active Spring profiles, separated by spaces");
    CommandLineParser cliParser = new GnuParser();
    CommandLine cli = null;
    try {
        cli = cliParser.parse(options, args);
    } catch (ParseException e) {
        _logger.error("Unable to parse command line parameters", e);
        System.exit(1);
    }

    if (cli.hasOption(OPTION_NAME_HELP)) {
        new HelpFormatter().printHelp("java -jar shared-test.jar", options);
        System.exit(0);
    }

    String contextConfigUrl = cli.getOptionValue(OPTION_NAME_CONTEXT_CONFIG, OPTION_DEFAULT_CONTEXT_CONFIG);
    String contextPropertiesUrl = cli.getOptionValue(OPTION_NAME_CONTEXT_PROPERTIES,
            OPTION_DEFAULT_CONTEXT_PROPERTIES);
    String springProfilesString = cli.getOptionValue(OPTION_NAME_SPRING_PROFILES,
            OPTION_DEFAULT_SPRING_PROFILES);

    // Configure the Spring context
    GenericXmlApplicationContext context = new GenericXmlApplicationContext();
    if (!StringUtils.isEmpty(springProfilesString)) {
        String[] springProfiles = springProfilesString.split(" ");
        context.getEnvironment().setActiveProfiles(springProfiles);
    }
    if (!StringUtils.isEmpty(contextPropertiesUrl)) {
        Properties p = new Properties();
        try {
            p.loadFromXML(new URL(contextPropertiesUrl).openStream());
        } catch (InvalidPropertiesFormatException e) {
            MDC.put("close", "true");
            _logger.error("Error parsing properties file {}", contextPropertiesUrl, e);
            System.exit(1);
        } catch (MalformedURLException e) {
            MDC.put("close", "true");
            _logger.error("Illegal URL for properties file {}", contextPropertiesUrl, e);
            System.exit(1);
        } catch (IOException e) {
            MDC.put("close", "true");
            _logger.error("IO error reading properties file {}", contextPropertiesUrl, e);
            System.exit(1);
        }
        context.getEnvironment().getPropertySources().addFirst(new PropertiesPropertySource("cmdline", p));
    }
    context.load(contextConfigUrl);
    context.refresh();
    setApplicationContext(context);

    // Get the lifecycle resources
    _lifecycleResources = new LifecycleResourceCombiner();
    _lifecycleResources.setApplicationContext(context);

    // Start lifecycle resources
    try {
        _lifecycleResources.startupBeforeDependencies();
        _lifecycleResources.startupAfterDependencies();
    } catch (Exception e) {
        MDC.put("close", "true");
        _logger.error("Error starting lifecycle resources", e);
        System.exit(1);
    }

    // Get the first-person users
    _users = new HashMap<>();
    for (@SuppressWarnings("rawtypes")
    Entry<String, FirstPersonInteractiveUser> entry_i : context.getBeansOfType(FirstPersonInteractiveUser.class)
            .entrySet()) {
        _users.put(entry_i.getKey(), entry_i.getValue());
    }

    // Start first-person scripts
    for (FirstPersonInteractiveUser<?, ?> user_i : _users.values()) {
        user_i.startScript();
    }

    // Wait for conclusion
    for (FirstPersonInteractiveUser<?, ?> user_i : _users.values()) {
        try {
            user_i.join();
        } catch (InterruptedException e) {
            _logger.error("Interrupted running test");
        }
    }

    // Expand any "chorus" users to get the actual users
    int i = 0;
    List<FirstPersonInteractiveUser<?, ?>> allUsers = new ArrayList<>(_users.values());
    while (i < allUsers.size()) {
        FirstPersonInteractiveUser<?, ?> user_i = allUsers.get(i);
        if (user_i instanceof Chorus) {
            allUsers.remove(i);
            for (FirstPersonInteractiveUser<?, ?> user_j : ((Chorus<?, ?>) user_i)) {
                allUsers.add(user_j);
            }
        } else {
            i++;
        }
    }

    // Log summary interaction statistics
    TimingRecordSummarizer summarizer = new TimingRecordSummarizerImpl();
    for (FirstPersonInteractiveUser<?, ?> user_i : allUsers) {
        for (InteractionContext<?> context_j : user_i.getInteractionContexts()) {
            for (InteractionTimingRecord record_k : context_j.getTimingRecordHistory()) {
                summarizer.addObservation(record_k);
            }
        }
    }
    _logger.info("Latency summary:\r\n\r\n" + summarizer.getSummaryAsString());

    // Shut down lifecycle resources
    try {
        _lifecycleResources.shutdownBeforeDependencies();
        _lifecycleResources.shutdownAfterDependencies();
    } catch (Exception e) {
        MDC.put("close", "true");
        _logger.error("Error stopping lifecycle resources", e);
        System.exit(1);
    }
    MDC.put("close", "true");
    System.exit(0);
}