Skip to content

Commit

Permalink
Remove unnecessary access to Config internals
Browse files Browse the repository at this point in the history
  • Loading branch information
benbc committed Feb 29, 2016
1 parent a70ad41 commit c2daf86
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 57 deletions.
Expand Up @@ -40,6 +40,8 @@


import static java.util.Arrays.asList; import static java.util.Arrays.asList;


import static org.neo4j.helpers.collection.Iterables.concat;

/** /**
* This class holds the overall configuration of a Neo4j database instance. Use the accessors to convert the internal * This class holds the overall configuration of a Neo4j database instance. Use the accessors to convert the internal
* key-value settings to other types. * key-value settings to other types.
Expand Down Expand Up @@ -101,11 +103,11 @@ public Config( Map<String, String> params, Iterable<Class<?>> settingsClasses )
* *
* @return a new modified config, leaves this config unchanged. * @return a new modified config, leaves this config unchanged.
*/ */
public Config with( Map<String, String> additionalConfig ) public Config with( Map<String, String> additionalConfig, Class<?>... settingsClasses )
{ {
Map<String, String> newParams = getParams(); // copy is returned Map<String, String> newParams = getParams(); // copy is returned
newParams.putAll( additionalConfig ); newParams.putAll( additionalConfig );
return new Config( newParams ); return new Config( newParams, concat( this.settingsClasses, asList( settingsClasses ) ) );
} }


