Search in sources :

Example 1 with EnumerablePropertySource

use of org.springframework.core.env.EnumerablePropertySource in project cas by apereo.

the class ShibbolethAttributeResolverConfiguration method attributeRepository.

@Bean
public IPersonAttributeDao attributeRepository() {
    try {
        final PropertyPlaceholderConfigurer cfg = new PropertyPlaceholderConfigurer();
        final Map<String, Object> result = new HashMap<>();
        environment.getPropertySources().forEach(s -> {
            if (s instanceof EnumerablePropertySource<?>) {
                final EnumerablePropertySource<?> ps = (EnumerablePropertySource<?>) s;
                Arrays.asList(ps.getPropertyNames()).forEach(key -> result.put(key, ps.getProperty(key)));
            }
        });
        final Properties p = new Properties();
        p.putAll(result);
        cfg.setProperties(p);
        registerBeanIntoApplicationContext(cfg, "shibboleth.PropertySourcesPlaceholderConfigurer");
        final ApplicationContext tempApplicationContext = SpringSupport.newContext(getClass().getName(), casProperties.getShibAttributeResolver().getResources(), Collections.singletonList(cfg), Collections.emptyList(), Collections.emptyList(), this.applicationContext);
        final Collection<DataConnector> values = BeanFactoryUtils.beansOfTypeIncludingAncestors(tempApplicationContext, DataConnector.class).values();
        final Collection<DataConnector> connectors = new HashSet<>(values);
        final AttributeResolverImpl impl = new AttributeResolverImpl();
        impl.setId(getClass().getSimpleName());
        impl.setApplicationContext(tempApplicationContext);
        impl.setAttributeDefinitions(BeanFactoryUtils.beansOfTypeIncludingAncestors(tempApplicationContext, AttributeDefinition.class).values());
        impl.setDataConnectors(connectors);
        if (!impl.isInitialized()) {
            impl.initialize();
        }
        return new ShibbolethPersonAttributeDao(impl);
    } catch (final Exception e) {
        LOGGER.warn(e.getMessage(), e);
    }
    return Beans.newStubAttributeRepository(casProperties.getAuthn().getAttributeRepository());
}
Also used : PropertyPlaceholderConfigurer(org.springframework.beans.factory.config.PropertyPlaceholderConfigurer) EnumerablePropertySource(org.springframework.core.env.EnumerablePropertySource) HashMap(java.util.HashMap) CasConfigurationProperties(org.apereo.cas.configuration.CasConfigurationProperties) EnableConfigurationProperties(org.springframework.boot.context.properties.EnableConfigurationProperties) Properties(java.util.Properties) ApplicationContext(org.springframework.context.ApplicationContext) ShibbolethPersonAttributeDao(org.apereo.cas.persondir.support.ShibbolethPersonAttributeDao) AttributeResolverImpl(net.shibboleth.idp.attribute.resolver.impl.AttributeResolverImpl) DataConnector(net.shibboleth.idp.attribute.resolver.DataConnector) HashSet(java.util.HashSet) Bean(org.springframework.context.annotation.Bean)

Example 2 with EnumerablePropertySource

use of org.springframework.core.env.EnumerablePropertySource in project hub-detect by blackducksoftware.

the class DetectConfiguration method init.

