Example usage for org.springframework.core.env PropertiesPropertySource PropertiesPropertySource

List of usage examples for org.springframework.core.env PropertiesPropertySource PropertiesPropertySource

Introduction

In this page you can find the example usage for org.springframework.core.env PropertiesPropertySource PropertiesPropertySource.

Prototype

protected PropertiesPropertySource(String name, Map<String, Object> source) 

Source Link

Usage

From source file:org.alfresco.bm.user.UserDataServiceTest.java

@Before
public void setUp() {
    Properties props = new Properties();
    props.put("mongoCollection", COLLECTION_BM_USER_DATA_SERVICE);

    ctx = new ClassPathXmlApplicationContext(new String[] { "test-MongoUserDataTest-context.xml" }, false);
    ctx.getEnvironment().getPropertySources().addFirst(new PropertiesPropertySource("TestProps", props));
    ctx.refresh();//from ww w  . j a  v a2 s. co m
    ctx.start();
    userDataService = ctx.getBean(UserDataService.class);

    // Generate some random users
    for (int i = 0; i < USERS.length; i++) {
        String username = USERS[i];
        UserData user = createUserData(username);
        userDataService.createNewUser(user);
    }
}

From source file:it.scoppelletti.programmerpower.spring.config.BeanConfigUtils.java

/**
 * Restituisce la sorgente per la sostituzione dei riferimenti alle
 * propriet&agrave; nelle risorse di definizione dei bean.
 * /*  w  w  w.j ava  2s .c om*/
 * @return Collezione. Se la sorgente delle propriet&agrave; non &egrave;
 *         rilevata, restituisce {@code null}.
 * @see    #CONFIG_FILE
 * @see    it.scoppelletti.programmerpower.console.spring.ConsoleApplicationRunner
 * @see    it.scoppelletti.programmerpower.web.spring.config.WebApplicationContextInitializer
 * @see    <A HREF="{@docRoot}/../reference/setup/envprops.html"
 *         TARGET="_top">Propriet&agrave; di ambiente</A>
 */
public static PropertySource<?> loadPropertySource() {
    InputStream in = null;
    Properties props;

    in = ReflectionUtils.getResourceAsStream(BeanConfigUtils.CONFIG_FILE);
    if (in == null) {
        return null;
    }

    props = new Properties();
    try {
        props.load(in);
    } catch (IOException ex) {
        myLogger.error(String.format("Failed to load %1$s.", BeanConfigUtils.CONFIG_FILE), ex);
        return null;
    } finally {
        if (in != null) {
            IOUtils.close(in);
            in = null;
        }
    }

    return new PropertiesPropertySource(BeanConfigUtils.CONFIG_FILE, props);
}

From source file:org.alfresco.bm.file.FileDataServiceTest.java

@BeforeClass
public static void setUp() throws IOException {
    // Create a test file
    File tempDir = new File(System.getProperty("java.io.tmpdir"));
    File testFiles = new File(tempDir, "FileDataServiceTest");
    testFiles.mkdirs();//from   w  ww  . j ava  2 s  .  co m
    File testFile = new File(testFiles, "test.txt");
    if (!testFile.exists()) {
        String text = "SOME TEXT";
        Files.write(text, testFile, Charsets.UTF_8);
    }

    File localDir = new File(System.getProperty("java.io.tmpdir") + "/" + "fileset-123");
    if (localDir.exists()) {
        localDir.delete();
    }

    Properties props = new Properties();
    props.put("test.mongoCollection", COLLECTION_BM_FILE_DATA_SERVICE);
    props.put("test.localDir", localDir.getCanonicalFile());
    props.put("test.ftpHost", "ftp.mirrorservice.org");
    props.put("test.ftpPort", "21");
    props.put("test.ftpUsername", "anonymous");
    props.put("test.ftpPassword", "");
    props.put("test.ftpPath", "/sites/www.linuxfromscratch.org/images");
    props.put("test.testFileDir", testFiles.getCanonicalPath());

    ctx = new ClassPathXmlApplicationContext(new String[] { "test-MongoFileDataTest-context.xml" }, false);
    ctx.getEnvironment().getPropertySources().addFirst(new PropertiesPropertySource("TestProps", props));
    ctx.refresh();
    ctx.start();

    // Get the new beans
    fileDataService = ctx.getBean(FileDataService.class);
    ftpTestFileService = ctx.getBean(FtpTestFileService.class);
    localTestFileService = ctx.getBean(LocalTestFileService.class);

    // Do a directory listing and use that as the dataset
    File randomDir = new File(System.getProperty("user.dir"));
    File[] localFiles = randomDir.listFiles(new FileFilter() {
        @Override
        public boolean accept(File pathname) {
            return !pathname.isDirectory();
        }
    });
    fileDatas = new FileData[localFiles.length];
    for (int i = 0; i < localFiles.length; i++) {
        String remoteName = localFiles[i].getName();
        if (remoteName.length() == 0) {
            continue;
        }
        fileDatas[i] = FileDataServiceTest.createFileData(remoteName);
    }
}

