|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package com.dtstack.flink.sql.classloader; |
| 20 | + |
| 21 | +import com.dtstack.flink.sql.util.PluginUtil; |
| 22 | +import org.apache.commons.lang3.StringUtils; |
| 23 | +import org.slf4j.Logger; |
| 24 | +import org.slf4j.LoggerFactory; |
| 25 | + |
| 26 | +import java.net.URL; |
| 27 | +import java.util.ArrayList; |
| 28 | +import java.util.Arrays; |
| 29 | +import java.util.Comparator; |
| 30 | +import java.util.List; |
| 31 | +import java.util.Map; |
| 32 | +import java.util.concurrent.ConcurrentHashMap; |
| 33 | + |
| 34 | +/** |
| 35 | + * company: www.dtstack.com |
| 36 | + * author: toutian |
| 37 | + * create: 2019/10/14 |
| 38 | + */ |
| 39 | +public class ClassLoaderManager { |
| 40 | + |
| 41 | + private static final Logger LOG = LoggerFactory.getLogger(ClassLoaderManager.class); |
| 42 | + |
| 43 | + private static Map<String, DtClassLoader> pluginClassLoader = new ConcurrentHashMap<>(); |
| 44 | + |
| 45 | + public static <R> R newInstance(String pluginJarPath, ClassLoaderSupplier<R> supplier) throws Exception { |
| 46 | + ClassLoader classLoader = retrieveClassLoad(pluginJarPath); |
| 47 | + return ClassLoaderSupplierCallBack.callbackAndReset(supplier, classLoader); |
| 48 | + } |
| 49 | + |
| 50 | + public static <R> R newInstance(List<URL> jarUrls, ClassLoaderSupplier<R> supplier) throws Exception { |
| 51 | + ClassLoader classLoader = retrieveClassLoad(jarUrls); |
| 52 | + return ClassLoaderSupplierCallBack.callbackAndReset(supplier, classLoader); |
| 53 | + } |
| 54 | + |
| 55 | + private static DtClassLoader retrieveClassLoad(String pluginJarPath) { |
| 56 | + return pluginClassLoader.computeIfAbsent(pluginJarPath, k -> { |
| 57 | + try { |
| 58 | + URL[] urls = PluginUtil.getPluginJarUrls(pluginJarPath); |
| 59 | + ClassLoader parentClassLoader = Thread.currentThread().getContextClassLoader(); |
| 60 | + DtClassLoader classLoader = new DtClassLoader(urls, parentClassLoader); |
| 61 | + LOG.info("pluginJarPath:{} create ClassLoad successful...", pluginJarPath); |
| 62 | + return classLoader; |
| 63 | + } catch (Throwable e) { |
| 64 | + LOG.error("retrieve ClassLoad happens error:{}", e); |
| 65 | + throw new RuntimeException("retrieve ClassLoad happens error"); |
| 66 | + } |
| 67 | + }); |
| 68 | + } |
| 69 | + |
| 70 | + private static DtClassLoader retrieveClassLoad(List<URL> jarUrls) { |
| 71 | + jarUrls.sort(Comparator.comparing(URL::toString)); |
| 72 | + String jarUrlkey = StringUtils.join(jarUrls, "_"); |
| 73 | + return pluginClassLoader.computeIfAbsent(jarUrlkey, k -> { |
| 74 | + try { |
| 75 | + URL[] urls = jarUrls.toArray(new URL[jarUrls.size()]); |
| 76 | + ClassLoader parentClassLoader = Thread.currentThread().getContextClassLoader(); |
| 77 | + DtClassLoader classLoader = new DtClassLoader(urls, parentClassLoader); |
| 78 | + LOG.info("jarUrl:{} create ClassLoad successful...", jarUrlkey); |
| 79 | + return classLoader; |
| 80 | + } catch (Throwable e) { |
| 81 | + LOG.error("retrieve ClassLoad happens error:{}", e); |
| 82 | + throw new RuntimeException("retrieve ClassLoad happens error"); |
| 83 | + } |
| 84 | + }); |
| 85 | + } |
| 86 | + |
| 87 | + public static List<URL> getClassPath() { |
| 88 | + List<URL> classPaths = new ArrayList<>(); |
| 89 | + for (Map.Entry<String, DtClassLoader> entry : pluginClassLoader.entrySet()) { |
| 90 | + classPaths.addAll(Arrays.asList(entry.getValue().getURLs())); |
| 91 | + } |
| 92 | + return classPaths; |
| 93 | + } |
| 94 | +} |
0 commit comments