001package org.dllearner.configuration.spring; 002 003import java.io.IOException; 004import java.net.URISyntaxException; 005import java.util.ArrayList; 006import java.util.List; 007 008import org.dllearner.configuration.IConfiguration; 009import org.slf4j.Logger; 010import org.slf4j.LoggerFactory; 011import org.springframework.beans.factory.BeanCreationException; 012import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor; 013import org.springframework.context.ApplicationContext; 014import org.springframework.context.ConfigurableApplicationContext; 015import org.springframework.context.support.ClassPathXmlApplicationContext; 016import org.springframework.core.io.ClassPathResource; 017import org.springframework.core.io.Resource; 018 019/** 020 * Created by IntelliJ IDEA. 021 * User: Chris 022 * Date: 8/23/11 023 * Time: 4:59 AM 024 * 025 * Default implementation of the ApplicationContextBuilder 026 */ 027public class DefaultApplicationContextBuilder implements ApplicationContextBuilder{ 028 029 private static Logger logger = LoggerFactory.getLogger(DefaultApplicationContextBuilder.class); 030 031 @Override 032 public ApplicationContext buildApplicationContext(IConfiguration configuration, List<Resource> springConfigurationLocations) throws IOException{ 033 ConfigurableApplicationContext context = null; 034 // Post Processors 035 BeanDefinitionRegistryPostProcessor beanDefinitionRegistryPostProcessor = new ConfigurationBasedBeanDefinitionRegistryPostProcessor(configuration); 036 037 //These files need to be loaded first 038 List<Resource> allSpringConfigFiles = new ArrayList<>(); 039 allSpringConfigFiles.add(new ClassPathResource("/org/dllearner/configuration/spring/bean-post-processor-configuration.xml")); 040 allSpringConfigFiles.addAll(springConfigurationLocations); 041 042 String[] springConfigurationFiles = new String[allSpringConfigFiles.size()]; 043 int ctr = 0; 044 for (Resource springConfigurationLocation : allSpringConfigFiles) { 045// springConfigurationFiles[ctr] = springConfigurationLocation.getFile().toURI().toString();//this works not if packaged as jar file 046 try { 047 springConfigurationFiles[ctr] = springConfigurationLocation.getURL().toURI().toString(); 048 } catch (URISyntaxException e) { 049 e.printStackTrace(); 050 } 051 ctr++; 052 } 053 context = new ClassPathXmlApplicationContext(springConfigurationFiles, false); 054 055 // These post processors run before object instantiation 056 context.addBeanFactoryPostProcessor(beanDefinitionRegistryPostProcessor); 057 058 //Instantiate and initialize the beans. 059 try { 060 context.refresh(); 061 } catch (BeanCreationException e) { 062 throw new RuntimeException(e); 063 } catch (Exception e) { 064 logger.error("There was a problem initializing the components...shutting down."); 065 throw new RuntimeException(e); 066 } 067 return context; 068 } 069}