Skip to content

Commit

Permalink
improve code
Browse files Browse the repository at this point in the history
  • Loading branch information
xunliu committed Dec 19, 2024
1 parent 539c107 commit abb3f0c
Showing 1 changed file with 43 additions and 43 deletions.
86 changes: 43 additions & 43 deletions core/src/main/java/org/apache/gravitino/connector/BaseCatalog.java
Original file line number Diff line number Diff line change
Expand Up @@ -194,56 +194,56 @@ public AuthorizationPlugin getAuthorizationPlugin() {
}
}
}
return authorization.newPlugin(entity.namespace().level(0), provider(), this.conf);
return (AuthorizationPlugin) authorization;
}

public void initAuthorizationPluginInstance(IsolatedClassLoader classLoader) {
if (authorization != null) {
if (authorization == null) {
synchronized (this) {
if (authorization != null) {
return;
if (authorization == null) {
String authorizationProvider =
(String) catalogPropertiesMetadata().getOrDefault(conf, AUTHORIZATION_PROVIDER);
if (authorizationProvider == null) {
LOG.info("Authorization provider is not set!");
return;
}
try {
authorization =
classLoader.withClassLoader(
cl -> {
try {
ServiceLoader<AuthorizationProvider> loader =
ServiceLoader.load(AuthorizationProvider.class, cl);

List<Class<? extends AuthorizationProvider>> providers =
Streams.stream(loader.iterator())
.filter(p -> p.shortName().equalsIgnoreCase(authorizationProvider))
.map(AuthorizationProvider::getClass)
.collect(Collectors.toList());
if (providers.isEmpty()) {
throw new IllegalArgumentException(
"No authorization provider found for: " + authorizationProvider);
} else if (providers.size() > 1) {
throw new IllegalArgumentException(
"Multiple authorization providers found for: "
+ authorizationProvider);
}
return (BaseAuthorization<?>)
Iterables.getOnlyElement(providers)
.getDeclaredConstructor()
.newInstance();
} catch (Exception e) {
LOG.error("Failed to create authorization instance", e);
throw new RuntimeException(e);
}
});
} catch (Exception e) {
LOG.error("Failed to load authorization with class loader", e);
throw new RuntimeException(e);
}
}
}
}

String authorizationProvider =
(String) catalogPropertiesMetadata().getOrDefault(conf, AUTHORIZATION_PROVIDER);
if (authorizationProvider == null) {
LOG.info("Authorization provider is not set!");
return;
}

try {
authorization =
classLoader.withClassLoader(
cl -> {
try {
ServiceLoader<AuthorizationProvider> loader =
ServiceLoader.load(AuthorizationProvider.class, cl);

List<Class<? extends AuthorizationProvider>> providers =
Streams.stream(loader.iterator())
.filter(p -> p.shortName().equalsIgnoreCase(authorizationProvider))
.map(AuthorizationProvider::getClass)
.collect(Collectors.toList());
if (providers.isEmpty()) {
throw new IllegalArgumentException(
"No authorization provider found for: " + authorizationProvider);
} else if (providers.size() > 1) {
throw new IllegalArgumentException(
"Multiple authorization providers found for: " + authorizationProvider);
}
return (BaseAuthorization<?>)
Iterables.getOnlyElement(providers).getDeclaredConstructor().newInstance();
} catch (Exception e) {
LOG.error("Failed to create authorization instance", e);
throw new RuntimeException(e);
}
});
} catch (Exception e) {
LOG.error("Failed to load authorization with class loader", e);
throw new RuntimeException(e);
}
}

@Override
Expand Down

0 comments on commit abb3f0c

Please sign in to comment.