Repository: incubator-blur Updated Branches: refs/heads/master b2fe85ae4 -> 6fdbd90c4
Adding a new table copy command and basic commands can now be invoked from the blur shell. Project: http://git-wip-us.apache.org/repos/asf/incubator-blur/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-blur/commit/6fdbd90c Tree: http://git-wip-us.apache.org/repos/asf/incubator-blur/tree/6fdbd90c Diff: http://git-wip-us.apache.org/repos/asf/incubator-blur/diff/6fdbd90c Branch: refs/heads/master Commit: 6fdbd90c4265d480754da189a87ab40fb9568e8d Parents: b2fe85a Author: Aaron McCurry <[email protected]> Authored: Mon Oct 20 10:28:13 2014 -0400 Committer: Aaron McCurry <[email protected]> Committed: Mon Oct 20 10:28:13 2014 -0400 ---------------------------------------------------------------------- .../apache/blur/command/TableCopyCommand.java | 178 ++++++++++++++++ .../services/org.apache.blur.command.Commands | 3 +- .../apache/blur/command/BaseCommandManager.java | 13 ++ .../org/apache/blur/command/CommandRunner.java | 20 +- .../blur/command/CommandStatusStateEnum.java | 21 ++ .../org/apache/blur/command/CommandUtil.java | 4 + .../blur/command/ShardCommandManager.java | 5 - .../org/apache/blur/thrift/BlurShardServer.java | 18 +- .../shell/ExecutePlatformCommandCommand.java | 205 +++++++++++++++++++ .../main/java/org/apache/blur/shell/Main.java | 5 +- 10 files changed, 455 insertions(+), 17 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/6fdbd90c/blur-command/src/main/java/org/apache/blur/command/TableCopyCommand.java ---------------------------------------------------------------------- diff --git a/blur-command/src/main/java/org/apache/blur/command/TableCopyCommand.java b/blur-command/src/main/java/org/apache/blur/command/TableCopyCommand.java new file mode 100644 index 0000000..2bd51ee --- /dev/null +++ b/blur-command/src/main/java/org/apache/blur/command/TableCopyCommand.java @@ -0,0 +1,178 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.blur.command; + +import java.io.IOException; +import java.security.PrivilegedExceptionAction; + +import org.apache.blur.command.annotation.Description; +import org.apache.blur.command.annotation.OptionalArgument; +import org.apache.blur.command.annotation.RequiredArgument; +import org.apache.blur.command.commandtype.IndexReadCommandSingleTable; +import org.apache.blur.log.Log; +import org.apache.blur.log.LogFactory; +import org.apache.blur.store.hdfs.DirectoryDecorator; +import org.apache.blur.store.hdfs.HdfsDirectory; +import org.apache.blur.store.hdfs_v2.JoinDirectory; +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.fs.Path; +import org.apache.hadoop.security.UserGroupInformation; +import org.apache.lucene.index.DirectoryReader; +import org.apache.lucene.index.IndexReader; +import org.apache.lucene.store.Directory; +import org.apache.lucene.store.FSDirectory; +import org.apache.lucene.store.IOContext; +import org.apache.lucene.store.IndexInput; +import org.apache.lucene.store.IndexOutput; +import org.apache.lucene.util.IOUtils; + +@Description("Copies the given table to another location.") +public class TableCopyCommand extends IndexReadCommandSingleTable<Long> { + + private static final Log LOG = LogFactory.getLog(TableCopyCommand.class); + + @RequiredArgument("The hdfs destination uri. (e.g. hdfs://namenode/path)") + private String destUri; + + @RequiredArgument("The hdfs user to run copy command.") + private String user; + + @OptionalArgument("If enabled, the copy will resume by removing files that did not complete and recopying.") + private boolean resume = false; + + @Override + public String getName() { + return "table-copy"; + } + + @Override + public Long execute(IndexContext context) throws IOException { + final Configuration configuration = context.getTableContext().getConfiguration(); + final IndexReader indexReader = context.getIndexReader(); + final Shard shard = context.getShard(); + UserGroupInformation remoteUser = UserGroupInformation.createRemoteUser(user); + try { + return remoteUser.doAs(new PrivilegedExceptionAction<Long>() { + @Override + public Long run() throws Exception { + Path path = new Path(destUri); + Directory srcDirectory = getDiretory(indexReader); + HdfsDirectory destDirectory = new HdfsDirectory(configuration, new Path(path, shard.getShard())); + long total = 0; + for (String srcFile : srcDirectory.listAll()) { + if (destDirectory.fileExists(srcFile)) { + LOG.info("File [{0}] already exists in dest directory."); + long srcFileLength = srcDirectory.fileLength(srcFile); + long destFileLength = destDirectory.fileLength(srcFile); + if (srcFileLength != destFileLength) { + LOG.info("Deleting file [{0}] length of [{1}] is not same as source [{2}].", srcFile, srcFileLength, + destFileLength); + destDirectory.deleteFile(srcFile); + } else { + continue; + } + } + LOG.info("Copying file [{0}] to dest directory.", srcFile); + total += copy(srcFile, srcDirectory, destDirectory); + } + return total; + } + }); + } catch (InterruptedException e) { + throw new IOException(e); + } + } + + private long copy(String file, Directory srcDirectory, HdfsDirectory destDirectory) throws IOException { + long fileLength = srcDirectory.fileLength(file); + IOContext context = IOContext.DEFAULT; + IndexOutput os = null; + IndexInput is = null; + IOException priorException = null; + try { + os = destDirectory.createOutput(file, context); + is = srcDirectory.openInput(file, context); + os.copyBytes(is, is.length()); + } catch (IOException ioe) { + priorException = ioe; + } finally { + boolean success = false; + try { + IOUtils.closeWhileHandlingException(priorException, os, is); + success = true; + } finally { + if (!success) { + try { + destDirectory.deleteFile(file); + } catch (Throwable t) { + } + } + } + } + return fileLength; + } + + private Directory getStorageDir(Directory directory) throws IOException { + if (directory instanceof DirectoryDecorator) { + DirectoryDecorator directoryDecorator = (DirectoryDecorator) directory; + return getStorageDir(directoryDecorator.getOriginalDirectory()); + } + if (directory instanceof JoinDirectory) { + return directory; + } + if (directory instanceof HdfsDirectory) { + return directory; + } + if (directory instanceof FSDirectory) { + return directory; + } + throw new IOException("Directory [" + directory + "] not supported."); + } + + private Directory getDiretory(IndexReader indexReader) throws IOException { + if (indexReader instanceof DirectoryReader) { + DirectoryReader reader = (DirectoryReader) indexReader; + return getStorageDir(reader.directory()); + } + throw new IOException("Reader Not DirectoryReader."); + } + + public String getDestUri() { + return destUri; + } + + public void setDestUri(String destUri) { + this.destUri = destUri; + } + + public String getUser() { + return user; + } + + public void setUser(String user) { + this.user = user; + } + + public boolean isResume() { + return resume; + } + + public void setResume(boolean resume) { + this.resume = resume; + } + +} http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/6fdbd90c/blur-command/src/main/resources/META-INF/services/org.apache.blur.command.Commands ---------------------------------------------------------------------- diff --git a/blur-command/src/main/resources/META-INF/services/org.apache.blur.command.Commands b/blur-command/src/main/resources/META-INF/services/org.apache.blur.command.Commands index 00fd53e..710e385 100644 --- a/blur-command/src/main/resources/META-INF/services/org.apache.blur.command.Commands +++ b/blur-command/src/main/resources/META-INF/services/org.apache.blur.command.Commands @@ -15,4 +15,5 @@ org.apache.blur.command.DocumentCount org.apache.blur.command.DocumentCountDefaultClusterCombine -org.apache.blur.command.TermsCommand \ No newline at end of file +org.apache.blur.command.TermsCommand +org.apache.blur.command.TableCopyCommand \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/6fdbd90c/blur-core/src/main/java/org/apache/blur/command/BaseCommandManager.java ---------------------------------------------------------------------- diff --git a/blur-core/src/main/java/org/apache/blur/command/BaseCommandManager.java b/blur-core/src/main/java/org/apache/blur/command/BaseCommandManager.java index 1f13e48..ab8c46c 100644 --- a/blur-core/src/main/java/org/apache/blur/command/BaseCommandManager.java +++ b/blur-core/src/main/java/org/apache/blur/command/BaseCommandManager.java @@ -103,6 +103,19 @@ public abstract class BaseCommandManager implements Closeable { } } + public void cancel(ExecutionId executionId) { + ResponseFuture responseFuture = _runningMap.get(executionId); + responseFuture.cancel(true); + } + + public List<String> commandStatusList(CommandStatusStateEnum commandStatus) { + throw new RuntimeException("Not implemented."); + } + + public void cancel(String executionId) { + cancel(new ExecutionId(executionId)); + } + private TimerTask getTimerTaskForRemovalOfOldCommands() { return new TimerTask() { @Override http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/6fdbd90c/blur-core/src/main/java/org/apache/blur/command/CommandRunner.java ---------------------------------------------------------------------- diff --git a/blur-core/src/main/java/org/apache/blur/command/CommandRunner.java b/blur-core/src/main/java/org/apache/blur/command/CommandRunner.java index 9818bc8..f88d6c2 100644 --- a/blur-core/src/main/java/org/apache/blur/command/CommandRunner.java +++ b/blur-core/src/main/java/org/apache/blur/command/CommandRunner.java @@ -38,6 +38,7 @@ import org.apache.blur.thrift.BlurClient.BlurClientInvocationHandler; import org.apache.blur.thrift.BlurClientManager; import org.apache.blur.thrift.ClientPool; import org.apache.blur.thrift.Connection; +import org.apache.blur.thrift.generated.Arguments; import org.apache.blur.thrift.generated.Blur; import org.apache.blur.thrift.generated.Blur.Client; import org.apache.blur.thrift.generated.Blur.Iface; @@ -47,7 +48,7 @@ import org.apache.blur.thrift.generated.Response; import org.apache.blur.thrift.generated.TimeoutException; public class CommandRunner { - private static Connection[] getConnection(Iface client) { + public static Connection[] getConnection(Iface client) { if (client instanceof Proxy) { InvocationHandler invocationHandler = Proxy.getInvocationHandler(client); if (invocationHandler instanceof BlurClientInvocationHandler) { @@ -189,25 +190,31 @@ public class CommandRunner { return (T) runInternal((Command<?>) command, connection); } - private static Object runInternal(Command<?> command, Connection... connectionsArray) throws TTransportException, + public static Object runInternal(Command<?> command, Connection... connectionsArray) throws TTransportException, IOException, BlurException, TimeoutException, TException { + BlurObjectSerDe serde = new BlurObjectSerDe(); + Arguments arguments = CommandUtil.toArguments(command, serde); + Object thriftObject = runInternalReturnThriftObject(command.getName(), arguments, connectionsArray); + return serde.fromSupportedThriftObject(thriftObject); + } + + public static Object runInternalReturnThriftObject(String name, Arguments arguments, Connection... connectionsArray) + throws TTransportException, IOException, BlurException, TimeoutException, TException { List<Connection> connections = new ArrayList<Connection>(Arrays.asList(connectionsArray)); Collections.shuffle(connections); - BlurObjectSerDe serde = new BlurObjectSerDe(); for (Connection connection : connections) { if (BlurClientManager.isBadConnection(connection)) { continue; } ClientPool clientPool = BlurClientManager.getClientPool(); Client client = clientPool.getClient(connection); - try { String executionId = null; Response response; INNER: while (true) { try { if (executionId == null) { - response = client.execute(command.getName(), CommandUtil.toArguments(command, serde)); + response = client.execute(name, arguments); } else { response = client.reconnect(executionId); } @@ -216,8 +223,7 @@ public class CommandRunner { executionId = te.getExecutionId(); } } - Object thriftObject = CommandUtil.fromThriftResponseToObject(response); - return serde.fromSupportedThriftObject(thriftObject); + return CommandUtil.fromThriftResponseToObject(response); } finally { clientPool.returnClient(connection, client); } http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/6fdbd90c/blur-core/src/main/java/org/apache/blur/command/CommandStatusStateEnum.java ---------------------------------------------------------------------- diff --git a/blur-core/src/main/java/org/apache/blur/command/CommandStatusStateEnum.java b/blur-core/src/main/java/org/apache/blur/command/CommandStatusStateEnum.java new file mode 100644 index 0000000..575bb9d --- /dev/null +++ b/blur-core/src/main/java/org/apache/blur/command/CommandStatusStateEnum.java @@ -0,0 +1,21 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.blur.command; + +public enum CommandStatusStateEnum { + RUNNING, INTERRUPTED, COMPLETE, BACK_PRESSURE_INTERRUPTED; +} http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/6fdbd90c/blur-core/src/main/java/org/apache/blur/command/CommandUtil.java ---------------------------------------------------------------------- diff --git a/blur-core/src/main/java/org/apache/blur/command/CommandUtil.java b/blur-core/src/main/java/org/apache/blur/command/CommandUtil.java index bb87f2d..1c5b652 100644 --- a/blur-core/src/main/java/org/apache/blur/command/CommandUtil.java +++ b/blur-core/src/main/java/org/apache/blur/command/CommandUtil.java @@ -216,6 +216,10 @@ public class CommandUtil { Map<String, Object> args = new HashMap<String, Object>(); addArguments(clazz, args, command); BlurObject blurObject = serde.serialize(args); + return toArguments(blurObject); + } + + public static Arguments toArguments(BlurObject blurObject) throws BlurException { Arguments arguments = new Arguments(); Iterator<String> keys = blurObject.keys(); while (keys.hasNext()) { http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/6fdbd90c/blur-core/src/main/java/org/apache/blur/command/ShardCommandManager.java ---------------------------------------------------------------------- diff --git a/blur-core/src/main/java/org/apache/blur/command/ShardCommandManager.java b/blur-core/src/main/java/org/apache/blur/command/ShardCommandManager.java index d22df33..a7b10bd 100644 --- a/blur-core/src/main/java/org/apache/blur/command/ShardCommandManager.java +++ b/blur-core/src/main/java/org/apache/blur/command/ShardCommandManager.java @@ -272,9 +272,4 @@ public class ShardCommandManager extends BaseCommandManager { } - public void cancel(ExecutionId executionId) { - // TODO - System.out.println("IMPLEMENT ME!!!!"); - } - } http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/6fdbd90c/blur-core/src/main/java/org/apache/blur/thrift/BlurShardServer.java ---------------------------------------------------------------------- diff --git a/blur-core/src/main/java/org/apache/blur/thrift/BlurShardServer.java b/blur-core/src/main/java/org/apache/blur/thrift/BlurShardServer.java index c6e2a2f..c1a5b16 100644 --- a/blur-core/src/main/java/org/apache/blur/thrift/BlurShardServer.java +++ b/blur-core/src/main/java/org/apache/blur/thrift/BlurShardServer.java @@ -34,6 +34,7 @@ import java.util.concurrent.atomic.AtomicLongArray; import org.apache.blur.command.ArgumentOverlay; import org.apache.blur.command.BlurObject; import org.apache.blur.command.BlurObjectSerDe; +import org.apache.blur.command.CommandStatusStateEnum; import org.apache.blur.command.CommandUtil; import org.apache.blur.command.ExecutionId; import org.apache.blur.command.Response; @@ -669,7 +670,12 @@ public class BlurShardServer extends TableAdmin implements Iface { @Override public List<String> commandStatusList(int startingAt, short fetch, CommandStatusState state) throws BlurException, TException { - throw new BException("Not Implemented"); + try { + List<String> ids = _commandManager.commandStatusList(toCommandStatus(state)); + return ids.subList(startingAt, fetch); + } catch (Exception e) { + throw new BException(e.getMessage(), e); + } } @Override @@ -679,6 +685,14 @@ public class BlurShardServer extends TableAdmin implements Iface { @Override public void commandCancel(String executionId) throws BlurException, TException { - throw new BException("Not Implemented"); + try { + _commandManager.cancel(executionId); + } catch (Exception e) { + throw new BException(e.getMessage(), e); + } + } + + private CommandStatusStateEnum toCommandStatus(CommandStatusState state) { + return CommandStatusStateEnum.valueOf(state.name()); } } http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/6fdbd90c/blur-shell/src/main/java/org/apache/blur/shell/ExecutePlatformCommandCommand.java ---------------------------------------------------------------------- diff --git a/blur-shell/src/main/java/org/apache/blur/shell/ExecutePlatformCommandCommand.java b/blur-shell/src/main/java/org/apache/blur/shell/ExecutePlatformCommandCommand.java new file mode 100644 index 0000000..fe55e88 --- /dev/null +++ b/blur-shell/src/main/java/org/apache/blur/shell/ExecutePlatformCommandCommand.java @@ -0,0 +1,205 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.blur.shell; + +import java.io.IOException; +import java.io.PrintWriter; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Set; +import java.util.TreeMap; + +import org.apache.blur.command.BlurObject; +import org.apache.blur.command.BlurObjectSerDe; +import org.apache.blur.command.CommandRunner; +import org.apache.blur.command.CommandUtil; +import org.apache.blur.thirdparty.thrift_0_9_0.TException; +import org.apache.blur.thrift.Connection; +import org.apache.blur.thrift.generated.ArgumentDescriptor; +import org.apache.blur.thrift.generated.Arguments; +import org.apache.blur.thrift.generated.Blur; +import org.apache.blur.thrift.generated.CommandDescriptor; +import org.apache.blur.thrift.generated.Blur.Iface; +import org.apache.blur.thrift.generated.BlurException; +import org.apache.commons.cli.CommandLine; +import org.apache.commons.cli.CommandLineParser; +import org.apache.commons.cli.HelpFormatter; +import org.apache.commons.cli.Option; +import org.apache.commons.cli.OptionBuilder; +import org.apache.commons.cli.Options; +import org.apache.commons.cli.ParseException; +import org.apache.commons.cli.PosixParser; + +public class ExecutePlatformCommandCommand extends Command implements CommandFirstArgCommand { + @Override + public void doit(PrintWriter out, Blur.Iface client, String[] args) throws CommandException, TException, + BlurException { + if (args.length < 2) { + throw new CommandException("Invalid args: " + help()); + } + String commandName = args[1]; + + CommandDescriptor command = getCommandDescriptor(client, commandName); + if (command == null) { + throw new CommandException("Command [" + commandName + "] not found."); + } + + BlurObjectSerDe serde = new BlurObjectSerDe(); + BlurObject blurObject = getArgs(args, command, out); + if (blurObject == null) { + return; + } + Arguments arguments = CommandUtil.toArguments(blurObject); + + Connection[] connection = CommandRunner.getConnection(client); + Object thriftObject; + try { + thriftObject = CommandRunner.runInternalReturnThriftObject(commandName, arguments, connection); + } catch (IOException e) { + if (Main.debug) { + e.printStackTrace(); + } + throw new CommandException(e.getMessage()); + } + Object javaObject = serde.fromSupportedThriftObject(thriftObject); + out.println(javaObject); + } + + private BlurObject getArgs(String[] args, CommandDescriptor command, PrintWriter out) throws CommandException { + Options options = new Options(); + Map<String, ArgumentDescriptor> requiredArguments = new TreeMap<String, ArgumentDescriptor>( + command.getRequiredArguments()); + addOptions(true, options, requiredArguments.entrySet()); + Map<String, ArgumentDescriptor> optionalArguments = new TreeMap<String, ArgumentDescriptor>( + command.getOptionalArguments()); + addOptions(false, options, optionalArguments.entrySet()); + CommandLine commandLine = parse(command.getCommandName(), options, args, out); + return createBlurObject(commandLine, command); + } + + private BlurObject createBlurObject(CommandLine commandLine, CommandDescriptor descriptor) throws CommandException { + Map<String, ArgumentDescriptor> arguments = new TreeMap<String, ArgumentDescriptor>( + descriptor.getOptionalArguments()); + arguments.putAll(descriptor.getRequiredArguments()); + BlurObject blurObject = new BlurObject(); + if (commandLine == null) { + return null; + } + Option[] options = commandLine.getOptions(); + for (Option option : options) { + String name = option.getOpt(); + String value = option.getValue(); + ArgumentDescriptor argumentDescriptor = arguments.get(name); + String type = argumentDescriptor.getType(); + blurObject.put(name, convertIfNeeded(value, type)); + } + return blurObject; + } + + private Object convertIfNeeded(String value, String type) throws CommandException { + if (type.equals("String")) { + return value; + } else if (type.equals("Long")) { + return Long.parseLong(value); + } else if (type.equals("Integer")) { + return Integer.parseInt(value); + } else if (type.equals("Short")) { + return Short.parseShort(value); + } else if (type.equals("Float")) { + return Float.parseFloat(value); + } else if (type.equals("Double")) { + return Double.parseDouble(value); + } else if (type.equals("Boolean")) { + return Boolean.parseBoolean(value); + } + throw new CommandException("Type of [" + type + "] is not supported by the shell."); + } + + private CommandLine parse(String commandName, Options options, String[] args, PrintWriter out) { + CommandLineParser parser = new PosixParser(); + CommandLine cmd = null; + try { + cmd = parser.parse(options, args); + if (cmd.hasOption("h")) { + HelpFormatter formatter = new HelpFormatter(); + PrintWriter pw = new PrintWriter(out, true); + formatter.printHelp(pw, HelpFormatter.DEFAULT_WIDTH, commandName, null, options, + HelpFormatter.DEFAULT_LEFT_PAD, HelpFormatter.DEFAULT_DESC_PAD, null, false); + return null; + } + } catch (ParseException e) { + HelpFormatter formatter = new HelpFormatter(); + PrintWriter pw = new PrintWriter(out, true); + formatter.printHelp(pw, HelpFormatter.DEFAULT_WIDTH, commandName, null, options, HelpFormatter.DEFAULT_LEFT_PAD, + HelpFormatter.DEFAULT_DESC_PAD, null, false); + return null; + } + return cmd; + } + + private void addOptions(boolean required, Options options, Set<Entry<String, ArgumentDescriptor>> entrySet) { + for (Entry<String, ArgumentDescriptor> e : entrySet) { + String argumentName = e.getKey(); + ArgumentDescriptor argumentDescriptor = e.getValue(); + Option option = OptionBuilder.create(argumentName); + option.setRequired(required); + String description = argumentDescriptor.getDescription(); + option.setDescription(createDescription(description, required)); + option.setArgs(1); + options.addOption(option); + } + } + + private String createDescription(String description, boolean required) { + if (description == null) { + description = "No description."; + } + if (required) { + return "Required - " + description; + } else { + return "Optional - " + description; + } + } + + private CommandDescriptor getCommandDescriptor(Iface client, String commandName) throws BlurException, TException { + List<CommandDescriptor> listInstalledCommands = client.listInstalledCommands(); + for (CommandDescriptor commandDescriptor : listInstalledCommands) { + if (commandDescriptor.getCommandName().equals(commandName)) { + return commandDescriptor; + } + } + return null; + } + + @Override + public String description() { + return "Execute a platform command."; + } + + @Override + public String usage() { + return ""; + } + + @Override + public String name() { + return "command-exec"; + } +} http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/6fdbd90c/blur-shell/src/main/java/org/apache/blur/shell/Main.java ---------------------------------------------------------------------- diff --git a/blur-shell/src/main/java/org/apache/blur/shell/Main.java b/blur-shell/src/main/java/org/apache/blur/shell/Main.java index 6a21e44..63ee125 100644 --- a/blur-shell/src/main/java/org/apache/blur/shell/Main.java +++ b/blur-shell/src/main/java/org/apache/blur/shell/Main.java @@ -694,6 +694,7 @@ public class Main { register(builder, new QueryCommand()); register(builder, new ListPlatformCommandsCommand()); register(builder, new DescribePlatformCommandCommand()); + register(builder, new ExecutePlatformCommandCommand()); commands = builder.build(); } @@ -701,8 +702,8 @@ public class Main { builder.put(command.name(), command); } - private static void setPrompt(Iface client, ConsoleReader reader, PrintWriter out) - throws BlurException, TException, CommandException, IOException { + private static void setPrompt(Iface client, ConsoleReader reader, PrintWriter out) throws BlurException, TException, + CommandException, IOException { List<String> shardClusterList; try { shardClusterList = client.shardClusterList();
