finish the JNDI function.

This commit is contained in:
septer
2012-08-11 10:57:08 +08:00
parent bae9ffccf6
commit 492fb5c7bd
6 changed files with 155 additions and 76 deletions
@@ -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<Object, HashMap<String, Method>> classMethodHM = new HashMap<Object, HashMap<String, Method>>();
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<String, Method> methodHM = null;
String methodName = null;
for (int j = 0; j < methodNameArr.length; j++) {
methodHM = new HashMap<String, Method>();
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<String, Method> 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<String, Object>) invokeRes);
} else if (invokeRes instanceof List) {
result = invokeListResHandler(null, (List<Map<String, Object>>) invokeRes);
result = cacheMethodAndRtnInvodeRes(hm, obj, methodName, id);
}
} else {
hm = new HashMap<String, Method>();
result = cacheMethodAndRtnInvodeRes(hm, obj, methodName, id);
}
}
} catch (Exception e) {
@@ -84,24 +80,50 @@ public class DruidDataSourceStatJNDIStatStrategy implements DruidDataSourceStatS
return result;
}
private Map<String, Object> invokeMapResHandler(Map<String, Object> finalRs, Map<String, Object> invokeRes) {
Map<String, Object> res = null;
if (finalRs == null) {
res = finalRs;
private Object cacheMethodAndRtnInvodeRes(HashMap<String, Method> 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<Map<String, Object>> finalRs, List<Map<String, Object>> invokeRes) {
List<Map<String, Object>> 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<String, Object>) invokeRes;
} else if (invokeRes instanceof List) {
res = (List<Map<String, Object>>) invokeRes;
} else {
res = invokeRes;
}
} else {
finalRs.addAll(invokeRes);
res = finalRs;
if (invokeRes instanceof Map) {
Map<String, Object> finalRs_ = (Map<String, Object>) finalRs;
Map<String, Object> invokeRes_ = (Map<String, Object>) invokeRes;
if (invokeRes != null) finalRs_.putAll(invokeRes_);
res = finalRs_;
} else if (invokeRes instanceof List) {
List<Map<String, Object>> finalRs_ = (List<Map<String, Object>>) finalRs;
List<Map<String, Object>> invokeRes_ = (List<Map<String, Object>>) 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<Map<String, Object>>) 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);
}
}
@@ -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<String, Object> 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);
}
@@ -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);
}
@@ -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);
}
}
@@ -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<Map<String, Object>> getSqlStatDataList(Map<String, String> 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<Map<String, Object>> 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<Map<String, Object>> array = druidStatManager.getSqlStatDataList();
// orderby
// orderby the statData array
if (orderBy != null && orderBy.trim().length() != 0) {
Collections.sort(array, new MapComparator<String, Object>(orderBy, DEFAULT_ORDER_TYPE.equals(orderType)));
}
@@ -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";