Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check for HAM/config provider more efficiently #18399

Merged
merged 8 commits into from
Dec 7, 2021
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2017, 2020 IBM Corporation and others.
* Copyright (c) 2017, 2021 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
Expand Down Expand Up @@ -48,25 +48,28 @@ protected void activate(ComponentContext cc) {}
@Deactivate
protected void deactivate(ComponentContext cc) {}

// Synchronized since checking if there is a provider and registering one need to be done as a single atomic operation.
@Override
public synchronized void buildBridgeIfNeeded(String appContext, AuthConfigFactory providerFactory) {
AuthConfigProvider authConfigProvider = providerFactory.getConfigProvider(JASPIC_LAYER_HTTP_SERVLET, appContext, (RegistrationListener) null);
if (authConfigProvider != null) {
// A provider was registered already for this application context.
return;
}
public void buildBridgeIfNeeded(String appContext, AuthConfigFactory providerFactory) {

if (getModulePropertiesUtils().isHttpAuthenticationMechanism()) {
// Create AuthConfigProvider, AuthConfig, AuthContext, and ServerAuthModule bridge.
Map<String, String> props = new ConcurrentHashMap<String, String>();
authConfigProvider = new AuthProvider(props, providerFactory);
providerFactory.registerConfigProvider(authConfigProvider, JASPIC_LAYER_HTTP_SERVLET, appContext, PROVIDER_DESCRIPTION);
} else {
if (!getModulePropertiesUtils().isHttpAuthenticationMechanism()) {
if (tc.isDebugEnabled()) {
Tr.debug(tc, "HttpAuthenticationMechanism bean is not identified. JSR375 BridgeProvider is not enabled.");
}
}
} else {
AuthConfigProvider authConfigProvider = providerFactory.getConfigProvider(JASPIC_LAYER_HTTP_SERVLET, appContext, (RegistrationListener) null);
if (authConfigProvider == null) {
// Synchronized since checking if there is a provider and registering one need to be done as a single atomic operation.
synchronized (this) {
authConfigProvider = providerFactory.getConfigProvider(JASPIC_LAYER_HTTP_SERVLET, appContext, (RegistrationListener) null);
if (authConfigProvider == null) {
// Create AuthConfigProvider, AuthConfig, AuthContext, and ServerAuthModule bridge.
Map<String, String> props = new ConcurrentHashMap<String, String>();
authConfigProvider = new AuthProvider(props, providerFactory);
providerFactory.registerConfigProvider(authConfigProvider, JASPIC_LAYER_HTTP_SERVLET, appContext, PROVIDER_DESCRIPTION);
}
}
}
}
}

@Override
Expand Down Expand Up @@ -95,4 +98,4 @@ protected ModulePropertiesUtils getModulePropertiesUtils() {
return ModulePropertiesUtils.getInstance();
}

}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2017 IBM Corporation and others.
* Copyright (c) 2017, 2021 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
Expand Down Expand Up @@ -99,7 +99,7 @@ public void tearDown() throws Exception {

@Test
public void testNoAuthMechDoesNotRegisterProvider() throws Exception {
withNoCachedProvider().doesNotRegisterProvider();
withNoHAM().doesNotRegisterProvider();
isHAM = false;
bridgeBuilder.buildBridgeIfNeeded(APP_CONTEXT, providerFactory);
}
Expand All @@ -121,15 +121,25 @@ public void testOneAuthMechRegistersProvider() throws Exception {

@Test
public void testWithCachedProviderDoesNotRegisterProvider() throws Exception {
isHAM = true;
withCachedProvider().doesNotRegisterProvider();

bridgeBuilder.buildBridgeIfNeeded(APP_CONTEXT, providerFactory);
}

private BridgeBuilderImplTest withNoCachedProvider() throws Exception {
mockery.checking(new Expectations() {
{
one(providerFactory).getConfigProvider("HttpServlet", APP_CONTEXT, null);
exactly(2).of(providerFactory).getConfigProvider("HttpServlet", APP_CONTEXT, null);
will(returnValue(null));
}
});
return this;
}

private BridgeBuilderImplTest withNoHAM() throws Exception {
mockery.checking(new Expectations() {
{
never(providerFactory).getConfigProvider("HttpServlet", APP_CONTEXT, null);
will(returnValue(null));
}
});
Expand Down