All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.meltmedia.dropwizard.jongo.JongoBundle Maven / Gradle / Ivy

The newest version!
package com.meltmedia.dropwizard.jongo;

import static com.meltmedia.dropwizard.jongo.Functions.consumerRequired;
import static com.meltmedia.dropwizard.jongo.Functions.mapSupplier;

import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;

import org.jongo.Jongo;

import com.meltmedia.dropwizard.mongo.MongoConfiguration;
import com.mongodb.MongoClient;

import io.dropwizard.Configuration;
import io.dropwizard.ConfiguredBundle;
import io.dropwizard.setup.Bootstrap;
import io.dropwizard.setup.Environment;

public class JongoBundle implements ConfiguredBundle {
  public static class Builder {
    AtomicReference configuration = new AtomicReference<>();
    Consumer builderOps = b->{};
    public Builder addJongoBuilderOps( Consumer op ) {
      this.builderOps = builderOps.andThen(op);
      return this;
    }

    public Builder withClient(Supplier client) {
      return addJongoBuilderOps(op->op.withClient(client));
    }

    public Builder withDatabaseName( Supplier databaseName ) {
      return addJongoBuilderOps(op->op.withDatabaseName(databaseName));
    }

    public Builder withDatabaseName( Function mongoConfiguration ) {
      Function mapName = mongoConfiguration
        .andThen(MongoConfiguration::getDatabase);
      return addJongoBuilderOps(op->op
        .withDatabaseName(
          mapSupplier(configuration::get, mapName)));
    }

    public JongoBundle build() {
      return new JongoBundle(builderOps, configuration::set);
    }
  }
  Consumer builderOps = b->{};
  Consumer defineConfiguration = consumerRequired("defineConfiguraiton");
  JongoBuilder builder = new JongoBuilder();
  
  Jongo jongo;

  public JongoBundle( Consumer builderOps, Consumer defineConfiguration ) {
    this.builderOps = builderOps;
    this.defineConfiguration = defineConfiguration;
  }

  @Override
  public void initialize( Bootstrap bootstrap ) {
    builderOps.accept(builder);
  }

  @Override
  public void run( C configuration, Environment environment ) throws Exception {
    defineConfiguration.accept(configuration);
    jongo = builder.build();
  }

  public Jongo getJongo() {
    return this.jongo;
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy