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

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

Introduction

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

Prototype

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

Source Link

Usage

From source file:com.turbospaces.api.EmbeddedJSpaceRunner.java

/**
 * launcher method//from w w w.j a v a2s.c o  m
 * 
 * @param args
 *            [1 argument = application context path]
 * @throws Exception
 *             re-throw execution errors if any
 */
public static void main(final String... args) throws Exception {
    JVMUtil.gcOnExit();
    String appContextPath = args[0];
    if (System.getProperty(Global.IPv4) == null && System.getProperty(Global.IPv6) == null)
        System.setProperty(Global.IPv4, Boolean.TRUE.toString());
    System.setProperty(Global.CUSTOM_LOG_FACTORY, JGroupsCustomLoggerFactory.class.getName());

    LOGGER.info("Welcome to turbospaces:version = {}, build date = {}", SpaceUtility.projectVersion(),
            SpaceUtility.projecBuildTimestamp());
    LOGGER.info("{}: launching configuration {}", EmbeddedJSpaceRunner.class.getSimpleName(), appContextPath);
    AbstractXmlApplicationContext c = appContextPath.startsWith("file")
            ? new FileSystemXmlApplicationContext(appContextPath)
            : new ClassPathXmlApplicationContext(appContextPath);
    c.getBeansOfType(JSpace.class);
    c.registerShutdownHook();
    Collection<SpaceConfiguration> configurations = c.getBeansOfType(SpaceConfiguration.class).values();
    for (SpaceConfiguration spaceConfiguration : configurations)
        spaceConfiguration.joinNetwork();
    LOGGER.info("all jspaces joined network, notifying waiting threads...");
    synchronized (joinNetworkMonitor) {
        joinNetworkMonitor.notifyAll();
    }

    while (!Thread.currentThread().isInterrupted())
        synchronized (c) {
            try {
                c.wait(TimeUnit.SECONDS.toMillis(1));
            } catch (InterruptedException e) {
                LOGGER.info("got interruption signal, terminating jspaces... stack_trace = {}",
                        Throwables.getStackTraceAsString(e));
                Thread.currentThread().interrupt();
                Util.printThreads();
            }
        }

    c.destroy();
}

From source file:com.clican.pluto.dataprocess.engine.impl.ProcessorContainerImpl.java

/**
 * Springinit-method???//w ww . j  a v  a 2 s.  c o m
 */
