From 492fb5c7bd486663bf39effe6aa732f49ff25f75 Mon Sep 17 00:00:00 2001 From: septer Date: Sat, 11 Aug 2012 10:57:08 +0800 Subject: [PATCH] finish the JNDI function. --- .../DruidDataSourceStatJNDIStatStrategy.java | 124 +++++++++++------- .../stat/DruidDataSourceStatStrategy.java | 6 +- .../DruidDataSourceStatStrategyContext.java | 6 +- .../druid/stat/DruidStatManagerFacade.java | 6 +- .../druid/support/JSONDruidStatService.java | 39 ++++-- .../alibaba/druid/util/ReflectionUtils.java | 50 ++++++- 6 files changed, 155 insertions(+), 76 deletions(-) diff --git a/src/main/java/com/alibaba/druid/stat/DruidDataSourceStatJNDIStatStrategy.java b/src/main/java/com/alibaba/druid/stat/DruidDataSourceStatJNDIStatStrategy.java index 7a0070c01d..5281bc679f 100644 --- a/src/main/java/com/alibaba/druid/stat/DruidDataSourceStatJNDIStatStrategy.java +++ b/src/main/java/com/alibaba/druid/stat/DruidDataSourceStatJNDIStatStrategy.java @@ -6,7 +6,6 @@ import java.util.HashMap; import java.util.List; import java.util.Map; -import com.alibaba.druid.pool.DruidDataSource; import com.alibaba.druid.support.logging.Log; import com.alibaba.druid.support.logging.LogFactory; import com.alibaba.druid.util.ReflectionUtils; @@ -19,11 +18,6 @@ public class DruidDataSourceStatJNDIStatStrategy implements DruidDataSourceStatS private final static HashMap> classMethodHM = new HashMap>(); - private final static String[] methodNameArr = new String[] { "getSqlStatData", - "getSqlStatDataList", "getActiveConnectionStackTraceByDataSourceId", "returnJSONBasicStat", - "getDataSourceStatList", "getDataSourceStatData", "getPoolingConnectionInfoByDataSourceId", "resetAll", - "getDruidDataSourceById", "getSqlStatById" }; - public DruidDataSourceStatJNDIStatStrategy(){ init(); } @@ -34,48 +28,50 @@ public class DruidDataSourceStatJNDIStatStrategy implements DruidDataSourceStatS String className = "com.alibaba.druid.stat.DruidDataSourceStatDefaultStrategy"; Class clazzFromWebContainer = ReflectionUtils.getClassFromWebContainer(className); Class clazzFromCurClassLoader = ReflectionUtils.getClassFromCurrentClassLoader(className); - if (clazzFromWebContainer != null) strategyList.add(clazzFromWebContainer.newInstance()); - if (clazzFromCurClassLoader != null) strategyList.add(clazzFromCurClassLoader.newInstance()); - - // cache the method - Object obj = null; - for (int i = 0; i < strategyList.size(); i++) { - obj = strategyList.get(i); - HashMap methodHM = null; - String methodName = null; - for (int j = 0; j < methodNameArr.length; j++) { - methodHM = new HashMap(); - methodName = methodNameArr[j]; - methodHM.put(methodName, ReflectionUtils.getObjectMethod(obj, methodName)); + if (clazzFromCurClassLoader != null) { + Object clazzFromCurClassLoaderInstance = clazzFromCurClassLoader.newInstance(); + if (!strategyList.contains(clazzFromCurClassLoaderInstance)) { + strategyList.add(clazzFromCurClassLoaderInstance); + } + } + if (clazzFromWebContainer != null) { + Object clazzFromWebContainerInstance = clazzFromWebContainer.newInstance(); + if (!strategyList.contains(clazzFromWebContainerInstance)) { + if (clazzFromCurClassLoader != null && clazzFromWebContainerInstance != clazzFromCurClassLoader) { + strategyList.add(clazzFromWebContainerInstance); + } } - classMethodHM.put(obj, methodHM); } } catch (Exception e) { LOG.error(e.getMessage(), e); } } - @SuppressWarnings("unchecked") - private Object invoke(String methodName, Integer methodParam) { + private Object invoke(String methodName, Integer id) { Object result = null; try { HashMap hm = null; Object invokeRes = null; Object obj = null; + Method method = null; for (int i = 0; i < strategyList.size(); i++) { obj = strategyList.get(i); hm = classMethodHM.get(obj); - if (hm.containsKey(methodName)) { - if (methodParam == null) { - invokeRes = hm.get(methodName).invoke(obj); + if (hm != null) { + if (hm.containsKey(methodName)) { + method = hm.get(methodName); + if (id == null) { + invokeRes = ReflectionUtils.callObjectMethod(obj, method); + } else { + invokeRes = ReflectionUtils.callObjectMethod(obj, method, id); + } + result = invokeMapResHandler(result, invokeRes); } else { - invokeRes = hm.get(methodName).invoke(obj, methodParam); - } - if (invokeRes instanceof Map) { - result = invokeMapResHandler(null, (Map) invokeRes); - } else if (invokeRes instanceof List) { - result = invokeListResHandler(null, (List>) invokeRes); + result = cacheMethodAndRtnInvodeRes(hm, obj, methodName, id); } + } else { + hm = new HashMap(); + result = cacheMethodAndRtnInvodeRes(hm, obj, methodName, id); } } } catch (Exception e) { @@ -84,24 +80,50 @@ public class DruidDataSourceStatJNDIStatStrategy implements DruidDataSourceStatS return result; } - private Map invokeMapResHandler(Map finalRs, Map invokeRes) { - Map res = null; - if (finalRs == null) { - res = finalRs; + private Object cacheMethodAndRtnInvodeRes(HashMap methodHM, Object obj, String methodName, + Integer id) throws Exception { + Method method = null; + Object invokeRes = null; + if (id == null) { + method = ReflectionUtils.getObjectMethod(obj, methodName); + invokeRes = ReflectionUtils.callObjectMethod(obj, method); } else { - finalRs.putAll(invokeRes); - res = finalRs; + method = ReflectionUtils.getObjectMethod(obj, methodName, id); + invokeRes = ReflectionUtils.callObjectMethod(obj, method, id); } - return res; + methodHM.put(methodName, method); + classMethodHM.put(obj, methodHM); + return invokeRes; } - private List invokeListResHandler(List> finalRs, List> invokeRes) { - List> res = null; + @SuppressWarnings("unchecked") + private Object invokeMapResHandler(Object finalRs, Object invokeRes) { + Object res = null; if (finalRs == null) { - res = finalRs; + if (invokeRes instanceof Map) { + res = (Map) invokeRes; + } else if (invokeRes instanceof List) { + res = (List>) invokeRes; + } else { + res = invokeRes; + } } else { - finalRs.addAll(invokeRes); - res = finalRs; + if (invokeRes instanceof Map) { + Map finalRs_ = (Map) finalRs; + Map invokeRes_ = (Map) invokeRes; + if (invokeRes != null) finalRs_.putAll(invokeRes_); + res = finalRs_; + } else if (invokeRes instanceof List) { + List> finalRs_ = (List>) finalRs; + List> invokeRes_ = (List>) invokeRes; + if (invokeRes != null) finalRs_.addAll(invokeRes_); + res = finalRs_; + } else { + final List list = new ArrayList(); + list.add(finalRs); + list.add(invokeRes); + res = list;// merge the others class type,but in this + } } return res; } @@ -141,16 +163,16 @@ public class DruidDataSourceStatJNDIStatStrategy implements DruidDataSourceStatS return (List>) invoke("getPoolingConnectionInfoByDataSourceId", id); } + public Object getDruidDataSourceById(Integer id) { + return invoke("getDruidDataSourceById", id); + } + + public Object getSqlStatById(Integer id) { + return invoke("getSqlStatById", id); + } + public void resetAll() { invoke("resetAll", null); } - public DruidDataSource getDruidDataSourceById(Integer id) { - return (DruidDataSource) invoke("getDruidDataSourceById", id); - } - - public JdbcSqlStat getSqlStatById(Integer id) { - return (JdbcSqlStat) invoke("getSqlStatById", id); - } - } diff --git a/src/main/java/com/alibaba/druid/stat/DruidDataSourceStatStrategy.java b/src/main/java/com/alibaba/druid/stat/DruidDataSourceStatStrategy.java index 6a4681a1e7..74a8eabd75 100644 --- a/src/main/java/com/alibaba/druid/stat/DruidDataSourceStatStrategy.java +++ b/src/main/java/com/alibaba/druid/stat/DruidDataSourceStatStrategy.java @@ -3,8 +3,6 @@ package com.alibaba.druid.stat; import java.util.List; import java.util.Map; -import com.alibaba.druid.pool.DruidDataSource; - public interface DruidDataSourceStatStrategy { public Map getSqlStatData(Integer id); @@ -23,7 +21,7 @@ public interface DruidDataSourceStatStrategy { public void resetAll(); - public DruidDataSource getDruidDataSourceById(Integer id); + public Object getDruidDataSourceById(Integer id); - public JdbcSqlStat getSqlStatById(Integer id); + public Object getSqlStatById(Integer id); } diff --git a/src/main/java/com/alibaba/druid/stat/DruidDataSourceStatStrategyContext.java b/src/main/java/com/alibaba/druid/stat/DruidDataSourceStatStrategyContext.java index 3ffa4ea18e..9df2bc2279 100644 --- a/src/main/java/com/alibaba/druid/stat/DruidDataSourceStatStrategyContext.java +++ b/src/main/java/com/alibaba/druid/stat/DruidDataSourceStatStrategyContext.java @@ -3,8 +3,6 @@ package com.alibaba.druid.stat; import java.util.List; import java.util.Map; -import com.alibaba.druid.pool.DruidDataSource; - public class DruidDataSourceStatStrategyContext { private DruidDataSourceStatStrategy druidDataSourceStatStrategy; @@ -45,11 +43,11 @@ public class DruidDataSourceStatStrategyContext { druidDataSourceStatStrategy.resetAll(); } - public DruidDataSource getDruidDataSourceById(Integer id) { + public Object getDruidDataSourceById(Integer id) { return druidDataSourceStatStrategy.getDruidDataSourceById(id); } - public JdbcSqlStat getSqlStatById(Integer id) { + public Object getSqlStatById(Integer id) { return druidDataSourceStatStrategy.getSqlStatById(id); } diff --git a/src/main/java/com/alibaba/druid/stat/DruidStatManagerFacade.java b/src/main/java/com/alibaba/druid/stat/DruidStatManagerFacade.java index fef8b5f4c1..efa89be2a2 100644 --- a/src/main/java/com/alibaba/druid/stat/DruidStatManagerFacade.java +++ b/src/main/java/com/alibaba/druid/stat/DruidStatManagerFacade.java @@ -3,8 +3,6 @@ package com.alibaba.druid.stat; import java.util.List; import java.util.Map; -import com.alibaba.druid.pool.DruidDataSource; - /** * 监控相关的对外数据暴露 * @@ -67,11 +65,11 @@ public class DruidStatManagerFacade { ctx.resetAll(); } - public DruidDataSource getDruidDataSourceById(Integer id) { + public Object getDruidDataSourceById(Integer id) { return ctx.getDruidDataSourceById(id); } - public JdbcSqlStat getSqlStatById(Integer id) { + public Object getSqlStatById(Integer id) { return ctx.getSqlStatById(id); } } diff --git a/src/main/java/com/alibaba/druid/support/JSONDruidStatService.java b/src/main/java/com/alibaba/druid/support/JSONDruidStatService.java index d5797d596e..da73035d47 100644 --- a/src/main/java/com/alibaba/druid/support/JSONDruidStatService.java +++ b/src/main/java/com/alibaba/druid/support/JSONDruidStatService.java @@ -27,8 +27,9 @@ public class JSONDruidStatService { private final static int DEFAULT_PAGE = 1; private final static int DEFAULT_PER_PAGE_COUNT = Integer.MAX_VALUE; private static final String DEFAULT_ORDER_TYPE = "asc"; + private static final String DEFAULT_ORDERBY = "SQL"; - private JSONDruidStatService() { + private JSONDruidStatService(){ } public static JSONDruidStatService getInstance() { @@ -85,19 +86,35 @@ public class JSONDruidStatService { } private List> getSqlStatDataList(Map parameters) { - String orderBy = parameters.get("orderBy"); - String orderType = parameters.get("orderType"); - Integer page = Integer.parseInt(parameters.get("page")); - Integer perPageCount = Integer.parseInt(parameters.get("perPageCount")); + List> array = druidStatManager.getSqlStatDataList(); + + // when open the stat page before executing some sql + if (array == null || array.isEmpty()) return null; + + // when parameters is null + String orderBy, orderType = null; + Integer page, perPageCount = null; + if (parameters == null) { + orderBy = DEFAULT_ORDER_TYPE; + orderType = DEFAULT_ORDER_TYPE; + page = DEFAULT_PAGE; + perPageCount = DEFAULT_PER_PAGE_COUNT; + } else { + orderBy = parameters.get("orderBy"); + orderType = parameters.get("orderType"); + page = Integer.parseInt(parameters.get("page")); + perPageCount = Integer.parseInt(parameters.get("perPageCount")); + } + + // others,such as order + orderBy = orderBy == null ? DEFAULT_ORDERBY : orderBy; + orderType = orderType == null ? DEFAULT_ORDER_TYPE : orderType; + page = page == null ? DEFAULT_PAGE : page; + perPageCount = perPageCount == null ? DEFAULT_PER_PAGE_COUNT : perPageCount; if (!"desc".equals(orderType)) orderType = DEFAULT_ORDER_TYPE; - if (page == null) page = DEFAULT_PAGE; - if (perPageCount == null) perPageCount = DEFAULT_PER_PAGE_COUNT; - - List> array = druidStatManager.getSqlStatDataList(); - - // orderby + // orderby the statData array if (orderBy != null && orderBy.trim().length() != 0) { Collections.sort(array, new MapComparator(orderBy, DEFAULT_ORDER_TYPE.equals(orderType))); } diff --git a/src/main/java/com/alibaba/druid/util/ReflectionUtils.java b/src/main/java/com/alibaba/druid/util/ReflectionUtils.java index 5ead770179..0d03b3fab8 100644 --- a/src/main/java/com/alibaba/druid/util/ReflectionUtils.java +++ b/src/main/java/com/alibaba/druid/util/ReflectionUtils.java @@ -72,15 +72,43 @@ public class ReflectionUtils { } } - public static Method getObjectMethod(Object obj, String methodName, Integer id) { + public static Method getObjectMethod(Object obj, String methodName, Class... parameterTypes) { try { - return obj.getClass().getMethod(methodName, Integer.class); + return obj.getClass().getMethod(methodName, parameterTypes); } catch (Exception e) { LOG.warn("getObjectMethod fail:class=" + obj.getClass().getName() + " method=" + methodName, e); return null; } } + public static Object callObjectMethod(Object obj, String methodName, Class[] parameterTypes, Object[] parameters) { + try { + Method m = getObjectMethod(obj, methodName, parameterTypes); + return m.invoke(obj, parameters); + } catch (Exception e) { + LOG.warn("callObjectMethod fail:class=" + obj.getClass().getName() + " method=" + methodName, e); + return null; + } + } + + public static Object callObjectMethod(Object obj, Method m, Object[] parameters) { + try { + return m.invoke(obj, parameters); + } catch (Exception e) { + LOG.warn("callObjectMethod fail:class=" + obj.getClass().getName() + " method=" + m.getName(), e); + return null; + } + } + + public static Object callObjectMethod(Object obj, Method m) { + try { + return m.invoke(obj); + } catch (Exception e) { + LOG.warn("callObjectMethod fail:class=" + obj.getClass().getName() + " method=" + m.getName(), e); + return null; + } + } + public static Object callObjectMethod(Object obj, String methodName, Integer id) { try { Method m = obj.getClass().getMethod(methodName, Integer.class); @@ -91,6 +119,24 @@ public class ReflectionUtils { } } + public static Method getObjectMethod(Object obj, String methodName, Integer id) { + try { + return obj.getClass().getMethod(methodName, Integer.class); + } catch (Exception e) { + LOG.warn("getObjectMethod fail:class=" + obj.getClass().getName() + " method=" + methodName, e); + return null; + } + } + + public static Object callObjectMethod(Object obj, Method m, Integer id) { + try { + return m.invoke(obj, id); + } catch (Exception e) { + LOG.warn("callObjectMethod fail:class=" + obj.getClass().getName() + " method=" + m.getName(), e); + return null; + } + } + public void getTest() { System.out.println("dd"); // return "test";