Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.yammer.dropwizard;
  
 
 
 import  javax.annotation.CheckForNull;
 import java.util.List;
The base class for both Java and Scala services. Do not extend this directly. Use Service instead.

Parameters:
<T> the type of configuration class for this service
 
 @SuppressWarnings("EmptyMethod")
 public abstract class AbstractService<T extends Configuration> {
     static {
         // make sure spinning up Hibernate Validator doesn't yell at us
         LoggingFactory.bootstrap();
     }
 
     private final String name;
     private final List<Bundlebundles;
     private final List<ConfiguredBundle<? super T>> configuredBundles;
     private final List<Modulemodules;
     private final SortedMap<StringCommandcommands;

    
Creates a new service with the given name.

Parameters:
name the service's name
 
     protected AbstractService(String name) {
         this. = name;
         this. = Lists.newArrayList();
         this. = Lists.newArrayList();
         this. = Lists.newArrayList();
         this. = Maps.newTreeMap();
         addCommand(new ServerCommand<T>(getConfigurationClass()));
     }

    
A simple reminder that this particular class isn't meant to be extended by non-DW classes.
 
     protected abstract void subclassServiceInsteadOfThis();
 
     public final String getName() {
         return ;
     }

    
Returns the Class of the configuration class type parameter.

Returns:
the configuration class
See also:
Super Type Tokens
 
     @SuppressWarnings("unchecked")
     public final Class<T> getConfigurationClass() {
         return (Class<T>) ((ParameterizedTypegetClass().getGenericSuperclass()).getActualTypeArguments()[0];
     }

    
Registers a Bundle to be used in initializing the service's Environment.

Parameters:
bundle a bundle
See also:
Bundle
 
     protected final void addBundle(Bundle bundle) {
         .add(bundle);
     }

    
Registers a ConfiguredBundle to be used in initializing the service's Environment.

Parameters:
bundle a bundle
See also:
ConfiguredBundle
 
     protected final void addBundle(ConfiguredBundle<? super T> bundle) {
         .add(bundle);
     }

    
Returns a list of registered Command instances.

Returns:
a list of commands
    public final ImmutableList<CommandgetCommands() {
        return ImmutableList.copyOf(.values());
    }

    
Registers a Command which the service will provide.

Parameters:
command a command
    protected final void addCommand(Command command) {
        .put(command.getName(), command);
    }

    
Registers a ConfiguredCommand which the service will provide.

Parameters:
command a command
    protected final void addCommand(ConfiguredCommand<T> command) {
        .put(command.getName(), command);
    }

    
Registers a Jackson Module which the service will use to parse the configuration file and to parse/generate any application/json entities.

Parameters:
module a Module
    protected final void addJacksonModule(Module module) {
        .add(module);
    }

    
When the service runs, this is called after the Bundles are run. Override it to add providers, resources, etc. for your service.

Parameters:
configuration the parsed Configuration object
environment the service's Environment
Throws:
Exception if something goes wrong
    protected abstract void initialize(T configurationEnvironment environmentthrows Exception;

    
Initializes the given Environment given a Configuration instances. First the bundles are initialized in the order they were added, then the service's initialize(Configuration, Environment) method is called.

Parameters:
configuration the parsed Configuration object
environment the service's Environment
Throws:
Exception if something goes wrong
    public final void initializeWithBundles(T configurationEnvironment environmentthrows Exception {
        for (Bundle bundle : ) {
            bundle.initialize(environment);
        }
        for (ConfiguredBundle<? super T> bundle : ) {
            bundle.initialize(configurationenvironment);
        }
        initialize(configurationenvironment);
    }

    
Parses command-line arguments and runs the service. Call this method from a public static void main entry point in your application.

Parameters:
arguments the command-line arguments
Throws:
Exception if something goes wrong
    public final void run(String[] argumentsthrows Exception {
        if (isHelp(arguments)) {
            UsagePrinter.printRootHelp(this);
        } else {
            final Command cmd = .get(arguments[0]);
            if (cmd != null) {
                cmd.run(this, Arrays.copyOfRange(arguments, 1, arguments.length));
            } else {
                UsagePrinter.printRootHelp(this);
            }
        }
    }

    
Returns a list of Jackson Modules used to parse JSON (and the configuration files).

Returns:
a list of Modules
        return ImmutableList.copyOf();
    }
    
    
Returns the json environment wrapper that configures and calls into jackson

Returns:
an instanceof Json
    public Json getJson() {
        final Json json = new Json();
        for (Module module : getJacksonModules()) {
            json.registerModule(module);
        }
        return json;
    }

    
Returns the Jersey servlet container used to serve HTTP requests. This implementation creates a new ServletContainer instance with the given resource configuration. Subclasses must either use the same config instance or delegate to it.

This method may be called before the service initialized with initialize(Configuration, Environment); service implementations must not assume the service has been initialized.

An implementation that chooses to return null is responsible for creating a container with the given config by other means during initialization and startup.

Parameters:
config the Jersey resource config to use for the container
Returns:
a Jersey servlet container, or null if the Jersey container will be created by other means
    @CheckForNull
        return new ServletContainer(config);
    }
    
    private static boolean isHelp(String[] arguments) {
        return (arguments.length == 0) ||
                ((arguments.length == 1) &&
                        ("-h".equals(arguments[0]) ||
                                "--help".equals(arguments[0])));
    }
New to GrepCode? Check out our FAQ X