// TODO: Get rid of this, to allow us to have something more // TODO: Get rid of this, to allow us to have something more
Expand Down
Expand Up @@ -93,20 +93,14 @@ public BatchingNeoStores( FileSystemAbstraction fileSystem, File storeDir, Confi
this.fileSystem = fileSystem; this.fileSystem = fileSystem;
this.logProvider = logService.getInternalLogProvider(); this.logProvider = logService.getInternalLogProvider();
this.storeDir = storeDir; this.storeDir = storeDir;
this.neo4jConfig = new Config( stringMap( dbConfig.getParams(), this.neo4jConfig = dbConfig.with(
dense_node_threshold.name(), valueOf( config.denseNodeThreshold() ), stringMap(
pagecache_memory.name(), valueOf( config.writeBufferSize() ) ), dense_node_threshold.name(), valueOf( config.denseNodeThreshold() ),
pagecache_memory.name(), valueOf( config.writeBufferSize() ) ),
GraphDatabaseSettings.class ); GraphDatabaseSettings.class );
final PageCacheTracer tracer = new DefaultPageCacheTracer(); final PageCacheTracer tracer = new DefaultPageCacheTracer();
this.pageCache = createPageCache( fileSystem, neo4jConfig, logProvider, tracer ); this.pageCache = createPageCache( fileSystem, neo4jConfig, logProvider, tracer );
this.ioTracer = new IoTracer() this.ioTracer = tracer::bytesWritten;
{
@Override
public long countBytesWritten()
{
return tracer.bytesWritten();
}
};
this.neoStores = newNeoStores( pageCache ); this.neoStores = newNeoStores( pageCache );
if ( alreadyContainsData( neoStores ) ) if ( alreadyContainsData( neoStores ) )
{ {
Expand All @@ -131,7 +125,7 @@ public long countBytesWritten()
this.relationshipTypeRepository = new BatchingRelationshipTypeTokenRepository( this.relationshipTypeRepository = new BatchingRelationshipTypeTokenRepository(
neoStores.getRelationshipTypeTokenStore(), initialIds.highRelationshipTypeTokenId() ); neoStores.getRelationshipTypeTokenStore(), initialIds.highRelationshipTypeTokenId() );


// Initialze kernel extensions // Initialize kernel extensions
Dependencies dependencies = new Dependencies(); Dependencies dependencies = new Dependencies();
dependencies.satisfyDependency( neo4jConfig ); dependencies.satisfyDependency( neo4jConfig );
dependencies.satisfyDependency( fileSystem ); dependencies.satisfyDependency( fileSystem );
Expand Down
Expand Up @@ -32,6 +32,7 @@
import org.neo4j.helpers.collection.Pair; import org.neo4j.helpers.collection.Pair;
import org.neo4j.kernel.configuration.AnnotatedFieldHarvester; import org.neo4j.kernel.configuration.AnnotatedFieldHarvester;
import org.neo4j.kernel.configuration.Config; import org.neo4j.kernel.configuration.Config;
import org.neo4j.kernel.configuration.Settings;


import static org.neo4j.helpers.collection.MapUtil.stringMap; import static org.neo4j.helpers.collection.MapUtil.stringMap;


Expand Down Expand Up @@ -60,8 +61,7 @@ public boolean containsKey( String key )
@Override @Override
public Object getProperty( String key ) public Object getProperty( String key )
{ {
Setting<?> setting = getSettingForKey( key ); return config.get( getSettingForKey( key ) );
return setting == null ? config.getParams().get( key ) : config.get( setting );
} }


@Override @Override
Expand All @@ -87,7 +87,12 @@ protected void addPropertyDirect( String key, Object value )


private Setting<?> getSettingForKey( String key ) private Setting<?> getSettingForKey( String key )
{ {
return getRegisteredSettings().get( key ); Setting<?> setting = getRegisteredSettings().get( key );
if ( setting != null )
{
return setting;
}
return Settings.setting( key, Settings.STRING, Settings.NO_DEFAULT );
} }


private Map<String,Setting<?>> getRegisteredSettings() private Map<String,Setting<?>> getRegisteredSettings()
Expand Down
Expand Up @@ -37,6 +37,10 @@
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;


import static org.neo4j.kernel.configuration.Settings.NO_DEFAULT;
import static org.neo4j.kernel.configuration.Settings.STRING;
import static org.neo4j.kernel.configuration.Settings.setting;

public class ServerConfigLoaderTest public class ServerConfigLoaderTest
{ {
@Rule @Rule
Expand Down Expand Up @@ -72,7 +76,7 @@ public void shouldUseSpecifiedConfigFile() throws Exception


// then // then
final String EXPECTED_VALUE = "bar"; final String EXPECTED_VALUE = "bar";
assertEquals( EXPECTED_VALUE, testConf.getParams().get( "foo" ) ); assertEquals( EXPECTED_VALUE, testConf.get( setting( "foo", STRING, NO_DEFAULT ) ) );
} }


@Test @Test
Expand All @@ -90,7 +94,7 @@ public void shouldAcceptDuplicateKeysWithSameValue() throws IOException
// then // then
assertNotNull( testConf ); assertNotNull( testConf );
final String EXPECTED_VALUE = "bar"; final String EXPECTED_VALUE = "bar";
assertEquals( EXPECTED_VALUE, testConf.getParams().get( "foo" ) ); assertEquals( EXPECTED_VALUE, testConf.get( setting( "foo", STRING, NO_DEFAULT ) ) );
} }


@Test @Test
Expand Down
Expand Up @@ -19,19 +19,18 @@
*/ */
package org.neo4j.ext.udc.impl; package org.neo4j.ext.udc.impl;


import java.io.InputStream;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Properties;
import java.util.Timer; import java.util.Timer;


import org.neo4j.ext.udc.UdcSettings; import org.neo4j.ext.udc.UdcSettings;
import org.neo4j.helpers.Service; import org.neo4j.helpers.Service;
import org.neo4j.kernel.impl.spi.KernelContext; import org.neo4j.helpers.collection.MapUtil;
import org.neo4j.kernel.impl.store.id.IdGeneratorFactory;
import org.neo4j.kernel.configuration.Config; import org.neo4j.kernel.configuration.Config;
import org.neo4j.kernel.extension.KernelExtensionFactory; import org.neo4j.kernel.extension.KernelExtensionFactory;
import org.neo4j.kernel.impl.core.StartupStatistics; import org.neo4j.kernel.impl.core.StartupStatistics;
import org.neo4j.kernel.impl.spi.KernelContext;
import org.neo4j.kernel.impl.store.id.IdGeneratorFactory;
import org.neo4j.kernel.impl.transaction.state.DataSourceManager; import org.neo4j.kernel.impl.transaction.state.DataSourceManager;
import org.neo4j.kernel.lifecycle.Lifecycle; import org.neo4j.kernel.lifecycle.Lifecycle;
import org.neo4j.udc.UsageData; import org.neo4j.udc.UsageData;
Expand Down Expand Up @@ -59,9 +58,6 @@ public interface Dependencies
StartupStatistics startupStats(); StartupStatistics startupStats();
} }


