Example usage for org.springframework.context ConfigurableApplicationContext getResource

List of usage examples for org.springframework.context ConfigurableApplicationContext getResource

Introduction

In this page you can find the example usage for org.springframework.context ConfigurableApplicationContext getResource.

Prototype

Resource getResource(String location);

Source Link

Document

Return a Resource handle for the specified resource location.

Usage

From source file:com.netflix.spinnaker.fiat.YamlFileApplicationContextInitializer.java

@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
    try {// w  ww.  j ava2  s.  c  o m
        Resource resource = applicationContext.getResource("classpath:application.yml");
        YamlPropertySourceLoader sourceLoader = new YamlPropertySourceLoader();
        PropertySource<?> yamlTestProperties = sourceLoader.load("yamlTestProperties", resource, null);
        applicationContext.getEnvironment().getPropertySources().addLast(yamlTestProperties);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.github.mrstampy.gameboot.GameBootDependencyWriter.java

/**
 * Write dependencies./*from   w  w  w . ja v a2s  .c om*/
 *
 * @param ctx
 *          the ctx
 * @throws IOException
 *           Signals that an I/O exception has occurred.
 */
public void writeDependencies(ConfigurableApplicationContext ctx) throws IOException {
    writeResource(ctx.getResource(DATABASE_PROPERTIES));
    writeResource(ctx.getResource(OTP_PROPERTIES));
    writeResource(ctx.getResource(GAMEBOOT_PROPERTIES));
    writeResource(ctx.getResource(SECURITY_PROPERTIES));
    writeResource(ctx.getResource(ERROR_PROPERTIES));
    writeResource(ctx.getResource(APPLICATION_PROPERTIES));
    writeResource(ctx.getResource(EHCACHE_XML));
    writeResource(ctx.getResource(GAMEBOOT_SQL_INIT));
    writeResource(ctx.getResource(LOGBACK));
}

From source file:org.wso2.msf4j.spring.property.YamlFileApplicationContextInitializer.java

@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
    Resource resource = applicationContext.getResource("classpath:" + YAML_CONFIG_FILE_NAME);
    if (!resource.exists()) {
        resource = applicationContext.getResource("file:" + YAML_CONFIG_FILE_NAME);
    }//w  ww  . jav  a  2  s  .  co m

    if (resource.exists()) {
        List<Properties> applicationYmlProperties = new ArrayList<>();
        String[] activeProfileNames = null;
        try (InputStream input = resource.getInputStream()) {
            Yaml yml = new Yaml(new SafeConstructor());
            Iterable<Object> objects = yml.loadAll(input);
            for (Object obj : objects) {
                Map<String, Object> flattenedMap = getFlattenedMap(asMap(obj));
                Properties properties = new Properties();
                properties.putAll(flattenedMap);
                Object activeProfile = properties.get("spring.profiles.active");
                if (activeProfile != null) {
                    activeProfileNames = activeProfile.toString().split(",");
                }
                applicationYmlProperties.add(properties);
            }
        } catch (FileNotFoundException e) {
            throw new RuntimeException("Couldn't find " + YAML_CONFIG_FILE_NAME, e);
        } catch (IOException e) {
            throw new RuntimeException("Error while reading " + YAML_CONFIG_FILE_NAME, e);
        }

        if (activeProfileNames == null) {
            activeProfileNames = applicationContext.getEnvironment().getActiveProfiles();
        }

        for (Properties properties : applicationYmlProperties) {
            String profile = properties.getProperty("spring.profiles");
            PropertySource<?> propertySource;
            if (profile == null) {
                propertySource = new MapPropertySource(YAML_CONFIG_FILE_NAME, new HashMap(properties));
                applicationContext.getEnvironment().getPropertySources().addLast(propertySource);
            } else if (activeProfileNames != null && ("default".equals(profile)
                    || (activeProfileNames.length == 1 && activeProfileNames[0].equals(profile)))) {
                propertySource = new MapPropertySource(YAML_CONFIG_FILE_NAME + "[" + profile + "]",
                        new HashMap(properties));
                applicationContext.getEnvironment().getPropertySources().addAfter("systemEnvironment",
                        propertySource);
            }
            activeProfileNames = applicationContext.getEnvironment().getActiveProfiles();
        }
    }
    applicationContext.getEnvironment().getActiveProfiles();
}

From source file:cf.spring.config.YamlPropertyContextInitializer.java

@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
    try {//  w  ww  .  jav  a2  s . c  o m
        final ConfigurableEnvironment environment = applicationContext.getEnvironment();
        final Resource resource = applicationContext
                .getResource(environment.getProperty(locationProperty, locationDefault));
        final YamlDocument yamlDocument;
        LOGGER.info("Loading config from: {}", resource);
        yamlDocument = YamlDocument.load(resource);
        final MutablePropertySources propertySources = environment.getPropertySources();
        final PropertySource propertySource = new YamlPropertySource(name, yamlDocument);
        if (addFirst) {
            propertySources.addFirst(propertySource);
        } else {
            propertySources.addLast(propertySource);
        }

        applicationContext.getBeanFactory().registerSingleton(name, yamlDocument);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.indeed.imhotep.web.config.PropertiesInitializer.java

protected boolean tryAddPropertySource(ConfigurableApplicationContext applicationContext,
        MutablePropertySources propSources, String filePath) {
    if (filePath == null) {
        return false;
    }/*from www.j  a  va2s. c  o  m*/
    Resource propertiesResource = applicationContext.getResource(filePath);
    if (!propertiesResource.exists()) {
        return false;
    }
    try {
        ResourcePropertySource propertySource = new ResourcePropertySource(propertiesResource);
        propSources.addFirst(propertySource);
    } catch (IOException e) {
        return false;
    }
    log.debug("Successfully added property source: " + filePath);
    return true;
}

From source file:io.pivotal.springcloud.ssl.CloudFoundryCertificateTruster.java

/**
 * import trust from truststore file/*from  w w w .  j av a2 s  .  co  m*/
 *
 * @param applicationContext
 * @param trustStore
 * @param trustStorePassword
 */
private void trustCertificatesFromStoreInternal(ConfigurableApplicationContext applicationContext,
        String trustStore, String trustStorePassword) {
    if (trustStore != null) {
        try {
            KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
            keystore.load(applicationContext.getResource(trustStore).getInputStream(),
                    trustStorePassword.toCharArray());
            Enumeration<String> aliases = keystore.aliases();

            List<X509Certificate> certCollect = new ArrayList<X509Certificate>();
            while (aliases.hasMoreElements()) {
                String alias = aliases.nextElement();

                Certificate[] certs = keystore.getCertificateChain(alias);
                if (certs != null && certs.length > 0)
                    for (Certificate cert : certs)
                        if (cert instanceof X509Certificate)
                            certCollect.add((X509Certificate) cert);

                Certificate cert = keystore.getCertificate(alias);
                if (cert != null && cert instanceof X509Certificate) {
                    certCollect.add((X509Certificate) cert);
                }
            }

            if (certCollect.size() > 0)
                sslCertificateTruster.appendToTruststoreInternal(certCollect.toArray(new X509Certificate[0]));

        } catch (Exception e) {
            log.error("trusting trustore at {}:{} failed", trustStore, trustStorePassword, e);
        }
    }
}