Search in sources :

Example 1 with JndiPropertySource

use of org.springframework.jndi.JndiPropertySource in project spring-framework by spring-projects.

the class StandardServletEnvironment method customizePropertySources.

/**
 * Customize the set of property sources with those contributed by superclasses as
 * well as those appropriate for standard servlet-based environments:
 * <ul>
 * <li>{@value #SERVLET_CONFIG_PROPERTY_SOURCE_NAME}
 * <li>{@value #SERVLET_CONTEXT_PROPERTY_SOURCE_NAME}
 * <li>{@value #JNDI_PROPERTY_SOURCE_NAME}
 * </ul>
 * <p>Properties present in {@value #SERVLET_CONFIG_PROPERTY_SOURCE_NAME} will
 * take precedence over those in {@value #SERVLET_CONTEXT_PROPERTY_SOURCE_NAME}, and
 * properties found in either of the above take precedence over those found in
 * {@value #JNDI_PROPERTY_SOURCE_NAME}.
 * <p>Properties in any of the above will take precedence over system properties and
 * environment variables contributed by the {@link StandardEnvironment} superclass.
 * <p>The {@code Servlet}-related property sources are added as
 * {@link StubPropertySource stubs} at this stage, and will be
 * {@linkplain #initPropertySources(ServletContext, ServletConfig) fully initialized}
 * once the actual {@link ServletContext} object becomes available.
 * @see StandardEnvironment#customizePropertySources
 * @see org.springframework.core.env.AbstractEnvironment#customizePropertySources
 * @see ServletConfigPropertySource
 * @see ServletContextPropertySource
 * @see org.springframework.jndi.JndiPropertySource
 * @see org.springframework.context.support.AbstractApplicationContext#initPropertySources
 * @see #initPropertySources(ServletContext, ServletConfig)
 */
@Override
protected void customizePropertySources(MutablePropertySources propertySources) {
    propertySources.addLast(new StubPropertySource(SERVLET_CONFIG_PROPERTY_SOURCE_NAME));
    propertySources.addLast(new StubPropertySource(SERVLET_CONTEXT_PROPERTY_SOURCE_NAME));
    if (jndiPresent && JndiLocatorDelegate.isDefaultJndiEnvironmentAvailable()) {
        propertySources.addLast(new JndiPropertySource(JNDI_PROPERTY_SOURCE_NAME));
    }
    super.customizePropertySources(propertySources);
}
Also used : StubPropertySource(org.springframework.core.env.PropertySource.StubPropertySource) JndiPropertySource(org.springframework.jndi.JndiPropertySource)

Example 2 with JndiPropertySource

use of org.springframework.jndi.JndiPropertySource in project waltz by khartec.

the class DIConfiguration method propertySourcesPlaceholderConfigurer.

/* Required for property interpolation to work correctly */
/**
 * @see <a href="http://stackoverflow.com/a/41760877/2311919">Explanation</a>
 */
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer(ConfigurableEnvironment env) {
    PropertySourcesPlaceholderConfigurer placeholderConfigurer = new PropertySourcesPlaceholderConfigurer();
    JndiPropertySource jndiPropertySource = new JndiPropertySource("java:comp");
    env.getPropertySources().addFirst(jndiPropertySource);
    return placeholderConfigurer;
}
Also used : JndiPropertySource(org.springframework.jndi.JndiPropertySource) PropertySourcesPlaceholderConfigurer(org.springframework.context.support.PropertySourcesPlaceholderConfigurer)

Example 3 with JndiPropertySource

use of org.springframework.jndi.JndiPropertySource in project java-chassis by ServiceComb.

the class TestConfigurationSpringInitializer method testSetEnvironment.

