Example usage for org.springframework.context.support GenericApplicationContext refresh

List of usage examples for org.springframework.context.support GenericApplicationContext refresh

Introduction

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

Prototype

@Override
    public void refresh() throws BeansException, IllegalStateException 

Source Link

Usage

From source file:org.wso2.carbon.springservices.GenericApplicationContextUtil.java

public static GenericApplicationContext getSpringApplicationContext(InputStream applicationContxtInStream,
        String springBeansFilePath) throws AxisFault {
    GenericApplicationContext appContext = new GenericApplicationContext();

    XmlBeanDefinitionReader xbdr = new XmlBeanDefinitionReader(appContext);
    xbdr.setValidating(false);//  ww  w .  j  a  v a2 s .co m
    xbdr.loadBeanDefinitions(new InputStreamResource(applicationContxtInStream));
    appContext.refresh();
    return appContext;
}

From source file:org.yardstickframework.gridgain.GridGainNode.java

/**
 * @param springCfgPath Spring configuration file path.
 * @return Grid configuration.//from  w ww . j a va  2s.  co m
 * @throws Exception If failed.
 */
private static GridConfiguration loadConfiguration(String springCfgPath) throws Exception {
    URL url;

    try {
        url = new URL(springCfgPath);
    } catch (MalformedURLException e) {
        url = GridUtils.resolveGridGainUrl(springCfgPath);

        if (url == null)
            throw new GridException("Spring XML configuration path is invalid: " + springCfgPath
                    + ". Note that this path should be either absolute or a relative local file system path, "
                    + "relative to META-INF in classpath or valid URL to GRIDGAIN_HOME.", e);
    }

    GenericApplicationContext springCtx;

    try {
        springCtx = new GenericApplicationContext();

        new XmlBeanDefinitionReader(springCtx).loadBeanDefinitions(new UrlResource(url));

        springCtx.refresh();
    } catch (BeansException e) {
        throw new Exception("Failed to instantiate Spring XML application context [springUrl=" + url + ", err="
                + e.getMessage() + ']', e);
    }

    Map<String, GridConfiguration> cfgMap;

    try {
        cfgMap = springCtx.getBeansOfType(GridConfiguration.class);
    } catch (BeansException e) {
        throw new Exception(
                "Failed to instantiate bean [type=" + GridConfiguration.class + ", err=" + e.getMessage() + ']',
                e);
    }

    if (cfgMap == null || cfgMap.isEmpty())
        throw new Exception("Failed to find grid configuration in: " + url);

    return cfgMap.values().iterator().next();
}

From source file:exercise.cca.data.cli.main.MainCli.java

protected static ConfigurableApplicationContext loadContext(final String contextPath) {
    final GenericApplicationContext ctx = new GenericApplicationContext();
    final XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(ctx);
    xmlReader.loadBeanDefinitions(new ClassPathResource(contextPath));
    final PropertyPlaceholderConfigurer placeholderProcessor = new PropertyPlaceholderConfigurer();
    ctx.addBeanFactoryPostProcessor(placeholderProcessor);
    ctx.refresh();
    return ctx;/*from ww w .j  a  v a2s . c  o  m*/
}

From source file:com.brienwheeler.lib.spring.beans.PropertyPlaceholderConfigurer.java

/**
 * Programatically process a single property file location, using System
 * properties in preference to included definitions for placeholder
 * resolution, and setting any resolved properties as System properties, if
 * no property by that name already exists.
 * //from   w w  w  . j a va2 s. c o m
 * @param locationName String-encoded resource location for the properties file to process
 * @param placeholderPrefix the placeholder prefix String
 * @param placeholderSuffix the placeholder suffix String
 */
public static void processLocation(String locationName, String placeholderPrefix, String placeholderSuffix) {
    ResourceEditor resourceEditor = new ResourceEditor();
    resourceEditor.setAsText(locationName);

    PropertyPlaceholderConfigurer configurer = new PropertyPlaceholderConfigurer();
    configurer.setLocation((Resource) resourceEditor.getValue());
    if (placeholderPrefix != null)
        configurer.setPlaceholderPrefix(placeholderPrefix);
    if (placeholderSuffix != null)
        configurer.setPlaceholderSuffix(placeholderSuffix);
    configurer.quiet = true;

    GenericApplicationContext context = new GenericApplicationContext();
    context.getBeanFactory().registerSingleton("propertyPlaceholderConfigurer", configurer);
    context.refresh();
    context.close();
}

From source file:com.opengamma.language.connector.LanguageSpringContext.java

/**
 * Creates a Spring context from the base configuration file in OG-Language and any other Spring XML configuration
 * files found in the configuration directory.  The directory must be specified using the system property named
 * {@link #LANGUAGE_EXT_PATH}.// w ww  .j av a2  s  . c o  m
 * @return A Spring context built from all the XML config files.
 */
public static GenericApplicationContext createSpringContext() {
    s_logger.info("Starting OpenGamma language integration service");
    GenericApplicationContext context = new GenericApplicationContext();
    final XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(context);
    xmlReader.loadBeanDefinitions(new ClassPathResource(CLIENT_XML));
    String[] xmlFiles = findSpringXmlConfig();
    xmlReader.loadBeanDefinitions(xmlFiles);
    s_logger.info("Creating context beans");
    context.refresh();
    s_logger.info("Starting application context");
    context.start();
    s_logger.info("Application context started");
    return context;
}

