Skip to content

Commit

Permalink
[WFLY-12373] Cache injected objects in WeldSecurityServices to avoid …
Browse files Browse the repository at this point in the history
…contended gets from the Supplier
  • Loading branch information
bstansberry committed Aug 13, 2019
1 parent 27d5534 commit 32235bd
Showing 1 changed file with 12 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ public class WeldSecurityServices implements Service, SecurityServices {
// that doesn't matter, just to make it harder for someone to modify this class and
// accidentally introduce any unnecessary loading of ServerSecurityManager
private final Supplier<?> securityManagerSupplier;
// Cache the object obtained from securityManagerSupplier to avoid contended reads of the supplier
private volatile Object securityManagerRef;

public WeldSecurityServices(final Consumer<SecurityServices> securityServicesConsumer, final Supplier<?> securityManagerSupplier) {
this.securityServicesConsumer = securityServicesConsumer;
Expand All @@ -66,6 +68,7 @@ public void start(final StartContext context) throws StartException {

@Override
public void stop(final StopContext context) {
securityManagerRef = null;
securityServicesConsumer.accept(null);
}

Expand All @@ -78,7 +81,7 @@ public Principal getPrincipal() {

// Use 'Object' initially to avoid loading ServerSecurityManager (which may not be present)
// until we know for sure we need it.
final Object securityManager = securityManagerSupplier != null ? securityManagerSupplier.get() : null;
final Object securityManager = getSecurityManagerRef();
if (securityManager == null)
throw WeldLogger.ROOT_LOGGER.securityNotEnabled();
if (WildFlySecurityManager.isChecking()) {
Expand Down Expand Up @@ -123,6 +126,14 @@ private SecurityDomain getCurrentSecurityDomain() {
}
}

private Object getSecurityManagerRef() {
Object result = this.securityManagerRef;
if (result == null && this.securityManagerSupplier != null) {
result = this.securityManagerRef = securityManagerSupplier.get();
}
return result;
}

static class WeldSecurityContext implements org.jboss.weld.security.spi.SecurityContext, PrivilegedAction<Void> {

private final SecurityContext ctx;
Expand Down

0 comments on commit 32235bd

Please sign in to comment.