Skip to content
This repository was archived by the owner on Dec 15, 2018. It is now read-only.

Commit 2c75c6b

Browse files
committed
Created new ServiceLoaders class to encapsulate ClassLoader workarounds
1 parent 9d77c0c commit 2c75c6b

File tree

2 files changed

+58
-14
lines changed

2 files changed

+58
-14
lines changed

core/src/main/java/org/mvcspec/ozark/bootstrap/OzarkInitializer.java

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@
1717

1818
import org.mvcspec.ozark.core.ViewResponseFilter;
1919
import org.mvcspec.ozark.servlet.OzarkContainerInitializer;
20+
import org.mvcspec.ozark.util.ServiceLoaders;
2021

2122
import javax.servlet.ServletContext;
2223
import javax.ws.rs.core.Configuration;
2324
import javax.ws.rs.core.FeatureContext;
2425
import java.util.Objects;
25-
import java.util.ServiceLoader;
2626
import java.util.Set;
2727
import java.util.logging.Level;
2828
import java.util.logging.Logger;
@@ -56,19 +56,7 @@ public static void initialize(FeatureContext context, ServletContext servletCont
5656

5757
log.info("Initializing Ozark...");
5858

59-
// classloader to use for ServiceLoader lookups
60-
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
61-
62-
/*
63-
* Special workaround for TomEE . The context classloader is an instance of CxfContainerClassLoader
64-
* and NOT the TomEEWebappClassLoader, which seems to break when redeploying apps into a running
65-
* container for some reason.
66-
*/
67-
if (classLoader.getClass().getName().contains("CxfContainerClassLoader")) {
68-
classLoader = ConfigProvider.class.getClassLoader();
69-
}
70-
71-
for (ConfigProvider provider : ServiceLoader.load(ConfigProvider.class, classLoader)) {
59+
for (ConfigProvider provider : ServiceLoaders.list(ConfigProvider.class)) {
7260
log.log(Level.FINE, "Executing: {0}", provider.getClass().getName());
7361
provider.configure(context);
7462
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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

Comments
 (0)