Skip to content

Fix IllegalAccessException in MongoDbAtlasLocalContainerConnectionDetails.getSslBundle() #4186

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,13 @@

package org.springframework.ai.testcontainers.service.connection.mongo;

import java.lang.invoke.MethodHandles;
import java.lang.reflect.Method;

import com.mongodb.ConnectionString;
import org.testcontainers.mongodb.MongoDBAtlasLocalContainer;

import org.springframework.boot.autoconfigure.mongo.MongoConnectionDetails;
import org.springframework.boot.ssl.SslBundle;
import org.springframework.boot.testcontainers.service.connection.ContainerConnectionDetailsFactory;
import org.springframework.boot.testcontainers.service.connection.ContainerConnectionSource;
import org.springframework.util.ReflectionUtils;

/**
* A {@link ContainerConnectionDetailsFactory} implementation that provides
Expand All @@ -51,12 +47,6 @@
class MongoDbAtlasLocalContainerConnectionDetailsFactory
extends ContainerConnectionDetailsFactory<MongoDBAtlasLocalContainer, MongoConnectionDetails> {

private static final Method GET_SSL_BUNDLE_METHOD;

static {
GET_SSL_BUNDLE_METHOD = ReflectionUtils.findMethod(MongoConnectionDetails.class, "getSslBundle");
}

@Override
protected MongoConnectionDetails getContainerConnectionDetails(
ContainerConnectionSource<MongoDBAtlasLocalContainer> source) {
Expand All @@ -79,21 +69,10 @@ public ConnectionString getConnectionString() {
return new ConnectionString(getContainer().getConnectionString());
}

// Conditional implementation based on whether the method exists
@Override
public SslBundle getSslBundle() {
if (GET_SSL_BUNDLE_METHOD != null) { // Boot 3.5.x+
try {
return (SslBundle) MethodHandles.lookup()
.in(GET_SSL_BUNDLE_METHOD.getDeclaringClass())
.unreflectSpecial(GET_SSL_BUNDLE_METHOD, GET_SSL_BUNDLE_METHOD.getDeclaringClass())
.bindTo(this)
.invokeWithArguments();
}
catch (Throwable e) {
throw new RuntimeException(e);
}
}
return null; // Boot 3.4.x (No-Op)
// Delegate to the interface default method by calling it explicitly
return MongoConnectionDetails.super.getSslBundle();
}

}
Expand Down