-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow client-supplied
AnnotationScanner
s, bypassing service loader (#…
…1567) Signed-off-by: Michael Edgar <[email protected]>
- Loading branch information
Showing
6 changed files
with
135 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 26 additions & 14 deletions
40
core/src/main/java/io/smallrye/openapi/runtime/scanner/spi/AnnotationScannerFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,42 @@ | ||
package io.smallrye.openapi.runtime.scanner.spi; | ||
|
||
import static java.util.Comparator.comparing; | ||
import static java.util.Comparator.nullsLast; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.ServiceLoader; | ||
import java.util.function.Supplier; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.StreamSupport; | ||
|
||
/** | ||
* Factory that allows plugging in more scanners. | ||
* | ||
* @author Phillip Kruger ([email protected]) | ||
*/ | ||
public class AnnotationScannerFactory { | ||
private final Map<String, AnnotationScanner> loadedScanners = new HashMap<>(); | ||
|
||
public AnnotationScannerFactory(ClassLoader cl) { | ||
ServiceLoader<AnnotationScanner> loader = ServiceLoader.load(AnnotationScanner.class, cl); | ||
Iterator<AnnotationScanner> scannerIterator = loader.iterator(); | ||
while (scannerIterator.hasNext()) { | ||
AnnotationScanner scanner = scannerIterator.next(); | ||
loadedScanners.put(scanner.getName(), scanner); | ||
} | ||
public class AnnotationScannerFactory implements Supplier<Iterable<AnnotationScanner>> { | ||
|
||
/** | ||
* List of AnnotationScanners discovered via the ServiceLoader, ordered by | ||
* {@linkplain AnnotationScanner#getName() name} | ||
*/ | ||
private final List<AnnotationScanner> loadedScanners; | ||
|
||
public AnnotationScannerFactory(ClassLoader loader) { | ||
Iterable<AnnotationScanner> scanners = ServiceLoader.load(AnnotationScanner.class, loader); | ||
loadedScanners = StreamSupport.stream(scanners.spliterator(), false) | ||
.sorted(comparing(AnnotationScanner::getName, nullsLast(String::compareTo))) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
public List<AnnotationScanner> getAnnotationScanners() { | ||
return new ArrayList<>(loadedScanners.values()); | ||
return new ArrayList<>(loadedScanners); | ||
} | ||
|
||
@Override | ||
public Iterable<AnnotationScanner> get() { | ||
return getAnnotationScanners(); | ||
} | ||
|
||
} |
Oops, something went wrong.