com.github.aidensuen.mongo.util.MsUtil Maven / Gradle / Ivy
package com.github.aidensuen.mongo.util;
import com.github.aidensuen.mongo.core.MongoDaoStatement;
import com.github.aidensuen.mongo.exception.MongoDaoException;
import com.github.aidensuen.mongo.exception.RegistryException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
public class MsUtil {
public static final Map cache = new ConcurrentHashMap<>();
public static Class> getMongoDaoClass(String msId) {
if (msId.indexOf(".") == -1) {
throw new RegistryException("current id=" + msId + " invalid !");
}
String mongoDaoClassStr = msId.substring(0, msId.lastIndexOf("."));
Class> mongoDaoClass = (Class>) cache.get(mongoDaoClassStr);
if (mongoDaoClass != null) {
return mongoDaoClass;
}
ClassLoader[] classLoader = getClassLoaders();
for (ClassLoader cl : classLoader) {
if (null != cl) {
try {
mongoDaoClass = Class.forName(mongoDaoClassStr, true, cl);
if (mongoDaoClass != null) {
break;
}
} catch (ClassNotFoundException e) {
}
}
}
if (mongoDaoClass == null) {
throw new MongoDaoException("class loaders failed to locate the class " + mongoDaoClassStr);
}
cache.put(mongoDaoClassStr, mongoDaoClass);
return mongoDaoClass;
}
private static ClassLoader[] getClassLoaders() {
return new ClassLoader[]{Thread.currentThread().getContextClassLoader(), MsUtil.class.getClassLoader()};
}
public static String getMethodName(MongoDaoStatement ms) {
return getMethodName(ms.getId());
}
public static String getMethodName(String msId) {
return msId.substring(msId.lastIndexOf(".") + 1, msId.lastIndexOf("-@"));
}
}