Skip to content

Commit 9229e1a

Browse files
committed
ProxyFactory moved to settings to preserve dtd unchanged
1 parent 0619426 commit 9229e1a

File tree

8 files changed

+43
-98
lines changed

8 files changed

+43
-98
lines changed

src/main/java/org/apache/ibatis/builder/BaseBuilder.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,6 @@ public Configuration getConfiguration() {
4242
return configuration;
4343
}
4444

45-
protected String stringValueOf(String value, String defaultValue) {
46-
return value == null ? defaultValue : value;
47-
}
48-
4945
protected Boolean booleanValueOf(String value, Boolean defaultValue) {
5046
return value == null ? defaultValue : Boolean.valueOf(value);
5147
}
@@ -86,12 +82,22 @@ protected ParameterMode resolveParameterMode(String alias) {
8682
}
8783
}
8884

85+
protected Object createInstance(String alias) {
86+
Class<?> clazz = resolveClass(alias);
87+
if (clazz == null) return null;
88+
try {
89+
return resolveClass(alias).newInstance();
90+
} catch (Exception e) {
91+
throw new BuilderException("Error creating instance. Cause: " + e, e);
92+
}
93+
}
94+
8995
protected Class<?> resolveClass(String alias) {
9096
if (alias == null) return null;
9197
try {
9298
return resolveAlias(alias);
9399
} catch (Exception e) {
94-
throw new BuilderException("Error resolving class . Cause: " + e, e);
100+
throw new BuilderException("Error resolving class. Cause: " + e, e);
95101
}
96102
}
97103

src/main/java/org/apache/ibatis/builder/xml/XMLConfigBuilder.java

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,6 @@ private void parseConfiguration(XNode root) {
9797
pluginElement(root.evalNode("plugins"));
9898
objectFactoryElement(root.evalNode("objectFactory"));
9999
objectWrapperFactoryElement(root.evalNode("objectWrapperFactory"));
100-
proxyFactoryElement(root.evalNode("proxyFactory")); // read it before reading settings
101100
settingsElement(root.evalNode("settings"));
102101
environmentsElement(root.evalNode("environments")); // read it after objectFactory and objectWrapperFactory issue #631
103102
databaseIdProviderElement(root.evalNode("databaseIdProvider"));
@@ -162,16 +161,6 @@ private void objectWrapperFactoryElement(XNode context) throws Exception {
162161
}
163162
}
164163