From source file:org.zalando.crypto.spring.boot.EncryptedPropertySourceLoader.java

@Override
public PropertySource<?> load(final String name, final Resource resource, final String profile)
        throws IOException {
    validateDecrypter();/*w ww . j a  v a 2 s .  c om*/
    if (profile == null) {
        Properties properties = PropertiesLoaderUtils.loadProperties(resource);
        if (!properties.isEmpty()) {
            return new PropertiesPropertySource(name,
                    new EncryptableProperties(properties, decrypter, getPrefix()));
        }
    }

    return null;
}

From source file:org.alfresco.bm.web.WebApp.java

@Override
public void onStartup(ServletContext container) {
    // Grab the server capabilities, otherwise just use the java version
    String javaVersion = System.getProperty("java.version");
    String systemCapabilities = System.getProperty(PROP_SYSTEM_CAPABILITIES, javaVersion);

    String appDir = new File(".").getAbsolutePath();
    String appContext = container.getContextPath();
    String appName = container.getContextPath().replace(SEPARATOR, "");

    // Create an application context (don't start, yet)
    XmlWebApplicationContext ctx = new XmlWebApplicationContext();
    ctx.setConfigLocations(new String[] { "classpath:config/spring/app-context.xml" });

    // Pass our properties to the new context
    Properties ctxProperties = new Properties();
    {// ww  w  .j ava 2s . c  o  m
        ctxProperties.put(PROP_SYSTEM_CAPABILITIES, systemCapabilities);
        ctxProperties.put(PROP_APP_CONTEXT_PATH, appContext);
        ctxProperties.put(PROP_APP_DIR, appDir);
    }
    ConfigurableEnvironment ctxEnv = ctx.getEnvironment();
    ctxEnv.getPropertySources().addFirst(new PropertiesPropertySource(appName, ctxProperties));
    // Override all properties with system properties
    ctxEnv.getPropertySources().addFirst(new PropertiesPropertySource("system", System.getProperties()));
    // Bind to shutdown
    ctx.registerShutdownHook();

    ContextLoaderListener ctxLoaderListener = new ContextLoaderListener(ctx);
    container.addListener(ctxLoaderListener);

    ServletRegistration.Dynamic jerseyServlet = container.addServlet("jersey-serlvet", SpringServlet.class);
    jerseyServlet.setInitParameter("com.sun.jersey.config.property.packages", "org.alfresco.bm.rest");
    jerseyServlet.setInitParameter("com.sun.jersey.api.json.POJOMappingFeature", "true");
    jerseyServlet.addMapping("/api/*");
}

From source file:org.cloudfoundry.identity.varz.BootstrapTests.java

private GenericXmlApplicationContext getServletContext(String... resources) {

    String profiles = null;/*from   ww  w . j  a  v a 2s .  co m*/
    String[] resourcesToLoad = resources;
    if (!resources[0].endsWith(".xml")) {
        profiles = resources[0];
        resourcesToLoad = new String[resources.length - 1];
        System.arraycopy(resources, 1, resourcesToLoad, 0, resourcesToLoad.length);
    }

    GenericXmlApplicationContext context = new GenericXmlApplicationContext();
    context.load(resourcesToLoad);

    if (profiles != null) {
        context.getEnvironment().setActiveProfiles(StringUtils.commaDelimitedListToStringArray(profiles));
    }

    // Simulate what happens in the webapp when the YamlServletProfileInitializer kicks in
    YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
    factory.setResources(
            new Resource[] { new FileSystemResource("./src/test/resources/test/config/varz.yml") });
    context.getEnvironment().getPropertySources()
            .addLast(new PropertiesPropertySource("servletProperties", factory.getObject()));

    context.refresh();

    return context;

}

