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

org.mybatis.guice.datasource.dbcp.PerUserPoolDataSourceModule Maven / Gradle / Ivy

Go to download

The MyBatis Guice module is easy-to-use Google Guice bridge for MyBatis sql mapping framework.

There is a newer version: 4.0.0
Show newest version
/*
 *    Copyright 2009-2022 the original author or authors.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *       https://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
package org.mybatis.guice.datasource.dbcp;

import com.google.inject.AbstractModule;
import com.google.inject.Scopes;
import com.google.inject.TypeLiteral;

import java.lang.annotation.Annotation;
import java.util.Map;

import javax.inject.Provider;

public final class PerUserPoolDataSourceModule extends AbstractModule {

  private static final TypeLiteral> STRING_BOOLEAN_MAP = new TypeLiteral>() {
  };

  private static final TypeLiteral> STRING_INTEGER_MAP = new TypeLiteral>() {
  };

  private static final TypeLiteral> STRING_LONG_MAP = new TypeLiteral>() {
  };

  private final Class>> perUserDefaultAutoCommitProviderClass;

  private final Class>> perUserDefaultReadOnlyProviderClass;

  private final Class>> perUserDefaultTransactionIsolationProviderClass;

  private final Class>> perUserMaxActiveProviderClass;

  private final Class>> perUserMaxIdleProviderClass;

  private final Class>> perUserMaxWaitMillisProviderClass;

  private PerUserPoolDataSourceModule(
      Class>> perUserDefaultAutoCommitProviderClass,
      Class>> perUserDefaultReadOnlyProviderClass,
      Class>> perUserDefaultTransactionIsolationProviderClass,
      Class>> perUserMaxActiveProviderClass,
      Class>> perUserMaxIdleProviderClass,
      Class>> perUserMaxWaitMillisProviderClass) {
    this.perUserDefaultAutoCommitProviderClass = perUserDefaultAutoCommitProviderClass;
    this.perUserDefaultReadOnlyProviderClass = perUserDefaultReadOnlyProviderClass;
    this.perUserDefaultTransactionIsolationProviderClass = perUserDefaultTransactionIsolationProviderClass;
    this.perUserMaxActiveProviderClass = perUserMaxActiveProviderClass;
    this.perUserMaxIdleProviderClass = perUserMaxIdleProviderClass;
    this.perUserMaxWaitMillisProviderClass = perUserMaxWaitMillisProviderClass;
  }

  @Override
  protected void configure() {
    doBind(perUserDefaultAutoCommitProviderClass, STRING_BOOLEAN_MAP, PerUserDefaultAutoCommit.class);
    doBind(perUserDefaultReadOnlyProviderClass, STRING_BOOLEAN_MAP, PerUserDefaultReadOnly.class);
    doBind(perUserDefaultTransactionIsolationProviderClass, STRING_INTEGER_MAP,
        PerUserDefaultTransactionIsolation.class);
    doBind(perUserMaxActiveProviderClass, STRING_INTEGER_MAP, PerUserMaxTotal.class);
    doBind(perUserMaxIdleProviderClass, STRING_INTEGER_MAP, PerUserMaxIdle.class);
    doBind(perUserMaxWaitMillisProviderClass, STRING_LONG_MAP, PerUserMaxWaitMillis.class);
  }

  private  void doBind(Class> providerClass, TypeLiteral typeLiteral,
      Class annotation) {
    if (providerClass != null) {
      bind(typeLiteral).annotatedWith(annotation).toProvider(providerClass).in(Scopes.SINGLETON);
    }
  }

  public static final class Builder {

    private Class>> perUserDefaultAutoCommitProviderClass;

    private Class>> perUserDefaultReadOnlyProviderClass;

    private Class>> perUserDefaultTransactionIsolationProviderClass;

    private Class>> perUserMaxActiveProviderClass;

    private Class>> perUserMaxIdleProviderClass;

    private Class>> perUserMaxWaitProviderClass;

    /**
     * Sets the per user default auto commit provider class.
     *
     * @param perUserDefaultAutoCommitProviderClass
     *          the perUserDefaultAutoCommitProviderClass to set
     * @return the builder
     */
    public Builder setPerUserDefaultAutoCommitProviderClass(
        Class>> perUserDefaultAutoCommitProviderClass) {
      this.perUserDefaultAutoCommitProviderClass = perUserDefaultAutoCommitProviderClass;
      return this;
    }

    /**
     * Sets the per user default read only provider class.
     *
     * @param perUserDefaultReadOnlyProviderClass
     *          the perUserDefaultReadOnlyProviderClass to set
     * @return the builder
     */
    public Builder setPerUserDefaultReadOnlyProviderClass(
        Class>> perUserDefaultReadOnlyProviderClass) {
      this.perUserDefaultReadOnlyProviderClass = perUserDefaultReadOnlyProviderClass;
      return this;
    }

    /**
     * Sets the per user default transaction isolation provider class.
     *
     * @param perUserDefaultTransactionIsolationProviderClass
     *          the perUserDefaultTransactionIsolationProviderClass to set
     * @return the builder
     */
    public Builder setPerUserDefaultTransactionIsolationProviderClass(
        Class>> perUserDefaultTransactionIsolationProviderClass) {
      this.perUserDefaultTransactionIsolationProviderClass = perUserDefaultTransactionIsolationProviderClass;
      return this;
    }

    /**
     * Sets the per user max active provider class.
     *
     * @param perUserMaxActiveProviderClass
     *          the perUserMaxActiveProviderClass to set
     * @return the builder
     */
    public Builder setPerUserMaxTotalProviderClass(
        Class>> perUserMaxActiveProviderClass) {
      this.perUserMaxActiveProviderClass = perUserMaxActiveProviderClass;
      return this;
    }

    /**
     * Sets the per user max idle provider class.
     *
     * @param perUserMaxIdleProviderClass
     *          the perUserMaxIdleProviderClass to set
     * @return the builder
     */
    public Builder setPerUserMaxIdleProviderClass(
        Class>> perUserMaxIdleProviderClass) {
      this.perUserMaxIdleProviderClass = perUserMaxIdleProviderClass;
      return this;
    }

    /**
     * Sets the per user max wait provider class.
     *
     * @param perUserMaxWaitProviderClass
     *          the perUserMaxWaitProviderClass to set
     * @return the builder
     */
    public Builder setPerUserMaxWaitMillisProviderClass(
        Class>> perUserMaxWaitProviderClass) {
      this.perUserMaxWaitProviderClass = perUserMaxWaitProviderClass;
      return this;
    }

    /**
     * Creates the pool.
     *
     * @return the per user pool data source module
     */
    public PerUserPoolDataSourceModule create() {
      return new PerUserPoolDataSourceModule(perUserDefaultAutoCommitProviderClass, perUserDefaultReadOnlyProviderClass,
          perUserDefaultTransactionIsolationProviderClass, perUserMaxActiveProviderClass, perUserMaxIdleProviderClass,
          perUserMaxWaitProviderClass);
    }

  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy