Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
 package com.yammer.dropwizard.cli;
 
 
A command whose first parameter is the location of a YAML configuration file. That file is parsed into an instance of a com.yammer.dropwizard.config.Configuration subclass, which is then validated. If the configuration is valid, the command is run.

Parameters:
<T> the com.yammer.dropwizard.config.Configuration subclass which is loaded from the configuration file
See also:
com.yammer.dropwizard.config.Configuration
public abstract class ConfiguredCommand<T extends Configurationextends Command {
    protected ConfiguredCommand(String nameString description) {
        super(namedescription);
    }

    
Returns the java.lang.Class of the configuration type.

Returns:
the java.lang.Class of the configuration type
    protected Class<T> getConfigurationClass() {
        return Generics.getTypeParameter(getClass(), Configuration.class);
    }

    
Configure the command's net.sourceforge.argparse4j.inf.Subparser.

N.B.: if you override this method, you must call super.override(subparser) in order to preserve the configuration file parameter in the subparser.

Parameters:
subparser the net.sourceforge.argparse4j.inf.Subparser specific to the command
    @Override
    public void configure(Subparser subparser) {
        subparser.addArgument("file").nargs("?").help("service configuration file");
    }
    @Override
    @SuppressWarnings("unchecked")
    public final void run(Bootstrap<?> bootstrapNamespace namespacethrows Exception {
        final T configuration = parseConfiguration(namespace.getString("file"),
                                                   getConfigurationClass(),
                                                   bootstrap.getObjectMapperFactory().copy());
        if (configuration != null) {
            new LoggingFactory(configuration.getLoggingConfiguration(),
                               bootstrap.getName()).configure();
        }
        run((Bootstrap<T>) bootstrapnamespaceconfiguration);
    }

    
Runs the command with the given com.yammer.dropwizard.config.Bootstrap and com.yammer.dropwizard.config.Configuration.

Parameters:
bootstrap the bootstrap bootstrap
namespace the parsed command line namespace
configuration the configuration object
Throws:
java.lang.Exception if something goes wrong
    protected abstract void run(Bootstrap<T> bootstrap,
                                Namespace namespace,
                                T configurationthrows Exception;
    private T parseConfiguration(String filename,
                                 Class<T> configurationClass,
                                 ObjectMapperFactory objectMapperFactorythrows IOExceptionConfigurationException {
        final ConfigurationFactory<T> configurationFactory =
                ConfigurationFactory.forClass(configurationClassnew Validator(), objectMapperFactory);
        if (filename != null) {
            final File file = new File(filename);
            if (!file.exists()) {
                throw new FileNotFoundException("File " + file + " not found");
            }
            return configurationFactory.build(file);
        }
        return configurationFactory.build();
    }
New to GrepCode? Check out our FAQ X