Java 类io.dropwizard.cli.ServerCommand 实例源码

项目:monasca-common    文件:AbstractAppTest.java   
private void startIfRequired() {
  if (jettyServer != null) {
    return;
  }

  try {
    application = newApplication();

    final Bootstrap<C> bootstrap = new Bootstrap<C>(application) {
      @Override
      public void run(C configuration, Environment environment) throws Exception {
        environment.lifecycle().addServerLifecycleListener(new ServerLifecycleListener() {
          @Override
          public void serverStarted(Server server) {
            jettyServer = server;
          }
        });
        AbstractAppTest.this.configuration = configuration;
        AbstractAppTest.this.environment = environment;
        super.run(configuration, environment);
      }
    };

    application.initialize(bootstrap);
    final ServerCommand<C> command = new ServerCommand<>(application);
    final Namespace namespace = new Namespace(ImmutableMap.<String, Object>of("file", configPath));
    command.run(bootstrap, namespace);
  } catch (Exception e) {
    throw new RuntimeException(e);
  }
}
项目:dropwizard-helix    文件:DropWizardApplicationRunner.java   
/**
 * Creates a Jetty server for an application that can be started / stopped in-process
 *
 * @param config           An application configuration instance (with properties set)
 * @param applicationClass The {@link io.dropwizard.Application} implementation class
 * @param <T>              The configuration class
 * @return A Jetty server
 */
@SuppressWarnings("unchecked")
public static <T extends Configuration> DropWizardServer<T> createServer(
    T config,
    Class<? extends Application<T>> applicationClass) throws Exception {
  // Create application
  final Application<T> application = applicationClass.getConstructor().newInstance();

  // Create bootstrap
  final ServerCommand<T> serverCommand = new ServerCommand<T>(application);
  final Bootstrap<T> bootstrap = new Bootstrap<T>(application);
  bootstrap.addCommand(serverCommand);

  // Write a temporary config file
  File tmpConfigFile = new File(
      System.getProperty("java.io.tmpdir"),
      config.getClass().getCanonicalName() + "_" + System.currentTimeMillis());
  tmpConfigFile.deleteOnExit();
  bootstrap.getObjectMapper().writeValue(tmpConfigFile, config);

  // Parse configuration
  ConfigurationFactory<T> configurationFactory
      = bootstrap.getConfigurationFactoryFactory()
      .create((Class<T>) config.getClass(),
          bootstrap.getValidatorFactory().getValidator(),
          bootstrap.getObjectMapper(),
          "dw");
  final T builtConfig = configurationFactory.build(
      bootstrap.getConfigurationSourceProvider(), tmpConfigFile.getAbsolutePath());

  // Configure logging
  builtConfig.getLoggingFactory()
      .configure(bootstrap.getMetricRegistry(),
          bootstrap.getApplication().getName());

  // Environment
  final Environment environment = new Environment(bootstrap.getApplication().getName(),
      bootstrap.getObjectMapper(),
      bootstrap.getValidatorFactory().getValidator(),
      bootstrap.getMetricRegistry(),
      bootstrap.getClassLoader());

  // Initialize environment
  builtConfig.getMetricsFactory().configure(environment.lifecycle(), bootstrap.getMetricRegistry());

  // Server
  final Server server = builtConfig.getServerFactory().build(environment);
  server.addLifeCycleListener(new AbstractLifeCycle.AbstractLifeCycleListener() {
    @Override
    public void lifeCycleStopped(LifeCycle event) {
      builtConfig.getLoggingFactory().stop();
    }
  });

  return new DropWizardServer(builtConfig, bootstrap, application, environment, server, environment.metrics());
}
项目:Pinot    文件:DropWizardApplicationRunner.java   
/**
 * Creates a Jetty server for an application that can be started / stopped in-process
 *
 * @param config
 *  An application configuration instance (with properties set)
 * @param applicationClass
 *  The {@link io.dropwizard.Application} implementation class
 * @param <T>
 *  The configuration class
 * @return
 *  A Jetty server
 */
