Adds @ConditionalOnClass for optional server dependencies.

fixes gh-942
This commit is contained in:
Spencer Gibb
2018-03-20 16:29:04 -04:00
parent 3b12bafbdf
commit 96472f77e6
4 changed files with 127 additions and 18 deletions

View File

@@ -17,11 +17,15 @@ package org.springframework.cloud.config.server.config;
import java.util.List;
import java.util.Optional;
import javax.servlet.http.HttpServletRequest;
import org.eclipse.jgit.api.TransportConfigCallback;
import org.tmatesoft.svn.core.SVNException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
@@ -173,8 +177,11 @@ class VaultRepositoryConfiguration {
@Configuration
@Profile("jdbc")
@ConditionalOnClass(JdbcTemplate.class)
class JdbcRepositoryConfiguration {
@Bean
@ConditionalOnBean(JdbcTemplate.class)
public JdbcEnvironmentRepository jdbcEnvironmentRepository(JdbcTemplate jdbc,
JdbcEnvironmentProperties environmentProperties) {
return new JdbcEnvironmentRepositoryFactory(jdbc).build(environmentProperties);
@@ -185,17 +192,25 @@ class JdbcRepositoryConfiguration {
@Profile("composite")
class CompositeRepositoryConfiguration {
@Bean
public MultipleJGitEnvironmentRepositoryFactory gitEnvironmentRepositoryFactory(
ConfigurableEnvironment environment, ConfigServerProperties server,
Optional<TransportConfigCallback> transportConfigCallback) {
return new MultipleJGitEnvironmentRepositoryFactory(environment, server, transportConfigCallback);
@Configuration
@ConditionalOnClass(TransportConfigCallback.class)
static class JGitCompositeConfig {
@Bean
public MultipleJGitEnvironmentRepositoryFactory gitEnvironmentRepositoryFactory(
ConfigurableEnvironment environment, ConfigServerProperties server,
Optional<TransportConfigCallback> transportConfigCallback) {
return new MultipleJGitEnvironmentRepositoryFactory(environment, server, transportConfigCallback);
}
}
@Bean
public SvnEnvironmentRepositoryFactory svnEnvironmentRepositoryFactory(ConfigurableEnvironment environment,
ConfigServerProperties server) {
return new SvnEnvironmentRepositoryFactory(environment, server);
@Configuration
@ConditionalOnClass(SVNException.class)
static class SvnCompositeConfig {
@Bean
public SvnEnvironmentRepositoryFactory svnEnvironmentRepositoryFactory(ConfigurableEnvironment environment,
ConfigServerProperties server) {
return new SvnEnvironmentRepositoryFactory(environment, server);
}
}
@Bean
@@ -204,9 +219,13 @@ class CompositeRepositoryConfiguration {
return new VaultEnvironmentRepositoryFactory(request, watch);
}
@Bean
public JdbcEnvironmentRepositoryFactory jdbcEnvironmentRepositoryFactory(JdbcTemplate jdbc) {
return new JdbcEnvironmentRepositoryFactory(jdbc);
@Configuration
@ConditionalOnClass(JdbcTemplate.class)
static class JdbcCompositeConfig {
@Bean
public JdbcEnvironmentRepositoryFactory jdbcEnvironmentRepositoryFactory(JdbcTemplate jdbc) {
return new JdbcEnvironmentRepositoryFactory(jdbc);
}
}
@Bean

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2015 the original author or authors.
* Copyright 2015-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -20,6 +20,7 @@ import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import org.eclipse.jgit.api.TransportConfigCallback;
import org.eclipse.jgit.transport.*;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.config.server.ssh.PropertyBasedSshSessionFactory;
@@ -34,6 +35,7 @@ import org.springframework.context.annotation.Configuration;
* @author Ollie Hughes
*/
@Configuration
@ConditionalOnClass(TransportConfigCallback.class)
@EnableConfigurationProperties(SshUriProperties.class)
public class TransportConfiguration {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2015 the original author or authors.
* Copyright 2015-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -19,6 +19,7 @@ package org.springframework.cloud.config.server.support;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashSet;
@@ -26,7 +27,6 @@ import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.jgit.util.FileUtils;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.core.env.ConfigurableEnvironment;
@@ -35,6 +35,7 @@ import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.io.UrlResource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.util.FileSystemUtils;
import org.springframework.util.StringUtils;
/**
@@ -103,12 +104,12 @@ public abstract class AbstractScmAccessor implements ResourceLoaderAware {
protected File createBaseDir() {
try {
final File basedir = Files.createTempDirectory("config-repo-").toFile();
final Path basedir = Files.createTempDirectory("config-repo-");
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
try {
FileUtils.delete(basedir, FileUtils.RECURSIVE);
FileSystemUtils.deleteRecursively(basedir);
}
catch (IOException e) {
AbstractScmAccessor.this.logger.warn(
@@ -116,7 +117,7 @@ public abstract class AbstractScmAccessor implements ResourceLoaderAware {
}
}
});
return basedir;
return basedir.toFile();
}
catch (IOException e) {
throw new IllegalStateException("Cannot create temp dir", e);

View File

@@ -0,0 +1,87 @@
/*
* Copyright 2013-2018 the original author or authors.
*
* 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 org.springframework.cloud.config.server;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.runner.WebApplicationContextRunner;
import org.springframework.cloud.config.server.composite.CompositeUtils;
import org.springframework.cloud.test.ClassPathExclusions;
import org.springframework.cloud.test.ModifiedClassPathRunner;
public class CompositeClasspathTests {
@RunWith(ModifiedClassPathRunner.class)
@ClassPathExclusions("spring-jdbc-*.jar")
public static class JdbcTests {
@Test
public void contextLoads() {
new WebApplicationContextRunner()
.withUserConfiguration(ConfigServerApplication.class)
.withPropertyValues("spring.profiles.active:test,composite",
"spring.config.name:compositeconfigserver",
"spring.cloud.config.server.composite[0].uri:file:./target/repos/config-repo",
"spring.cloud.config.server.composite[0].type:git",
"spring.cloud.config.server.composite[1].uri:file:///./target/repos/svn-config-repo",
"spring.cloud.config.server.composite[1].type:svn")
.run(context -> {
CompositeUtils.getCompositeTypeList(context.getEnvironment());
});
}
}
@RunWith(ModifiedClassPathRunner.class)
@ClassPathExclusions("svnkit-*.jar")
public static class SvnTests {
@Test
public void contextLoads() {
new WebApplicationContextRunner()
.withUserConfiguration(ConfigServerApplication.class)
.withPropertyValues("spring.profiles.active:test,composite",
"spring.config.name:compositeconfigserver",
"spring.cloud.config.server.composite[0].uri:file:./target/repos/config-repo",
"spring.cloud.config.server.composite[0].type:git",
"spring.cloud.config.server.composite[1].uri:file:./target/repos/config-repo",
"spring.cloud.config.server.composite[1].type:native")
.run(context -> {
CompositeUtils.getCompositeTypeList(context.getEnvironment());
});
}
}
@RunWith(ModifiedClassPathRunner.class)
@ClassPathExclusions("org.eclipse.jgit-*.jar")
public static class JGitTests {
@Test
public void contextLoads() {
new WebApplicationContextRunner()
.withUserConfiguration(ConfigServerApplication.class)
.withPropertyValues("spring.profiles.active:test,composite",
"spring.config.name:compositeconfigserver",
"spring.cloud.config.server.composite[0].uri:file:///./target/repos/svn-config-repo",
"spring.cloud.config.server.composite[0].type:svn",
"spring.cloud.config.server.composite[1].uri:file:./target/repos/config-repo",
"spring.cloud.config.server.composite[1].type:native")
.run(context -> {
CompositeUtils.getCompositeTypeList(context.getEnvironment());
});
}
}
}