Skip to content

Commit

Permalink
backport for issue helidon-io#7698
Browse files Browse the repository at this point in the history
  • Loading branch information
trentjeff committed Oct 9, 2023
1 parent 18043cb commit 6a874e9
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ public String configKey() {
public TlsManager create(Config config,
String name) {
OciCertificatesTlsManagerConfig cfg = OciCertificatesTlsManagerConfig.create(config);
return new DefaultOciCertificatesTlsManager(cfg, name, config);
return TlsManagerProvider.getOrCreate(cfg,
(c) -> new DefaultOciCertificatesTlsManager(cfg, name, config));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright (c) 2023 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.helidon.webserver.spi;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.locks.ReentrantLock;
import java.util.function.Function;

import io.helidon.webserver.TlsManager;

class TlsManagerCache {
private static final ReentrantLock LOCK = new ReentrantLock();
private static final Map<Object, TlsManager> CACHE = new HashMap<>();

private TlsManagerCache() {
}

static <T> TlsManager getOrCreate(T configBean,
Function<T, TlsManager> creator) {
Objects.requireNonNull(configBean);
Objects.requireNonNull(creator);
LOCK.lock();
try {
TlsManager manager = CACHE.get(configBean);
if (manager != null) {
return manager;
}

manager = creator.apply(configBean);
Object existing = CACHE.put(configBean, manager);
assert (existing == null);

return manager;
} finally {
LOCK.unlock();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.List;
import java.util.Map;
import java.util.ServiceLoader;
import java.util.function.Function;

import io.helidon.common.serviceloader.HelidonServiceLoader;
import io.helidon.config.Config;
Expand Down Expand Up @@ -49,6 +50,19 @@ public interface TlsManagerProvider {
*/
TlsManager create(Config config, String name);

/**
* Provides the ability to have a unique {@link TlsManager} per unique {@link Config} instance provided.
*
* @param configBean the config bean instance
* @param creator the creator to apply if not already in cache, which takes the config bean instance
* @param <T> the type of the config bean
* @return the tls manager instance from cache, defaulting to creation from the {@code creator} if not in cache
*/
static <T> TlsManager getOrCreate(T configBean,
Function<T, TlsManager> creator) {
return TlsManagerCache.getOrCreate(configBean, creator);
}

/**
* Takes a configuration and looks for a suitable {@link TlsManager} instance based upon that configuration.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,23 @@
package io.helidon.webserver.spi;

import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

import io.helidon.config.Config;
import io.helidon.config.ConfigSources;
import io.helidon.webserver.ConfiguredTlsManager;
import io.helidon.webserver.TlsManager;

import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.sameInstance;
import static org.junit.jupiter.api.Assertions.assertThrows;

class TlsManagerProviderTest {
Expand Down Expand Up @@ -86,4 +92,33 @@ void goodConfig() {
equalTo("fake-type"));
}

@Test
void caching() {
TlsManager mock = Mockito.mock(TlsManager.class);
AtomicInteger count = new AtomicInteger();

// we are using "1" and "2" here abstractly to stand in for Config beans, which would hash properly
TlsManager manager1 = TlsManagerProvider.getOrCreate("1", (c) -> {
count.incrementAndGet();
return mock;
});
assertThat(manager1, sameInstance(mock));
assertThat(count.get(), is(1));

TlsManager manager2 = TlsManagerProvider.getOrCreate("1", (c) -> {
count.incrementAndGet();
return Mockito.mock(TlsManager.class);
});
assertThat(manager2, sameInstance(mock));
assertThat(count.get(), is(1));

TlsManager manager3 = TlsManagerProvider.getOrCreate("2", (c) -> {
count.incrementAndGet();
return Mockito.mock(TlsManager.class);
});
assertThat(manager3, notNullValue());
assertThat(manager3, not(sameInstance(mock)));
assertThat(count.get(), is(2));
}

}

0 comments on commit 6a874e9

Please sign in to comment.