public void init() throws DetectUserFriendlyException, IOException, IllegalArgumentException, IllegalAccessException {
    final String systemUserHome = System.getProperty("user.home");
    if (resolveTildeInPaths) {
        tildeInPathResolver.resolveTildeInAllPathFields(systemUserHome, this);
    }
    if (StringUtils.isBlank(sourcePath)) {
        usingDefaultSourcePath = true;
        sourcePath = System.getProperty("user.dir");
    }
    sourceDirectory = new File(sourcePath);
    if (!sourceDirectory.exists() || !sourceDirectory.isDirectory()) {
        throw new DetectUserFriendlyException("The source path ${sourcePath} either doesn't exist, isn't a directory, or doesn't have appropriate permissions.", ExitCodeType.FAILURE_GENERAL_ERROR);
    }
    // make sure the path is absolute
    sourcePath = sourceDirectory.getCanonicalPath();
    usingDefaultOutputPath = StringUtils.isBlank(outputDirectoryPath);
    outputDirectoryPath = createDirectoryPath(outputDirectoryPath, systemUserHome, "blackduck");
    bdioOutputDirectoryPath = createDirectoryPath(bdioOutputDirectoryPath, outputDirectoryPath, "bdio");
    scanOutputDirectoryPath = createDirectoryPath(scanOutputDirectoryPath, outputDirectoryPath, "scan");
    ensureDirectoryExists(outputDirectoryPath, "The system property 'user.home' will be used by default, but the output directory must exist.");
    ensureDirectoryExists(bdioOutputDirectoryPath, "By default, the directory 'bdio' will be created in the outputDirectory, but the directory must exist.");
    ensureDirectoryExists(scanOutputDirectoryPath, "By default, the directory 'scan' will be created in the outputDirectory, but the directory must exist.");
    outputDirectory = new File(outputDirectoryPath);
    nugetInspectorPackageName = nugetInspectorPackageName.trim();
    nugetInspectorPackageVersion = nugetInspectorPackageVersion.trim();
    final MutablePropertySources mutablePropertySources = configurableEnvironment.getPropertySources();
    for (final PropertySource<?> propertySource : mutablePropertySources) {
        if (propertySource instanceof EnumerablePropertySource) {
            final EnumerablePropertySource<?> enumerablePropertySource = (EnumerablePropertySource<?>) propertySource;
            for (final String propertyName : enumerablePropertySource.getPropertyNames()) {
                if (StringUtils.isNotBlank(propertyName) && propertyName.startsWith(DETECT_PROPERTY_PREFIX)) {
                    allDetectPropertyKeys.add(propertyName);
                }
            }
        }
    }
    if (hubSignatureScannerParallelProcessors == -1) {
        hubSignatureScannerParallelProcessors = Runtime.getRuntime().availableProcessors();
    }
    bomToolFilter = new ExcludedIncludedFilter(getExcludedBomToolTypes(), getIncludedBomToolTypes());
    if (dockerBomTool.isBomToolApplicable() && bomToolFilter.shouldInclude(dockerBomTool.getBomToolType().toString())) {
        configureForDocker();
    }
    if (hubSignatureScannerRelativePathsToExclude != null && hubSignatureScannerRelativePathsToExclude.length > 0) {
        for (final String path : hubSignatureScannerRelativePathsToExclude) {
            excludedScanPaths.add(new File(sourceDirectory, path).getCanonicalPath());
        }
    }
    if (StringUtils.isNotBlank(hubSignatureScannerHostUrl) && StringUtils.isNotBlank(hubSignatureScannerOfflineLocalPath)) {
        throw new DetectUserFriendlyException("You have provided both a hub signature scanner url AND a local hub signature scanner path. Only one of these properties can be set at a time. If both are used together, the *correct* source of the signature scanner can not be determined.", ExitCodeType.FAILURE_GENERAL_ERROR);
    }
    if (StringUtils.isNotBlank(hubSignatureScannerHostUrl)) {
        logger.info("A hub signature scanner url was provided, which requires hub offline mode. Setting hub offline mode to true.");
        hubOfflineMode = true;
    }
    if (StringUtils.isNotBlank(hubSignatureScannerOfflineLocalPath)) {
        logger.info("A local hub signature scanner path was provided, which requires hub offline mode. Setting hub offline mode to true.");
        hubOfflineMode = true;
    }
    if (gradleBomTool.isBomToolApplicable() && bomToolFilter.shouldInclude(gradleBomTool.getBomToolType().toString())) {
        gradleInspectorVersion = gradleBomTool.getInspectorVersion();
    }
    if (nugetBomTool.isBomToolApplicable() && bomToolFilter.shouldInclude(nugetBomTool.getBomToolType().toString())) {
        nugetInspectorPackageVersion = nugetBomTool.getInspectorVersion();
    }
    if (dockerBomTool.isBomToolApplicable() && bomToolFilter.shouldInclude(dockerBomTool.getBomToolType().toString())) {
        dockerInspectorVersion = dockerBomTool.getInspectorVersion();
    }
    configureForPhoneHome();
}
Also used : DetectUserFriendlyException(com.blackducksoftware.integration.hub.detect.exception.DetectUserFriendlyException) EnumerablePropertySource(org.springframework.core.env.EnumerablePropertySource) ExcludedIncludedFilter(com.blackducksoftware.integration.util.ExcludedIncludedFilter) MutablePropertySources(org.springframework.core.env.MutablePropertySources) File(java.io.File)

Example 3 with EnumerablePropertySource

use of org.springframework.core.env.EnumerablePropertySource in project pinpoint by naver.

the class ExperimentalConfig method readExperimentalProperties.

private Map<String, Object> readExperimentalProperties(Environment environment) {
    MutablePropertySources propertySources = ((AbstractEnvironment) environment).getPropertySources();
    Map<String, Object> collect = propertySources.stream().filter(ps -> ps instanceof EnumerablePropertySource).map(ps -> ((EnumerablePropertySource) ps).getPropertyNames()).flatMap(Arrays::stream).filter(propName -> propName.startsWith(PREFIX)).collect(Collectors.toMap(Function.identity(), toValue(environment)));
    return collect;
}
Also used : Objects(java.util.Objects) Component(org.springframework.stereotype.Component) Arrays(java.util.Arrays) Environment(org.springframework.core.env.Environment) Map(java.util.Map) EnumerablePropertySource(org.springframework.core.env.EnumerablePropertySource) MutablePropertySources(org.springframework.core.env.MutablePropertySources) AbstractEnvironment(org.springframework.core.env.AbstractEnvironment) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) EnumerablePropertySource(org.springframework.core.env.EnumerablePropertySource) AbstractEnvironment(org.springframework.core.env.AbstractEnvironment) MutablePropertySources(org.springframework.core.env.MutablePropertySources) Arrays(java.util.Arrays)

