|
| 1 | +/* |
| 2 | + * Copyright © 2017 Ivar Grimstad ([email protected]) |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.mvcspec.ozark.util; |
| 17 | + |
| 18 | +import org.mvcspec.ozark.bootstrap.ConfigProvider; |
| 19 | + |
| 20 | +import java.util.List; |
| 21 | +import java.util.ServiceLoader; |
| 22 | +import java.util.stream.Collectors; |
| 23 | +import java.util.stream.StreamSupport; |
| 24 | + |
| 25 | +/** |
| 26 | + * Utility code for the {@link ServiceLoader} class. |
| 27 | + * |
| 28 | + * @author Christian Kaltepoth |
| 29 | + */ |
| 30 | +public class ServiceLoaders { |
| 31 | + |
| 32 | + private ServiceLoaders() { |
| 33 | + // only static methods |
| 34 | + } |
| 35 | + |
| 36 | + public static <T> List<T> list(Class<T> type) { |
| 37 | + |
| 38 | + // classloader to use for ServiceLoader lookups |
| 39 | + ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); |
| 40 | + |
| 41 | + /* |
| 42 | + * Special workaround for TomEE . The context classloader is an instance of CxfContainerClassLoader |
| 43 | + * and NOT the TomEEWebappClassLoader, which seems to break when redeploying apps into a running |
| 44 | + * container for some reason. |
| 45 | + */ |
| 46 | + if (classLoader.getClass().getName().contains("CxfContainerClassLoader")) { |
| 47 | + classLoader = ConfigProvider.class.getClassLoader(); |
| 48 | + } |
| 49 | + |
| 50 | + // return the SPI implementations as a list |
| 51 | + return StreamSupport.stream(ServiceLoader.load(type, classLoader).spliterator(), false) |
| 52 | + .collect(Collectors.toList()); |
| 53 | + |
| 54 | + } |
| 55 | + |
| 56 | +} |
0 commit comments