예제 #1
0
  /**
   * {@inheritDoc}
   *
   * @throws IOException when instance in configuration can not be opened and closed.
   */
  @Override
  public final void run(final KijiRESTConfiguration configuration, final Environment environment)
      throws IOException {
    final KijiURI clusterURI = KijiURI.newBuilder(configuration.getClusterURI()).build();

    // Load specified instances and health checks for each.
    final Set<KijiURI> instances =
        InstanceUtil.getInstances(
            clusterURI,
            new InstancesMapTo<KijiURI>() {
              public KijiURI apply(KijiURI instanceURI) throws IOException {
                InstanceUtil.openAndCloseInstance(instanceURI);
                LOG.info("Loading instance {} upon startup.", instanceURI.toOrderedString());
                environment.addHealthCheck(new InstanceHealthCheck(instanceURI));
                return instanceURI;
              }
            });

    final ManagedKijiClient managedKijiClient = new ManagedKijiClient(instances);
    environment.manage(managedKijiClient);

    // Remove all built-in Dropwizard ExceptionHandler.
    // Always depend on custom ones.
    // Inspired by Jeremy Whitlock's suggestion on thoughtspark.org.
    Set<Object> jerseyResources = environment.getJerseyResourceConfig().getSingletons();
    Iterator<Object> jerseyResourcesIterator = jerseyResources.iterator();
    while (jerseyResourcesIterator.hasNext()) {
      Object jerseyResource = jerseyResourcesIterator.next();
      if (jerseyResource instanceof ExceptionMapper
          && jerseyResource.getClass().getName().startsWith("com.yammer.dropwizard.jersey")) {
        jerseyResourcesIterator.remove();
      }
    }

    // Update instances periodically.
    final RefreshInstances instanceRefresher = new RefreshInstances(clusterURI, managedKijiClient);
    ScheduledExecutorService scheduler =
        environment.managedScheduledExecutorService("instance_refresh_scheduler", 1);
    scheduler.scheduleAtFixedRate(
        instanceRefresher,
        INSTANCE_REFRESH_PERIOD_MINUTES, // Start a period from now.
        INSTANCE_REFRESH_PERIOD_MINUTES,
        MINUTES);

    // Load admin task to manually update instances.
    environment.addTask(new RefreshInstancesTask(instanceRefresher));

    // Load resources.
    for (KijiRestPlugin plugin : Lookups.get(KijiRestPlugin.class)) {
      LOG.info("Loading plugin {}", plugin.getClass());
      plugin.install(managedKijiClient, configuration, environment);
    }

    // Allow global CORS filter. CORS off by default.
    if (configuration.getCORS()) {
      environment.addFilter(
          CrossOriginFilter.class, configuration.getHttpConfiguration().getRootPath());
      LOG.info("Global cross-origin resource sharing is allowed.");
    }
  }
예제 #2
0
  /** {@inheritDoc} */
  @Override
  public final void initialize(final Bootstrap<KijiRESTConfiguration> bootstrap) {
    bootstrap.setName("kiji-rest");

    // Initialize plugins.
    for (KijiRestPlugin plugin : Lookups.get(KijiRestPlugin.class)) {
      LOG.info("Initializing plugin {}", plugin.getClass());
      plugin.initialize(bootstrap);
    }
  }