Skip to content

Commit

Permalink
Switch server to use new --config-dir argument
Browse files Browse the repository at this point in the history
  • Loading branch information
benbc committed Mar 22, 2016
1 parent d28b303 commit 3972c9d
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 56 deletions.
Expand Up @@ -25,7 +25,7 @@
import org.neo4j.helpers.Args;
import org.neo4j.helpers.collection.Pair;
import org.neo4j.kernel.impl.util.Converters;
import org.neo4j.server.configuration.ServerSettings;
import org.neo4j.server.configuration.ConfigLoader;

import static org.neo4j.helpers.collection.Pair.pair;

Expand All @@ -41,21 +41,20 @@
*/
public class ServerCommandLineArgs
{
static final String CONFIG_KEY_ALT_1 = "config";
static final String CONFIG_KEY_ALT_2 = "C";
private final File configFile;
private static final String CONFIG_DIR_ARG = "config-dir";
private final Args args;
private final Pair<String, String>[] configOverrides;

private ServerCommandLineArgs( File configFile, Pair<String, String>[] configOverrides )
private ServerCommandLineArgs( Args args, Pair<String, String>[] configOverrides )
{
this.configFile = configFile;
this.args = args;
this.configOverrides = configOverrides;
}

public static ServerCommandLineArgs parse( String[] argv )
{
Args args = Args.parse( argv );
return new ServerCommandLineArgs( detemineConfigFile( args ), parseConfigOverrides( args ) );
return new ServerCommandLineArgs( args, parseConfigOverrides( args ) );
}

public Pair<String, String>[] configOverrides()
Expand All @@ -65,13 +64,11 @@ public Pair<String, String>[] configOverrides()

public File configFile()
{
return configFile;
}

private static File detemineConfigFile( Args arguments )
{
return new File( arguments.get( CONFIG_KEY_ALT_2,
arguments.get( CONFIG_KEY_ALT_1, ServerSettings.SERVER_CONFIG_FILE ) ) );
if ( !args.has( CONFIG_DIR_ARG ) )
{
return null;
}
return new File( new File( args.get( CONFIG_DIR_ARG ) ), ConfigLoader.DEFAULT_CONFIG_FILE_NAME );
}

private static Pair<String, String>[] parseConfigOverrides( Args arguments )
Expand Down
Expand Up @@ -36,6 +36,8 @@

public class ConfigLoader
{
public static final String DEFAULT_CONFIG_FILE_NAME = "neo4j.conf";

private final SettingsClasses settingsClasses;

public ConfigLoader( SettingsClasses settingsClasses )
Expand Down
Expand Up @@ -31,6 +31,7 @@
import org.junit.rules.TemporaryFolder;

import org.neo4j.graphdb.config.Setting;
import org.neo4j.server.configuration.ConfigLoader;
import org.neo4j.test.server.ExclusiveServerTestBase;

import static org.hamcrest.Matchers.equalTo;
Expand Down Expand Up @@ -98,7 +99,7 @@ public void shouldStartStopNeoServerWithoutAnyConfigFiles() throws IOException
public void canSpecifyConfigFile() throws Throwable
{
// Given
File configFile = tempDir.newFile( "neo4j.config" );
File configFile = tempDir.newFile( ConfigLoader.DEFAULT_CONFIG_FILE_NAME );

Map<String, String> properties = stringMap( forced_kernel_id.name(), "ourcustomvalue" );
properties.putAll( ServerTestUtils.getDefaultRelativeProperties() );
Expand All @@ -107,7 +108,7 @@ public void canSpecifyConfigFile() throws Throwable
store( properties, configFile );

// When
ServerBootstrapper.start( bootstrapper, "-C", configFile.getAbsolutePath() );
ServerBootstrapper.start( bootstrapper, "--config-dir", configFile.getParentFile().getAbsolutePath() );

// Then
assertThat( bootstrapper.getServer().getConfig().get( forced_kernel_id ), equalTo( "ourcustomvalue" ) );
Expand All @@ -117,7 +118,7 @@ public void canSpecifyConfigFile() throws Throwable
public void canOverrideConfigValues() throws Throwable
{
// Given
File configFile = tempDir.newFile( "neo4j.config" );
File configFile = tempDir.newFile( ConfigLoader.DEFAULT_CONFIG_FILE_NAME);

Map<String, String> properties = stringMap( forced_kernel_id.name(), "thisshouldnotshowup" );
properties.putAll( ServerTestUtils.getDefaultRelativeProperties() );
Expand All @@ -127,7 +128,7 @@ public void canOverrideConfigValues() throws Throwable

// When
ServerBootstrapper.start( bootstrapper,
"-C", configFile.getAbsolutePath(),
"--config-dir", configFile.getParentFile().getAbsolutePath(),
"-c", configOption( forced_kernel_id, "mycustomvalue" ) );

// Then
Expand Down
Expand Up @@ -24,6 +24,7 @@
import org.junit.Test;

import org.neo4j.helpers.collection.Pair;
import org.neo4j.server.configuration.ConfigLoader;

import static org.junit.Assert.assertEquals;

Expand All @@ -35,21 +36,15 @@ public class ServerCommandLineArgsTest
@Test
public void shouldPickUpSpecifiedConfigFile() throws Exception
{
shouldPickUpSpecifiedConfigFile( ServerCommandLineArgs.CONFIG_KEY_ALT_1 );
shouldPickUpSpecifiedConfigFile( ServerCommandLineArgs.CONFIG_KEY_ALT_2 );
File expectedFile = new File( "some-dir/" + ConfigLoader.DEFAULT_CONFIG_FILE_NAME );
assertEquals( expectedFile, parse( "--config-dir", "some-dir" ).configFile() );
assertEquals( expectedFile, parse( "--config-dir=some-dir" ).configFile() );
}

public void shouldPickUpSpecifiedConfigFile( String key )
@Test
public void shouldReturnNullIfConfigDirIsNotSpecified()
{
// GIVEN
String customConfigFile = "MyConfigFile";
String[] args = array( "--" + key, customConfigFile );

// WHEN
ServerCommandLineArgs parsed = ServerCommandLineArgs.parse( args );

// THEN
assertEquals( new File( customConfigFile ), parsed.configFile() );
assertEquals( null, parse().configFile() );
}

@Test
Expand Down Expand Up @@ -94,10 +89,15 @@ public void shouldPickUpMultipleOverriddenConfigurationParameters() throws Excep

// THEN
assertEquals( asSet(
Pair.of( "my_first_option", "first" ),
Pair.of( "myoptionenabled", Boolean.TRUE.toString() ),
Pair.of( "my_second_option", "second" ) ),
Pair.of( "my_first_option", "first" ),
Pair.of( "myoptionenabled", Boolean.TRUE.toString() ),
Pair.of( "my_second_option", "second" ) ),

asSet( parsed.configOverrides() ) );
}

private ServerCommandLineArgs parse( String... args )
{
return ServerCommandLineArgs.parse( args );
}
}
Expand Up @@ -19,21 +19,21 @@
*/
package org.neo4j.server.enterprise;

import java.io.File;
import java.io.IOException;

import org.neo4j.server.BlockingBootstrapper;
import org.neo4j.server.Bootstrapper;
import org.neo4j.server.ServerBootstrapper;
import org.neo4j.server.configuration.ServerSettings;
import org.neo4j.server.ServerCommandLineArgs;

public class ArbiterEntryPoint
{
private static Bootstrapper bootstrapper;

public static void main( String[] args ) throws IOException
public static void main( String[] argv ) throws IOException
{
int status = new ArbiterBootstrapper().start( getConfigFile() );
ServerCommandLineArgs args = ServerCommandLineArgs.parse( argv );
int status = new ArbiterBootstrapper().start( args.configFile() );
if ( status != 0 )
{
System.exit( status );
Expand All @@ -53,21 +53,4 @@ public static void stop( @SuppressWarnings("UnusedParameters") String[] args )
bootstrapper.stop();
}
}

static File getConfigFile()
{
String configPath = System.getProperty( ServerSettings.SERVER_CONFIG_FILE_KEY );
if ( configPath == null )
{
throw new RuntimeException( "System property " + ServerSettings.SERVER_CONFIG_FILE_KEY +
" must be provided" );
}

File configFile = new File( configPath );
if ( !configFile.exists() )
{
throw new IllegalArgumentException( configFile + " doesn't exist" );
}
return configFile;
}
}
Expand Up @@ -21,19 +21,23 @@

import java.io.IOException;

import org.neo4j.server.ServerCommandLineArgs;

public class ArbiterBootstrapperTestProxy
{
public static final String START_SIGNAL = "starting";

public static void main( String[] args ) throws IOException
public static void main( String[] argv ) throws IOException
{
ServerCommandLineArgs args = ServerCommandLineArgs.parse( argv );

// This sysout will be intercepted by the parent process and will trigger
// a start of a timeout. The whole reason for this class to be here is to
// split awaiting for the process to start and actually awaiting the cluster client to start.
System.out.println( START_SIGNAL );
try ( ArbiterBootstrapper arbiter = new ArbiterBootstrapper() )
{
arbiter.start( ArbiterEntryPoint.getConfigFile() );
arbiter.start( args.configFile() );
System.in.read();
}
}
Expand Down

0 comments on commit 3972c9d

Please sign in to comment.