View Javadoc
1   /**
2    *    Copyright 2009-2019 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  
18  import java.util.Arrays;
19  import java.util.HashSet;
20  import java.util.Set;
21  import java.util.regex.Pattern;
22  
23  import org.apache.ibatis.mapping.ParameterMode;
24  import org.apache.ibatis.mapping.ResultSetType;
25  import org.apache.ibatis.session.Configuration;
26  import org.apache.ibatis.type.JdbcType;
27  import org.apache.ibatis.type.TypeAliasRegistry;
28  import org.apache.ibatis.type.TypeHandler;
29  import org.apache.ibatis.type.TypeHandlerRegistry;
30  
31  /**
32   * @author Clinton Begin
33   */
34  public abstract class BaseBuilder {
35    protected final Configuration configuration;
36    protected final TypeAliasRegistry typeAliasRegistry;
37    protected final TypeHandlerRegistry typeHandlerRegistry;
38  
39    public BaseBuilder(Configuration configuration) {
40      this.configuration = configuration;
41      this.typeAliasRegistry = this.configuration.getTypeAliasRegistry();
42      this.typeHandlerRegistry = this.configuration.getTypeHandlerRegistry();
43    }
44  
45    public Configuration getConfiguration() {
46      return configuration;
47    }
48  
49    protected Pattern parseExpression(String regex, String defaultValue) {
50      return Pattern.compile(regex == null ? defaultValue : regex);
51    }
52  
53    protected Boolean booleanValueOf(String value, Boolean defaultValue) {
54      return value == null ? defaultValue : Boolean.valueOf(value);
55    }
56  
57    protected Integer integerValueOf(String value, Integer defaultValue) {
58      return value == null ? defaultValue : Integer.valueOf(value);
59    }
60  
61    protected Set<String> stringSetValueOf(String value, String defaultValue) {
62      value = value == null ? defaultValue : value;
63      return new HashSet<>(Arrays.asList(value.split(",")));
64    }
65  
66    protected JdbcType resolveJdbcType(String alias) {
67      if (alias == null) {
68        return null;
69      }
70      try {
71        return JdbcType.valueOf(alias);
72      } catch (IllegalArgumentException e) {
73        throw new BuilderException("Error resolving JdbcType. Cause: " + e, e);
74      }
75    }
76  
77    protected ResultSetType resolveResultSetType(String alias) {
78      if (alias == null) {
79        return null;
80      }
81      try {
82        return ResultSetType.valueOf(alias);
83      } catch (IllegalArgumentException e) {
84        throw new BuilderException("Error resolving ResultSetType. Cause: " + e, e);
85      }
86    }
87  
88    protected ParameterMode resolveParameterMode(String alias) {
89      if (alias == null) {
90        return null;
91      }
92      try {
93        return ParameterMode.valueOf(alias);
94      } catch (IllegalArgumentException e) {
95        throw new BuilderException("Error resolving ParameterMode. Cause: " + e, e);
96      }
97    }
98  
99    protected Object createInstance(String alias) {
100     Class<?> clazz = resolveClass(alias);
101     if (clazz == null) {
102       return null;
103     }
104     try {
105       return clazz.getDeclaredConstructor().newInstance();
106     } catch (Exception e) {
107       throw new BuilderException("Error creating instance. Cause: " + e, e);
108     }
109   }
110 
111   protected <T> Class<? extends T> resolveClass(String alias) {
112     if (alias == null) {
113       return null;
114     }
115     try {
116       return resolveAlias(alias);
117     } catch (Exception e) {
118       throw new BuilderException("Error resolving class. Cause: " + e, e);
119     }
120   }
121 
122   protected TypeHandler<?> resolveTypeHandler(Class<?> javaType, String typeHandlerAlias) {
123     if (typeHandlerAlias == null) {
124       return null;
125     }
126     Class<?> type = resolveClass(typeHandlerAlias);
127     if (type != null && !TypeHandler.class.isAssignableFrom(type)) {
128       throw new BuilderException("Type " + type.getName() + " is not a valid TypeHandler because it does not implement TypeHandler interface");
129     }
130     @SuppressWarnings("unchecked") // already verified it is a TypeHandler
131     Class<? extends TypeHandler<?>> typeHandlerType = (Class<? extends TypeHandler<?>>) type;
132     return resolveTypeHandler(javaType, typeHandlerType);
133   }
134 
135   protected TypeHandler<?> resolveTypeHandler(Class<?> javaType, Class<? extends TypeHandler<?>> typeHandlerType) {
136     if (typeHandlerType == null) {
137       return null;
138     }
139     // javaType ignored for injected handlers see issue #746 for full detail
140     TypeHandler<?> handler = typeHandlerRegistry.getMappingTypeHandler(typeHandlerType);
141     if (handler == null) {
142       // not in registry, create a new one
143       handler = typeHandlerRegistry.getInstance(javaType, typeHandlerType);
144     }
145     return handler;
146   }
147 
148   protected <T> Class<? extends T> resolveAlias(String alias) {
149     return typeAliasRegistry.resolveAlias(alias);
150   }
151 }