Skip to content

Commit

Permalink
Update configuration builder settings classes setup, fallback to conf…
Browse files Browse the repository at this point in the history
…ig map in case if settings classes is missing.
  • Loading branch information
MishaDemianenko committed Jul 8, 2015
1 parent f7dd1dc commit 5617444
Show file tree
Hide file tree
Showing 8 changed files with 179 additions and 44 deletions.
Expand Up @@ -65,31 +65,22 @@ public boolean containsKey( String key )
public Object getProperty( String key ) public Object getProperty( String key )
{ {
Setting<?> setting = getSettingForKey( key ); Setting<?> setting = getSettingForKey( key );
if( setting == null ) return setting == null ? config.getParams().get( key ) : config.get( setting );
{
return null;
}
else
{
return config.get( setting );
}
} }


@Override @Override
public Iterator<String> getKeys() public Iterator<String> getKeys()
{ {
// get all the properties keys Set<String> propertyKeys = new HashSet<>( config.getParams().keySet() );
Set<String> staticKeys = getRegisteredSettings().keySet(); // only keep the properties which have been assigned some values
// only keep the properties which have been assigned default values or user-defined values for ( String registeredSettingName : getRegisteredSettings().keySet() )
Set<String> notNullKeys = new HashSet<>();
for ( String key : staticKeys )
{ {
if ( containsKey( key ) ) if ( containsKey( registeredSettingName ) )
{ {
notNullKeys.add( key ); propertyKeys.add( registeredSettingName );
} }
} }
return notNullKeys.iterator(); return propertyKeys.iterator();
} }


@Override @Override
Expand All @@ -103,7 +94,7 @@ private Setting<?> getSettingForKey( String key )
return getRegisteredSettings().get( key ); return getRegisteredSettings().get( key );
} }


