forked from helidon-io/helidon
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Partial fix for issue helidon-io#7698
- Loading branch information
Showing
6 changed files
with
148 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
common/tls/src/main/java/io/helidon/common/tls/spi/TlsManagerCache.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* 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.common.tls.spi; | ||
|
||
import java.util.Objects; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.function.Function; | ||
|
||
import io.helidon.common.tls.TlsManager; | ||
|
||
class TlsManagerCache { | ||
private static final ConcurrentHashMap<Object, TlsManager> CACHE = new ConcurrentHashMap<>(); | ||
|
||
private TlsManagerCache() { | ||
} | ||
|
||
static TlsManager getOrCreate(Object configBean, | ||
Function<Object, TlsManager> creator) { | ||
Objects.requireNonNull(configBean); | ||
Objects.requireNonNull(creator); | ||
return CACHE.computeIfAbsent(configBean, creator); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
common/tls/src/test/java/io/helidon/common/tls/spi/TlsManagerProviderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* 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.common.tls.spi; | ||
|
||
import io.helidon.common.tls.ConfiguredTlsManager; | ||
import io.helidon.common.tls.TlsManager; | ||
import io.helidon.config.Config; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.Mockito; | ||
|
||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
import static org.hamcrest.CoreMatchers.*; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
|
||
class TlsManagerProviderTest { | ||
|
||
@Test | ||
void caching() { | ||
TlsManager mock = Mockito.mock(TlsManager.class); | ||
Config config = Config.create(); | ||
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(config, "test", "1", (c) -> { | ||
count.incrementAndGet(); | ||
return mock; | ||
}); | ||
assertThat(manager1, sameInstance(mock)); | ||
assertThat(count.get(), is(1)); | ||
|
||
TlsManager manager2 = TlsManagerProvider.getOrCreate(config, "test", "1", (c) -> { | ||
count.incrementAndGet(); | ||
return Mockito.mock(TlsManager.class); | ||
}); | ||
assertThat(manager2, sameInstance(mock)); | ||
assertThat(count.get(), is(1)); | ||
|
||
TlsManager manager3 = TlsManagerProvider.getOrCreate(config, "test", "2", (c) -> { | ||
count.incrementAndGet(); | ||
return Mockito.mock(TlsManager.class); | ||
}); | ||
assertThat(manager3, notNullValue()); | ||
assertThat(manager3, not(sameInstance(mock))); | ||
assertThat(count.get(), is(2)); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters