Environment.java

  1. /*
  2.  *    Copyright 2009-2022 the original author or authors.
  3.  *
  4.  *    Licensed under the Apache License, Version 2.0 (the "License");
  5.  *    you may not use this file except in compliance with the License.
  6.  *    You may obtain a copy of the License at
  7.  *
  8.  *       https://www.apache.org/licenses/LICENSE-2.0
  9.  *
  10.  *    Unless required by applicable law or agreed to in writing, software
  11.  *    distributed under the License is distributed on an "AS IS" BASIS,
  12.  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  *    See the License for the specific language governing permissions and
  14.  *    limitations under the License.
  15.  */
  16. package org.apache.ibatis.mapping;

  17. import javax.sql.DataSource;

  18. import org.apache.ibatis.transaction.TransactionFactory;

  19. /**
  20.  * @author Clinton Begin
  21.  */
  22. public final class Environment {
  23.   private final String id;
  24.   private final TransactionFactory transactionFactory;
  25.   private final DataSource dataSource;

  26.   public Environment(String id, TransactionFactory transactionFactory, DataSource dataSource) {
  27.     if (id == null) {
  28.       throw new IllegalArgumentException("Parameter 'id' must not be null");
  29.     }
  30.     if (transactionFactory == null) {
  31.       throw new IllegalArgumentException("Parameter 'transactionFactory' must not be null");
  32.     }
  33.     this.id = id;
  34.     if (dataSource == null) {
  35.       throw new IllegalArgumentException("Parameter 'dataSource' must not be null");
  36.     }
  37.     this.transactionFactory = transactionFactory;
  38.     this.dataSource = dataSource;
  39.   }

  40.   public static class Builder {
  41.     private final String id;
  42.     private TransactionFactory transactionFactory;
  43.     private DataSource dataSource;

  44.     public Builder(String id) {
  45.       this.id = id;
  46.     }

  47.     public Builder transactionFactory(TransactionFactory transactionFactory) {
  48.       this.transactionFactory = transactionFactory;
  49.       return this;
  50.     }

  51.     public Builder dataSource(DataSource dataSource) {
  52.       this.dataSource = dataSource;
  53.       return this;
  54.     }

  55.     public String id() {
  56.       return this.id;
  57.     }

  58.     public Environment build() {
  59.       return new Environment(this.id, this.transactionFactory, this.dataSource);
  60.     }

  61.   }

  62.   public String getId() {
  63.     return this.id;
  64.   }

  65.   public TransactionFactory getTransactionFactory() {
  66.     return this.transactionFactory;
  67.   }

  68.   public DataSource getDataSource() {
  69.     return this.dataSource;
  70.   }

  71. }