001    /*
002     * Cumulus4j - Securing your data in the cloud - http://cumulus4j.org
003     * Copyright (C) 2011 NightLabs Consulting GmbH
004     *
005     * This program is free software: you can redistribute it and/or modify
006     * it under the terms of the GNU Affero General Public License as
007     * published by the Free Software Foundation, either version 3 of the
008     * License, or (at your option) any later version.
009     *
010     * This program is distributed in the hope that it will be useful,
011     * but WITHOUT ANY WARRANTY; without even the implied warranty of
012     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013     * GNU Affero General Public License for more details.
014     *
015     * You should have received a copy of the GNU Affero General Public License
016     * along with this program.  If not, see <http://www.gnu.org/licenses/>.
017     */
018    package org.cumulus4j.keymanager.cli;
019    
020    import java.io.Console;
021    
022    import org.kohsuke.args4j.Option;
023    
024    /**
025     * <p>
026     * Sub-command for a certain CLI operation.
027     * </p>
028     * <p>
029     * The key-store-command-line-interface uses a syntax similar to the svn command and the logic of the
030     * command 'java -jar org.cumulus4j.keymanager.cli-VERSION.jar SUBCOMMAND -arg1 val1 -arg2 val2 ...'
031     * is thus actually implemented by a class extending this class and {@link #getSubCommandName() registering}
032     * for a certain 'SUBCOMMAND'.
033     * </p>
034     * <p>
035     * Every subclass of this class can declare its arguments using annotations like {@link Option}.
036     * </p>
037     *
038     * @author Marco หงุ่ยตระกูล-Schulze - marco at nightlabs dot de
039     */
040    public abstract class SubCommand
041    {
042            /**
043             * Get the name of the sub-command, i.e. what the user has to write in the command line.
044             * @return the name of the sub-command.
045             */
046            public abstract String getSubCommandName();
047    
048            /**
049             * Get the description for this sub-command.
050             * @return the description.
051             */
052            public abstract String getSubCommandDescription();
053    
054            public void prepare()
055            throws Exception
056            {
057    
058            }
059    
060            public abstract void run()
061            throws Exception;
062    
063            protected String promptPassword(String fmt, Object ... args) {
064                    Console console = System.console();
065                    if (console == null)
066                            throw new IllegalStateException("There is no system console! Cannot prompt \"" + String.format(fmt, args) + "\"!!!");
067            
068                    char[] pw = console.readPassword(fmt, args);
069                    if (pw == null)
070                            return null;
071                    else
072                            return new String(pw);
073            }
074    }