Allow ConnectionDetailsFactories to use context class loader
Update `ConnectionDetailsFactories` so that the context classloader can be used to load factories. See gh-45014 Signed-off-by: lengors <24527258+lengors@users.noreply.github.com>
This commit is contained in:
@@ -46,6 +46,11 @@ public class DockerComposeProperties {
|
||||
*/
|
||||
private boolean enabled = true;
|
||||
|
||||
/**
|
||||
* Whether to try to use the context class loader for connection details factories.
|
||||
*/
|
||||
private boolean useContextClassLoader = false;
|
||||
|
||||
/**
|
||||
* Arguments to pass to the Docker Compose command.
|
||||
*/
|
||||
@@ -89,10 +94,18 @@ public class DockerComposeProperties {
|
||||
return this.enabled;
|
||||
}
|
||||
|
||||
public boolean isUseContextClassLoader() {
|
||||
return this.useContextClassLoader;
|
||||
}
|
||||
|
||||
public void setEnabled(boolean enabled) {
|
||||
this.enabled = enabled;
|
||||
}
|
||||
|
||||
public void setUseContextClassLoader(boolean useContextClassLoader) {
|
||||
this.useContextClassLoader = useContextClassLoader;
|
||||
}
|
||||
|
||||
public List<String> getArguments() {
|
||||
return this.arguments;
|
||||
}
|
||||
@@ -137,7 +150,7 @@ public class DockerComposeProperties {
|
||||
return this.readiness;
|
||||
}
|
||||
|
||||
static DockerComposeProperties get(Binder binder) {
|
||||
public static DockerComposeProperties get(Binder binder) {
|
||||
return binder.bind(NAME, DockerComposeProperties.class).orElseGet(DockerComposeProperties::new);
|
||||
}
|
||||
|
||||
|
||||
@@ -27,7 +27,9 @@ import org.springframework.beans.factory.support.RootBeanDefinition;
|
||||
import org.springframework.boot.autoconfigure.container.ContainerImageMetadata;
|
||||
import org.springframework.boot.autoconfigure.service.connection.ConnectionDetails;
|
||||
import org.springframework.boot.autoconfigure.service.connection.ConnectionDetailsFactories;
|
||||
import org.springframework.boot.context.properties.bind.Binder;
|
||||
import org.springframework.boot.docker.compose.core.RunningService;
|
||||
import org.springframework.boot.docker.compose.lifecycle.DockerComposeProperties;
|
||||
import org.springframework.boot.docker.compose.lifecycle.DockerComposeServicesReadyEvent;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationListener;
|
||||
@@ -46,32 +48,30 @@ import org.springframework.util.StringUtils;
|
||||
class DockerComposeServiceConnectionsApplicationListener
|
||||
implements ApplicationListener<DockerComposeServicesReadyEvent> {
|
||||
|
||||
private final ConnectionDetailsFactories factories;
|
||||
|
||||
DockerComposeServiceConnectionsApplicationListener() {
|
||||
this(new ConnectionDetailsFactories());
|
||||
}
|
||||
|
||||
DockerComposeServiceConnectionsApplicationListener(ConnectionDetailsFactories factories) {
|
||||
this.factories = factories;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onApplicationEvent(DockerComposeServicesReadyEvent event) {
|
||||
ApplicationContext applicationContext = event.getSource();
|
||||
if (applicationContext instanceof BeanDefinitionRegistry registry) {
|
||||
Binder binder = Binder.get(applicationContext.getEnvironment());
|
||||
DockerComposeProperties properties = DockerComposeProperties.get(binder);
|
||||
boolean useContextClassLoader = properties.isUseContextClassLoader();
|
||||
ConnectionDetailsFactories factories = new ConnectionDetailsFactories(useContextClassLoader);
|
||||
Environment environment = applicationContext.getEnvironment();
|
||||
registerConnectionDetails(registry, environment, event.getRunningServices());
|
||||
registerConnectionDetails(registry, environment, event.getRunningServices(), factories);
|
||||
}
|
||||
}
|
||||
|
||||
private void registerConnectionDetails(BeanDefinitionRegistry registry, Environment environment,
|
||||
List<RunningService> runningServices) {
|
||||
List<RunningService> runningServices, ConnectionDetailsFactories factories) {
|
||||
for (RunningService runningService : runningServices) {
|
||||
DockerComposeConnectionSource source = new DockerComposeConnectionSource(runningService, environment);
|
||||
this.factories.getConnectionDetails(source, false).forEach((connectionDetailsType, connectionDetails) -> {
|
||||
factories.getConnectionDetails(source, false).forEach((connectionDetailsType, connectionDetails) -> {
|
||||
register(registry, runningService, connectionDetailsType, connectionDetails);
|
||||
this.factories.getConnectionDetails(connectionDetails, false)
|
||||
factories.getConnectionDetails(connectionDetails, false)
|
||||
.forEach((adaptedType, adaptedDetails) -> register(registry, runningService, adaptedType,
|
||||
adaptedDetails));
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user