forked from alibaba/druid
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
wenshao
committed
Dec 5, 2012
1 parent
c07c55f
commit dc417b5
Showing
11 changed files
with
812 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,4 +10,5 @@ | |
out/ | ||
*.ipr | ||
*.iws | ||
*.iml | ||
*.iml | ||
/bin |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
129 changes: 129 additions & 0 deletions
129
src/main/java/com/alibaba/druid/support/ibatis/IbatisUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
package com.alibaba.druid.support.ibatis; | ||
|
||
import java.lang.reflect.Field; | ||
import java.lang.reflect.Method; | ||
|
||
import com.alibaba.druid.support.logging.Log; | ||
import com.alibaba.druid.support.logging.LogFactory; | ||
import com.ibatis.sqlmap.client.SqlMapExecutor; | ||
import com.ibatis.sqlmap.engine.impl.SqlMapClientImpl; | ||
import com.ibatis.sqlmap.engine.impl.SqlMapSessionImpl; | ||
import com.ibatis.sqlmap.engine.scope.SessionScope; | ||
|
||
public class IbatisUtils { | ||
private static Log LOG = LogFactory.getLog(IbatisUtils.class); | ||
|
||
private static boolean VERSION_2_3_4 = false; | ||
|
||
private static Method methodGetId = null; | ||
private static Method methodGetResource = null; | ||
|
||
static { | ||
try { | ||
Class<?> clazz = Thread.currentThread().getContextClassLoader().loadClass("com.ibatis.sqlmap.engine.mapping.result.AutoResultMap"); | ||
Method[] methods = clazz.getMethods(); | ||
for (Method method : methods) { | ||
if (method.equals("setResultObjectValues")) { // ibatis 2.3.4 add method 'setResultObjectValues' | ||
VERSION_2_3_4 = true; | ||
break; | ||
} | ||
} | ||
} catch (Throwable e) { | ||
// skip | ||
} | ||
} | ||
|
||
public static boolean isVersion2_3_4() { | ||
return VERSION_2_3_4; | ||
} | ||
|
||
public static SqlMapExecutor setClientImpl(SqlMapExecutor session, SqlMapClientImplWrapper clientImplWrapper) { | ||
if (session == null || clientImplWrapper == null) { | ||
return session; | ||
} | ||
|
||
if (session.getClass() == SqlMapSessionImpl.class) { | ||
SqlMapSessionImpl sessionImpl = (SqlMapSessionImpl) session; | ||
set(sessionImpl, clientImplWrapper); | ||
} | ||
|
||
return session; | ||
} | ||
|
||
/** | ||
* 通过反射的方式得到id,能够兼容2.3.0和2.3.4 | ||
* | ||
* @return | ||
*/ | ||
protected static String createId(Object statement) { | ||
try { | ||
if (methodGetId == null) { | ||
Class<?> clazz = statement.getClass(); | ||
methodGetId = clazz.getMethod("getId"); | ||
} | ||
|
||
Object returnValue = methodGetId.invoke(statement); | ||
|
||
if (returnValue == null) { | ||
return null; | ||
} | ||
|
||
return returnValue.toString(); | ||
} catch (Exception ex) { | ||
LOG.error("createIdError", ex); | ||
return null; | ||
} | ||
} | ||
|
||
/** | ||
* 通过反射的方式得到resource,能够兼容2.3.0和2.3.4 | ||
* | ||
* @return | ||
*/ | ||
protected static String createResource(Object statement) { | ||
try { | ||
if (methodGetResource == null) { | ||
methodGetResource = statement.getClass().getMethod("getResource"); | ||
} | ||
|
||
return (String) methodGetResource.invoke(statement); | ||
} catch (Exception ex) { | ||
return null; | ||
} | ||
} | ||
|
||
private static Field sessionField; | ||
|
||
public static void set(SqlMapSessionImpl session, SqlMapClientImpl client) { | ||
if (sessionField == null) { | ||
for (Field field : SqlMapSessionImpl.class.getDeclaredFields()) { | ||
if (field.getName().equals("session") || field.getName().equals("sessionScope")) { | ||
sessionField = field; | ||
sessionField.setAccessible(true); | ||
break; | ||
} | ||
} | ||
} | ||
|
||
if (sessionField != null) { | ||
SessionScope sessionScope; | ||
try { | ||
sessionScope = (SessionScope) sessionField.get(session); | ||
|
||
if (sessionScope != null) { | ||
if (sessionScope.getSqlMapClient() != null && sessionScope.getSqlMapClient().getClass() == SqlMapClientImpl.class) { | ||
sessionScope.setSqlMapClient(client); | ||
} | ||
if (sessionScope.getSqlMapExecutor() != null && sessionScope.getSqlMapExecutor().getClass() == SqlMapClientImpl.class) { | ||
sessionScope.setSqlMapExecutor(client); | ||
} | ||
if (sessionScope.getSqlMapTxMgr() != null && sessionScope.getSqlMapTxMgr().getClass() == SqlMapClientImpl.class) { | ||
sessionScope.setSqlMapTxMgr(client); | ||
} | ||
} | ||
} catch (Exception e) { | ||
LOG.error(e.getMessage(), e); | ||
} | ||
} | ||
} | ||
} |
128 changes: 128 additions & 0 deletions
128
src/main/java/com/alibaba/druid/support/ibatis/SpringIbatisBeanTypeAutoProxyCreator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
package com.alibaba.druid.support.ibatis; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
|
||
import org.apache.commons.logging.Log; | ||
import org.apache.commons.logging.LogFactory; | ||
import org.springframework.aop.TargetSource; | ||
import org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator; | ||
import org.springframework.beans.BeansException; | ||
import org.springframework.beans.factory.BeanFactory; | ||
import org.springframework.beans.factory.FactoryBean; | ||
import org.springframework.beans.factory.InitializingBean; | ||
import org.springframework.context.ApplicationContext; | ||
import org.springframework.context.ApplicationContextAware; | ||
import org.springframework.util.Assert; | ||
import org.springframework.util.PatternMatchUtils; | ||
|
||
import com.ibatis.sqlmap.client.SqlMapClient; | ||
import com.ibatis.sqlmap.engine.impl.ExtendedSqlMapClient; | ||
|
||
/** | ||
* 类BeanTypeAutoProxyCreator.java的实现描述:使用配置类型代替Springframework中配置名称的实现 | ||
* | ||
* @author hualiang.lihl 2011-12-31 上午10:48:20 | ||
*/ | ||
@SuppressWarnings("deprecation") | ||
public class SpringIbatisBeanTypeAutoProxyCreator extends AbstractAutoProxyCreator implements InitializingBean, ApplicationContextAware, SpringIbatisBeanTypeAutoProxyCreatorMBean { | ||
|
||
private final static Log LOG = LogFactory.getLog(SpringIbatisBeanTypeAutoProxyCreator.class); | ||
|
||
private static final long serialVersionUID = -9094985530794052264L; | ||
|
||
private Class<?> targetBeanType = SqlMapClient.class; | ||
|
||
private ApplicationContext context; | ||
|
||
private List<String> beanNames = new ArrayList<String>(); | ||
private final List<String> proxyBeanNames = new ArrayList<String>(); | ||
|
||
/** | ||
* @param targetClass the targetClass to set | ||
*/ | ||
public void setTargetBeanType(Class<?> targetClass) { | ||
this.targetBeanType = targetClass; | ||
} | ||
|
||
public void setApplicationContext(ApplicationContext context) throws BeansException { | ||
this.context = context; | ||
} | ||
|
||
/** | ||
* Identify as bean to proxy if the bean name is in the configured list of names. | ||
*/ | ||
@SuppressWarnings("rawtypes") | ||
protected Object[] getAdvicesAndAdvisorsForBean(Class beanClass, String beanName, TargetSource targetSource) { | ||
for (Iterator<String> it = this.beanNames.iterator(); it.hasNext();) { | ||
String mappedName = (String) it.next(); | ||
if (FactoryBean.class.isAssignableFrom(beanClass)) { | ||
if (!mappedName.startsWith(BeanFactory.FACTORY_BEAN_PREFIX)) { | ||
continue; | ||
} | ||
mappedName = mappedName.substring(BeanFactory.FACTORY_BEAN_PREFIX.length()); | ||
} | ||
if (isMatch(beanName, mappedName)) { | ||
return PROXY_WITHOUT_ADDITIONAL_INTERCEPTORS; | ||
} | ||
} | ||
return DO_NOT_PROXY; | ||
} | ||
|
||
@SuppressWarnings({ "rawtypes" }) | ||
protected Object createProxy(Class beanClass, String beanName, Object[] specificInterceptors, | ||
TargetSource targetSource) { | ||
try { | ||
Object target = targetSource.getTarget(); | ||
|
||
if (target instanceof SqlMapClientWrapper) { | ||
proxyBeanNames.add(beanName); | ||
return target; | ||
} | ||
|
||
if (target instanceof SqlMapClient) { | ||
proxyBeanNames.add(beanName); | ||
|
||
return new SqlMapClientWrapper((ExtendedSqlMapClient) target); | ||
} | ||
|
||
return super.createProxy(beanClass, beanName, specificInterceptors, targetSource); | ||
} catch (Throwable ex) { | ||
LOG.error(ex.getMessage(), ex); | ||
return super.createProxy(beanClass, beanName, specificInterceptors, targetSource); | ||
} | ||
} | ||
|
||
/** | ||
* Return if the given bean name matches the mapped name. | ||
* <p> | ||
* The default implementation checks for "xxx*", "*xxx" and "*xxx*" matches, as well as direct equality. Can be | ||
* overridden in subclasses. | ||
* | ||
* @param beanName the bean name to check | ||
* @param mappedName the name in the configured list of names | ||
* @return if the names match | ||
* @see org.springframework.util.PatternMatchUtils#simpleMatch(String, String) | ||
*/ | ||
protected boolean isMatch(String beanName, String mappedName) { | ||
return PatternMatchUtils.simpleMatch(mappedName, beanName); | ||
} | ||
|
||
public void afterPropertiesSet() throws Exception { | ||
Assert.notNull(targetBeanType, "targetType cannot be null"); | ||
String[] beanNames = context.getBeanNamesForType(targetBeanType); | ||
for (String name : beanNames) { | ||
this.beanNames.add(name); | ||
} | ||
} | ||
|
||
public List<String> getBeanNames() { | ||
return beanNames; | ||
} | ||
|
||
public List<String> getProxyBeanNames() { | ||
return proxyBeanNames; | ||
} | ||
|
||
} |
8 changes: 8 additions & 0 deletions
8
...main/java/com/alibaba/druid/support/ibatis/SpringIbatisBeanTypeAutoProxyCreatorMBean.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.alibaba.druid.support.ibatis; | ||
|
||
import java.util.List; | ||
|
||
public interface SpringIbatisBeanTypeAutoProxyCreatorMBean { | ||
|
||
List<String> getProxyBeanNames(); | ||
} |
Oops, something went wrong.