@SuppressWarnings("unchecked")
public static <T extends Configuration>
DropWizardServer<T> createServer(T config, Class<? extends Application<T>> applicationClass) throws Exception
{
  // Create application
  final Application<T> application = applicationClass.getConstructor().newInstance();

  // Create bootstrap
  final ServerCommand<T> serverCommand = new ServerCommand<T>(application);
  final Bootstrap<T> bootstrap = new Bootstrap<T>(application);
  bootstrap.addCommand(serverCommand);

  // Write a temporary config file
  File tmpConfigFile = new File(
          System.getProperty("java.io.tmpdir"),
          config.getClass().getCanonicalName() + "_" + System.currentTimeMillis());
  tmpConfigFile.deleteOnExit();
  bootstrap.getObjectMapper().writeValue(tmpConfigFile, config);

  // Parse configuration
  ConfigurationFactory<T> configurationFactory
          = bootstrap.getConfigurationFactoryFactory()
                     .create((Class<T>) config.getClass(),
                             bootstrap.getValidatorFactory().getValidator(),
                             bootstrap.getObjectMapper(),
                             "dw");
  final T builtConfig = configurationFactory.build(
          bootstrap.getConfigurationSourceProvider(), tmpConfigFile.getAbsolutePath());

  // Configure logging
  builtConfig.getLoggingFactory()
             .configure(bootstrap.getMetricRegistry(),
                        bootstrap.getApplication().getName());

  // Environment
  final Environment environment = new Environment(bootstrap.getApplication().getName(),
                                                  bootstrap.getObjectMapper(),
                                                  bootstrap.getValidatorFactory().getValidator(),
                                                  bootstrap.getMetricRegistry(),
                                                  bootstrap.getClassLoader());

  // Initialize environment
  builtConfig.getMetricsFactory().configure(environment.lifecycle(), bootstrap.getMetricRegistry());

  // Server
  final Server server = builtConfig.getServerFactory().build(environment);
  server.addLifeCycleListener(new AbstractLifeCycle.AbstractLifeCycleListener()
  {
    @Override
    public void lifeCycleStopped(LifeCycle event)
    {
      builtConfig.getLoggingFactory().stop();
    }
  });

  return new DropWizardServer(builtConfig, bootstrap, application, environment, server, environment.metrics());
}
项目:Pinot    文件:DropWizardApplicationRunner.java   
/**
 * Creates a Jetty server for an application that can be started / stopped in-process
 *
 * @param config
 *  An application configuration instance (with properties set)
 * @param applicationClass
 *  The {@link io.dropwizard.Application} implementation class
 * @param <T>
 *  The configuration class
 * @return
 *  A Jetty server
 */
