adutra commented on code in PR #4544:
URL: https://github.com/apache/polaris/pull/4544#discussion_r3303263327
##########
persistence/nosql/authz/impl/src/main/java/org/apache/polaris/persistence/nosql/authz/impl/JacksonPrivilegesModule.java:
##########
@@ -18,72 +18,71 @@
*/
package org.apache.polaris.persistence.nosql.authz.impl;
-import static com.google.common.base.Preconditions.checkArgument;
+import static com.google.common.base.Preconditions.checkState;
+import static java.util.Objects.requireNonNull;
import com.fasterxml.jackson.databind.module.SimpleModule;
-import jakarta.enterprise.inject.Instance;
+import jakarta.enterprise.context.ApplicationScoped;
+import jakarta.enterprise.event.Observes;
+import jakarta.enterprise.event.Shutdown;
+import jakarta.enterprise.event.Startup;
import jakarta.enterprise.inject.spi.CDI;
-import java.util.function.Function;
+import jakarta.inject.Inject;
+import java.util.function.Supplier;
import org.apache.polaris.persistence.nosql.authz.api.Acl;
import org.apache.polaris.persistence.nosql.authz.api.PrivilegeSet;
import org.apache.polaris.persistence.nosql.authz.api.Privileges;
public class JacksonPrivilegesModule extends SimpleModule {
+ @SuppressWarnings("unused")
public JacksonPrivilegesModule() {
- addDeserializer(PrivilegeSet.class, new PrivilegeSetDeserializer());
- addSerializer(PrivilegeSet.class, new PrivilegeSetSerializer());
- addDeserializer(Acl.class, new AclDeserializer());
- addSerializer(Acl.class, new AclSerializer());
+ this(PrivilegesViaCDI::privileges);
}
- static Privileges currentPrivileges() {
- return cdiResolve(Privileges.class);
+ JacksonPrivilegesModule(Supplier<Privileges> privilegesResolver) {
+ requireNonNull(privilegesResolver, "privilegesResolver must not be null");
+ addDeserializer(PrivilegeSet.class, new
PrivilegeSetDeserializer(privilegesResolver));
+ addSerializer(PrivilegeSet.class, new
PrivilegeSetSerializer(privilegesResolver));
+ addDeserializer(Acl.class, new AclDeserializer(privilegesResolver));
+ addSerializer(Acl.class, new AclSerializer());
}
- // TODO the following is the same as in AbstractTypeIdResolver
-
/**
- * Resolve the given type via {@link CDI#current() CDI.current()}. For tests
the resolution via
- * CDI can be {@linkplain CDIResolver#setResolver(Function) routed to a
custom function}.
+ * Helper to memoize the {@link Privileges} instance in a CDI environment.
This class bridges the
+ * gap between CDI and Jackson module lifecycles.
+ *
+ * <p>The implementation attempts to infer the {@link Privileges} instance
from the CDI container
+ * by observing the {@link Startup} event. However, other beans that also
observe the {@link
+ * Startup} event can trigger a call to the {@link #privileges()} method
before this bean's {@link
+ * #init(Startup)} function is called.
*/
- private static <R> R cdiResolve(Class<R> type) {
- // TODO instead of doing the 'CDIResolver' dance, we could (should?) have
an attribute in the
- // `DatabindContext` holding a reference to the CDI instance (referred to
as `Instance<?>`).
- var resolved = CDIResolver.resolver.apply(type);
- @SuppressWarnings("unchecked")
- var r = (R) resolved;
- return r;
- }
+ @ApplicationScoped
+ static class PrivilegesViaCDI {
Review Comment:
The static volatile field looks hacky. Isn't possible to stick to standard
Quarkus ways to do this, e.g.
```java
@Singleton
class PrivilegesJacksonCustomizer implements ObjectMapperCustomizer {
@Inject Privileges privileges;
@Override
public void customize(ObjectMapper mapper) {
mapper.registerModule(new JacksonPrivilegesModule(() ->
privileges));
}
}
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]