package com.yammer.dropwizard.cli;

import com.google.common.base.Charsets;
import com.google.common.io.Resources;
import com.yammer.dropwizard.AbstractService;
import com.yammer.dropwizard.config.Configuration;
import com.yammer.dropwizard.config.Environment;
import com.yammer.dropwizard.config.ServerFactory;
import com.yammer.dropwizard.logging.Log;
import org.apache.commons.cli.CommandLine;
import org.eclipse.jetty.server.Server;

// TODO: 10/12/11 <coda> -- write tests for ServerCommand

/**
 * Runs a service as an HTTP server.
 *
 * @param <T> the {@link Configuration} subclass which is loaded from the configuration file
 */
public class ServerCommand<T extends Configuration> extends ConfiguredCommand<T> {
    private final Class<T> configurationClass;

    /**
     * Creates a new {@link ServerCommand} with the given configuration class.
     *
     * @param configurationClass    the configuration class the YAML file is parsed as
     */
    public ServerCommand(Class<T> configurationClass) {
        super("server", "Starts an HTTP server running the service");
        this.configurationClass = configurationClass;
    }

    /*
     * Since we don't subclass ServerCommand, we need a concrete reference to the configuration
     * class.
     */
    @Override
    protected Class<T> getConfigurationClass() {
        return configurationClass;
    }

    @Override
    protected void run(AbstractService<T> service,
                       T configuration,
                       CommandLine params) throws Exception {
        final Environment environment = new Environment(configuration, service);
        service.initializeWithBundles(configuration, environment);

        final Server server = new ServerFactory(configuration.getHttpConfiguration()).buildServer(environment);

        final Log log = Log.forClass(ServerCommand.class);
        try {
            final String banner = Resources.toString(Resources.getResource("banner.txt"), Charsets.UTF_8);
            log.info("Starting {}\n{}", service.getName(), banner);
        } catch (IllegalArgumentException ignored) {
            // don't display the banner if there isn't one
            log.info("Starting {}", service.getName());
        }

        try {
            server.start();
            server.join();
        } catch (Exception e) {
            log.error(e, "Unable to start server, shutting down");
            server.stop();
        }
    }
}

@@ -431,16 +431,45 @@
 erver;%0A%0A
+import java.io.IOException;%0A%0A
 // TODO:
@@ -1704,33 +1704,32 @@
 , environment);%0A
-%0A
         final Se
@@ -1824,17 +1824,16 @@
 nment);%0A
-%0A
         
@@ -1883,16 +1883,327 @@
 class);%0A
+        logBanner(service, log);%0A        try %7B%0A            server.start();%0A            server.join();%0A        %7D catch (Exception e) %7B%0A            log.error(e, %22Unable to start server, shutting down%22);%0A            server.stop();%0A        %7D%0A    %7D%0A%0A    private void logBanner(AbstractService%3CT%3E service, Log log) %7B%0A
         
@@ -2297,16 +2297,69 @@
 r.txt%22),
+%0A                                                    
  Charset
@@ -2617,96 +2617,16 @@
    %7D
-%0A%0A        try %7B%0A            server.start();%0A            server.join();%0A        %7D
  catch (
 Exce
@@ -2613,32 +2613,34 @@
        %7D catch (
+IO
 Exception e) %7B%0A 
@@ -2625,33 +2625,39 @@
 ch (IOException 
-e
+ignored
 ) %7B%0A            
@@ -2664,83 +2664,45 @@
 log.
-error(e, %22Unable to start server, shutting down%22);%0A            server.stop(
+info(%22Starting %7B%7D%22, service.getName()
 );%0A 

Clean up ServerCommand a little.