From source file:edu.internet2.middleware.shibboleth.common.attribute.AttributeAuthorityCLI.java

/**
 * Loads the configuration files into a Spring application context.
 * /*from w ww.j  av a  2  s  .co m*/
 * @param configDir directory containing spring configuration files
 * @param springExts colon-separated list of spring extension files 
 * 
 * @return loaded application context
 * 
 * @throws java.io.IOException throw if there is an error loading the configuration files
 * @throws ResourceException if there is an error loading the configuration files
 */
private static ApplicationContext loadConfigurations(String configDir, String springExts)
        throws IOException, ResourceException {
    File configDirectory;

    if (configDir != null) {
        configDirectory = new File(configDir);
    } else {
        configDirectory = new File(System.getenv("IDP_HOME") + "/conf");
    }

    if (!configDirectory.exists() || !configDirectory.isDirectory() || !configDirectory.canRead()) {
        errorAndExit("Configuration directory " + configDir
                + " does not exist, is not a directory, or is not readable", null);
    }

    loadLoggingConfiguration(configDirectory.getAbsolutePath());

    File config;
    List<String> configFiles = new ArrayList<String>();
    List<Resource> configs = new ArrayList<Resource>();

    // Add built-in files.
    for (String i : aacliConfigs) {
        configFiles.add(i);
    }

    // Add extensions, if any.
    if (springExts != null && !springExts.isEmpty()) {
        String[] extFiles = springExts.split(":");
        for (String extFile : extFiles) {
            configFiles.add(extFile);
        }
    }

    for (String cfile : configFiles) {
        config = new File(configDirectory.getPath() + File.separator + cfile);
        if (config.isDirectory() || !config.canRead()) {
            errorAndExit(
                    "Configuration file " + config.getAbsolutePath() + " is a directory or is not readable",
                    null);
        }
        configs.add(new FilesystemResource(config.getPath()));
    }

    GenericApplicationContext gContext = new GenericApplicationContext();
    SpringConfigurationUtils.populateRegistry(gContext, configs);
    gContext.refresh();
    return gContext;
}

From source file:edu.berkeley.compbio.ncbitaxonomy.NcbiTaxonomyDbContextFactory.java

public static ApplicationContext makeNcbiTaxonomyDbContext() //String dbName)
        throws IOException

{
    /*/*  ww  w . j  a  va2 s .  c om*/
    File propsFile = PropertiesUtils
    .findPropertiesFile("NCBI_TAXONOMY_PROPERTIES", ".ncbitaxonomy", "ncbi_taxonomy.properties");
    EnvironmentUtils.init(propsFile);
            
    logger.debug("Using properties file: " + propsFile);
    Properties p = new Properties();
    FileInputStream is = null;
    try
       {
       is = new FileInputStream(propsFile);
       p.load(is);
       }
    finally
       {
       is.close();
       }
    //String dbName = (String) p.get("default");
            
    Map<String, Properties> databases = PropertiesUtils.splitPeriodDelimitedProperties(p);
            
    */
    GenericApplicationContext ctx = new GenericApplicationContext();
    XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(ctx);
    //         xmlReader.loadBeanDefinitions(new ClassPathResource("springjpautils.xml"));
    xmlReader.loadBeanDefinitions(new ClassPathResource("ncbitaxonomy.xml"));

    //PropertyPlaceholderConfigurer cfg = new PropertyPlaceholderConfigurer();
    //cfg.setProperties(databases.get(dbName));
    //ctx.addBeanFactoryPostProcessor(cfg);

    ctx.refresh();

    // add a shutdown hook for the above context...
    ctx.registerShutdownHook();

    return ctx;
}

From source file:edu.internet2.middleware.psp.util.PSPUtil.java

public static GenericApplicationContext createSpringContext(List<Resource> resources) throws ResourceException {

    GenericApplicationContext gContext = new GenericApplicationContext();
    SpringConfigurationUtils.populateRegistry(gContext, resources);
    gContext.refresh();
    gContext.registerShutdownHook();/*  w w  w  .  j  ava2  s  .c  om*/

    return gContext;
}

From source file:com.opengamma.engine.calcnode.CalculationNodeProcess.java

private static boolean startContext(final String configuration) {
    try {//from  w w w .  j a va2 s .com
        final GenericApplicationContext context = new GenericApplicationContext();
        final XmlBeanDefinitionReader beanReader = new XmlBeanDefinitionReader(context);
        beanReader.setValidationMode(XmlBeanDefinitionReader.VALIDATION_NONE);
        s_logger.debug("Loading configuration");
        beanReader.loadBeanDefinitions(new InputSource(new StringReader(configuration)));
        s_logger.debug("Instantiating beans");
        context.refresh();
        s_logger.debug("Starting node");
        context.start();
        return true;
    } catch (RuntimeException e) {
        s_logger.warn("Spring initialisation error", e);
        return false;
    }
}

From source file:com.github.yihtserns.logback.spring.config.LogbackNamespaceHandlerTest.java

private static GenericApplicationContext newApplicationContextFor(String xml) {
    GenericApplicationContext applicationContext = new GenericApplicationContext();
    XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(applicationContext);
    xmlReader.setValidationMode(XmlBeanDefinitionReader.VALIDATION_XSD);
    xmlReader.loadBeanDefinitions(new InputSource(new StringReader(xml)));

    applicationContext.refresh();
    applicationContext.start();//from w w w.  j a  v a 2s.co  m

    return applicationContext;
}