Skip to content

Commit

Permalink
Refactor to findInstance
Browse files Browse the repository at this point in the history
  • Loading branch information
vinishjail97 committed Dec 31, 2024
1 parent d60625c commit a691f02
Showing 1 changed file with 9 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,14 @@ public class ExternalCatalogConfigFactory {
public static ExternalCatalogConfig fromCatalogType(
String catalogType, String catalogId, Map<String, String> properties) {
String catalogSyncClientImpl =
findImplClassName(CatalogSyncClient.class, catalogType, CatalogSyncClient::getCatalogType);
findInstance(CatalogSyncClient.class, catalogType, CatalogSyncClient::getCatalogType)
.getClass()
.getName();
String catalogConversionSourceImpl =
findImplClassName(
CatalogConversionSource.class, catalogType, CatalogConversionSource::getCatalogType);
findInstance(
CatalogConversionSource.class, catalogType, CatalogConversionSource::getCatalogType)
.getClass()
.getName();
return ExternalCatalogConfig.builder()
.catalogType(catalogType)
.catalogSyncClientImpl(catalogSyncClientImpl)
Expand All @@ -46,13 +50,13 @@ public static ExternalCatalogConfig fromCatalogType(
.build();
}

private static <T> String findImplClassName(
private static <T> T findInstance(
Class<T> serviceClass, String catalogType, Function<T, String> catalogTypeExtractor) {
ServiceLoader<T> loader = ServiceLoader.load(serviceClass);
for (T instance : loader) {
String instanceCatalogType = catalogTypeExtractor.apply(instance);
if (catalogType.equals(instanceCatalogType)) {
return instance.getClass().getName();
return instance;
}
}
throw new NotSupportedException("catalogType is not yet supported: " + catalogType);
Expand Down

0 comments on commit a691f02

Please sign in to comment.