Example 4 with EnumerablePropertySource

use of org.springframework.core.env.EnumerablePropertySource in project spring-boot by spring-projects.

the class YamlPropertySourceLoaderTests method orderedItems.

@Test
void orderedItems() throws Exception {
    StringBuilder yaml = new StringBuilder();
    List<String> expected = new ArrayList<>();
    for (char c = 'a'; c <= 'z'; c++) {
        yaml.append(c).append(": value").append(c).append("\n");
        expected.add(String.valueOf(c));
    }
    ByteArrayResource resource = new ByteArrayResource(yaml.toString().getBytes());
    EnumerablePropertySource<?> source = (EnumerablePropertySource<?>) this.loader.load("resource", resource).get(0);
    assertThat(source).isNotNull();
    assertThat(source.getPropertyNames()).isEqualTo(StringUtils.toStringArray(expected));
}
Also used : EnumerablePropertySource(org.springframework.core.env.EnumerablePropertySource) ArrayList(java.util.ArrayList) ByteArrayResource(org.springframework.core.io.ByteArrayResource) Test(org.junit.jupiter.api.Test)

Example 5 with EnumerablePropertySource

use of org.springframework.core.env.EnumerablePropertySource in project canal by alibaba.

the class CanalAdapterLoader method loadAdapter.

private void loadAdapter(OuterAdapterConfig config, List<OuterAdapter> canalOutConnectors) {
    try {
        OuterAdapter adapter;
        adapter = loader.getExtension(config.getName(), StringUtils.trimToEmpty(config.getKey()));
        ClassLoader cl = Thread.currentThread().getContextClassLoader();
        // 替换ClassLoader
        Thread.currentThread().setContextClassLoader(adapter.getClass().getClassLoader());
        Environment env = (Environment) SpringContext.getBean(Environment.class);
        Properties evnProperties = null;
        if (env instanceof StandardEnvironment) {
            evnProperties = new Properties();
            for (PropertySource<?> propertySource : ((StandardEnvironment) env).getPropertySources()) {
                if (propertySource instanceof EnumerablePropertySource) {
                    String[] names = ((EnumerablePropertySource<?>) propertySource).getPropertyNames();
                    for (String name : names) {
                        Object val = env.getProperty(name);
                        if (val != null) {
                            evnProperties.put(name, val);
                        }
                    }
                }
            }
        }
        adapter.init(config, evnProperties);
        Thread.currentThread().setContextClassLoader(cl);
        canalOutConnectors.add(adapter);
        logger.info("Load canal adapter: {} succeed", config.getName());
    } catch (Exception e) {
        logger.error("Load canal adapter: {} failed", config.getName(), e);
    }
}
Also used : EnumerablePropertySource(org.springframework.core.env.EnumerablePropertySource) StandardEnvironment(org.springframework.core.env.StandardEnvironment) Environment(org.springframework.core.env.Environment) Properties(java.util.Properties) OuterAdapter(com.alibaba.otter.canal.client.adapter.OuterAdapter) StandardEnvironment(org.springframework.core.env.StandardEnvironment)

Aggregations

EnumerablePropertySource (org.springframework.core.env.EnumerablePropertySource)18 CompositePropertySource (org.springframework.core.env.CompositePropertySource)7 PropertySource (org.springframework.core.env.PropertySource)6 MutablePropertySources (org.springframework.core.env.MutablePropertySources)5 HashMap (java.util.HashMap)4 LinkedHashMap (java.util.LinkedHashMap)3 Properties (java.util.Properties)3 Test (org.junit.jupiter.api.Test)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 Collectors (java.util.stream.Collectors)2 ConfigurableEnvironment (org.springframework.core.env.ConfigurableEnvironment)2 Environment (org.springframework.core.env.Environment)2 MapPropertySource (org.springframework.core.env.MapPropertySource)2 OuterAdapter (com.alibaba.otter.canal.client.adapter.OuterAdapter)1 DetectUserFriendlyException (com.blackducksoftware.integration.hub.detect.exception.DetectUserFriendlyException)1 ExcludedIncludedFilter (com.blackducksoftware.integration.util.ExcludedIncludedFilter)1 Subscribe (com.google.common.eventbus.Subscribe)1 ConfigurationManager (com.netflix.config.ConfigurationManager)1