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

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

Introduction

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

Prototype

public AbstractXmlApplicationContext(@Nullable ApplicationContext parent) 

Source Link

Document

Create a new AbstractXmlApplicationContext with the given parent context.

Usage

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

/**
 * Springinit-method???/*from w ww.  j a  va 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.");
}