Git Product home page Git Product logo

dw-tenant-resolver's Introduction

dw-tenant-resolver

Dropwizard bundle which helps to achieve multi-tenancy in dropwizard application

Configuration:

Add the database configuration for each tenant, database configuration accepts all the properties which dropwizard DatabaseConfiguration object accepts - .

Shown in below example.

multiTenantDataSourceConfiguration:
  tenantHeaderName: X_TENANT_ID
  enforceTenantHeaderInAllRequests: true
  databaseConfigurations:
    tenant_1:
      driverClass: com.mysql.jdbc.Driver
      user: root
      password:
      url: jdbc:mysql://db_server_1:port/database_tenant_1
    tenant_2:
      driverClass: com.mysql.jdbc.Driver
      user: root
      password:
      url: jdbc:mysql://db_server_2:port/databse_tenant_2

Above configuration accepts, tenantHeaderName - which is been used the resolve the tenant for each request and initializes the necessary things to connect to defined datasource.

and enforceTenantHeaderInAllRequests defines - whether headerName is optional or mandatory in all requests. If value is true and tenantHeaderName is missing, then request will be rejected, and 400 response will be sent with the message Invalid Tenant Nil

Adding the tenant-resolver bundle to the Application:

During the initialization of dropwizard application, add tenant-resolver bundle, as mentioned below -

@Override
public void initialize(Bootstrap<DropwizardServiceConfiguration> bootstrap) {
    
    bootstrap.addBundle(new MultiTenantResolverBundle<DropwizardServiceConfiguration>() {
        @Override
        protected MultiTenantDataSourceConfiguration getMultiTenantDataSourceConfiguration(
                                                        DropwizardServiceConfiguration configuration) {
            return configuration.getMultiTenantDataSourceConfiguration();
        }

        @Override
        protected List<String> getPackagesToScan() {
            List<String> packagesToScan  = new LinkedList<String>();
            packagesToScan.add("com.sample.service.model");
            packagesToScan.add("com.sample.service.extensions");
            return packagesToScan;
        }
    });

and define the Configuration in Service Configuration like -

@Valid
private MultiTenantDataSourceConfiguration multiTenantDataSourceConfiguration = 
                                             new MultiTenantDataSourceConfiguration();

and define the provider for the same like -

@Provides
@Singleton
MultiTenantDataSourceConfiguration providesMultiTenantDsConfigurations(
                           Provider<DropwizardServiceConfiguration> provider) {
    return provider.get().getMultiTenantDataSourceConfiguration();
}

How to use

If you want to access the entityManager, where this snippet is getting ececuted in the scope of request and request has tenantHeader present -

TenantResolver.getEntityManager()

and if you explicitly want to use the specific tenant's entity manager -

TenantResolver.getEntityManager(tenantName)

dw-tenant-resolver's People

Contributors

anandhi avatar anupama-agarwal avatar kanhaiya-agarwal avatar vaibhavguptabits avatar virajbajoria avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.