@Test
public void testSetEnvironment() {
    ConfigurableEnvironment environment = Mockito.mock(ConfigurableEnvironment.class);
    MutablePropertySources propertySources = new MutablePropertySources();
    Map<String, String> propertyMap = new HashMap<>();
    final String map0Key0 = "map0-Key0";
    final String map1Key0 = "map1-Key0";
    final String map2Key0 = "map2-Key0";
    final String map3Key0 = "map3-Key0";
    propertyMap.put(map0Key0, "map0-Value0");
    propertyMap.put(map1Key0, "map1-Value0");
    propertyMap.put(map2Key0, "map2-Value0");
    propertyMap.put(map3Key0, "map3-Value0");
    /*
    propertySources
    |- compositePropertySource0
    |  |- mapPropertySource0
    |  |  |- map0-Key0 = map0-Value0
    |  |- compositePropertySource1
    |     |- mapPropertySource1
    |     |  |- map1-Key0 = map1-Value0
    |     |- mapPropertySource2
    |        |- map2-Key0 = map2-Value0
    |     |- JndiPropertySource(mocked)
    |- mapPropertySource3
      |- map3-Key0 = map3-Value0
     */
    CompositePropertySource compositePropertySource0 = new CompositePropertySource("compositePropertySource0");
    propertySources.addFirst(compositePropertySource0);
    HashMap<String, Object> map0 = new HashMap<>();
    map0.put(map0Key0, propertyMap.get(map0Key0));
    MapPropertySource mapPropertySource0 = new MapPropertySource("mapPropertySource0", map0);
    compositePropertySource0.addFirstPropertySource(mapPropertySource0);
    CompositePropertySource compositePropertySource1 = new CompositePropertySource("compositePropertySource1");
    compositePropertySource0.addPropertySource(compositePropertySource1);
    HashMap<String, Object> map1 = new HashMap<>();
    map1.put(map1Key0, propertyMap.get(map1Key0));
    MapPropertySource mapPropertySource1 = new MapPropertySource("mapPropertySource1", map1);
    compositePropertySource1.addPropertySource(mapPropertySource1);
    HashMap<String, Object> map2 = new HashMap<>();
    map2.put(map2Key0, propertyMap.get(map2Key0));
    MapPropertySource mapPropertySource2 = new MapPropertySource("mapPropertySource2", map2);
    compositePropertySource1.addPropertySource(mapPropertySource2);
    compositePropertySource1.addPropertySource(Mockito.mock(JndiPropertySource.class));
    HashMap<String, Object> map3 = new HashMap<>();
    map3.put(map3Key0, propertyMap.get(map3Key0));
    MapPropertySource mapPropertySource3 = new MapPropertySource("mapPropertySource3", map3);
    compositePropertySource0.addPropertySource(mapPropertySource3);
    Mockito.when(environment.getPropertySources()).thenReturn(propertySources);
    Mockito.doAnswer((Answer<String>) invocation -> {
        Object[] args = invocation.getArguments();
        String propertyName = (String) args[0];
        if ("spring.config.name".equals(propertyName) || "spring.application.name".equals(propertyName)) {
            return null;
        }
        String value = propertyMap.get(propertyName);
        if (null == value) {
            fail("get unexpected property name: " + propertyName);
        }
        return value;
    }).when(environment).getProperty(anyString(), Matchers.eq(Object.class));
    new ConfigurationSpringInitializer().setEnvironment(environment);
    Map<String, Map<String, Object>> extraLocalConfig = getExtraConfigMapFromConfigUtil();
    assertFalse(extraLocalConfig.isEmpty());
    Map<String, Object> extraProperties = extraLocalConfig.get(ConfigurationSpringInitializer.EXTRA_CONFIG_SOURCE_PREFIX + environment.getClass().getName() + "@" + environment.hashCode());
    assertNotNull(extraLocalConfig);
    for (Entry<String, String> entry : propertyMap.entrySet()) {
        assertEquals(entry.getValue(), extraProperties.get(entry.getKey()));
    }
}
Also used : Configuration(org.apache.commons.configuration.Configuration) PropertySource(org.springframework.core.env.PropertySource) Matchers(org.mockito.Matchers) ArchaiusUtils(org.apache.servicecomb.foundation.test.scaffolding.config.ArchaiusUtils) HashMap(java.util.HashMap) Matchers.anyString(org.mockito.Matchers.anyString) Logger(org.apache.log4j.Logger) Answer(org.mockito.stubbing.Answer) ConfigurableEnvironment(org.springframework.core.env.ConfigurableEnvironment) Map(java.util.Map) Level(org.apache.log4j.Level) After(org.junit.After) Assert.fail(org.junit.Assert.fail) Before(org.junit.Before) Assert.assertNotNull(org.junit.Assert.assertNotNull) ConfigurationManager(com.netflix.config.ConfigurationManager) ConfigUtil(org.apache.servicecomb.config.ConfigUtil) Test(org.junit.Test) StandardEnvironment(org.springframework.core.env.StandardEnvironment) SystemEnvironmentPropertySource(org.springframework.core.env.SystemEnvironmentPropertySource) Deencapsulation(mockit.Deencapsulation) Mockito(org.mockito.Mockito) CompositePropertySource(org.springframework.core.env.CompositePropertySource) List(java.util.List) Assert.assertFalse(org.junit.Assert.assertFalse) MapPropertySource(org.springframework.core.env.MapPropertySource) Entry(java.util.Map.Entry) MicroserviceConfigLoader(org.apache.servicecomb.config.archaius.sources.MicroserviceConfigLoader) MutablePropertySources(org.springframework.core.env.MutablePropertySources) Assert(org.junit.Assert) JndiPropertySource(org.springframework.jndi.JndiPropertySource) Assert.assertEquals(org.junit.Assert.assertEquals) HashMap(java.util.HashMap) Matchers.anyString(org.mockito.Matchers.anyString) ConfigurableEnvironment(org.springframework.core.env.ConfigurableEnvironment) CompositePropertySource(org.springframework.core.env.CompositePropertySource) MapPropertySource(org.springframework.core.env.MapPropertySource) JndiPropertySource(org.springframework.jndi.JndiPropertySource) MutablePropertySources(org.springframework.core.env.MutablePropertySources) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 4 with JndiPropertySource

use of org.springframework.jndi.JndiPropertySource in project waltz by khartec.

the class DIConfiguration method propertySourcesPlaceholderConfigurer.

/**
 * Required for property interpolation to work correctly
 * @see <a href="http://stackoverflow.com/a/41760877/2311919">Explanation</a>
 */
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer(ConfigurableEnvironment env) {
    PropertySourcesPlaceholderConfigurer placeholderConfigurer = new PropertySourcesPlaceholderConfigurer();
    JndiPropertySource jndiPropertySource = new JndiPropertySource("java:comp");
    env.getPropertySources().addFirst(jndiPropertySource);
    return placeholderConfigurer;
}
Also used : JndiPropertySource(org.springframework.jndi.JndiPropertySource) PropertySourcesPlaceholderConfigurer(org.springframework.context.support.PropertySourcesPlaceholderConfigurer)

Aggregations

JndiPropertySource (org.springframework.jndi.JndiPropertySource)4 PropertySourcesPlaceholderConfigurer (org.springframework.context.support.PropertySourcesPlaceholderConfigurer)2 ConfigurationManager (com.netflix.config.ConfigurationManager)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Entry (java.util.Map.Entry)1 Deencapsulation (mockit.Deencapsulation)1 Configuration (org.apache.commons.configuration.Configuration)1 Level (org.apache.log4j.Level)1 Logger (org.apache.log4j.Logger)1 ConfigUtil (org.apache.servicecomb.config.ConfigUtil)1 MicroserviceConfigLoader (org.apache.servicecomb.config.archaius.sources.MicroserviceConfigLoader)1 ArchaiusUtils (org.apache.servicecomb.foundation.test.scaffolding.config.ArchaiusUtils)1 After (org.junit.After)1 Assert (org.junit.Assert)1 Assert.assertEquals (org.junit.Assert.assertEquals)1 Assert.assertFalse (org.junit.Assert.assertFalse)1 Assert.assertNotNull (org.junit.Assert.assertNotNull)1 Assert.fail (org.junit.Assert.fail)1