From source file:org.cloudfoundry.identity.login.BootstrapTests.java

private GenericXmlApplicationContext getServletContext(String... resources) {

    String profiles = null;//from   w  w  w . j  a va  2 s .  c  om
    String[] resourcesToLoad = resources;
    if (!resources[0].endsWith(".xml")) {
        profiles = resources[0];
        resourcesToLoad = new String[resources.length - 1];
        System.arraycopy(resources, 1, resourcesToLoad, 0, resourcesToLoad.length);
    }

    GenericXmlApplicationContext context = new GenericXmlApplicationContext();
    context.load(resourcesToLoad);

    if (profiles != null) {
        context.getEnvironment().setActiveProfiles(StringUtils.commaDelimitedListToStringArray(profiles));
    }

    // Simulate what happens in the webapp when the YamlServletProfileInitializer kicks in
    YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
    factory.setResources(
            new Resource[] { new FileSystemResource("./src/test/resources/test/config/login.yml") });
    context.getEnvironment().getPropertySources()
            .addLast(new PropertiesPropertySource("servletProperties", factory.getObject()));

    context.refresh();

    return context;

}

From source file:org.greencheek.utils.environment.propertyplaceholder.spring.EnvironmentalPropertySourcesPlaceholderConfigurerWithSpringValueResolution.java

@Override
public void afterPropertiesSet() {
    if (mergerBuilder == null) {
        throw new IllegalStateException("The PropertyMergerBuilder must not be null.");
    }//from w  w  w.ja  v  a2  s  .com

    Properties p = mergerBuilder.build().getMergedProperties();

    StandardEnvironment env = new StandardEnvironment();
    MutablePropertySources sources = new MutablePropertySources();

    if (isSystemPropertiesResolutionEnabled())
        sources.addLast(new MapPropertySource(StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME,
                env.getSystemProperties()));
    if (isEnvironmentPropertiesResolutionEnabled())
        sources.addLast(new SystemEnvironmentPropertySource(
                StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, env.getSystemEnvironment()));

    sources.addFirst(new PropertiesPropertySource(ENVIRONMENT_SPECIFIC_PROPERTIES, p));
    super.setPropertySources(sources);
}

From source file:org.cloudfoundry.identity.batch.BootstrapTests.java

private GenericXmlApplicationContext getServletContext(String... resources) {

    String profiles = null;/*w  w  w  . j a v a 2s.  c  om*/
    String[] resourcesToLoad = resources;
    if (!resources[0].endsWith(".xml")) {
        profiles = resources[0];
        resourcesToLoad = new String[resources.length - 1];
        System.arraycopy(resources, 1, resourcesToLoad, 0, resourcesToLoad.length);
    }

    GenericXmlApplicationContext context = new GenericXmlApplicationContext();
    context.load(resourcesToLoad);

    if (profiles != null) {
        context.getEnvironment().setActiveProfiles(StringUtils.commaDelimitedListToStringArray(profiles));
    }

    // Simulate what happens in the webapp when the YamlServletProfileInitializer kicks in
    YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
    factory.setResources(
            new Resource[] { new FileSystemResource("./src/test/resources/test/config/batch.yml") });
    context.getEnvironment().getPropertySources()
            .addLast(new PropertiesPropertySource("servletProperties", factory.getObject()));

    context.refresh();

    return context;

}

From source file:org.zalando.crypto.spring.boot.EncryptedYamlPropertySourceLoader.java

@Override
public PropertySource<?> load(final String name, final Resource resource, final String profile)
        throws IOException {
    validateDecrypter();/*w w  w  . j  av a2 s  . c o  m*/
    if (ClassUtils.isPresent("org.yaml.snakeyaml.Yaml", null)) {
        YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
        if (profile == null) {
            factory.setMatchDefault(true);
            factory.setDocumentMatchers(new SpringProfileDocumentMatcher());
        } else {
            factory.setMatchDefault(false);
            factory.setDocumentMatchers(new SpringProfileDocumentMatcher(profile));
        }

        factory.setResources(new Resource[] { resource });

        Properties properties = factory.getObject();
        if (!properties.isEmpty()) {
            return new PropertiesPropertySource(name,
                    new EncryptableProperties(properties, decrypter, getPrefix()));
        }
    }

    return null;
}