/**
* No-arg constructor, sets the extension key to "kernel udc".
*/
public UdcKernelExtensionFactory() public UdcKernelExtensionFactory()
{ {
super( KEY ); super( KEY );
Expand All @@ -78,7 +74,7 @@ public Lifecycle newInstance( KernelContext kernelContext, UdcKernelExtensionFac
throws Throwable throws Throwable
{ {
return new UdcKernelExtension( return new UdcKernelExtension(
loadConfig( dependencies.config() ), dependencies.config().with( loadUdcProperties() ),
dependencies.dataSourceManager(), dependencies.dataSourceManager(),
dependencies.idGeneratorFactory(), dependencies.idGeneratorFactory(),
dependencies.startupStats(), dependencies.startupStats(),
Expand All @@ -91,33 +87,15 @@ private boolean isAlwaysDaemon()
return true; return true;
} }


private Config loadConfig( Config config ) private Map<String, String> loadUdcProperties()
{
Properties udcProps = loadUdcProperties();
HashMap<String, String> configParams = new HashMap<String, String>( config.getParams() );
for ( Map.Entry<Object, Object> entry : udcProps.entrySet() )
{
configParams.put( (String) entry.getKey(), (String) entry.getValue() );
}
return new Config( configParams );
}

private Properties loadUdcProperties()
{ {
Properties sysProps = new Properties();
try try
{ {
InputStream resource = getClass().getResourceAsStream( "/org/neo4j/ext/udc/udc.properties" ); return MapUtil.load( getClass().getResourceAsStream( "/org/neo4j/ext/udc/udc.properties" ) );
if ( resource != null )
{
sysProps.load( resource );
}
} }
catch ( Exception e ) catch ( Exception e )
{ {
// AN: commenting out as this provides no value to the user return new HashMap<>();
//System.err.println( "failed to load udc.properties, because: " + e );
} }
return sysProps;
} }
} }
Expand Up @@ -43,6 +43,10 @@
import org.neo4j.kernel.impl.logging.LogService; import org.neo4j.kernel.impl.logging.LogService;
import org.neo4j.logging.Log; import org.neo4j.logging.Log;


import static org.neo4j.kernel.configuration.Settings.BOOLEAN;
import static org.neo4j.kernel.configuration.Settings.INTEGER;
import static org.neo4j.kernel.configuration.Settings.setting;

@Service.Implementation( ManagementSupport.class ) @Service.Implementation( ManagementSupport.class )
public class HotspotManagementSupport extends AdvancedManagementSupport public class HotspotManagementSupport extends AdvancedManagementSupport
{ {
Expand Down Expand Up @@ -76,11 +80,7 @@ protected JMXServiceURL getJMXServiceURL( KernelData kernel )
{ {
log.warn( "Failed to load local JMX connection URL.", e.getTargetException() ); log.warn( "Failed to load local JMX connection URL.", e.getTargetException() );
} }
catch ( LinkageError e ) catch ( LinkageError | Exception e )
{
log.warn( "Failed to load local JMX connection URL.", e );
}
catch ( Exception e )
{ {
log.warn( "Failed to load local JMX connection URL.", e ); log.warn( "Failed to load local JMX connection URL.", e );
} }
Expand All @@ -90,15 +90,15 @@ protected JMXServiceURL getJMXServiceURL( KernelData kernel )
int port = 0; int port = 0;
try try
{ {
port = Integer.parseInt( kernel.getConfig().getParams().get( "jmx.port" ) ); port = kernel.getConfig().get( setting( "jmx.port", INTEGER, "0" ) );
} }
catch ( NumberFormatException ok ) catch ( NumberFormatException ok )
{ {
// handled by 0-check // handled by 0-check
} }
if ( port > 0 ) if ( port > 0 )
{ {
boolean useSSL = Boolean.parseBoolean( kernel.getConfig().getParams().get( "jmx.use_ssl" ) ); boolean useSSL = kernel.getConfig().get( setting( "jmx.use_ssl", BOOLEAN, "false" ) );
log.debug( "Creating new MBean server on port %s%s", port, useSSL ? " using ssl" : "" ); log.debug( "Creating new MBean server on port %s%s", port, useSSL ? " using ssl" : "" );
JMXConnectorServer server = createServer( port, useSSL, log ); JMXConnectorServer server = createServer( port, useSSL, log );
if ( server != null ) if ( server != null )
Expand Down Expand Up @@ -133,7 +133,7 @@ protected JMXServiceURL getJMXServiceURL( KernelData kernel )


private JMXServiceURL getUrlFrom( Map<String, String> remote ) private JMXServiceURL getUrlFrom( Map<String, String> remote )
{ {
Set<Integer> instances = new HashSet<Integer>(); Set<Integer> instances = new HashSet<>();
for ( String key : remote.keySet() ) for ( String key : remote.keySet() )
{ {
if ( key.startsWith( "sun.management.JMXConnectorServer" ) ) if ( key.startsWith( "sun.management.JMXConnectorServer" ) )
Expand Down Expand Up @@ -248,7 +248,7 @@ private JMXConnectorServer createServer( int port, boolean useSSL, Log log )
log.warn( "Failed to start JMX Server", e ); log.warn( "Failed to start JMX Server", e );
return null; return null;
} }
Map<String, Object> env = new HashMap<String, Object>(); Map<String, Object> env = new HashMap<>();
if ( useSSL ) if ( useSSL )
{ {
env.put( RMIConnectorServer.RMI_CLIENT_SOCKET_FACTORY_ATTRIBUTE, new SslRMIClientSocketFactory() ); env.put( RMIConnectorServer.RMI_CLIENT_SOCKET_FACTORY_ATTRIBUTE, new SslRMIClientSocketFactory() );
Expand Down

0 comments on commit c2daf86

Please sign in to comment.