diff --git a/community/command-line/src/main/java/org/neo4j/commandline/admin/AdminTool.java b/community/command-line/src/main/java/org/neo4j/commandline/admin/AdminTool.java index d6d5dbbb81f7d..a9864cf101562 100644 --- a/community/command-line/src/main/java/org/neo4j/commandline/admin/AdminTool.java +++ b/community/command-line/src/main/java/org/neo4j/commandline/admin/AdminTool.java @@ -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 @@ -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"; @@ -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 ); @@ -153,54 +148,4 @@ private static Result success() { return () -> System.exit( 0 ); } - - private interface Result - { - void exit(); - } - - public interface CommandLocator - extends Function, Supplier> - { - } - - private static class ServiceCommandLocator implements CommandLocator - { - @Override - public AdminCommand.Provider apply( String name ) - { - return Service.load( AdminCommand.Provider.class, name ); - } - - @Override - public Iterable get() - { - return Service.load( AdminCommand.Provider.class ); - } - } - - private class AppendingLocator implements CommandLocator - { - private final Supplier command; - private final CommandLocator commands; - - public AppendingLocator( Supplier 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 get() - { - return Iterables.append( command.get(), commands.get() ); - } - } } diff --git a/community/command-line/src/main/java/org/neo4j/commandline/admin/CommandLocator.java b/community/command-line/src/main/java/org/neo4j/commandline/admin/CommandLocator.java new file mode 100644 index 0000000000000..277e850047244 --- /dev/null +++ b/community/command-line/src/main/java/org/neo4j/commandline/admin/CommandLocator.java @@ -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 . + */ +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, Supplier> +{ + + static CommandLocator fromServiceLocator() + { + return new CommandLocator() + { + @Override + public AdminCommand.Provider apply( String name ) + { + return Service.load( AdminCommand.Provider.class, name ); + } + + @Override + public Iterable get() + { + return Service.load( AdminCommand.Provider.class ); + } + }; + } + + static CommandLocator withAdditionalCommand( Supplier 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 get() + { + return Iterables.append( command.get(), commands.get() ); + } + }; + } +} diff --git a/community/command-line/src/main/java/org/neo4j/commandline/admin/Result.java b/community/command-line/src/main/java/org/neo4j/commandline/admin/Result.java new file mode 100644 index 0000000000000..d056c777e87a6 --- /dev/null +++ b/community/command-line/src/main/java/org/neo4j/commandline/admin/Result.java @@ -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 . + */ +package org.neo4j.commandline.admin; + +/** + * The result of an admin command execution, with a delayed program termination action. + */ +public interface Result +{ + void exit(); +} diff --git a/community/command-line/src/test/java/org/neo4j/commandline/admin/AdminToolTest.java b/community/command-line/src/test/java/org/neo4j/commandline/admin/AdminToolTest.java index fb2fb53115b52..e48383538dd03 100644 --- a/community/command-line/src/test/java/org/neo4j/commandline/admin/AdminToolTest.java +++ b/community/command-line/src/test/java/org/neo4j/commandline/admin/AdminToolTest.java @@ -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; @@ -116,7 +116,7 @@ public Iterable get() } } - private static class NullCommandLocator implements AdminTool.CommandLocator + private static class NullCommandLocator implements CommandLocator { @Override public AdminCommand.Provider apply( String s )