Skip to content

Commit

Permalink
AdminTool internals refactorings
Browse files Browse the repository at this point in the history
This makes it slightly easier to test command providers, and also aligns the visibilities.
For instance, methods that returned `Result` objects were publicly visible, while the `Result` interface was private.
  • Loading branch information
chrisvest committed Jul 20, 2016
1 parent 1853fd4 commit 1b1ccb4
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 61 deletions.
Expand Up @@ -23,13 +23,8 @@
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.function.Function;
import java.util.function.Supplier;

import org.neo4j.helpers.Service;
import org.neo4j.helpers.collection.Iterables;

import static java.lang.String.format;

public class AdminTool
Expand All @@ -41,9 +36,9 @@ public static void main( String[] args )
String extraHelp = System.getenv( "NEO4J_EXTRA_HELP" );
boolean debug = System.getenv( "NEO4J_DEBUG" ) != null;

new AdminTool( new ServiceCommandLocator(), System.out::println, extraHelp, debug )
.execute( homeDir, configDir, args )
.exit();
AdminTool tool = new AdminTool( CommandLocator.fromServiceLocator(), System.out::println, extraHelp, debug );
Result result = tool.execute( homeDir, configDir, args );
result.exit();
}

private final String scriptName = "neo4j-admin";
Expand All @@ -54,7 +49,7 @@ public static void main( String[] args )

public AdminTool( CommandLocator locator, Output out, String extraHelp, boolean debug )
{
this.locator = new AppendingLocator( help(), locator );
this.locator = CommandLocator.withAdditionalCommand( help(), locator );
this.out = out;
this.debug = debug;
this.usage = new Usage( scriptName, out, this.locator, extraHelp );
Expand Down Expand Up @@ -153,54 +148,4 @@ private static Result success()
{
return () -> System.exit( 0 );
}

private interface Result
{
void exit();
}

public interface CommandLocator
extends Function<String, AdminCommand.Provider>, Supplier<Iterable<AdminCommand.Provider>>
{
}

private static class ServiceCommandLocator implements CommandLocator
{
@Override
public AdminCommand.Provider apply( String name )
{
return Service.load( AdminCommand.Provider.class, name );
}

@Override
public Iterable<AdminCommand.Provider> get()
{
return Service.load( AdminCommand.Provider.class );
}
}

private class AppendingLocator implements CommandLocator
{
private final Supplier<AdminCommand.Provider> command;
private final CommandLocator commands;

public AppendingLocator( Supplier<AdminCommand.Provider> command, CommandLocator commands )
{
this.command = command;
this.commands = commands;
}

@Override
public AdminCommand.Provider apply( String name )
{
AdminCommand.Provider provider = command.get();
return Objects.equals( name, provider.name() ) ? provider : commands.apply( name );
}

@Override
public Iterable<AdminCommand.Provider> get()
{
return Iterables.append( command.get(), commands.get() );
}
}
}
@@ -0,0 +1,73 @@
/*
* Copyright (c) 2002-2016 "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.commandline.admin;

import java.util.Objects;
import java.util.function.Function;
import java.util.function.Supplier;

import org.neo4j.helpers.Service;
import org.neo4j.helpers.collection.Iterables;

/**
* The CommandLocator locates named commands for the AdminTool, or supplies the set of available commands for printing
* help output.
*/
public interface CommandLocator
extends Function<String, AdminCommand.Provider>, Supplier<Iterable<AdminCommand.Provider>>
{

static CommandLocator fromServiceLocator()
{
return new CommandLocator()
{
@Override
public AdminCommand.Provider apply( String name )
{
return Service.load( AdminCommand.Provider.class, name );
}

@Override
public Iterable<AdminCommand.Provider> get()
{
return Service.load( AdminCommand.Provider.class );
}
};
}

static CommandLocator withAdditionalCommand( Supplier<AdminCommand.Provider> command, CommandLocator commands )
{
return new CommandLocator()
{
@Override
public AdminCommand.Provider apply( String name )
{
AdminCommand.Provider provider = command.get();
return Objects.equals( name, provider.name() ) ? provider : commands.apply( name );
}

@Override
public Iterable<AdminCommand.Provider> get()
{
return Iterables.append( command.get(), commands.get() );
}
};
}
}
@@ -0,0 +1,28 @@
/*
* Copyright (c) 2002-2016 "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.commandline.admin;

/**
* The result of an admin command execution, with a delayed program termination action.
*/
public interface Result
{
void exit();
}
Expand Up @@ -94,7 +94,7 @@ public void line( String text )
}
}

private static class CannedLocator implements AdminTool.CommandLocator
private static class CannedLocator implements CommandLocator
{
private final RecordingCommand command;

Expand All @@ -116,7 +116,7 @@ public Iterable<AdminCommand.Provider> get()
}
}

private static class NullCommandLocator implements AdminTool.CommandLocator
private static class NullCommandLocator implements CommandLocator
{
@Override
public AdminCommand.Provider apply( String s )
Expand Down

0 comments on commit 1b1ccb4

Please sign in to comment.