@SuppressWarnings("unchecked")
public void start() {
    if (log.isInfoEnabled()) {
        log.info("Begin to start Process Data Container");
    }
    for (String scan : scanList) {
        scan = scan.trim();
        try {
            String pattern1;
            String pattern2;
            if (scan.startsWith("file")) {
                pattern1 = scan + "/**/*.xml";
                pattern2 = scan + "/**/*.properties";
            } else {
                pattern1 = ClassUtils.convertClassNameToResourcePath(scan) + "/**/*.xml";
                pattern2 = ClassUtils.convertClassNameToResourcePath(scan) + "/**/*.properties";
            }
            Resource[] resources1 = resourcePatternResolver.getResources(pattern1);
            Resource[] resources2 = resourcePatternResolver.getResources(pattern2);
            Map<String, Resource> map1 = new HashMap<String, Resource>();
            Map<String, Resource> map2 = new HashMap<String, Resource>();
            Set<String> usingPropertyResource = new HashSet<String>();
            for (Resource resource : resources1) {
                map1.put(resource.getFilename().split("\\.")[0], resource);
            }
            for (Resource resource : resources2) {
                String fileName = resource.getFilename().split("\\.")[0];
                String[] fn = fileName.split("\\_");
                if (fn.length == 2 && map1.containsKey(fn[0])) {
                    usingPropertyResource.add(fn[0]);
                } else {
                    throw new RuntimeException("properties[" + fileName + "]???");
                }
                map2.put(resource.getFilename().split("\\.")[0], resource);
            }
            for (Resource resource1 : resources1) {
                final Resource xmlRes = resource1;

                String fileName1 = resource1.getFilename().split("\\.")[0];
                if (usingPropertyResource.contains(fileName1)) {
                    for (Resource resource2 : resources2) {
                        AbstractXmlApplicationContext subContext = new AbstractXmlApplicationContext(
                                this.applicationContext) {
                            protected Resource[] getConfigResources() {
                                return new Resource[] { xmlRes };
                            }
                        };
                        String fileName2 = resource2.getFilename().split("\\.")[0];
                        String[] fn = fileName2.split("\\_");
                        if (fn[0].equals(fileName1)) {
                            com.clican.pluto.common.support.spring.PropertyPlaceholderConfigurer parentConf = (com.clican.pluto.common.support.spring.PropertyPlaceholderConfigurer) applicationContext
                                    .getBean("propertyConfigurer");
                            Resource[] resources = new Resource[1 + parentConf.getLocations().length];
                            for (int i = 0; i < parentConf.getLocations().length; i++) {
                                resources[i] = parentConf.getLocations()[i];
                            }
                            resources[resources.length - 1] = resource2;
                            com.clican.pluto.common.support.spring.PropertyPlaceholderConfigurer subContainerConf = new com.clican.pluto.common.support.spring.PropertyPlaceholderConfigurer();
                            subContainerConf.setFileEncoding("utf-8");
                            subContainerConf.setLocations(resources);
                            subContext.addBeanFactoryPostProcessor(subContainerConf);
                            subContext.refresh();
                            processorGroupSpringMap.put(fn[1], subContext);
                            Collection<String> coll = (Collection<String>) subContext
                                    .getBeansOfType(BaseDataProcessor.class).keySet();
                            for (String beanName : coll) {
                                BaseDataProcessor bean = (BaseDataProcessor) subContext.getBean(beanName);
                                if (bean.isStartProcessor()) {
                                    startProcessorNameMap.put(fn[1], beanName);
                                    break;
                                }
                            }
                            if (!startProcessorNameMap.containsKey(fn[1])) {
                                throw new RuntimeException(
                                        "?Process Container," + fn[1] + "?");
                            }
                        }
                    }
                } else {
                    AbstractXmlApplicationContext subContext = new AbstractXmlApplicationContext(
                            this.applicationContext) {
                        protected Resource[] getConfigResources() {
                            return new Resource[] { xmlRes };
                        }
                    };
                    subContext.addBeanFactoryPostProcessor(
                            (BeanFactoryPostProcessor) applicationContext.getBean("propertyConfigurer"));
                    subContext.refresh();
                    processorGroupSpringMap.put(fileName1, subContext);
                    Collection<String> coll = (Collection<String>) subContext
                            .getBeansOfType(BaseDataProcessor.class).keySet();
                    for (String beanName : coll) {
                        BaseDataProcessor bean = (BaseDataProcessor) subContext.getBean(beanName);
                        if (bean.isStartProcessor()) {
                            startProcessorNameMap.put(fileName1, beanName);
                            break;
                        }
                    }
                    if (!startProcessorNameMap.containsKey(fileName1)) {
                        throw new RuntimeException(
                                "?Process Container," + fileName1 + "?");
                    }
                }

            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    for (Deploy deploy : deployList) {
        try {
            ClassPathXmlApplicationContext subContext = new ClassPathXmlApplicationContext(
                    new String[] { deploy.getUrl() }, this.applicationContext);
            if (StringUtils.isNotEmpty(deploy.getPropertyResources())) {
                com.clican.pluto.common.support.spring.PropertyPlaceholderConfigurer parentConf = (com.clican.pluto.common.support.spring.PropertyPlaceholderConfigurer) applicationContext
                        .getBean("propertyConfigurer");

                String[] propertyResources = deploy.getPropertyResources().split(",");
                Resource[] resources = new Resource[propertyResources.length
                        + parentConf.getLocations().length];
                for (int i = 0; i < parentConf.getLocations().length; i++) {
                    resources[i] = parentConf.getLocations()[i];
                }
                for (int i = parentConf.getLocations().length; i < resources.length; i++) {
                    String propertyResource = propertyResources[i - parentConf.getLocations().length];
                    if (propertyResource.startsWith("classpath")) {
                        resources[i] = new ClassPathResource(
                                propertyResource.substring(propertyResource.indexOf(":") + 1));
                    } else {
                        resources[i] = new FileSystemResource(
                                propertyResource.substring(propertyResource.indexOf(":") + 1));
                    }

                }

                com.clican.pluto.common.support.spring.PropertyPlaceholderConfigurer subContainerConf = new com.clican.pluto.common.support.spring.PropertyPlaceholderConfigurer();
                subContainerConf.setFileEncoding("utf-8");
                subContainerConf.setLocations(resources);
                subContext.addBeanFactoryPostProcessor(subContainerConf);
            } else {
                subContext.addBeanFactoryPostProcessor(
                        (BeanFactoryPostProcessor) applicationContext.getBean("propertyConfigurer"));
            }
            subContext.refresh();
            processorGroupSpringMap.put(deploy.getName(), subContext);
            Collection<String> coll = (Collection<String>) subContext.getBeansOfType(BaseDataProcessor.class)
                    .keySet();
            for (String beanName : coll) {
                BaseDataProcessor bean = (BaseDataProcessor) subContext.getBean(beanName);
                if (bean.isStartProcessor()) {
                    startProcessorNameMap.put(deploy.getName(), beanName);
                    break;
                }
            }
            if (!startProcessorNameMap.containsKey(deploy.getName())) {
                throw new RuntimeException(
                        "?Process Container," + deploy.getName() + "?");
            }
        } catch (Exception e) {
            log.error("Depoly [" + deploy.getName() + "] failure", e);
            throw new RuntimeException(e);
        }
    }

    log.info("The Process Data Container has been started successfully.");
}