| | |
| | | import java.lang.reflect.Method; |
| | | import java.net.URL; |
| | | import java.net.URLClassLoader; |
| | | import java.util.Arrays; |
| | | import java.util.HashMap; |
| | | import java.util.Iterator; |
| | | import java.util.Vector; |
| | | import java.util.stream.Collectors; |
| | | |
| | | @Slf4j |
| | | public class DllUtils { |
| | | |
| | | private static HashMap<String, URLClassLoader> classLoaderCache = new HashMap<>(); |
| | | private static HashMap<String, Object> classCache = new HashMap<>(); |
| | | private static HashMap<String, Method> classMethodCache = new HashMap<>(); |
| | | |
| | | /** |
| | | * @description: 加载dll到指定class下 |
| | |
| | | unloadDll(urlClassLoader); |
| | | unloadJar(urlClassLoader); |
| | | classLoaderCache.remove(projectId); |
| | | removeClassCache(projectId); |
| | | removeClassMethodCache(projectId); |
| | | } |
| | | } |
| | | public static synchronized void removeClassCache(String projectId) { |
| | | for (String key : classCache.keySet()) { |
| | | if (key.startsWith(projectId)) { |
| | | classCache.remove(key); |
| | | } |
| | | } |
| | | } |
| | | |
| | | public static synchronized void removeClassMethodCache(String projectId) { |
| | | for (String key : classMethodCache.keySet()) { |
| | | if (key.startsWith(projectId)) { |
| | | classMethodCache.remove(key); |
| | | } |
| | | } |
| | | } |
| | | |
| | |
| | | } else if (model == null) { |
| | | throw new RuntimeException("模型文件不能为空!"); |
| | | } else { |
| | | URLClassLoader classLoader = DllUtils.getClassLoader(projectId); |
| | | if (null == classLoader) { |
| | | throw new RuntimeException("dll未发布,classLoader为null"); |
| | | String classCacheKey = projectId + "_" + model.getClassName(); |
| | | String methodParams = Arrays.stream(model.getParamsArray()).map(e -> e.getName()).collect(Collectors.joining(",")); |
| | | String classMethodCacheKey = classCacheKey + "." + model.getMethodName() + "(" + methodParams + ")"; |
| | | if (classCache.containsKey(classCacheKey) && classMethodCache.containsKey(classMethodCacheKey)) { |
| | | return (HashMap)classMethodCache.get(classMethodCacheKey).invoke( classCache.get(classCacheKey), paramsValueArray); |
| | | }else { |
| | | URLClassLoader classLoader = DllUtils.getClassLoader(projectId); |
| | | if (null == classLoader) { |
| | | throw new RuntimeException("dll未发布,classLoader为null"); |
| | | } |
| | | Class<?> clazz = classLoader.loadClass(model.getClassName()); |
| | | Object o = clazz.newInstance(); |
| | | Method method = clazz.getMethod(model.getMethodName(), model.getParamsArray()); |
| | | classCache.put(classCacheKey,o); |
| | | classMethodCache.put(classMethodCacheKey,method); |
| | | return (HashMap)method.invoke(o, paramsValueArray); |
| | | } |
| | | Class<?> clazz = classLoader.loadClass(model.getClassName()); |
| | | Method method = clazz.getMethod(model.getMethodName(), model.getParamsArray()); |
| | | return (HashMap)method.invoke(clazz.newInstance(), paramsValueArray); |
| | | } |
| | | } |
| | | } |