@SuppressWarnings("unchecked")
public static <T extends Configuration>
DropWizardServer<T> createServer(T config, Class<? extends Application<T>> applicationClass) throws Exception
{
  // Create application
  final Application<T> application = applicationClass.getConstructor().newInstance();

  // Create bootstrap
  final ServerCommand<T> serverCommand = new ServerCommand<T>(application);
  final Bootstrap<T> bootstrap = new Bootstrap<T>(application);
  bootstrap.addCommand(serverCommand);

  // Write a temporary config file
  File tmpConfigFile = new File(
          System.getProperty("java.io.tmpdir"),
          config.getClass().getCanonicalName() + "_" + System.currentTimeMillis());
  tmpConfigFile.deleteOnExit();
  bootstrap.getObjectMapper().writeValue(tmpConfigFile, config);

  // Parse configuration
  ConfigurationFactory<T> configurationFactory
          = bootstrap.getConfigurationFactoryFactory()
                     .create((Class<T>) config.getClass(),
                             bootstrap.getValidatorFactory().getValidator(),
                             bootstrap.getObjectMapper(),
                             "dw");
  final T builtConfig = configurationFactory.build(
          bootstrap.getConfigurationSourceProvider(), tmpConfigFile.getAbsolutePath());

  // Configure logging
  builtConfig.getLoggingFactory()
             .configure(bootstrap.getMetricRegistry(),
                        bootstrap.getApplication().getName());

  // Environment
  final Environment environment = new Environment(bootstrap.getApplication().getName(),
                                                  bootstrap.getObjectMapper(),
                                                  bootstrap.getValidatorFactory().getValidator(),
                                                  bootstrap.getMetricRegistry(),
                                                  bootstrap.getClassLoader());

  // Initialize environment
  builtConfig.getMetricsFactory().configure(environment.lifecycle(), bootstrap.getMetricRegistry());

  // Server
  final Server server = builtConfig.getServerFactory().build(environment);
  server.addLifeCycleListener(new AbstractLifeCycle.AbstractLifeCycleListener()
  {
    @Override
    public void lifeCycleStopped(LifeCycle event)
    {
      builtConfig.getLoggingFactory().stop();
    }
  });

  return new DropWizardServer(builtConfig, bootstrap, application, environment, server, environment.metrics());
}
项目:Pinot    文件:DropWizardApplicationRunner.java   
/**
 * Creates a Jetty server for an application that can be started / stopped in-process
 *
 * @param config
 *  An application configuration instance (with properties set)
 * @param applicationClass
 *  The {@link io.dropwizard.Application} implementation class
 * @param <T>
 *  The configuration class
 * @return
 *  A Jetty server
 */
@SuppressWarnings("unchecked")
public static <T extends Configuration>
DropWizardServer<T> createServer(T config, Class<? extends Application<T>> applicationClass) throws Exception
{
  // Create application
  final Application<T> application = applicationClass.getConstructor().newInstance();

  // Create bootstrap
  final ServerCommand<T> serverCommand = new ServerCommand<T>(application);
  final Bootstrap<T> bootstrap = new Bootstrap<T>(application);
  bootstrap.addCommand(serverCommand);

  // Write a temporary config file
  File tmpConfigFile = new File(
          System.getProperty("java.io.tmpdir"),
          config.getClass().getCanonicalName() + "_" + System.currentTimeMillis());
  tmpConfigFile.deleteOnExit();
  bootstrap.getObjectMapper().writeValue(tmpConfigFile, config);

  // Parse configuration
  ConfigurationFactory<T> configurationFactory
          = bootstrap.getConfigurationFactoryFactory()
                     .create((Class<T>) config.getClass(),
                             bootstrap.getValidatorFactory().getValidator(),
                             bootstrap.getObjectMapper(),
                             "dw");
  final T builtConfig = configurationFactory.build(
          bootstrap.getConfigurationSourceProvider(), tmpConfigFile.getAbsolutePath());

  // Configure logging
  builtConfig.getLoggingFactory()
             .configure(bootstrap.getMetricRegistry(),
                        bootstrap.getApplication().getName());

  // Environment
  final Environment environment = new Environment(bootstrap.getApplication().getName(),
                                                  bootstrap.getObjectMapper(),
                                                  bootstrap.getValidatorFactory().getValidator(),
                                                  bootstrap.getMetricRegistry(),
                                                  bootstrap.getClassLoader());

  // Initialize environment
  builtConfig.getMetricsFactory().configure(environment.lifecycle(), bootstrap.getMetricRegistry());

  // Server
  final Server server = builtConfig.getServerFactory().build(environment);
  server.addLifeCycleListener(new AbstractLifeCycle.AbstractLifeCycleListener()
  {
    @Override
    public void lifeCycleStopped(LifeCycle event)
    {
      builtConfig.getLoggingFactory().stop();
    }
  });

  return new DropWizardServer(builtConfig, bootstrap, application, environment, server, environment.metrics());
}