165-
private void proxyFactoryElement(XNode context) throws Exception {
166-
if (context != null) {
167-
String type = context.getStringAttribute("type");
168-
Properties properties = context.getChildrenAsProperties();
169-
ProxyFactory factory = (ProxyFactory) resolveClass(type).newInstance();
170-
factory.setProperties(properties);
171-
configuration.setProxyFactory(factory);
172-
}
173-
}
174-
175164
private void propertiesElement(XNode context) throws Exception {
176165
if (context != null) {
177166
Properties defaults = context.getChildrenAsProperties();
@@ -204,24 +193,25 @@ private void settingsElement(XNode context) throws Exception {
204193
throw new BuilderException("The setting " + key + " is not known. Make sure you spelled it correctly (case sensitive).");
205194
}
206195
}
207-
configuration.setAutoMappingBehavior(AutoMappingBehavior.valueOf(stringValueOf(props.getProperty("autoMappingBehavior"), "PARTIAL")));
196+
configuration.setAutoMappingBehavior(AutoMappingBehavior.valueOf(props.getProperty("autoMappingBehavior", "PARTIAL")));
208197
configuration.setCacheEnabled(booleanValueOf(props.getProperty("cacheEnabled"), true));
198+
configuration.setProxyFactory((ProxyFactory) createInstance(props.getProperty("proxyFactory", "CGLIB")));
209199
configuration.setLazyLoadingEnabled(booleanValueOf(props.getProperty("lazyLoadingEnabled"), false));
210200
configuration.setAggressiveLazyLoading(booleanValueOf(props.getProperty("aggressiveLazyLoading"), true));
211201
configuration.setMultipleResultSetsEnabled(booleanValueOf(props.getProperty("multipleResultSetsEnabled"), true));
212202
configuration.setUseColumnLabel(booleanValueOf(props.getProperty("useColumnLabel"), true));
213203
configuration.setUseGeneratedKeys(booleanValueOf(props.getProperty("useGeneratedKeys"), false));
214-
configuration.setDefaultExecutorType(ExecutorType.valueOf(stringValueOf(props.getProperty("defaultExecutorType"), "SIMPLE")));
204+
configuration.setDefaultExecutorType(ExecutorType.valueOf(props.getProperty("defaultExecutorType", "SIMPLE")));
215205
configuration.setDefaultStatementTimeout(integerValueOf(props.getProperty("defaultStatementTimeout"), null));
216206
configuration.setMapUnderscoreToCamelCase(booleanValueOf(props.getProperty("mapUnderscoreToCamelCase"), false));
217207
configuration.setSafeRowBoundsEnabled(booleanValueOf(props.getProperty("safeRowBoundsEnabled"), false));
218-
configuration.setLocalCacheScope(LocalCacheScope.valueOf(stringValueOf(props.getProperty("localCacheScope"), "SESSION")));
219-
configuration.setJdbcTypeForNull(JdbcType.valueOf(stringValueOf(props.getProperty("jdbcTypeForNull"), "OTHER")));
208+
configuration.setLocalCacheScope(LocalCacheScope.valueOf(props.getProperty("localCacheScope", "SESSION")));
209+
configuration.setJdbcTypeForNull(JdbcType.valueOf(props.getProperty("jdbcTypeForNull", "OTHER")));
220210
configuration.setLazyLoadTriggerMethods(stringSetValueOf(props.getProperty("lazyLoadTriggerMethods"), "equals,clone,hashCode,toString"));
221211
configuration.setDefaultScriptingLanguage(resolveClass(props.getProperty("defaultScriptingLanguage")));
212+
configuration.setCallSettersOnNulls(booleanValueOf(props.getProperty("callSettersOnNulls"), false));
222213
configuration.setLogPrefix(props.getProperty("logPrefix"));
223214
configuration.setLogImpl(resolveClass(props.getProperty("logImpl")));
224-
configuration.setCallSettersOnNulls(booleanValueOf(props.getProperty("callSettersOnNulls"), false));
225215
}
226216
}
227217

src/main/java/org/apache/ibatis/builder/xml/mybatis-3-config.dtd

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
1818
-->
1919

20-
<!ELEMENT configuration (properties?, settings?, typeAliases?, typeHandlers?, objectFactory?, objectWrapperFactory?, proxyFactory?, plugins?, environments?, databaseIdProvider?, mappers?)>
20+
<!ELEMENT configuration (properties?, settings?, typeAliases?, typeHandlers?, objectFactory?, objectWrapperFactory?, plugins?, environments?, databaseIdProvider?, mappers?)>
2121

2222
<!ELEMENT databaseIdProvider (property*)>
2323
<!ATTLIST databaseIdProvider
@@ -71,11 +71,6 @@ type CDATA #REQUIRED
7171
type CDATA #REQUIRED
7272
>
7373

74-
<!ELEMENT proxyFactory (property*)>
75-
<!ATTLIST proxyFactory
76-
type CDATA #REQUIRED
77-
>
78-
7974
<!ELEMENT plugins (plugin+)>
8075

8176
<!ELEMENT plugin (property*)>

src/main/java/org/apache/ibatis/mapping/DefaultDatabaseIdProvider.java

Lines changed: 2 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -15,63 +15,6 @@
1515
*/
1616
package org.apache.ibatis.mapping;
1717

