/*
 * The MIT License (MIT)
 * 
 * Copyright (c) 2013-2014 Jeff Nelson, Cinchapi Software Collective
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package org.cinchapi.concourse.server.cli;

import java.io.IOException;
import javax.management.JMX;
import javax.management.MBeanServerConnection;
import javax.management.ObjectName;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;

import jline.console.ConsoleReader;

import org.cinchapi.concourse.server.ConcourseServer;
import org.cinchapi.concourse.server.jmx.ConcourseServerMXBean;
import com.beust.jcommander.JCommander;
import com.beust.jcommander.ParameterException;
import com.google.common.base.CaseFormat;
import com.google.common.base.Strings;

/**
 * A CLI that performs an operation on a {@link ConcourseServerMXBean}. Any CLI
 * that operates on a running {@link ConcourseServer} should extend this class.
 * 
 * @author jnelson
 */
public abstract class ManagedOperationCli {

    /**
     * The CLI options.
     */
    protected Options options;

    /**
     * The parser that validates the CLI options.
     */
    protected JCommander parser;

    /**
     * Handler to the console for interactive I/O.
     */
    protected ConsoleReader console;

    /**
     * Construct a new instance that is seeded with an object containing options
     * metadata. The {@code options} will be parsed by {@link JCommander} to
     * configure them appropriately.
     * 
     * @param options
     * @param args - these usually come from the main method
     */
    public ManagedOperationCli(Options options, String... args) {
        try {
            this.parser = new JCommander(options, args);
            this.options = options;
            parser.setProgramName(CaseFormat.UPPER_CAMEL.to(
                    CaseFormat.LOWER_HYPHEN, this.getClass().getSimpleName()));
            this.console = new ConsoleReader();
        }
        catch (ParameterException e) {
            die(e.getMessage());
        }
        catch (IOException e) {
            die(e.getMessage());
        }
    }

    /**
     * Run the CLI. This method should only be called from the main method.
     */
    public final void run() {
        try {
            MBeanServerConnection connection = JMXConnectorFactory.connect(
                    new JMXServiceURL(ConcourseServerMXBean.JMX_SERVICE_URL))
                    .getMBeanServerConnection();
            ObjectName objectName = new ObjectName(
                    "org.cinchapi.concourse.server.jmx:type=ConcourseServerMXBean");
            ConcourseServerMXBean bean = JMX.newMBeanProxy(connection,
                    objectName, ConcourseServerMXBean.class);
            if(options.help) {
                parser.usage();
                System.exit(1);
            }
            else {
                if(Strings.isNullOrEmpty(options.password)) {
                    options.password = console.readLine("Password ["
                            + options.username + "]: ", '*');
                }
                byte[] username = options.username.getBytes();
                byte[] password = options.password.getBytes();
                if(bean.login(username, password)) {
                    doTask(bean);
                    System.exit(0);
                }
                else {
                    die("Invalid username/password combination.");
                }
            }
        }
        catch (IOException e) {
            die("Could not connect to the management server. Please check "
                    + "that ConcourseServer is running with JMX enabled.");
        }
        catch (Exception e) {
            die(e.getMessage());
        }
    }

    /**
     * Print {@code message} to stderr and exit with a non-zero status.
     * 
     * @param message
     */
    protected void die(String message) {
        System.err.println("ERROR: " + message);
        System.exit(2);
    }

    /**
     * Implement a managed task that involves at least one of the operations
     * available from {@code bean}. This method is called by the main
     * {@link #run()} method, so the implementer should place all task logic
     * here.
     * <p>
     * DO NOT call {@link System#exit(int)} with '0' from this method
     * </p>
     * 
     * @param bean
     */
    protected abstract void doTask(ConcourseServerMXBean bean);

}

@@ -2909,24 +2909,133 @@
 leName()));%0A
+            if(options.help) %7B%0A                parser.usage();%0A                System.exit(1);%0A            %7D%0A
             
@@ -3857,140 +3857,8 @@
 s);%0A
-            if(options.help) %7B%0A                parser.usage();%0A                System.exit(1);%0A            %7D%0A            else %7B%0A    
     
@@ -3927,20 +3927,16 @@
         
-  
-  
 options.
@@ -4000,20 +4000,16 @@
         
-    
 + option
@@ -4042,32 +4042,26 @@
             
-    %7D%0A  
+%7D%0A
             
@@ -4056,26 +4056,24 @@
             
-  
 byte%5B%5D usern
@@ -4099,36 +4099,32 @@
 ame.getBytes();%0A
-    
             byte
@@ -4178,20 +4178,16 @@
         
-    
 if(bean.
@@ -4231,20 +4231,16 @@
         
-  
-  
 doTask(b
@@ -4245,20 +4245,16 @@
 (bean);%0A
-    
         
@@ -4285,32 +4285,26 @@
             
-    %7D%0A  
+%7D%0A
             
@@ -4307,21 +4307,15 @@
     
-  
 else %7B%0A
-    
     
@@ -4377,26 +4377,8 @@
 %22);%0A
-                %7D%0A
     

Make it possible to see CLI help text without logging in