diff --git a/cli/src/main/java/org/gatein/management/cli/crash/arguments/ImportModeOption.java b/cli/src/main/java/org/gatein/management/cli/crash/arguments/ImportModeOption.java index 3f529e77..2580f538 100644 --- a/cli/src/main/java/org/gatein/management/cli/crash/arguments/ImportModeOption.java +++ b/cli/src/main/java/org/gatein/management/cli/crash/arguments/ImportModeOption.java @@ -22,7 +22,6 @@ package org.gatein.management.cli.crash.arguments; -import org.crsh.cmdline.ParameterDescriptor; import org.crsh.cmdline.annotations.Man; import org.crsh.cmdline.annotations.Option; import org.crsh.cmdline.annotations.Usage; @@ -31,10 +30,7 @@ import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.util.Arrays; -import java.util.Collections; -import java.util.HashMap; import java.util.HashSet; -import java.util.Map; import java.util.Set; /** @@ -47,27 +43,13 @@ @Man("The import mode for an import operation. Valid values are: conserve, insert, merge, and overwrite.") public @interface ImportModeOption { - public static class ImportModeCompleter implements Completer + public static class ImportModeCompleter extends StringCollectionCompleter implements Completer { public static final Set modes = new HashSet(Arrays.asList("conserve", "insert", "merge", "overwrite")); - @Override - public Map complete(ParameterDescriptor parameter, String prefix) throws Exception + public ImportModeCompleter() { - Map completions = new HashMap(modes.size()); - for (String mode : modes) - { - if ("".equals(prefix)) - { - completions.put(mode, true); - } - else if (mode.startsWith(prefix)) - { - completions.put(mode.substring(prefix.length()), true); - } - } - - return completions; + super(modes); } } } diff --git a/cli/src/main/java/org/gatein/management/cli/crash/arguments/OperationOption.java b/cli/src/main/java/org/gatein/management/cli/crash/arguments/OperationOption.java index 2b3cc349..077ebcf4 100644 --- a/cli/src/main/java/org/gatein/management/cli/crash/arguments/OperationOption.java +++ b/cli/src/main/java/org/gatein/management/cli/crash/arguments/OperationOption.java @@ -25,18 +25,43 @@ import org.crsh.cmdline.annotations.Man; import org.crsh.cmdline.annotations.Option; import org.crsh.cmdline.annotations.Usage; +import org.crsh.cmdline.spi.Completer; +import org.gatein.management.api.operation.OperationNames; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; +import java.util.LinkedHashSet; +import java.util.Set; /** * @author Nick Scavelli * @version $Revision$ */ @Retention(RetentionPolicy.RUNTIME) -@Option(names = {"o", "operation"}) +@Option(names = {"o", "operation"}, completer = OperationOption.OperationOptionCompleter.class) @Usage("Operation name") @Man("Specifies the name of the operation of the management request.") public @interface OperationOption { + public static class OperationOptionCompleter extends StringCollectionCompleter implements Completer + { + public OperationOptionCompleter() + { + super(operationNames); + } + + private static final Set operationNames; + static + { + Set names = new LinkedHashSet(4); + + // Just add the CRUD operations for now + names.add(OperationNames.READ_RESOURCE); + names.add(OperationNames.UPDATE_RESOURCE); + names.add(OperationNames.ADD_RESOURCE); + names.add(OperationNames.REMOVE_RESOURCE); + + operationNames = names; + } + } } diff --git a/cli/src/main/java/org/gatein/management/cli/crash/arguments/StringCollectionCompleter.java b/cli/src/main/java/org/gatein/management/cli/crash/arguments/StringCollectionCompleter.java new file mode 100644 index 00000000..6287c0ea --- /dev/null +++ b/cli/src/main/java/org/gatein/management/cli/crash/arguments/StringCollectionCompleter.java @@ -0,0 +1,60 @@ +/* + * JBoss, Home of Professional Open Source. + * Copyright 2012, Red Hat, Inc., and individual contributors + * as indicated by the @author tags. See the copyright.txt file in the + * distribution for a full listing of individual contributors. + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ + +package org.gatein.management.cli.crash.arguments; + +import org.crsh.cmdline.ParameterDescriptor; +import org.crsh.cmdline.spi.Completer; + +import java.util.Collection; +import java.util.Collections; +import java.util.LinkedHashMap; +import java.util.Map; + +/** + * @author Nick Scavelli + */ +public class StringCollectionCompleter implements Completer +{ + private final Collection collection; + + public StringCollectionCompleter(Collection collection) + { + this.collection = collection; + } + + @Override + public Map complete(ParameterDescriptor parameter, String prefix) throws Exception + { + Map completions = Collections.emptyMap(); + for (String value : collection) { + if (value.startsWith(prefix)) { + if (completions.isEmpty()) { + completions = new LinkedHashMap(); + } + completions.put(value.substring(prefix.length()), true); + } + } + + return completions; + } +}