18-
import java.sql.Connection;
19-
import java.sql.DatabaseMetaData;
20-
import java.sql.SQLException;
21-
import java.util.Properties;
22-
23-
import javax.sql.DataSource;
24-
25-
/*
26-
* Default DatabaseId provider
27-
*
28-
* It returns database product name as a databaseId
29-
* If the user provides a properties it uses it to translate database product name
30-
* key="Microsoft SQL Server", value="ms" will return "ms"
31-
* It can return null, if no database product name or
32-
* a properties was specified and no translation was found
33-
*
34-
*/
35-
public class DefaultDatabaseIdProvider implements DatabaseIdProvider {
36-
37-
private Properties properties;
38-
39-
public String getDatabaseId(DataSource dataSource) {
40-
try {
41-
return getDatabaseName(dataSource);
42-
} catch (Exception e) {
43-
// intentionally ignored
44-
}
45-
return null;
46-
}
47-
48-
public void setProperties(Properties p) {
49-
this.properties = p;
50-
}
51-
52-
private String getDatabaseName(DataSource dataSource) throws SQLException {
53-
String productName = getDatabaseProductName(dataSource);
54-
if (this.properties != null) {
55-
return this.properties.getProperty(productName);
56-
}
57-
return productName;
58-
}
59-
60-
private String getDatabaseProductName(DataSource dataSource) throws SQLException {
61-
Connection con = null;
62-
try {
63-
con = dataSource.getConnection();
64-
DatabaseMetaData metaData = con.getMetaData();
65-
return metaData.getDatabaseProductName();
66-
} finally {
67-
if (con != null) {
68-
try {
69-
con.close();
70-
} catch (SQLException e) {
71-
// ignored
72-
}
73-
}
74-
}
75-
}
76-
18+
@Deprecated
19+
public class DefaultDatabaseIdProvider extends VendorDatabaseIdProvider {
7720
}

src/main/java/org/apache/ibatis/session/Configuration.java

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import org.apache.ibatis.executor.SimpleExecutor;
4646
import org.apache.ibatis.executor.keygen.KeyGenerator;
4747
import org.apache.ibatis.executor.loader.CglibProxyFactory;
48+
import org.apache.ibatis.executor.loader.JavassistProxyFactory;
4849
import org.apache.ibatis.executor.loader.ProxyFactory;
4950
import org.apache.ibatis.executor.parameter.ParameterHandler;
5051
import org.apache.ibatis.executor.resultset.FastResultSetHandler;
@@ -55,6 +56,12 @@
5556
import org.apache.ibatis.io.ResolverUtil;
5657
import org.apache.ibatis.logging.Log;
5758
import org.apache.ibatis.logging.LogFactory;
59+
import org.apache.ibatis.logging.commons.JakartaCommonsLoggingImpl;
60+
import org.apache.ibatis.logging.jdk14.Jdk14LoggingImpl;
61+
import org.apache.ibatis.logging.log4j.Log4jImpl;
62+
import org.apache.ibatis.logging.nologging.NoLoggingImpl;
63+
import org.apache.ibatis.logging.slf4j.Slf4jImpl;
64+
import org.apache.ibatis.logging.stdout.StdOutImpl;
5865
import org.apache.ibatis.mapping.BoundSql;
5966
import org.apache.ibatis.mapping.Environment;
6067
import org.apache.ibatis.mapping.MappedStatement;
@@ -161,12 +168,15 @@ public Configuration() {
161168
typeAliasRegistry.registerAlias("XML", XMLLanguageDriver.class.getName());
162169
typeAliasRegistry.registerAlias("RAW", RawLanguageDriver.class.getName());
163170

164-
typeAliasRegistry.registerAlias("SLF4J", org.apache.ibatis.logging.slf4j.Slf4jImpl.class.getName());
165-
typeAliasRegistry.registerAlias("COMMONS_LOGGING", org.apache.ibatis.logging.commons.JakartaCommonsLoggingImpl.class.getName());
166-
typeAliasRegistry.registerAlias("LOG4J", org.apache.ibatis.logging.log4j.Log4jImpl.class.getName());
167-
typeAliasRegistry.registerAlias("JDK_LOGGING", org.apache.ibatis.logging.jdk14.Jdk14LoggingImpl.class.getName());
168-
typeAliasRegistry.registerAlias("STDOUT_LOGGING", org.apache.ibatis.logging.stdout.StdOutImpl.class.getName());
169-
typeAliasRegistry.registerAlias("NO_LOGGING", org.apache.ibatis.logging.nologging.NoLoggingImpl.class.getName());
171+
typeAliasRegistry.registerAlias("SLF4J", Slf4jImpl.class.getName());
172+
typeAliasRegistry.registerAlias("COMMONS_LOGGING", JakartaCommonsLoggingImpl.class.getName());
173+
typeAliasRegistry.registerAlias("LOG4J", Log4jImpl.class.getName());
174+
typeAliasRegistry.registerAlias("JDK_LOGGING", Jdk14LoggingImpl.class.getName());
175+
typeAliasRegistry.registerAlias("STDOUT_LOGGING", StdOutImpl.class.getName());
176+
typeAliasRegistry.registerAlias("NO_LOGGING", NoLoggingImpl.class.getName());
177+
178+
typeAliasRegistry.registerAlias("CGLIB", CglibProxyFactory.class.getName());
179+
typeAliasRegistry.registerAlias("JAVASSIST", JavassistProxyFactory.class.getName());
170180

171181
languageRegistry.setDefaultDriverClass(XMLLanguageDriver.class);
172182
languageRegistry.register(RawLanguageDriver.class);
@@ -186,8 +196,10 @@ public Class<? extends Log> getLogImpl() {
186196

187197
@SuppressWarnings("unchecked")
188198
public void setLogImpl(Class<?> logImpl) {
189-
this.logImpl = (Class<? extends Log>) logImpl;
190-
LogFactory.useCustomLogging(this.logImpl);
199+
if (logImpl != null) {
200+
this.logImpl = (Class<? extends Log>) logImpl;
201+
LogFactory.useCustomLogging(this.logImpl);
202+
}
191203
}
192204

193205
public boolean isCallSettersOnNulls() {

src/test/java/org/apache/ibatis/submitted/javassist/mybatis-config.xml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,10 @@
2222

2323
<settings>
2424
<setting name="lazyLoadingEnabled" value="true"/>
25+
<setting name="proxyFactory" value="JAVASSIST"/>
2526
<setting name="aggressiveLazyLoading" value="false" />
2627
</settings>
2728

28-
<proxyFactory type="org.apache.ibatis.executor.loader.JavassistProxyFactory" />
29-
3029
<environments default="development">
3130
<environment id="development">
3231
<transactionManager type="JDBC">

src/test/java/org/apache/ibatis/submitted/lazyload_proxyfactory_comparison/mybatis-config-cglib.xml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,11 @@
2121
<configuration>
2222

2323
<settings>
24+
<setting name="proxyFactory" value="CGLIB"/>
2425
<setting name="lazyLoadingEnabled" value="true"/>
2526
<setting name="aggressiveLazyLoading" value="false" />
2627
</settings>
2728

28-
<proxyFactory type="org.apache.ibatis.executor.loader.CglibProxyFactory" />
29-
3029
<environments default="development">
3130
<environment id="development">
3231
<transactionManager type="JDBC">

src/test/java/org/apache/ibatis/submitted/lazyload_proxyfactory_comparison/mybatis-config-javassist.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
<configuration>
2222

2323
<settings>
24+
<setting name="proxyFactory" value="JAVASSIST"/>
2425
<setting name="lazyLoadingEnabled" value="true"/>
2526
<setting name="aggressiveLazyLoading" value="false" />
2627
</settings>

0 commit comments

Comments
 (0)