http://git-wip-us.apache.org/repos/asf/geode/blob/ce9e138c/geode-core/src/main/java/org/apache/geode/management/internal/cli/help/format/Block.java ---------------------------------------------------------------------- diff --git a/geode-core/src/main/java/org/apache/geode/management/internal/cli/help/format/Block.java b/geode-core/src/main/java/org/apache/geode/management/internal/cli/help/format/Block.java deleted file mode 100644 index dde1a20..0000000 --- a/geode-core/src/main/java/org/apache/geode/management/internal/cli/help/format/Block.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * 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.geode.management.internal.cli.help.format; - -/** - * - */ -public class Block { - private String heading; - private Row[] rows; - - public String getHeading() { - return heading; - } - - public Block setHeading(String heading) { - this.heading = heading; - return this; - } - - public Row[] getRows() { - return rows; - } - - public Block setRows(Row[] rows) { - this.rows = rows; - return this; - } - -}
http://git-wip-us.apache.org/repos/asf/geode/blob/ce9e138c/geode-core/src/main/java/org/apache/geode/management/internal/cli/help/format/DataNode.java ---------------------------------------------------------------------- diff --git a/geode-core/src/main/java/org/apache/geode/management/internal/cli/help/format/DataNode.java b/geode-core/src/main/java/org/apache/geode/management/internal/cli/help/format/DataNode.java deleted file mode 100644 index 8f5e570..0000000 --- a/geode-core/src/main/java/org/apache/geode/management/internal/cli/help/format/DataNode.java +++ /dev/null @@ -1,48 +0,0 @@ -/* - * 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.geode.management.internal.cli.help.format; - -import java.util.List; - -/** - * - */ -public class DataNode { - String data; - List<DataNode> children; - - public DataNode(String data, List<DataNode> dataNode) { - this.data = data; - this.children = dataNode; - } - - public String getData() { - return data; - } - - public List<DataNode> getChildren() { - return children; - } - - public boolean addChild(DataNode dataNode) { - if (this.children != null) { - this.children.add(dataNode); - return true; - } else { - return false; - } - } - -} http://git-wip-us.apache.org/repos/asf/geode/blob/ce9e138c/geode-core/src/main/java/org/apache/geode/management/internal/cli/help/format/Help.java ---------------------------------------------------------------------- diff --git a/geode-core/src/main/java/org/apache/geode/management/internal/cli/help/format/Help.java b/geode-core/src/main/java/org/apache/geode/management/internal/cli/help/format/Help.java deleted file mode 100644 index 68c10bb..0000000 --- a/geode-core/src/main/java/org/apache/geode/management/internal/cli/help/format/Help.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * 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.geode.management.internal.cli.help.format; - -/** - * - */ -public class Help { - private Block[] blocks; - - public Block[] getBlocks() { - return blocks; - } - - public Help setBlocks(Block[] block) { - this.blocks = block; - return this; - } - - @Override - public String toString() { - StringBuffer buffer = new StringBuffer(); - for (Block block : blocks) { - buffer.append(block.getHeading() + "\n"); - for (Row row : block.getRows()) { - buffer.append("\t" + row.getInfo()[0] + "\n"); - } - buffer.append("\n"); - } - return buffer.toString(); - } -} http://git-wip-us.apache.org/repos/asf/geode/blob/ce9e138c/geode-core/src/main/java/org/apache/geode/management/internal/cli/help/format/HelpBlock.java ---------------------------------------------------------------------- diff --git a/geode-core/src/main/java/org/apache/geode/management/internal/cli/help/format/HelpBlock.java b/geode-core/src/main/java/org/apache/geode/management/internal/cli/help/format/HelpBlock.java new file mode 100644 index 0000000..b3e01fe --- /dev/null +++ b/geode-core/src/main/java/org/apache/geode/management/internal/cli/help/format/HelpBlock.java @@ -0,0 +1,76 @@ +/* + * 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.geode.management.internal.cli.help.format; + +import org.apache.geode.internal.lang.StringUtils; +import org.apache.geode.management.internal.cli.GfshParser; +import org.apache.geode.management.internal.cli.shell.Gfsh; + +import java.util.ArrayList; +import java.util.List; + +/** + * + */ +public class HelpBlock { + String data = null; + List<HelpBlock> children = new ArrayList<>(); + // indent level + int level = -1; + + public HelpBlock() {} + + public HelpBlock(String data) { + if (!StringUtils.isBlank(data)) { + this.data = data; + this.level = 0; + } + } + + public String getData() { + return data; + } + + public List<HelpBlock> getChildren() { + return children; + } + + public void addChild(HelpBlock helpBlock) { + // before adding another block as the child, increment the indent level + helpBlock.setLevel(level + 1); + children.add(helpBlock); + } + + // recursively set the indent level of the decendents + public void setLevel(int level) { + this.level = level; + for (HelpBlock child : children) { + child.setLevel(level + 1); + } + } + + public String toString() { + StringBuffer builder = new StringBuffer(); + + if (data != null) { + builder.append(Gfsh.wrapText(data, level)); + builder.append(GfshParser.LINE_SEPARATOR); + } + for (HelpBlock child : children) { + builder.append(child.toString()); + } + return builder.toString(); + } +} http://git-wip-us.apache.org/repos/asf/geode/blob/ce9e138c/geode-core/src/main/java/org/apache/geode/management/internal/cli/help/format/NewHelp.java ---------------------------------------------------------------------- diff --git a/geode-core/src/main/java/org/apache/geode/management/internal/cli/help/format/NewHelp.java b/geode-core/src/main/java/org/apache/geode/management/internal/cli/help/format/NewHelp.java deleted file mode 100644 index 90f9eda..0000000 --- a/geode-core/src/main/java/org/apache/geode/management/internal/cli/help/format/NewHelp.java +++ /dev/null @@ -1,52 +0,0 @@ -/* - * 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.geode.management.internal.cli.help.format; - -import org.apache.geode.management.internal.cli.GfshParser; -import org.apache.geode.management.internal.cli.shell.Gfsh; - -/** - * - * @since GemFire 7.0 - */ -public class NewHelp { - DataNode root; - - public NewHelp(DataNode root) { - this.root = root; - } - - @Override - public String toString() { - StringBuilder builder = new StringBuilder(); - append(builder, root, 0); - return builder.toString(); - } - - private void append(StringBuilder builder, DataNode dataNode, int level) { - if (dataNode != null && builder != null) { - String data = dataNode.getData(); - if (data != null && !data.equals("")) { - builder.append(Gfsh.wrapText(data, level - 1)); - builder.append(GfshParser.LINE_SEPARATOR); - } - if (dataNode.getChildren() != null && dataNode.getChildren().size() > 0) { - for (DataNode child : dataNode.getChildren()) { - append(builder, child, level + 1); - } - } - } - } -} http://git-wip-us.apache.org/repos/asf/geode/blob/ce9e138c/geode-core/src/main/java/org/apache/geode/management/internal/cli/help/format/Row.java ---------------------------------------------------------------------- diff --git a/geode-core/src/main/java/org/apache/geode/management/internal/cli/help/format/Row.java b/geode-core/src/main/java/org/apache/geode/management/internal/cli/help/format/Row.java deleted file mode 100644 index ac1ca21..0000000 --- a/geode-core/src/main/java/org/apache/geode/management/internal/cli/help/format/Row.java +++ /dev/null @@ -1,28 +0,0 @@ -/* - * 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.geode.management.internal.cli.help.format; - -public class Row { - private String[] info; - - public String[] getInfo() { - return info; - } - - public Row setInfo(String[] info) { - this.info = info; - return this; - } -} http://git-wip-us.apache.org/repos/asf/geode/blob/ce9e138c/geode-core/src/main/java/org/apache/geode/management/internal/cli/help/utils/FormatOutput.java ---------------------------------------------------------------------- diff --git a/geode-core/src/main/java/org/apache/geode/management/internal/cli/help/utils/FormatOutput.java b/geode-core/src/main/java/org/apache/geode/management/internal/cli/help/utils/FormatOutput.java deleted file mode 100644 index 44998a0..0000000 --- a/geode-core/src/main/java/org/apache/geode/management/internal/cli/help/utils/FormatOutput.java +++ /dev/null @@ -1,33 +0,0 @@ -/* - * 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.geode.management.internal.cli.help.utils; - -import java.util.*; - -public class FormatOutput { - - public static String converListToString(List<String> outputStringList) { - Iterator<String> iters = outputStringList.iterator(); - - StringBuilder sb = new StringBuilder(200); - - while (iters.hasNext()) { - sb.append("\n"); - sb.append((String) iters.next()); - } - - return sb.toString(); - } -} http://git-wip-us.apache.org/repos/asf/geode/blob/ce9e138c/geode-core/src/main/java/org/apache/geode/management/internal/cli/help/utils/HelpUtils.java ---------------------------------------------------------------------- diff --git a/geode-core/src/main/java/org/apache/geode/management/internal/cli/help/utils/HelpUtils.java b/geode-core/src/main/java/org/apache/geode/management/internal/cli/help/utils/HelpUtils.java index 11765c5..2517d44 100644 --- a/geode-core/src/main/java/org/apache/geode/management/internal/cli/help/utils/HelpUtils.java +++ b/geode-core/src/main/java/org/apache/geode/management/internal/cli/help/utils/HelpUtils.java @@ -14,22 +14,19 @@ */ package org.apache.geode.management.internal.cli.help.utils; +import org.apache.commons.lang.StringUtils; import org.apache.geode.management.cli.CliMetaData; -import org.apache.geode.management.internal.cli.help.format.*; -import org.apache.geode.management.internal.cli.modes.CommandModes; -import org.apache.geode.management.internal.cli.modes.CommandModes.CommandMode; -import org.apache.geode.management.internal.cli.parser.Argument; -import org.apache.geode.management.internal.cli.parser.CommandTarget; -import org.apache.geode.management.internal.cli.parser.Option; -import org.apache.geode.management.internal.cli.parser.SyntaxConstants; +import org.apache.geode.management.internal.cli.GfshParser; +import org.apache.geode.management.internal.cli.help.format.HelpBlock; +import org.springframework.shell.core.annotation.CliCommand; +import org.springframework.shell.core.annotation.CliOption; +import java.lang.annotation.Annotation; +import java.lang.reflect.Method; import java.util.ArrayList; import java.util.Collection; import java.util.List; -/** - * @since GemFire 7.0 - */ public class HelpUtils { public static final String EXE_PREFIX_FOR_EXTERNAL_HELP = org.apache.geode.management.internal.cli.shell.Gfsh.GFSH_APP_NAME + " "; @@ -58,344 +55,181 @@ public class HelpUtils { private static final String FALSE_TOKEN = "false"; - private static Help help(Block[] blocks) { - return new Help().setBlocks(blocks); - } - - private static Block block(String heading, Row... rows) { - return new Block().setHeading(heading).setRows(rows); - } - - private static Row row(String... info) { - return new Row().setInfo(info); + public static CliOption getCliOption(Annotation[][] parameterAnnotations, int index) { + Annotation[] annotations = parameterAnnotations[index]; + return getAnnotation(annotations, CliOption.class); } - @Deprecated - public static Help getHelp(CommandTarget commandTarget) { - List<Block> blocks = new ArrayList<Block>(); - // First we will have the block for NAME of the command - blocks.add(block(NAME_NAME, row(commandTarget.getCommandName()))); - // Now add synonyms if any - if (commandTarget.getSynonyms() != null) { - blocks.add(block(SYNONYMS_NAME, row(commandTarget.getSynonyms()))); - } - - - - // Now comes the turn to display synopsis if any - if (commandTarget.getCommandHelp() != null && !commandTarget.getCommandHelp().equals("")) { - blocks.add(block(SYNOPSIS_NAME, row(commandTarget.getCommandHelp()))); - } - // Now display the syntax for the command - StringBuffer buffer = new StringBuffer(); - buffer.append(commandTarget.getCommandName()); - // Create a list which will store optional arguments - List<Argument> optionalArguments = new ArrayList<Argument>(); - for (Argument argument : commandTarget.getOptionParser().getArguments()) { - if (argument.isRequired()) { - buffer.append(" " + argument.getArgumentName()); - } else { - optionalArguments.add(argument); + public static <T> T getAnnotation(Annotation[] annotations, Class<?> klass) { + for (Annotation annotation : annotations) { + if (klass.isAssignableFrom(annotation.getClass())) { + return (T) annotation; } } - for (Argument argument : optionalArguments) { - buffer.append(" " + "[" + argument.getArgumentName() + "]"); - } - // Create a list which will store optional options - List<Option> optionalOptions = new ArrayList<Option>(); - for (Option option : commandTarget.getOptionParser().getOptions()) { - if (option.isRequired()) { - buffer.append(" " + SyntaxConstants.LONG_OPTION_SPECIFIER + option.getLongOption()); - // String temp = SyntaxConstants.OPTION_VALUE_SPECIFIER + VALUE_TOKEN + "(" - // + SyntaxConstants.OPTION_VALUE_SEPARATOR + VALUE_TOKEN + ")*"; - String temp = buildOptionHelpText(option); - // - if (option.getSpecifiedDefaultValue() != null - && !option.getSpecifiedDefaultValue().equals("")) { - buffer.append("("); - buffer.append(temp); - buffer.append(")?"); - } else { - buffer.append(temp); - } - } else { - optionalOptions.add(option); - } - } - for (Option option : optionalOptions) { - buffer.append(" " + "[" + SyntaxConstants.LONG_OPTION_SPECIFIER + option.getLongOption()); - // String temp = SyntaxConstants.OPTION_VALUE_SPECIFIER + VALUE_TOKEN + "(" - // + SyntaxConstants.OPTION_VALUE_SEPARATOR + VALUE_TOKEN + ")*"; - String temp = buildOptionHelpText(option); - // - if (option.getSpecifiedDefaultValue() != null - && !option.getSpecifiedDefaultValue().equals("")) { - buffer.append("("); - buffer.append(temp); - buffer.append(")?"); - } else { - buffer.append(temp); - } - buffer.append("]"); - } - blocks.add(block(SYNTAX_NAME, row(buffer.toString()))); - // Detailed description of Arguments - if (commandTarget.getOptionParser().getArguments().size() > 0) { - List<Row> rows = new ArrayList<Row>(); - for (Argument argument : commandTarget.getOptionParser().getArguments()) { - rows.add(row(argument.getArgumentName() - + ((argument.getHelp() != null && !argument.getHelp().equals("")) - ? ":" + argument.getHelp() : ""))); - } - Row[] rowsArray = new Row[rows.size()]; - blocks.add(block(ARGUMENTS_NAME, rows.toArray(rowsArray))); - } - - // Detailed description of Options - if (commandTarget.getOptionParser().getOptions().size() > 0) { - List<Row> rows = new ArrayList<Row>(); - for (Option option : commandTarget.getOptionParser().getOptions()) { - rows.add( - row(option.getLongOption() + ((option.getHelp() != null && !option.getHelp().equals("")) - ? ":" + option.getHelp() : ""))); - } - Row[] rowsArray = new Row[rows.size()]; - blocks.add(block(OPTIONS_NAME, rows.toArray(rowsArray))); - } - Block[] blocksArray = new Block[blocks.size()]; - for (int i = 0; i < blocks.size(); i++) { - blocksArray[i] = blocks.get(i); - } - return help(blocksArray); + return null; } - /** - * Builds help for the specified command. - * - * @param commandTarget command specific target to use to generate help - * @param withinShell if <code>true</code> includes availabilty & doesn't include application name - * @return built NewHelp object for the given command target - */ - public static NewHelp getNewHelp(CommandTarget commandTarget, boolean withinShell) { - DataNode root = new DataNode(null, new ArrayList<DataNode>()); + public static HelpBlock getHelp(Method method) { + CliCommand commandTarget = method.getDeclaredAnnotation(CliCommand.class); + String commandName = commandTarget.value()[0]; + HelpBlock root = new HelpBlock(); // First we will have the block for NAME of the command - DataNode name = new DataNode(NAME_NAME, new ArrayList<DataNode>()); - name.addChild(new DataNode(commandTarget.getCommandName(), null)); + HelpBlock name = new HelpBlock(NAME_NAME); + name.addChild(new HelpBlock(commandName)); root.addChild(name); - if (withinShell) {// include availabilty info - DataNode availability = new DataNode(IS_AVAILABLE_NAME, new ArrayList<DataNode>()); - boolean isAvailable = false; - try { - isAvailable = commandTarget.isAvailable(); - } catch (Exception e) { - isAvailable = false; - } - availability.addChild(new DataNode(String.valueOf(isAvailable), null)); - root.addChild(availability); - } + // Now add synonyms if any - if (commandTarget.getSynonyms() != null) { - DataNode synonyms = new DataNode(SYNONYMS_NAME, new ArrayList<DataNode>()); - for (String string : commandTarget.getSynonyms()) { - synonyms.addChild(new DataNode(string, null)); + String[] allNames = commandTarget.value(); + if (allNames.length > 1) { + HelpBlock synonyms = new HelpBlock(SYNONYMS_NAME); + for (int i = 1; i < allNames.length; i++) { + synonyms.addChild(new HelpBlock(allNames[i])); } root.addChild(synonyms); } - // Now comes the turn to display synopsis if any - if (commandTarget.getCommandHelp() != null && !commandTarget.getCommandHelp().equals("")) { - DataNode synopsis = new DataNode(SYNOPSIS_NAME, new ArrayList<DataNode>()); - synopsis.addChild(new DataNode(commandTarget.getCommandHelp(), null)); + if (!StringUtils.isBlank(commandTarget.help())) { + HelpBlock synopsis = new HelpBlock(SYNOPSIS_NAME); + synopsis.addChild(new HelpBlock(commandTarget.help())); root.addChild(synopsis); } - + // build a list of required options and optional options + Annotation[][] annotations = method.getParameterAnnotations(); + Class<?>[] parameterTypes = method.getParameterTypes(); // Now display the syntax for the command StringBuffer buffer = new StringBuffer(); - if (withinShell) { - buffer.append(commandTarget.getCommandName()); - } else { // add app name in the syntax - buffer.append(EXE_PREFIX_FOR_EXTERNAL_HELP).append(commandTarget.getCommandName()); - } - appendArguments(buffer, commandTarget); - appendOptions(buffer, commandTarget); - DataNode syntax = new DataNode(SYNTAX_NAME, new ArrayList<DataNode>()); - syntax.addChild(new DataNode(buffer.toString(), null)); - root.addChild(syntax); - - - // Detailed description of Arguments - if (commandTarget.getOptionParser().getArguments().size() > 0) { - DataNode arguments = new DataNode(ARGUMENTS_NAME, new ArrayList<DataNode>()); - for (Argument argument : commandTarget.getOptionParser().getArguments()) { - DataNode argumentNode = new DataNode(argument.getArgumentName(), new ArrayList<DataNode>()); - argumentNode - .addChild(new DataNode(((argument.getHelp() != null && !argument.getHelp().equals("")) - ? argument.getHelp() : ""), null)); - argumentNode.addChild(new DataNode( - REQUIRED_SUB_NAME + ((argument.isRequired()) ? TRUE_TOKEN : FALSE_TOKEN), null)); - if (argument.getUnspecifiedDefaultValue() != null) { - argumentNode.addChild( - new DataNode(DEFAULTVALUE_SUB_NAME + argument.getUnspecifiedDefaultValue(), null)); - } - arguments.addChild(argumentNode); - } - root.addChild(arguments); + buffer.append(commandName); + for (int i = 0; i < annotations.length; i++) { + appendOption(buffer, annotations[i], parameterTypes[i]); } - - try { - CommandModes modes = CommandModes.getInstance(); - Collection<CommandMode> comModes = modes.getCommandModes(commandTarget.getCommandName()); - DataNode modesDN = new DataNode(MODES, new ArrayList<DataNode>()); - if (comModes != null) { - for (CommandMode cmd : comModes) { - StringBuffer sb = new StringBuffer(); - List<Option> optionalOptions = new ArrayList<Option>(); - - sb.append(commandTarget.getCommandName()).append(" "); - if (!cmd.name.equals("default")) - appendRequiredOption(sb, getOption(commandTarget, cmd.leadOption)); - - for (String opt : cmd.options) { - if (!opt.equals(cmd.leadOption)) { - Option option = getOption(commandTarget, opt); - if (option.isRequired()) { - appendRequiredOption(sb, option); - } else - optionalOptions.add(option); - } - } - - for (Option optOpt : optionalOptions) - appendOption(sb, optOpt); - - DataNode modeDN = new DataNode(cmd.text, new ArrayList<DataNode>()); - modeDN.addChild(new DataNode(sb.toString(), null)); - modesDN.addChild(modeDN); - } - root.addChild(modesDN); - } else { - // modesDN.addChild(new DataNode("No command modes found", null)); - // root.addChild(modesDN); - } - - } catch (Exception e) { - } finally { - - } + HelpBlock syntax = new HelpBlock(SYNTAX_NAME); + syntax.addChild(new HelpBlock(buffer.toString())); + root.addChild(syntax); // Detailed description of Options - if (commandTarget.getOptionParser().getOptions().size() > 0) { - DataNode options = new DataNode(OPTIONS_NAME, new ArrayList<DataNode>()); - for (Option option : commandTarget.getOptionParser().getOptions()) { - DataNode optionNode = new DataNode(option.getLongOption(), new ArrayList<DataNode>()); - optionNode.addChild(new DataNode( - ((option.getHelp() != null && !option.getHelp().equals("")) ? option.getHelp() : ""), - null)); - if (option.getSynonyms() != null && option.getSynonyms().size() > 0) { + if (annotations.length > 0) { + HelpBlock options = new HelpBlock(OPTIONS_NAME); + for (int i = 0; i < annotations.length; i++) { + CliOption cliOption = getCliOption(annotations, i); + HelpBlock optionNode = new HelpBlock(getPrimaryKey(cliOption)); + String help = cliOption.help(); + optionNode.addChild(new HelpBlock((!StringUtils.isBlank(help) ? help : ""))); + if (getSynonyms(cliOption).size() > 0) { StringBuilder builder = new StringBuilder(); - for (String string : option.getSynonyms()) { + for (String string : getSynonyms(cliOption)) { if (builder.length() > 0) { builder.append(","); } builder.append(string); } - optionNode.addChild(new DataNode(SYNONYMS_SUB_NAME + builder.toString(), null)); + optionNode.addChild(new HelpBlock(SYNONYMS_SUB_NAME + builder.toString())); } - optionNode.addChild(new DataNode( - REQUIRED_SUB_NAME + ((option.isRequired()) ? TRUE_TOKEN : FALSE_TOKEN), null)); - if (option.getSpecifiedDefaultValue() != null - && !option.getSpecifiedDefaultValue().equals("")) { - optionNode.addChild(new DataNode( - SPECIFIEDDEFAULTVALUE_SUB_NAME + option.getSpecifiedDefaultValue(), null)); + optionNode.addChild(new HelpBlock( + REQUIRED_SUB_NAME + ((cliOption.mandatory()) ? TRUE_TOKEN : FALSE_TOKEN))); + if (!isNullOrBlank(cliOption.specifiedDefaultValue())) { + optionNode.addChild( + new HelpBlock(SPECIFIEDDEFAULTVALUE_SUB_NAME + cliOption.specifiedDefaultValue())); } - if (option.getUnspecifiedDefaultValue() != null - && !option.getUnspecifiedDefaultValue().equals("")) { - optionNode.addChild(new DataNode( - UNSPECIFIEDDEFAULTVALUE_VALUE_SUB_NAME + option.getUnspecifiedDefaultValue(), null)); + if (!isNullOrBlank(cliOption.unspecifiedDefaultValue())) { + optionNode.addChild(new HelpBlock( + UNSPECIFIEDDEFAULTVALUE_VALUE_SUB_NAME + cliOption.unspecifiedDefaultValue())); } options.addChild(optionNode); } root.addChild(options); } - return new NewHelp(root); + return root; } - private static Option getOption(CommandTarget commandTarget, String opt) { - for (Option option : commandTarget.getOptionParser().getOptions()) { - if (option.getLongOption().equals(opt)) - return option; + /** + * this checks if the option is mandatory, and if not, wraps [] around it. + */ + private static void appendOption(StringBuffer buffer, Annotation[] annotations, + Class<?> optionType) { + CliOption cliOption = getAnnotation(annotations, CliOption.class); + CliMetaData cliMetaData = getAnnotation(annotations, CliMetaData.class); + String optionString = getOptionString(cliOption, cliMetaData, optionType); + if (cliOption.mandatory()) { + buffer.append(" ").append(optionString); + } else { + buffer.append(" [").append(optionString).append("]"); } - return null; } - private static void appendOptions(StringBuffer buffer, CommandTarget commandTarget) { - List<Option> optionalOptions = new ArrayList<Option>(); - for (Option option : commandTarget.getOptionParser().getOptions()) { - if (option.isRequired()) { - appendRequiredOption(buffer, option); - } else { - optionalOptions.add(option); - } - } - for (Option option : optionalOptions) { - appendOption(buffer, option); + /** + * this builds the following format of strings: key (as in sh and help) --key=value --key(=value)? + * (if has specifiedDefaultValue) --key=value(,value)* (if the value is a list) + * + * @return option string + */ + private static String getOptionString(CliOption cliOption, CliMetaData cliMetaData, + Class<?> optionType) { + String key0 = cliOption.key()[0]; + if ("".equals(key0)) { + return (cliOption.key()[1]); } - } + StringBuffer buffer = new StringBuffer(); + buffer.append(GfshParser.LONG_OPTION_SPECIFIER).append(key0); - private static void appendRequiredOption(StringBuffer buffer, Option option) { - buffer.append(" " + SyntaxConstants.LONG_OPTION_SPECIFIER + option.getLongOption()); - String temp = buildOptionHelpText(option); - if (option.getSpecifiedDefaultValue() != null - && !option.getSpecifiedDefaultValue().equals("")) { - buffer.append("(").append(temp).append(")?"); - } else { - buffer.append(temp); + boolean hasSpecifiedDefault = !isNullOrBlank(cliOption.specifiedDefaultValue()); + + if (hasSpecifiedDefault) { + buffer.append("("); } - } - private static void appendOption(StringBuffer buffer, Option option) { - buffer.append(" " + "[" + SyntaxConstants.LONG_OPTION_SPECIFIER + option.getLongOption()); - String temp = buildOptionHelpText(option); - if (option.getSpecifiedDefaultValue() != null - && !option.getSpecifiedDefaultValue().equals("")) { - buffer.append("(").append(temp).append(")?"); - } else { - buffer.append(temp); + buffer.append(GfshParser.OPTION_VALUE_SPECIFIER).append(VALUE_FIELD); + + if (hasSpecifiedDefault) { + buffer.append(")?"); } - buffer.append("]"); - } - private static void appendArguments(StringBuffer buffer, CommandTarget commandTarget) { - // Create a list which will store optional arguments - List<Argument> optionalArguments = new ArrayList<Argument>(); - for (Argument argument : commandTarget.getOptionParser().getArguments()) { - if (argument.isRequired()) { - buffer.append(" " + argument.getArgumentName()); - } else { - optionalArguments.add(argument); + if (isCollectionOrArrayType(optionType)) { + String separator = ","; + if (cliMetaData != null) { + separator = cliMetaData.valueSeparator(); } + buffer.append("(").append(separator).append(VALUE_FIELD).append(")*"); } - for (Argument argument : optionalArguments) { - buffer.append(" " + "[" + argument.getArgumentName() + "]"); - } - } - public static String buildOptionHelpText(Option option) { - String temp = SyntaxConstants.OPTION_VALUE_SPECIFIER + VALUE_FIELD; - if ((option.getValueSeparator() != null - && !CliMetaData.ANNOTATION_NULL_VALUE.equals(option.getValueSeparator()) - && !option.getValueSeparator().equals("")) - || isCollectionOrArrayType(option.getDataType())) { - temp += "(" + option.getValueSeparator() + VALUE_FIELD + ")*"; - } - return temp; + return buffer.toString(); } private static boolean isCollectionOrArrayType(Class<?> typeToCheck) { return typeToCheck != null && (typeToCheck.isArray() || Collection.class.isAssignableFrom(typeToCheck)); } + + public static String getPrimaryKey(CliOption option) { + String[] keys = option.key(); + if (keys.length == 0) { + throw new RuntimeException("Invalid option keys"); + } else if ("".equals(keys[0])) { + return keys[1]; + } else { + return keys[0]; + } + } + + public static List<String> getSynonyms(CliOption option) { + List<String> synonyms = new ArrayList<>(); + String[] keys = option.key(); + if (keys.length < 2) + return synonyms; + // if the primary key is empty (like sh and help command), then there should be no synonyms. + if ("".equals(keys[0])) + return synonyms; + + for (int i = 1; i < keys.length; i++) { + synonyms.add(keys[i]); + } + return synonyms; + } + + public static boolean isNullOrBlank(String value) { + return StringUtils.isBlank(value) || CliMetaData.ANNOTATION_NULL_VALUE.equals(value); + } + } http://git-wip-us.apache.org/repos/asf/geode/blob/ce9e138c/geode-core/src/main/java/org/apache/geode/management/internal/cli/parser/Argument.java ---------------------------------------------------------------------- diff --git a/geode-core/src/main/java/org/apache/geode/management/internal/cli/parser/Argument.java b/geode-core/src/main/java/org/apache/geode/management/internal/cli/parser/Argument.java deleted file mode 100644 index 9acbc2a..0000000 --- a/geode-core/src/main/java/org/apache/geode/management/internal/cli/parser/Argument.java +++ /dev/null @@ -1,71 +0,0 @@ -/* - * 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.geode.management.internal.cli.parser; - -/** - * Argument of a Command - * - * @since GemFire 7.0 - * - */ -public class Argument extends Parameter { - private String argumentName; - - public String getArgumentName() { - return argumentName; - } - - public void setArgumentName(String argumentName) { - this.argumentName = argumentName; - } - - @Override - public int hashCode() { - final int prime = 13; - int result = 1; - result = prime * result + ((argumentName == null) ? 0 : argumentName.hashCode()); - return result; - } - - @Override - public boolean equals(Object obj) { - if (this == obj) { - return true; - } - if (obj == null) { - return false; - } - if (getClass() != obj.getClass()) { - return false; - } - Argument argument = (Argument) obj; - if (argumentName == null) { - if (argument.getArgumentName() != null) { - return false; - } - } else if (!argumentName.equals(argument.getArgumentName())) { - return false; - } - return true; - } - - @Override - public String toString() { - StringBuilder builder = new StringBuilder(); - builder.append(Argument.class.getSimpleName()).append("[name=" + argumentName) - .append(",help=" + help).append(",required" + required + "]"); - return builder.toString(); - } -} http://git-wip-us.apache.org/repos/asf/geode/blob/ce9e138c/geode-core/src/main/java/org/apache/geode/management/internal/cli/parser/AvailabilityTarget.java ---------------------------------------------------------------------- diff --git a/geode-core/src/main/java/org/apache/geode/management/internal/cli/parser/AvailabilityTarget.java b/geode-core/src/main/java/org/apache/geode/management/internal/cli/parser/AvailabilityTarget.java deleted file mode 100644 index eff5fd2..0000000 --- a/geode-core/src/main/java/org/apache/geode/management/internal/cli/parser/AvailabilityTarget.java +++ /dev/null @@ -1,106 +0,0 @@ -/* - * 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.geode.management.internal.cli.parser; - -import java.lang.reflect.Method; - -import org.apache.geode.management.cli.CliMetaData; -import org.apache.geode.management.cli.CliMetaData.AvailabilityMetadata; -import org.apache.geode.management.internal.cli.i18n.CliStrings; - -/** - * Used for checking availability of a command - * - * @since GemFire 7.0 - */ -public class AvailabilityTarget { - - private final Object target; - private final Method method; - private final String availabilityDescription; - - public AvailabilityTarget(Object target, Method method) { - this.target = target; - this.method = method; - AvailabilityMetadata availabilityMetadata = - this.method.getAnnotation(CliMetaData.AvailabilityMetadata.class); - String specifiedAvailabilityDesc = - CliStrings.AVAILABILITYTARGET_MSG_DEFAULT_UNAVAILABILITY_DESCRIPTION; - if (availabilityMetadata != null) { - specifiedAvailabilityDesc = availabilityMetadata.availabilityDescription(); - } - this.availabilityDescription = specifiedAvailabilityDesc; - } - - public Object getTarget() { - return target; - } - - public Method getMethod() { - return method; - } - - public String getAvailabilityDescription() { - return availabilityDescription; - } - - @Override - public int hashCode() { - final int prime = 17; - int result = 8; - result = prime * result + ((target == null) ? 0 : target.hashCode()); - result = prime * result + ((method == null) ? 0 : method.hashCode()); - return result; - } - - @Override - public boolean equals(Object obj) { - if (this == obj) { - return true; - } - if (obj == null) { - return false; - } - if (getClass() != obj.getClass()) { - return false; - } - AvailabilityTarget availabilityTarget = (AvailabilityTarget) obj; - if (target == null) { - if (availabilityTarget.getTarget() != null) { - return false; - } - } else if (!target.equals(availabilityTarget.getTarget())) { - return false; - } - if (method == null) { - if (availabilityTarget.getMethod() != null) { - return false; - } - } else if (!method.equals(availabilityTarget.getMethod())) { - return false; - } - return true; - } - - @Override - public String toString() { - StringBuilder builder = new StringBuilder(); - builder.append(AvailabilityTarget.class.getSimpleName()); - builder.append("[" + "target=" + target); - builder.append(",method=" + method); - builder.append("]"); - return builder.toString(); - } -} http://git-wip-us.apache.org/repos/asf/geode/blob/ce9e138c/geode-core/src/main/java/org/apache/geode/management/internal/cli/parser/CommandTarget.java ---------------------------------------------------------------------- diff --git a/geode-core/src/main/java/org/apache/geode/management/internal/cli/parser/CommandTarget.java b/geode-core/src/main/java/org/apache/geode/management/internal/cli/parser/CommandTarget.java deleted file mode 100644 index 3dfc01a..0000000 --- a/geode-core/src/main/java/org/apache/geode/management/internal/cli/parser/CommandTarget.java +++ /dev/null @@ -1,176 +0,0 @@ -/* - * 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.geode.management.internal.cli.parser; - -import java.lang.reflect.InvocationTargetException; - -import org.apache.geode.management.internal.cli.GfshParser; - -/** - * Used by {@link GfshParser} to store details of a command - * - * @since GemFire 7.0 - * - */ -public class CommandTarget { - private final String commandName; - private final String[] synonyms; - private final String commandHelp; - private final GfshMethodTarget gfshMethodTarget; - private final GfshOptionParser optionParser; - private AvailabilityTarget availabilityIndicator; - - public CommandTarget(String commandName, String[] synonyms, GfshMethodTarget methodTarget, - GfshOptionParser optionParser, AvailabilityTarget availabilityIndicator, String commandHelp) { - this.commandName = commandName; - this.synonyms = synonyms; - this.gfshMethodTarget = methodTarget; - this.optionParser = optionParser; - this.availabilityIndicator = availabilityIndicator; - this.commandHelp = commandHelp; - } - - public GfshMethodTarget getGfshMethodTarget() { - return gfshMethodTarget; - } - - public GfshOptionParser getOptionParser() { - return optionParser; - } - - public boolean isAvailable() - throws IllegalArgumentException, IllegalAccessException, InvocationTargetException { - if (availabilityIndicator != null) { - return (Boolean) availabilityIndicator.getMethod().invoke(availabilityIndicator.getTarget()); - } else { - return true; - } - } - - public AvailabilityTarget getAvailabilityIndicator() { - return availabilityIndicator; - } - - // TODO Change for concurrent access - public void setAvailabilityIndicator(AvailabilityTarget availabilityIndicator) { - this.availabilityIndicator = availabilityIndicator; - } - - public String getCommandHelp() { - return commandHelp; - } - - public CommandTarget duplicate(String key) { - return duplicate(key, null); - } - - public CommandTarget duplicate(String key, String remainingBuffer) { - return new CommandTarget( - commandName, synonyms, new GfshMethodTarget(gfshMethodTarget.getMethod(), - gfshMethodTarget.getTarget(), remainingBuffer, key), - optionParser, availabilityIndicator, commandHelp); - } - - @Override - public int hashCode() { - final int prime = 47; - int result = 3; - result = prime * result + ((commandName == null) ? 0 : commandName.hashCode()); - result = prime * result + ((commandHelp == null) ? 0 : commandHelp.hashCode()); - result = prime * result + ((gfshMethodTarget == null) ? 0 : gfshMethodTarget.hashCode()); - result = prime * result + ((optionParser == null) ? 0 : optionParser.hashCode()); - result = - prime * result + ((availabilityIndicator == null) ? 0 : availabilityIndicator.hashCode()); - return result; - } - - @Override - public boolean equals(Object obj) { - // If two command targets have the same OptionParser - // then they are equal - if (this == obj) { - return true; - } - if (obj == null) { - return false; - } - if (getClass() != obj.getClass()) { - return false; - } - CommandTarget commandTarget = (CommandTarget) obj; - if (commandName == null) { - if (commandTarget.getCommandName() != null) { - return false; - } - } else if (!commandName.equals(commandTarget.getCommandName())) { - return false; - } - if (commandHelp == null) { - if (commandTarget.getCommandHelp() != null) { - return false; - } - } else if (!commandHelp.equals(commandTarget.getCommandHelp())) { - return false; - } - if (gfshMethodTarget == null) { - if (commandTarget.getGfshMethodTarget() != null) { - return false; - } - } else if (!gfshMethodTarget.equals(commandTarget.getGfshMethodTarget())) { - return false; - } - if (optionParser == null) { - if (commandTarget.getOptionParser() != null) { - return false; - } - } else if (!optionParser.equals(commandTarget.getOptionParser())) { - return false; - } - if (availabilityIndicator == null) { - if (commandTarget.getAvailabilityIndicator() != null) { - return false; - } - } else if (!availabilityIndicator.equals(commandTarget.getAvailabilityIndicator())) { - return false; - } - return true; - } - - @Override - public String toString() { - StringBuilder builder = new StringBuilder(); - builder.append(CommandTarget.class.getSimpleName()).append("[" + "commandName=" + commandName) - .append(",commandHelp=" + commandHelp); - builder.append(",synonyms="); - if (synonyms != null) { - for (String string : synonyms) { - builder.append(string + " "); - } - } - builder.append(",gfshMethodTarget=" + gfshMethodTarget); - builder.append(",optionParser=" + optionParser); - builder.append(",availabilityIndicator=" + availabilityIndicator); - builder.append("]"); - return builder.toString(); - } - - public String getCommandName() { - return commandName; - } - - public String[] getSynonyms() { - return synonyms; - } -} http://git-wip-us.apache.org/repos/asf/geode/blob/ce9e138c/geode-core/src/main/java/org/apache/geode/management/internal/cli/parser/GfshMethodTarget.java ---------------------------------------------------------------------- diff --git a/geode-core/src/main/java/org/apache/geode/management/internal/cli/parser/GfshMethodTarget.java b/geode-core/src/main/java/org/apache/geode/management/internal/cli/parser/GfshMethodTarget.java deleted file mode 100644 index 6f28830..0000000 --- a/geode-core/src/main/java/org/apache/geode/management/internal/cli/parser/GfshMethodTarget.java +++ /dev/null @@ -1,121 +0,0 @@ -/* - * 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.geode.management.internal.cli.parser; - -import java.lang.reflect.Method; - -import org.apache.commons.lang.StringUtils; -import org.springframework.util.Assert; -import org.springframework.util.ObjectUtils; - -/** - * A method that can be executed via a shell command. - * - * @since GemFire 7.0 - */ -public class GfshMethodTarget { - - // Fields - private final Method method; - private final Object target; - private final String remainingBuffer; - private final String key; - - /** - * Constructor for a <code>null remainingBuffer</code> and <code>key</code> - * - * @param method the method to invoke (required) - * @param target the object on which the method is to be invoked (required) - */ - public GfshMethodTarget(final Method method, final Object target) { - this(method, target, null, null); - } - - /** - * Constructor that allows all fields to be set - * - * @param method the method to invoke (required) - * @param target the object on which the method is to be invoked (required) - * @param remainingBuffer can be blank - * @param key can be blank - */ - public GfshMethodTarget(final Method method, final Object target, final String remainingBuffer, - final String key) { - Assert.notNull(method, "Method is required"); - Assert.notNull(target, "Target is required"); - this.key = StringUtils.trimToEmpty(key); - this.method = method; - this.remainingBuffer = remainingBuffer; - this.target = target; - } - - @Override - public boolean equals(Object other) { - if (this == other) { - return true; - } - if (other == null) { - return false; - } - if (getClass() != other.getClass()) { - return false; - } - GfshMethodTarget gfshMethodTarget = (GfshMethodTarget) other; - if (method == null) { - if (gfshMethodTarget.getMethod() != null) { - return false; - } - } else if (!method.equals(gfshMethodTarget.getMethod())) { - return false; - } - if (target == null) { - if (gfshMethodTarget.getTarget() != null) { - return false; - } - } else if (!target.equals(gfshMethodTarget.getTarget())) { - return false; - } - return true; - } - - @Override - public int hashCode() { - return ObjectUtils.nullSafeHashCode(new Object[] {method, target}); - } - - @Override - public final String toString() { - StringBuilder builder = new StringBuilder(); - builder.append(GfshMethodTarget.class.getSimpleName()).append("[key=" + key).append( - ",remainingBuffer=" + remainingBuffer + ",target=" + target + ",method=" + method + "]"); - return builder.toString(); - } - - public String getKey() { - return this.key; - } - - public Method getMethod() { - return this.method; - } - - public String getRemainingBuffer() { - return this.remainingBuffer; - } - - public Object getTarget() { - return this.target; - } -} http://git-wip-us.apache.org/repos/asf/geode/blob/ce9e138c/geode-core/src/main/java/org/apache/geode/management/internal/cli/parser/GfshOptionParser.java ---------------------------------------------------------------------- diff --git a/geode-core/src/main/java/org/apache/geode/management/internal/cli/parser/GfshOptionParser.java b/geode-core/src/main/java/org/apache/geode/management/internal/cli/parser/GfshOptionParser.java deleted file mode 100644 index a64933c..0000000 --- a/geode-core/src/main/java/org/apache/geode/management/internal/cli/parser/GfshOptionParser.java +++ /dev/null @@ -1,37 +0,0 @@ -/* - * 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.geode.management.internal.cli.parser; - -import java.util.LinkedList; - -import org.apache.geode.management.internal.cli.GfshParser; -import org.apache.geode.management.internal.cli.exceptions.CliException; - -/** - * Delegate used for parsing by {@link GfshParser} - * - * @since GemFire 7.0 - */ -public interface GfshOptionParser { - public void setArguments(LinkedList<Argument> arguments); - - public LinkedList<Argument> getArguments(); - - public void setOptions(LinkedList<Option> options); - - public LinkedList<Option> getOptions(); - - OptionSet parse(String userInput) throws CliException; -} http://git-wip-us.apache.org/repos/asf/geode/blob/ce9e138c/geode-core/src/main/java/org/apache/geode/management/internal/cli/parser/MethodParameter.java ---------------------------------------------------------------------- diff --git a/geode-core/src/main/java/org/apache/geode/management/internal/cli/parser/MethodParameter.java b/geode-core/src/main/java/org/apache/geode/management/internal/cli/parser/MethodParameter.java deleted file mode 100644 index 599fb00..0000000 --- a/geode-core/src/main/java/org/apache/geode/management/internal/cli/parser/MethodParameter.java +++ /dev/null @@ -1,39 +0,0 @@ -/* - * 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.geode.management.internal.cli.parser; - -/** - * Object used for ordering method parameters - * - * @since GemFire 7.0 - * - */ -public class MethodParameter { - private final Object parameter; - private final int parameterNo; - - public MethodParameter(Object parameter, int parameterNo) { - this.parameter = parameter; - this.parameterNo = parameterNo; - } - - public Object getParameter() { - return parameter; - } - - public int getParameterNo() { - return parameterNo; - } -} http://git-wip-us.apache.org/repos/asf/geode/blob/ce9e138c/geode-core/src/main/java/org/apache/geode/management/internal/cli/parser/Option.java ---------------------------------------------------------------------- diff --git a/geode-core/src/main/java/org/apache/geode/management/internal/cli/parser/Option.java b/geode-core/src/main/java/org/apache/geode/management/internal/cli/parser/Option.java deleted file mode 100644 index 4eec112..0000000 --- a/geode-core/src/main/java/org/apache/geode/management/internal/cli/parser/Option.java +++ /dev/null @@ -1,217 +0,0 @@ -/* - * 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.geode.management.internal.cli.parser; - -import java.util.ArrayList; -import java.util.List; - -import org.apache.geode.management.internal.cli.parser.preprocessor.PreprocessorUtils; - -/** - * Option of a Command - * - * @since GemFire 7.0 - * - */ -public class Option extends Parameter { - - private static final String NULL = "__NULL__"; - private static final char SHORT_OPTION_DEFAULT = '\u0000'; - // Used for Option Identification - private char shortOption; - private String longOption; - private List<String> synonyms; - private List<String> aggregate; - - // Option Value related - private String specifiedDefaultValue; - - // Constraints on Option - private boolean withRequiredArgs; - private String valueSeparator; - - public Option() { - aggregate = new ArrayList<String>(); - } - - public Option(char shortOption) { - this(shortOption, null, null); - } - - public Option(char shortOption, List<String> synonyms) { - this(shortOption, null, synonyms); - } - - public Option(String longOption) { - this(SHORT_OPTION_DEFAULT, longOption, null); - } - - public Option(String longOption, List<String> synonyms) { - this(SHORT_OPTION_DEFAULT, longOption, synonyms); - } - - public Option(char shortOption, String longOption) { - this(shortOption, longOption, null); - } - - public Option(char shortOption, String longOption, List<String> synonyms) { - aggregate = new ArrayList<String>(); - this.shortOption = shortOption; - this.longOption = longOption; - this.synonyms = synonyms; - if (shortOption != SHORT_OPTION_DEFAULT) { - aggregate.add("" + shortOption); - } - if (longOption != null) { - aggregate.add(longOption); - } - if (synonyms != null) { - aggregate.addAll(synonyms); - } - } - - @Override - public String toString() { - StringBuilder builder = new StringBuilder(); - builder.append(Option.class.getSimpleName()).append("[longOption=" + longOption) - .append(",help=" + help).append(",required=" + required + "]"); - return builder.toString(); - } - - @Override - public int hashCode() { - final int prime = 41; - int result = 1; - result = prime * result + ((longOption == null) ? 0 : longOption.hashCode()); - return result; - } - - @Override - public boolean equals(Object obj) { - if (this == obj) { - return true; - } - if (obj == null) { - return false; - } - if (getClass() != obj.getClass()) { - return false; - } - Option option = (Option) obj; - if (longOption == null) { - if (option.getLongOption() != null) { - return false; - } - } else if (!longOption.equals(option.getLongOption())) { - return false; - } - return true; - } - - public List<String> getAggregate() { - return aggregate; - } - - public char getShortOption() { - return shortOption; - } - - public boolean setShortOption(char shortOption) { - if (shortOption != SHORT_OPTION_DEFAULT) { - int index = aggregate.indexOf("" + this.shortOption); - if (index != -1) { - return false; - } else { - this.shortOption = shortOption; - aggregate.add("" + shortOption); - return true; - } - } - return false; - } - - public String getLongOption() { - return longOption; - } - - public boolean setLongOption(String longOption) { - longOption = longOption.trim(); - if (!longOption.equals("")) { - if (this.longOption == null) { - int index = aggregate.indexOf(longOption); - if (index != -1) { - return false; - } else { - this.longOption = longOption; - aggregate.add(longOption); - return true; - } - } - } - return false; - } - - public List<String> getSynonyms() { - return synonyms; - } - - public void setSynonyms(List<String> synonyms) { - this.synonyms = new ArrayList<String>(); - for (String string : synonyms) { - if (!string.equals("")) { - this.synonyms.add(string); - } - } - if (this.synonyms.size() > 0) { - this.aggregate.addAll(this.synonyms); - } - } - - public boolean isWithRequiredArgs() { - return withRequiredArgs; - } - - public void setWithRequiredArgs(boolean withRequiredArgs) { - this.withRequiredArgs = withRequiredArgs; - } - - public String[] getStringArray() { - String[] stringArray = new String[aggregate.size()]; - for (int i = 0; i < stringArray.length; i++) { - stringArray[i] = aggregate.get(i); - } - return stringArray; - } - - public String getSpecifiedDefaultValue() { - if (specifiedDefaultValue.equals(NULL)) { - return null; - } else { - return specifiedDefaultValue; - } - } - - public void setSpecifiedDefaultValue(String specifiedDefaultValue) { - this.specifiedDefaultValue = PreprocessorUtils.trim(specifiedDefaultValue).getString(); - } - - public String getValueSeparator() { - return valueSeparator; - } - - public void setValueSeparator(String valueSeparator) { - this.valueSeparator = valueSeparator; - } -} http://git-wip-us.apache.org/repos/asf/geode/blob/ce9e138c/geode-core/src/main/java/org/apache/geode/management/internal/cli/parser/OptionSet.java ---------------------------------------------------------------------- diff --git a/geode-core/src/main/java/org/apache/geode/management/internal/cli/parser/OptionSet.java b/geode-core/src/main/java/org/apache/geode/management/internal/cli/parser/OptionSet.java deleted file mode 100644 index 42270e5..0000000 --- a/geode-core/src/main/java/org/apache/geode/management/internal/cli/parser/OptionSet.java +++ /dev/null @@ -1,128 +0,0 @@ -/* - * 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.geode.management.internal.cli.parser; - -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -/** - * Stores the result after parsing - * - * @since GemFire 7.0 - * - */ -public class OptionSet { - private Map<Option, String> optionsMap; - private Map<Argument, String> argumentsMap; - private int noOfSpacesRemoved; - private List<String> split; - private String userInput; - - public OptionSet() { - optionsMap = new HashMap<Option, String>(); - argumentsMap = new HashMap<Argument, String>(); - } - - public void put(Argument argument, String value) { - argumentsMap.put(argument, value); - } - - public void put(Option option, String value) { - optionsMap.put(option, value); - } - - public boolean hasOption(Option option) { - return optionsMap.containsKey(option); - } - - public boolean hasArgument(Argument argument) { - String string = argumentsMap.get(argument); - if (string != null) { - return true; - } else { - return false; - } - } - - public boolean hasValue(Option option) { - String string = optionsMap.get(option); - if (string != null && !string.equals("__NULL__")) { - return true; - } else { - return false; - } - } - - public String getValue(Argument argument) { - return argumentsMap.get(argument); - } - - public String getValue(Option option) { - return optionsMap.get(option); - } - - public boolean areArgumentsPresent() { - if (!argumentsMap.isEmpty()) { - return true; - } else - return false; - } - - public boolean areOptionsPresent() { - if (!optionsMap.isEmpty()) { - return true; - } else { - return false; - } - } - - public int getNoOfSpacesRemoved() { - return noOfSpacesRemoved; - } - - public void setNoOfSpacesRemoved(int noOfSpacesRemoved) { - this.noOfSpacesRemoved = noOfSpacesRemoved; - } - - /** - * @return the split - */ - public List<String> getSplit() { - return split; - } - - /** - * @param split the split to set - */ - public void setSplit(List<String> split) { - this.split = split; - } - - public String getUserInput() { - return userInput; - } - - public void setUserInput(String userInput) { - this.userInput = userInput; - } - - @Override - public String toString() { - return "OptionSet [optionsMap=" + optionsMap + ", argumentsMap=" + argumentsMap - + ", noOfSpacesRemoved=" + noOfSpacesRemoved + ", split=" + split + ", userInput=" - + userInput + "]"; - } -} http://git-wip-us.apache.org/repos/asf/geode/blob/ce9e138c/geode-core/src/main/java/org/apache/geode/management/internal/cli/parser/Parameter.java ---------------------------------------------------------------------- diff --git a/geode-core/src/main/java/org/apache/geode/management/internal/cli/parser/Parameter.java b/geode-core/src/main/java/org/apache/geode/management/internal/cli/parser/Parameter.java deleted file mode 100644 index dc371b3..0000000 --- a/geode-core/src/main/java/org/apache/geode/management/internal/cli/parser/Parameter.java +++ /dev/null @@ -1,116 +0,0 @@ -/* - * 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.geode.management.internal.cli.parser; - -import org.springframework.shell.core.Converter; - -import org.apache.geode.management.internal.cli.parser.preprocessor.PreprocessorUtils; - -/** - * Parameter of a Command - * - * @since GemFire 7.0 - */ -public abstract class Parameter { - // help for the Parameter - protected String help; - - // Constraint on the Parameter - protected boolean required; - - // Useful for Value conversion - protected String context; - protected Converter<?> converter; - - // Data type of the option - protected Class<?> dataType; - - // Necessary for preserving order in - // ParseResult object - protected int parameterNo; - - // value related - protected boolean systemProvided; - protected String unspecifiedDefaultValue; - - public String getHelp() { - return help; - } - - public void setHelp(String help) { - this.help = help; - } - - public boolean isRequired() { - return required; - } - - public void setRequired(boolean required) { - this.required = required; - } - - public String getContext() { - return context; - } - - public void setContext(String context) { - this.context = context; - } - - public Converter<?> getConverter() { - return converter; - } - - // TODO Change for concurrent access. - public void setConverter(Converter<?> converter) { - this.converter = converter; - } - - public Class<?> getDataType() { - return dataType; - } - - public void setDataType(Class<?> dataType) { - this.dataType = dataType; - } - - public int getParameterNo() { - return parameterNo; - } - - public void setParameterNo(int parameterNo) { - this.parameterNo = parameterNo; - } - - public boolean isSystemProvided() { - return systemProvided; - } - - public void setSystemProvided(boolean systemProvided) { - this.systemProvided = systemProvided; - } - - public String getUnspecifiedDefaultValue() { - if (unspecifiedDefaultValue.equals("__NULL__")) { - return null; - } else { - return unspecifiedDefaultValue; - } - } - - public void setUnspecifiedDefaultValue(String unspecifiedDefaultValue) { - this.unspecifiedDefaultValue = PreprocessorUtils.trim(unspecifiedDefaultValue).getString(); - } -} http://git-wip-us.apache.org/repos/asf/geode/blob/ce9e138c/geode-core/src/main/java/org/apache/geode/management/internal/cli/parser/ParserUtils.java ---------------------------------------------------------------------- diff --git a/geode-core/src/main/java/org/apache/geode/management/internal/cli/parser/ParserUtils.java b/geode-core/src/main/java/org/apache/geode/management/internal/cli/parser/ParserUtils.java deleted file mode 100644 index 9faccb6..0000000 --- a/geode-core/src/main/java/org/apache/geode/management/internal/cli/parser/ParserUtils.java +++ /dev/null @@ -1,186 +0,0 @@ -/* - * 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.geode.management.internal.cli.parser; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - -import org.apache.geode.management.internal.cli.parser.preprocessor.PreprocessorUtils; - -/** - * - * Utility class for parsing and pre-processing - * - * The methods herein always ensure that the syntax is proper before performing the desired - * operation - * - * @since GemFire 7.0 - */ -public class ParserUtils { - public static String[] split(String input, String splitAround) { - if (input != null && splitAround != null) { - List<String> parts = new ArrayList<String>(); - StringBuffer part = new StringBuffer(); - outer: for (int i = 0; i < input.length(); i++) { - char ch = input.charAt(i); - if (splitAround.startsWith("" + ch)) { - // First check whether syntax is valid - if (PreprocessorUtils.isSyntaxValid(part.toString())) { - // This means that we need to check further whether - // the splitAround is present - StringBuffer temp = new StringBuffer(""); - for (int j = 0; j < splitAround.length() && (i + j) < input.length(); j++) { - temp.append(input.charAt((i + j))); - if (temp.toString().equals(splitAround)) { - parts.add(part.toString().trim()); - part.delete(0, part.length()); - i = i + j + 1; - if (i < input.length()) { - ch = input.charAt(i); - } else { - break outer; - } - break; - } - } - } - } - part.append(ch); - } - // Need to copy the last part in the parts list - if (part.length() > 0) { - if (!PreprocessorUtils.containsOnlyWhiteSpaces(part.toString())) { - if (!part.toString().equals(splitAround)) - parts.add(part.toString().trim()); - } - } - // Convert the list into an array - String[] split = new String[parts.size()]; - for (int i = 0; i < split.length; i++) { - split[i] = parts.get(i); - } - return split; - } else { - return null; - } - } - - public static String[] splitValues(String value, String valueSeparator) { - if (value != null && valueSeparator != null) { - String[] split = split(value, valueSeparator); - if (value.endsWith(valueSeparator) - && PreprocessorUtils.isSyntaxValid(split[split.length - 1])) { - String[] extendedSplit = new String[split.length + 1]; - for (int i = 0; i < split.length; i++) { - extendedSplit[i] = split[i]; - } - extendedSplit[split.length] = ""; - return extendedSplit; - } - - // Remove quotes from the beginning and end of split strings - for (int i = 0; i < split.length; i++) { - if ((split[i].endsWith("\"") && split[i].endsWith("\"")) - || (split[i].startsWith("\'") && split[i].endsWith("\'"))) { - split[i] = split[i].substring(1, split[i].length() - 1); - } - } - - return split; - } else { - return null; - } - } - - public static boolean contains(String value, String subString) { - if (value != null && subString != null) { - // Here we need to keep in mind that once we get the substring, we - // should check whether the syntax remains valid - StringBuffer part = new StringBuffer(); - for (int i = 0; i < value.length(); i++) { - char ch = value.charAt(i); - if (subString.startsWith("" + ch)) { - StringBuffer subPart = new StringBuffer(ch); - if (PreprocessorUtils.isSyntaxValid(part.toString())) { - for (int j = 0; j < subString.length() && (i + j) < value.length(); j++) { - subPart.append("" + value.charAt(i + j)); - if (subPart.toString().equals(subString)) { - // The subString is present - // We can return from here - return true; - } - } - } - } - part.append(ch); - } - } - return false; - } - - public static int lastIndexOf(String value, String subString) { - int index = -1; - if (value != null && subString != null) { - StringBuffer part = new StringBuffer(); - outer: for (int i = 0; i < value.length(); i++) { - char ch = value.charAt(i); - if (subString.startsWith("" + ch)) { - StringBuffer subPart = new StringBuffer(ch); - if (PreprocessorUtils.isSyntaxValid(part.toString())) { - for (int j = 0; j < subString.length() && (i + j) < value.length(); j++) { - subPart.append(value.charAt(i + j)); - if (subPart.toString().equals(subString)) { - // The subString is present - // We can return from here - index = i; - part.delete(0, part.length()); - i += j + 1; - if (i < value.length()) { - ch = value.charAt(i); - } else { - break outer; - } - } - } - } - } - part.append(ch); - } - } - return index; - } - - public static String getPadding(int numOfSpaces) { - char[] arr = new char[numOfSpaces]; - Arrays.fill(arr, ' '); - return new String(arr); - } - - public static String trimBeginning(String stringToTrim) { - if (stringToTrim.startsWith(" ")) { - int i = 0; - for (; i < stringToTrim.length(); i++) { - if (stringToTrim.charAt(i) != ' ') { - break; - } - } - stringToTrim = stringToTrim.substring(i); - } - - return stringToTrim; - } - -} http://git-wip-us.apache.org/repos/asf/geode/blob/ce9e138c/geode-core/src/main/java/org/apache/geode/management/internal/cli/parser/SyntaxConstants.java ---------------------------------------------------------------------- diff --git a/geode-core/src/main/java/org/apache/geode/management/internal/cli/parser/SyntaxConstants.java b/geode-core/src/main/java/org/apache/geode/management/internal/cli/parser/SyntaxConstants.java deleted file mode 100644 index 52d9212..0000000 --- a/geode-core/src/main/java/org/apache/geode/management/internal/cli/parser/SyntaxConstants.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * 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.geode.management.internal.cli.parser; - -/** - * Syntax constants for the parser - * - * @since GemFire 7.0 - * - */ -// TODO merge with CliConstants -public class SyntaxConstants { - public static final String OPTION_VALUE_SPECIFIER = "="; - public static final String VALUE_SEPARATOR = ","; - public static final String ARGUMENT_SEPARATOR = "?"; - public static final String OPTION_SEPARATOR = " "; - public static final String SHORT_OPTION_SPECIFIER = "-"; - public static final String LONG_OPTION_SPECIFIER = "--"; - public static final String COMMAND_DELIMITER = ";"; - public static final String CONTINUATION_CHARACTER = "\\"; -} -
