slinkydeveloper commented on a change in pull request #17897: URL: https://github.com/apache/flink/pull/17897#discussion_r757613969
########## File path: flink-table/flink-table-common/src/main/java/org/apache/flink/table/factories/ServiceLoaderUtil.java ########## @@ -0,0 +1,93 @@ +/* + * 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.table.factories; + +import java.util.Iterator; +import java.util.ServiceLoader; + +class ServiceLoaderUtil { + + static <T> Iterator<LoadResult<T>> load(Class<T> clazz, ClassLoader classLoader) { + return new SafeIterator<>(ServiceLoader.load(clazz, classLoader).iterator()); + } + + static class LoadResult<T> { + private final T service; + private final Throwable error; + + private LoadResult(T service, Throwable error) { + this.service = service; + this.error = error; + } + + private LoadResult(T service) { + this(service, null); + } + + private LoadResult(Throwable error) { + this(null, error); + } + + public boolean failed() { + return error != null; + } + + public Throwable getError() { + return error; + } + + public T getService() { + return service; + } + } + + /** + * This iterator wraps {@link Iterator#hasNext()} and {@link Iterator#next()} in try-catch, and + * returns {@link LoadResult} to handle such failures. + */ + private static class SafeIterator<T> implements Iterator<LoadResult<T>> { + + private final Iterator<T> iterator; + + public SafeIterator(Iterator<T> iterator) { + this.iterator = iterator; + } + + @Override + public boolean hasNext() { + try { + return iterator.hasNext(); + } catch (Throwable t) { Review comment: Check https://docs.oracle.com/javase/8/docs/api/java/util/ServiceLoader.html#iterator-- for more details, but in short both `hasNext()` and `next()` might fail according to this javadoc, and they can throw any kind of error: > Its hasNext and next methods can therefore throw a ServiceConfigurationError if a provider-configuration file violates the specified format, or if it names a provider class that cannot be found and instantiated, or if the result of instantiating the class is not assignable to the service type, or if any other kind of exception or error is thrown as the next provider is located and instantiated. -- 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. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
