CatMybatisInterceptor.java 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import com.dianping.cat.Cat;
  2. import com.dianping.cat.CatConstants;
  3. import org.apache.ibatis.executor.Executor;
  4. import org.apache.ibatis.mapping.BoundSql;
  5. import org.apache.ibatis.mapping.MappedStatement;
  6. import org.apache.ibatis.mapping.ParameterMapping;
  7. import org.apache.ibatis.plugin.Interceptor;
  8. import org.apache.ibatis.plugin.Intercepts;
  9. import org.apache.ibatis.plugin.Invocation;
  10. import org.apache.ibatis.plugin.Plugin;
  11. import org.apache.ibatis.plugin.Signature;
  12. import org.apache.ibatis.reflection.MetaObject;
  13. import org.apache.ibatis.session.Configuration;
  14. import org.apache.ibatis.session.ResultHandler;
  15. import org.apache.ibatis.session.RowBounds;
  16. import org.apache.ibatis.type.TypeHandlerRegistry;
  17. import java.text.DateFormat;
  18. import java.util.Date;
  19. import java.util.List;
  20. import java.util.Locale;
  21. import java.util.Properties;
  22. @Intercepts({
  23. @Signature(type = Executor.class, method = "update", args = { MappedStatement.class, Object.class }),
  24. @Signature(type = Executor.class, method = "query", args = { MappedStatement.class, Object.class,
  25. RowBounds.class, ResultHandler.class }) })
  26. public class CatMybatisInterceptor implements Interceptor {
  27. private Properties properties;
  28. public Object intercept(Invocation invocation) throws Throwable {
  29. //begin cat transaction
  30. String datasourceUrl = properties.getProperty("datasourceUrl");
  31. MappedStatement mappedStatement = (MappedStatement) invocation.getArgs()[0];
  32. String sqlId = mappedStatement.getId();
  33. com.dianping.cat.message.Transaction t = Cat.newTransaction(CatConstants.TYPE_SQL, sqlId);
  34. Cat.logEvent("SQL.Database", datasourceUrl);
  35. Cat.logEvent("SQL.Method", invocation.getMethod().getName());
  36. try {
  37. Object returnValue = invocation.proceed();
  38. t.setStatus(com.dianping.cat.message.Transaction.SUCCESS);
  39. return returnValue;
  40. } catch (Throwable e) {
  41. Cat.logError(e);
  42. t.setStatus(e);
  43. throw e;
  44. }finally{
  45. t.complete();
  46. }
  47. }
  48. private static String getParameterValue(Object obj) {
  49. String value = null;
  50. if (obj instanceof String) {
  51. value = "'" + obj.toString() + "'";
  52. } else if (obj instanceof Date) {
  53. DateFormat formatter = DateFormat.getDateTimeInstance(DateFormat.DEFAULT, DateFormat.DEFAULT, Locale.CHINA);
  54. value = "'" + formatter.format(new Date()) + "'";
  55. } else {
  56. if (obj != null) {
  57. value = obj.toString();
  58. } else {
  59. value = "";
  60. }
  61. }
  62. return value;
  63. }
  64. public static String showSql(Configuration configuration, BoundSql boundSql) {
  65. Object parameterObject = boundSql.getParameterObject();
  66. List<ParameterMapping> parameterMappings = boundSql.getParameterMappings();
  67. String sql = boundSql.getSql().replaceAll("[\\s]+", " ");
  68. if (parameterMappings.size() > 0 && parameterObject != null) {
  69. TypeHandlerRegistry typeHandlerRegistry = configuration.getTypeHandlerRegistry();
  70. if (typeHandlerRegistry.hasTypeHandler(parameterObject.getClass())) {
  71. sql = sql.replaceFirst("\\?", getParameterValue(parameterObject));
  72. } else {
  73. MetaObject metaObject = configuration.newMetaObject(parameterObject);
  74. for (ParameterMapping parameterMapping : parameterMappings) {
  75. String propertyName = parameterMapping.getProperty();
  76. if (metaObject.hasGetter(propertyName)) {
  77. Object obj = metaObject.getValue(propertyName);
  78. sql = sql.replaceFirst("\\?", getParameterValue(obj));
  79. } else if (boundSql.hasAdditionalParameter(propertyName)) {
  80. Object obj = boundSql.getAdditionalParameter(propertyName);
  81. sql = sql.replaceFirst("\\?", getParameterValue(obj));
  82. }
  83. }
  84. }
  85. }
  86. return sql;
  87. }
  88. public Object plugin(Object target) {
  89. return Plugin.wrap(target, this);
  90. }
  91. public void setProperties(Properties properties0) {
  92. this.properties = properties0;
  93. }
  94. }