BaseBuilder.java

  1. /*
  2.  *    Copyright 2009-2021 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.  *       http://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.builder;

  17. import java.util.Arrays;
  18. import java.util.HashSet;
  19. import java.util.Set;
  20. import java.util.regex.Pattern;

  21. import org.apache.ibatis.mapping.ParameterMode;
  22. import org.apache.ibatis.mapping.ResultSetType;
  23. import org.apache.ibatis.session.Configuration;
  24. import org.apache.ibatis.type.JdbcType;
  25. import org.apache.ibatis.type.TypeAliasRegistry;
  26. import org.apache.ibatis.type.TypeHandler;
  27. import org.apache.ibatis.type.TypeHandlerRegistry;

  28. /**
  29.  * @author Clinton Begin
  30.  */
  31. public abstract class BaseBuilder {
  32.   protected final Configuration configuration;
  33.   protected final TypeAliasRegistry typeAliasRegistry;
  34.   protected final TypeHandlerRegistry typeHandlerRegistry;

  35.   public BaseBuilder(Configuration configuration) {
  36.     this.configuration = configuration;
  37.     this.typeAliasRegistry = this.configuration.getTypeAliasRegistry();
  38.     this.typeHandlerRegistry = this.configuration.getTypeHandlerRegistry();
  39.   }

  40.   public Configuration getConfiguration() {
  41.     return configuration;
  42.   }

  43.   protected Pattern parseExpression(String regex, String defaultValue) {
  44.     return Pattern.compile(regex == null ? defaultValue : regex);
  45.   }

  46.   protected Boolean booleanValueOf(String value, Boolean defaultValue) {
  47.     return value == null ? defaultValue : Boolean.valueOf(value);
  48.   }

  49.   protected Integer integerValueOf(String value, Integer defaultValue) {
  50.     return value == null ? defaultValue : Integer.valueOf(value);
  51.   }

  52.   protected Set<String> stringSetValueOf(String value, String defaultValue) {
  53.     value = value == null ? defaultValue : value;
  54.     return new HashSet<>(Arrays.asList(value.split(",")));
  55.   }

  56.   protected JdbcType resolveJdbcType(String alias) {
  57.     if (alias == null) {
  58.       return null;
  59.     }
  60.     try {
  61.       return JdbcType.valueOf(alias);
  62.     } catch (IllegalArgumentException e) {
  63.       throw new BuilderException("Error resolving JdbcType. Cause: " + e, e);
  64.     }
  65.   }

  66.   protected ResultSetType resolveResultSetType(String alias) {
  67.     if (alias == null) {
  68.       return null;
  69.     }
  70.     try {
  71.       return ResultSetType.valueOf(alias);
  72.     } catch (IllegalArgumentException e) {
  73.       throw new BuilderException("Error resolving ResultSetType. Cause: " + e, e);
  74.     }
  75.   }

  76.   protected ParameterMode resolveParameterMode(String alias) {
  77.     if (alias == null) {
  78.       return null;
  79.     }
  80.     try {
  81.       return ParameterMode.valueOf(alias);
  82.     } catch (IllegalArgumentException e) {
  83.       throw new BuilderException("Error resolving ParameterMode. Cause: " + e, e);
  84.     }
  85.   }

  86.   protected Object createInstance(String alias) {
  87.     Class<?> clazz = resolveClass(alias);
  88.     if (clazz == null) {
  89.       return null;
  90.     }
  91.     try {
  92.       return clazz.getDeclaredConstructor().newInstance();
  93.     } catch (Exception e) {
  94.       throw new BuilderException("Error creating instance. Cause: " + e, e);
  95.     }
  96.   }

  97.   protected <T> Class<? extends T> resolveClass(String alias) {
  98.     if (alias == null) {
  99.       return null;
  100.     }
  101.     try {
  102.       return resolveAlias(alias);
  103.     } catch (Exception e) {
  104.       throw new BuilderException("Error resolving class. Cause: " + e, e);
  105.     }
  106.   }

  107.   protected TypeHandler<?> resolveTypeHandler(Class<?> javaType, String typeHandlerAlias) {
  108.     if (typeHandlerAlias == null) {
  109.       return null;
  110.     }
  111.     Class<?> type = resolveClass(typeHandlerAlias);
  112.     if (type != null && !TypeHandler.class.isAssignableFrom(type)) {
  113.       throw new BuilderException("Type " + type.getName() + " is not a valid TypeHandler because it does not implement TypeHandler interface");
  114.     }
  115.     @SuppressWarnings("unchecked") // already verified it is a TypeHandler
  116.     Class<? extends TypeHandler<?>> typeHandlerType = (Class<? extends TypeHandler<?>>) type;
  117.     return resolveTypeHandler(javaType, typeHandlerType);
  118.   }

  119.   protected TypeHandler<?> resolveTypeHandler(Class<?> javaType, Class<? extends TypeHandler<?>> typeHandlerType) {
  120.     if (typeHandlerType == null) {
  121.       return null;
  122.     }
  123.     // javaType ignored for injected handlers see issue #746 for full detail
  124.     TypeHandler<?> handler = typeHandlerRegistry.getMappingTypeHandler(typeHandlerType);
  125.     if (handler == null) {
  126.       // not in registry, create a new one
  127.       handler = typeHandlerRegistry.getInstance(javaType, typeHandlerType);
  128.     }
  129.     return handler;
  130.   }

  131.   protected <T> Class<? extends T> resolveAlias(String alias) {
  132.     return typeAliasRegistry.resolveAlias(alias);
  133.   }
  134. }