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

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

Introduction

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

Prototype

@Override
    public void addBeanFactoryPostProcessor(BeanFactoryPostProcessor postProcessor) 

Source Link

Usage

From source file:il.ac.tau.yoavram.pes.SpringRunner.java

public static void run(String[] args) throws IOException {
    System.out.println("Starting " + SpringRunner.class.getSimpleName());
    SimulationConfigurer configurer = new SimulationConfigurer(args);
    if (configurer.getSpringXmlConfig() == null) {
        System.err.println("Spring XML config file not defined");
        System.err.println();//w  w w.  j  ava2 s.co  m
        System.exit(1);
    }
    if (configurer.getProperties() == null) {
        System.err.println("Properties not defined");
        System.err.println();
        System.exit(1);
    }

    // get the properties
    Properties properties = configurer.getProperties();
    String jobName = properties.getProperty(SimulationConfigurer.JOB_NAME_KEY);

    // create context
    AbstractXmlApplicationContext context = new ClassPathXmlApplicationContext();

    // add properties to context
    logger.info("Adding properties to context: " + properties.toString());
    PropertyPlaceholderConfigurer propertyPlaceholderConfigurer = new PropertyPlaceholderConfigurer();
    propertyPlaceholderConfigurer.setProperties(properties);
    context.addBeanFactoryPostProcessor(propertyPlaceholderConfigurer);

    // set config location
    String configLocation = configurer.getSpringXmlConfig().toString();
    logger.info("Loading context from file " + configLocation);
    context.setConfigLocation(configLocation);

    // make sure destroy methods will be called and refresh the context
    context.registerShutdownHook();
    context.refresh();

    // persist properties
    try {
        PropertiesPersister persister = context.getBean(PropertiesPersister.class);
        if (persister != null) {
            persister.persist(properties);
        }
    } catch (NoSuchBeanDefinitionException e) {
        // nothing to do
    }

    // get the simulation bean and run it
    Simulation simulation = context.getBean("simulation", Simulation.class);

    logger.debug("Starting simulation " + jobName);
    simulation.start();
}

From source file:de.hoegertn.demo.cxfsimple.SpringStarter.java

public final void doStart() throws Exception {
    try {//w  w  w.j av a2  s. c o  m
        this.doBeforeSpringStart();
    } catch (Exception e) {
        throw new RuntimeException("Before spring failed", e);
    }

    Lock writeLock = this.rwLock.writeLock();
    AbstractXmlApplicationContext ctx = null;
    try {
        writeLock.lock();
        if (this.context.get() != null) {
            throw new RuntimeException("Already started");
        }
        ctx = this.createSpringContext();

        final PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
        configurer.setProperties(System.getProperties());
        ctx.addBeanFactoryPostProcessor(configurer);

        ctx.setConfigLocation(this.getSpringResource());
        ctx.refresh();
    } catch (Exception e) {
        if (ctx != null) {
            try {
                ctx.close();
            } catch (Exception e1) {
                this.logger.warn("Failed to close context", e1);
            }
            ctx = null;
        }
        throw new RuntimeException("Spring context failed", e);
    } finally {
        if (ctx != null) {
            this.context.set(ctx);
        }
        writeLock.unlock();
    }

    try {
        this.doAfterSpringStart();
    } catch (Exception e) {
        throw new RuntimeException("After spring failed", e);
    }
}

From source file:de.taimos.daemon.spring.SpringDaemonAdapter.java

@Override
public final void doStart() throws Exception {
    super.doStart();
    try {//from  w  ww . ja  v  a2 s. c om
        this.doBeforeSpringStart();
    } catch (Exception e) {
        throw new RuntimeException("Before spring failed", e);
    }

    Lock writeLock = this.rwLock.writeLock();
    AbstractXmlApplicationContext ctx = null;
    try {
        writeLock.lock();
        if (this.context.get() != null) {
            throw new RuntimeException("Already started");
        }
        ctx = this.createSpringContext();
        String[] profiles = System.getProperty(Configuration.PROFILES, Configuration.PROFILES_PRODUCTION)
                .split(",");
        ctx.getEnvironment().setActiveProfiles(profiles);

        final PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
        configurer.setProperties(DaemonStarter.getDaemonProperties());
        ctx.addBeanFactoryPostProcessor(configurer);

        ctx.setConfigLocation(this.getSpringResource());
        ctx.refresh();
    } catch (Exception e) {
        if (ctx != null) {
            try {
                ctx.close();
            } catch (Exception e1) {
                this.logger.warn("Failed to close context", e1);
            }
            ctx = null;
        }
        throw new RuntimeException("Spring context failed", e);
    } finally {
        if (ctx != null) {
            this.context.set(ctx);
        }
        writeLock.unlock();
    }

    try {
        this.doAfterSpringStart();
    } catch (Exception e) {
        throw new RuntimeException("After spring failed", e);
    }
}

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

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