pnowojski commented on a change in pull request #8038: [FLINK-11953] Introduce Plugin/Loading system and integrate it with FileSystem URL: https://github.com/apache/flink/pull/8038#discussion_r272925609
########## File path: flink-core/src/main/java/org/apache/flink/core/plugin/PluginLoader.java ########## @@ -0,0 +1,101 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.flink.core.plugin; + +import org.apache.flink.annotation.VisibleForTesting; +import org.apache.flink.util.ChildFirstClassLoader; + +import javax.annotation.concurrent.ThreadSafe; + +import java.util.Iterator; +import java.util.ServiceLoader; + +/** + * A {@link PluginLoader} is used by the {@link PluginManager} to load a single plugin. It is essentially a combination + * of a {@link ChildFirstClassLoader} and {@link ServiceLoader}. This class can locate and load service implementations + * from the plugin for a given SPI. The {@link PluginDescriptor}, which among other information contains the resource + * URLs, is provided at construction. + */ +@ThreadSafe +public class PluginLoader { + + /** Classloader which is used to load the plugin classes. We expect this classloader is thread-safe.*/ + private final ClassLoader pluginClassLoader; + + @VisibleForTesting + public PluginLoader(PluginDescriptor pluginDescriptor, ClassLoader parentClassLoader) { + this.pluginClassLoader = + new ChildFirstClassLoader( + pluginDescriptor.getPluginResourceURLs(), + parentClassLoader, + pluginDescriptor.getLoaderExcludePatterns()); + } + + /** + * Returns in iterator over all available implementations of the given service interface (SPI) for the plugin. + * + * @param service the service interface (SPI) for which implementations are requested. + * @param <P> Type of the requested plugin service. + * @return An iterator of all implementations of the given service interface that could be loaded from the plugin. + */ + public <P extends Plugin> Iterator<P> load(Class<P> service) { + final ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader(); + try { + Thread.currentThread().setContextClassLoader(pluginClassLoader); + return new ContextClassLoaderSettingIterator<>( + ServiceLoader.load(service, pluginClassLoader).iterator(), + pluginClassLoader); + } finally { + Thread.currentThread().setContextClassLoader(contextClassLoader); + } + } + + /** + * Wrapper for the service iterator. The wrapper will set/unset the context classloader to the plugin classloader + * around the point where elements are returned. + * + * @param <P> type of the iterated plugin element. + */ + static class ContextClassLoaderSettingIterator<P extends Plugin> implements Iterator<P> { + + private final Iterator<P> delegate; + private final ClassLoader pluginClassLoader; + + ContextClassLoaderSettingIterator(Iterator<P> delegate, ClassLoader pluginClassLoader) { + this.delegate = delegate; + this.pluginClassLoader = pluginClassLoader; + } + + @Override + public boolean hasNext() { + return delegate.hasNext(); + } + + @Override + public P next() { + final ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader(); Review comment: I would wrap this setting and resetting logic into a `Closable` class and re-use it here and `load` method. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services