private Map<String, Setting<?>> getRegisteredSettings() private Map<String,Setting<?>> getRegisteredSettings()
{ {
Iterable<Class<?>> settingsClasses = config.getSettingsClasses(); Iterable<Class<?>> settingsClasses = config.getSettingsClasses();
AnnotatedFieldHarvester fieldHarvester = new AnnotatedFieldHarvester(); AnnotatedFieldHarvester fieldHarvester = new AnnotatedFieldHarvester();
Expand Down
Expand Up @@ -26,8 +26,12 @@
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;


import org.neo4j.graphdb.factory.GraphDatabaseSettings;
import org.neo4j.helpers.Settings; import org.neo4j.helpers.Settings;
import org.neo4j.kernel.configuration.Config; import org.neo4j.kernel.configuration.Config;
import org.neo4j.server.web.ServerInternalSettings;

import static java.util.Arrays.asList;


/** /**
* Used by the server to load server and database properties. * Used by the server to load server and database properties.
Expand Down Expand Up @@ -70,8 +74,8 @@ public ConfiguratorWrappingConfigurationBuilder ( Configurator configurator )
} }
serverProperties.put( ServerSettings.third_party_packages.name(), serverProperties.put( ServerSettings.third_party_packages.name(),
toStringForThirdPartyPackageProperty( configurator.getThirdpartyJaxRsPackages() ) ); toStringForThirdPartyPackageProperty( configurator.getThirdpartyJaxRsPackages() ) );
this.serverConfig = new Config( serverProperties );


this.serverConfig = new Config( serverProperties, ServerConfigFactory.getDefaultSettingsClasses() );
// use the db properties directly // use the db properties directly
this.dbProperties = configurator.getDatabaseTuningProperties(); this.dbProperties = configurator.getDatabaseTuningProperties();
} }
Expand Down
Expand Up @@ -24,13 +24,11 @@
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;


import org.neo4j.graphdb.factory.GraphDatabaseSettings;
import org.neo4j.helpers.collection.MapUtil; import org.neo4j.helpers.collection.MapUtil;
import org.neo4j.kernel.configuration.Config; import org.neo4j.kernel.configuration.Config;
import org.neo4j.logging.Log; import org.neo4j.logging.Log;
import org.neo4j.server.web.ServerInternalSettings; import org.neo4j.server.web.ServerInternalSettings;


import static java.util.Arrays.asList;


/** /**
* @deprecated this will be removed in the next major version of Neo4j * @deprecated this will be removed in the next major version of Neo4j
Expand All @@ -55,14 +53,7 @@ public PropertyFileConfigurator( File propertiesFile, Log log )
loadServerProperties( propertiesFile, log ); loadServerProperties( propertiesFile, log );
loadDatabaseTuningProperties( propertiesFile, log ); loadDatabaseTuningProperties( propertiesFile, log );


serverConfig = new Config( serverProperties ); serverConfig = new Config( serverProperties, ServerConfigFactory.getDefaultSettingsClasses() );
setServerSettingsClasses( serverConfig );
}

public static void setServerSettingsClasses( Config config )
{
config.registerSettingsClasses( asList( ServerSettings.class,
ServerInternalSettings.class, GraphDatabaseSettings.class ) );
} }


@Override @Override
Expand Down
Expand Up @@ -78,7 +78,7 @@ public static Config loadConfig( File configFile, File legacyConfigFile, File le
overrideEmbeddedDefaults( config ); overrideEmbeddedDefaults( config );
applyUserOverrides( config, configOverrides ); applyUserOverrides( config, configOverrides );


setServerSettingsClasses( config ); config.registerSettingsClasses( getDefaultSettingsClasses() );


return config; return config;
} }
Expand Down Expand Up @@ -114,10 +114,9 @@ private static void overrideEmbeddedDefaults( Config config )
config.applyChanges( params ); config.applyChanges( params );
} }


public static void setServerSettingsClasses( Config config ) public static Iterable<Class<?>> getDefaultSettingsClasses()
{ {
config.registerSettingsClasses( asList( ServerSettings.class, return asList( ServerSettings.class, ServerInternalSettings.class, GraphDatabaseSettings.class );
ServerInternalSettings.class, GraphDatabaseSettings.class ) );
} }


private static Map<String, String> loadServerConfig( File legacyServerConfigFile, Log log ) private static Map<String, String> loadServerConfig( File legacyServerConfigFile, Log log )
Expand Down
Expand Up @@ -50,7 +50,7 @@ public Collection<Injectable<?>> initializePackages( Iterable<String> packageNam
GraphDatabaseAPI graphDatabaseService = neoServer.getDatabase().getGraph(); GraphDatabaseAPI graphDatabaseService = neoServer.getDatabase().getGraph();
Config configuration = neoServer.getConfig(); Config configuration = neoServer.getConfig();


Collection<Injectable<?>> injectables = new HashSet<Injectable<?>>(); Collection<Injectable<?>> injectables = new HashSet<>();
for ( PluginLifecycle lifecycle : lifecycles ) for ( PluginLifecycle lifecycle : lifecycles )
{ {
if ( hasPackage( lifecycle, packageNames ) ) if ( hasPackage( lifecycle, packageNames ) )
Expand Down
Expand Up @@ -22,22 +22,24 @@
import org.junit.Test; import org.junit.Test;


import java.net.URI; import java.net.URI;
import java.util.HashMap;


import org.neo4j.kernel.configuration.Config; import org.neo4j.kernel.configuration.Config;
import org.neo4j.server.web.ServerInternalSettings; import org.neo4j.server.web.ServerInternalSettings;


import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.neo4j.helpers.collection.MapUtil.stringMap;


public class ConfigWrappingConfigurationTest public class ConfigWrappingConfigurationTest
{ {

@Test @Test
public void shouldGetDefaultPropertyByKey() throws Exception public void shouldGetDefaultPropertyByKey() throws Exception
{ {
// GIVEN // GIVEN
final Config config = new Config(); Config config = new Config( new HashMap<String,String>(), ServerConfigFactory.getDefaultSettingsClasses() );
ServerConfigFactory.setServerSettingsClasses( config ); ConfigWrappingConfiguration wrappingConfiguration = new ConfigWrappingConfiguration( config );
final ConfigWrappingConfiguration wrappingConfiguration = new ConfigWrappingConfiguration( config );


// WHEN // WHEN
final Object propertyValue = wrappingConfiguration.getProperty( ServerInternalSettings.rest_api_path.name() ); final Object propertyValue = wrappingConfiguration.getProperty( ServerInternalSettings.rest_api_path.name() );
Expand All @@ -50,12 +52,12 @@ public void shouldGetDefaultPropertyByKey() throws Exception
public void shouldGetPropertyInRightFormat() throws Exception public void shouldGetPropertyInRightFormat() throws Exception
{ {
// GIVEN // GIVEN
final Config config = new Config(); Config config = new Config( new HashMap<String,String>(), ServerConfigFactory.getDefaultSettingsClasses() );
ServerConfigFactory.setServerSettingsClasses( config ); ConfigWrappingConfiguration wrappingConfiguration = new ConfigWrappingConfiguration( config );
final ConfigWrappingConfiguration wrappingConfiguration = new ConfigWrappingConfiguration( config );


// WHEN // WHEN
wrappingConfiguration.setProperty( ServerInternalSettings.rest_api_path.name(), "http://localhost:7474///db///data///" ); wrappingConfiguration
.setProperty( ServerInternalSettings.rest_api_path.name(), "http://localhost:7474///db///data///" );
final Object dataPath = wrappingConfiguration.getProperty( ServerInternalSettings.rest_api_path.name() ); final Object dataPath = wrappingConfiguration.getProperty( ServerInternalSettings.rest_api_path.name() );


// THEN // THEN
Expand All @@ -66,13 +68,29 @@ public void shouldGetPropertyInRightFormat() throws Exception
public void shouldContainAllKeysOfPropertiesWithDefaultOrUserDefinedValues() throws Exception public void shouldContainAllKeysOfPropertiesWithDefaultOrUserDefinedValues() throws Exception
{ {
// GIVEN // GIVEN
final Config config = new Config();
ServerConfigFactory.setServerSettingsClasses( config );


// WHEN Config config = new Config( new HashMap<String,String>(), ServerConfigFactory.getDefaultSettingsClasses() );
final ConfigWrappingConfiguration wrappingConfiguration = new ConfigWrappingConfiguration( config ); ConfigWrappingConfiguration wrappingConfiguration = new ConfigWrappingConfiguration( config );


// THEN // THEN
assertTrue( wrappingConfiguration.getKeys().hasNext() ); assertTrue( wrappingConfiguration.getKeys().hasNext() );
} }

@Test
public void shouldAbleToAccessRegisteredPropertyByName()
{
Config config = new Config( new HashMap<String,String>(), ServerConfigFactory.getDefaultSettingsClasses() );
ConfigWrappingConfiguration wrappingConfiguration = new ConfigWrappingConfiguration( config );

assertEquals( 60000L, wrappingConfiguration.getProperty( ServerSettings.transaction_timeout.name() ) );
}

@Test
public void shouldAbleToAccessNonRegisteredPropertyByName()
{
Config config = new Config( stringMap( ServerSettings.transaction_timeout.name(), "600" ) );
ConfigWrappingConfiguration wrappingConfiguration = new ConfigWrappingConfiguration( config );

assertEquals( "600", wrappingConfiguration.getProperty( ServerSettings.transaction_timeout.name() ) );
}
} }
@@ -0,0 +1,131 @@
/*
* Copyright (c) 2002-2015 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.server.modules;

import org.apache.commons.configuration.Configuration;
import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;
import org.hamcrest.Matchers;
import org.junit.Test;
import org.mockito.Mockito;

import java.util.Arrays;
import java.util.Collection;

import org.neo4j.function.Function;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.helpers.collection.Iterables;
import org.neo4j.kernel.configuration.Config;
import org.neo4j.server.NeoServer;
import org.neo4j.server.configuration.ServerSettings;
import org.neo4j.server.plugins.Injectable;
import org.neo4j.server.plugins.PluginLifecycle;

import static org.junit.Assert.assertThat;
import static org.neo4j.helpers.collection.MapUtil.stringMap;


public class ExtensionInitializerTest
{

@Test
public void testPluginInitialization()
{
Config config = new Config( stringMap( ServerSettings.transaction_timeout.name(), "600" ) );
NeoServer neoServer = Mockito.mock( NeoServer.class, Mockito.RETURNS_DEEP_STUBS );
Mockito.when( neoServer.getConfig() ).thenReturn( config );
ExtensionInitializer extensionInitializer = new ExtensionInitializer( neoServer );

Collection<Injectable<?>> injectableProperties =
extensionInitializer.initializePackages( Arrays.asList( "org.neo4j.server.modules" ) );

assertThat( injectableProperties, Matchers.hasSize( 1 ) );
assertThat( injectableProperties, Matchers.contains( new InjectableMatcher<>( ServerSettings
.transaction_timeout.name() ) ) );
}

private class InjectableMatcher<T> extends BaseMatcher<Injectable<?>>
{
private T value;

public InjectableMatcher( T value )
{
this.value = value;
}

@Override
public boolean matches( Object o )
{
return o instanceof Injectable && value.equals( ((Injectable) o).getValue() );
}

@Override
public void describeMismatch( Object o, Description description )
{
description.appendValue( String.format( "Expect Injectable with value: '%s', but actual value was: '%s'",
value, o ) );
}

@Override
public void describeTo( Description description )
{

}
}

public static class PropertyCollectorPlugin implements PluginLifecycle
{

@Override
public Collection<Injectable<?>> start( GraphDatabaseService graphDatabaseService, Configuration config )
{
return Iterables.toList( Iterables.map( new StringToInjectableFunction(), config.getKeys() ) );
}

@Override
public void stop()
{

}

private class StringToInjectableFunction implements Function<String,Injectable<?>>
{

@Override
public Injectable<String> apply( final String value )
{
return new Injectable<String>()
{
@Override
public String getValue()
{
return value;
}

@Override
public Class<String> getType()
{
return String.class;
}
};
}
}
}
}
@@ -0,0 +1 @@
org.neo4j.server.modules.ExtensionInitializerTest$PropertyCollectorPlugin

0 comments on commit 5617444

Please sign in to comment.