Skip to content

Commit

Permalink
HSEARCH-3827 Add a ConfigurationPropertySource for system properties
Browse files Browse the repository at this point in the history
  • Loading branch information
yrodiere authored and fax4ever committed Feb 17, 2020
1 parent 1c8a1ce commit 155123f
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
@@ -0,0 +1,57 @@
/*
* Hibernate Search, full-text search for your domain model
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.search.engine.cfg.impl;

import java.util.HashSet;
import java.util.Map;
import java.util.Optional;
import java.util.Set;

import org.hibernate.search.engine.cfg.spi.AllAwareConfigurationPropertySource;

public class SystemConfigurationPropertySource implements AllAwareConfigurationPropertySource {

private static final SystemConfigurationPropertySource INSTANCE = new SystemConfigurationPropertySource();

public static AllAwareConfigurationPropertySource get() {
return INSTANCE;
}

private SystemConfigurationPropertySource() {
}

@Override
public Optional<?> get(String key) {
Object value = System.getProperty( key );
return Optional.ofNullable( value );
}

@Override
public Optional<String> resolve(String key) {
return Optional.of( key );
}

@Override
public Set<String> resolveAll(String prefix) {
Set<String> prefixedPropertyKeys = new HashSet<>();
for ( Map.Entry<Object, Object> entry : System.getProperties().entrySet() ) {
Object key = entry.getKey();
if ( key instanceof String ) {
String stringKey = (String) key;
if ( stringKey.startsWith( prefix ) ) {
prefixedPropertyKeys.add( stringKey );
}
}
}
return prefixedPropertyKeys;
}

@Override
public String toString() {
return getClass().getSimpleName();
}
}
Expand Up @@ -15,6 +15,7 @@
import org.hibernate.search.engine.cfg.impl.MaskedConfigurationPropertySource;
import org.hibernate.search.engine.cfg.impl.OverriddenConfigurationPropertySource;
import org.hibernate.search.engine.cfg.impl.PrefixedConfigurationPropertySource;
import org.hibernate.search.engine.cfg.impl.SystemConfigurationPropertySource;

/**
* A source of property values for Hibernate Search.
Expand Down Expand Up @@ -97,6 +98,13 @@ static AllAwareConfigurationPropertySource fromMap(Map<String, ?> map) {
return new MapConfigurationPropertySource( map );
}

/**
* @return A source containing the system properties ({@link System#getProperty(String)}).
*/
static AllAwareConfigurationPropertySource system() {
return SystemConfigurationPropertySource.get();
}

/**
* @return A source without any property.
*/
Expand Down

0 comments on commit 155123f

Please sign in to comment.