Use Spring Cloud code conventions.

This commit is contained in:
eidottermihi
2015-02-05 21:46:54 +01:00
parent fcb6200f90
commit d6f3177bfb
6 changed files with 99 additions and 56 deletions

View File

@@ -59,13 +59,15 @@ public abstract class AbstractSCMEnvironmentRepository implements EnvironmentRep
public void run() {
try {
FileUtils.delete(basedir, FileUtils.RECURSIVE);
} catch (IOException e) {
}
catch (IOException e) {
logger.warn("Failed to delete temporary directory on exit: " + e);
}
}
});
return basedir;
} catch (IOException e) {
}
catch (IOException e) {
throw new IllegalStateException("Cannot create temp dir", e);
}
}
@@ -117,7 +119,8 @@ public abstract class AbstractSCMEnvironmentRepository implements EnvironmentRep
if (uri.startsWith("file:")) {
try {
return new UrlResource(StringUtils.cleanPath(uri)).getFile();
} catch (Exception e) {
}
catch (Exception e) {
throw new IllegalStateException("Cannot convert uri to file: " + uri);
}
}
@@ -139,7 +142,8 @@ public abstract class AbstractSCMEnvironmentRepository implements EnvironmentRep
protected Environment clean(Environment value) {
Environment result = new Environment(value.getName(), value.getLabel());
for (PropertySource source : value.getPropertySources()) {
String name = source.getName().replace(getWorkingDirectory().toURI().toString(), "");
String name = source.getName().replace(
getWorkingDirectory().toURI().toString(), "");
if (name.contains(("classpath:/"))) {
continue;
}

View File

@@ -71,16 +71,20 @@ public class JGitEnvironmentRepository extends AbstractSCMEnvironmentRepository
try {
git = createGitClient();
return loadEnvironment(git, application, profile, label);
} catch (GitAPIException e) {
}
catch (GitAPIException e) {
throw new IllegalStateException("Cannot clone repository", e);
} catch (Exception e) {
}
catch (Exception e) {
throw new IllegalStateException("Cannot load environment", e);
} finally {
}
finally {
try {
if (git != null) {
git.getRepository().close();
}
} catch (Exception e) {
}
catch (Exception e) {
logger.warn("Could not close git repository", e);
}
}
@@ -91,8 +95,8 @@ public class JGitEnvironmentRepository extends AbstractSCMEnvironmentRepository
Assert.state(uri != null, "You need to configure a uri for the git repository");
}
private synchronized Environment loadEnvironment(Git git, String application, String profile,
String label) throws GitAPIException {
private synchronized Environment loadEnvironment(Git git, String application,
String profile, String label) throws GitAPIException {
SpringApplicationEnvironmentRepository environment = new SpringApplicationEnvironmentRepository();
git.getRepository().getConfig().setString("branch", label, "merge", label);
Ref ref = checkout(git, label);
@@ -107,7 +111,8 @@ public class JGitEnvironmentRepository extends AbstractSCMEnvironmentRepository
CheckoutCommand checkout = git.checkout();
if (shouldTrack(git, label)) {
trackBranch(git, checkout, label);
} else {
}
else {
// works for tags and local branches
checkout.setName(label);
}
@@ -115,7 +120,8 @@ public class JGitEnvironmentRepository extends AbstractSCMEnvironmentRepository
}
private boolean shouldPull(Git git, Ref ref) throws GitAPIException {
return git.status().call().isClean() && ref != null
return git.status().call().isClean()
&& ref != null
&& git.getRepository().getConfig().getString("remote", "origin", "url") != null;
}
@@ -133,16 +139,23 @@ public class JGitEnvironmentRepository extends AbstractSCMEnvironmentRepository
setCredentialsProvider(pull);
}
pull.call();
} catch (Exception e) {
logger.warn("Could not pull remote for " + label + " (current ref=" + ref + "), remote: "
+ git.getRepository().getConfig().getString("remote", "origin", "url"));
}
catch (Exception e) {
logger.warn("Could not pull remote for "
+ label
+ " (current ref="
+ ref
+ "), remote: "
+ git.getRepository().getConfig()
.getString("remote", "origin", "url"));
}
}
private Git createGitClient() throws IOException, GitAPIException {
if (new File(basedir, ".git").exists()) {
return openGitRepository();
} else {
}
else {
return copyRepository();
}
}
@@ -152,7 +165,8 @@ public class JGitEnvironmentRepository extends AbstractSCMEnvironmentRepository
Assert.state(basedir.mkdirs(), "Could not create basedir: " + basedir);
if (uri.startsWith("file:")) {
return copyFromLocalRepository();
} else {
}
else {
return cloneToBasedir();
}
}
@@ -189,7 +203,8 @@ public class JGitEnvironmentRepository extends AbstractSCMEnvironmentRepository
setCredentialsProvider(fetch);
}
fetch.call();
} catch (Exception e) {
}
catch (Exception e) {
logger.warn("Remote repository not available");
}
}
@@ -198,7 +213,8 @@ public class JGitEnvironmentRepository extends AbstractSCMEnvironmentRepository
if (basedir.exists()) {
try {
FileUtils.delete(basedir, FileUtils.RECURSIVE);
} catch (IOException e) {
}
catch (IOException e) {
throw new IllegalStateException("Failed to initialize base directory", e);
}
}
@@ -217,11 +233,13 @@ public class JGitEnvironmentRepository extends AbstractSCMEnvironmentRepository
}
private void setCredentialsProvider(TransportCommand<?, ?> cmd) {
cmd.setCredentialsProvider(new UsernamePasswordCredentialsProvider(username, password));
cmd.setCredentialsProvider(new UsernamePasswordCredentialsProvider(username,
password));
}
private void trackBranch(Git git, CheckoutCommand checkout, String label) {
checkout.setCreateBranch(true).setName(label).setUpstreamMode(SetupUpstreamMode.TRACK)
checkout.setCreateBranch(true).setName(label)
.setUpstreamMode(SetupUpstreamMode.TRACK)
.setStartPoint("origin/" + label);
}
@@ -233,7 +251,8 @@ public class JGitEnvironmentRepository extends AbstractSCMEnvironmentRepository
return containsBranch(git, label, null);
}
private boolean containsBranch(Git git, String label, ListMode listMode) throws GitAPIException {
private boolean containsBranch(Git git, String label, ListMode listMode)
throws GitAPIException {
ListBranchCommand command = git.branchList();
if (listMode != null) {
command.setListMode(listMode);

View File

@@ -49,32 +49,37 @@ public class SVNKitEnvironmentRepository extends AbstractSCMEnvironmentRepositor
public Environment findOne(String application, String profile, String label) {
SvnOperationFactory svnOperationFactory = new SvnOperationFactory();
if (hasText(getUsername())) {
svnOperationFactory.setAuthenticationManager(new DefaultSVNAuthenticationManager(null, false,
getUsername(), getPassword()));
svnOperationFactory
.setAuthenticationManager(new DefaultSVNAuthenticationManager(null,
false, getUsername(), getPassword()));
}
try {
if (new File(getWorkingDirectory(), ".svn").exists()) {
update(svnOperationFactory);
} else {
}
else {
checkout(svnOperationFactory);
}
return clean(loadEnvironment(application, profile, label));
} catch (SVNException e) {
}
catch (SVNException e) {
throw new IllegalStateException("Cannot checkout repository", e);
} finally {
}
finally {
svnOperationFactory.dispose();
}
}
private Environment loadEnvironment(String application, String profile, String label) {
final SpringApplicationEnvironmentRepository environmentRepository = new SpringApplicationEnvironmentRepository();
environmentRepository.setSearchLocations(getSearchLocations(getSvnPath(getWorkingDirectory(),
label)));
environmentRepository.setSearchLocations(getSearchLocations(getSvnPath(
getWorkingDirectory(), label)));
return environmentRepository.findOne(application, profile, label);
}
private void checkout(SvnOperationFactory svnOperationFactory) throws SVNException {
logger.debug("Checking out " + getUri() + " to: " + getWorkingDirectory().getAbsolutePath());
logger.debug("Checking out " + getUri() + " to: "
+ getWorkingDirectory().getAbsolutePath());
final SvnCheckout checkout = svnOperationFactory.createCheckout();
checkout.setSource(SvnTarget.fromURL(SVNURL.parseURIEncoded(getUri())));
checkout.setSingleTarget(SvnTarget.fromFile(getWorkingDirectory()));
@@ -90,9 +95,9 @@ public class SVNKitEnvironmentRepository extends AbstractSCMEnvironmentRepositor
@Override
public void afterPropertiesSet() throws Exception {
Assert
.state(getUri() != null,
"You need to configure a uri for the subversion repository (e.g. 'http://example.com/svn/')");
Assert.state(
getUri() != null,
"You need to configure a uri for the subversion repository (e.g. 'http://example.com/svn/')");
resolveRelativeFileUri();
}

View File

@@ -69,10 +69,11 @@ public class SVNKitEnvironmentRepositoryIntegrationTests {
@Test
public void vanilla() throws Exception {
String uri = ConfigServerTestUtils.prepareLocalSvnRepo("src/test/resources/svn-config-repo",
"target/config");
String uri = ConfigServerTestUtils.prepareLocalSvnRepo(
"src/test/resources/svn-config-repo", "target/config");
context = new SpringApplicationBuilder(TestConfiguration.class).web(false)
.profiles("subversion").run("--spring.cloud.config.server.svn.uri=" + uri);
.profiles("subversion")
.run("--spring.cloud.config.server.svn.uri=" + uri);
EnvironmentRepository repository = context.getBean(EnvironmentRepository.class);
repository.findOne("bar", "staging", "trunk");
Environment environment = repository.findOne("bar", "staging", "trunk");
@@ -81,21 +82,24 @@ public class SVNKitEnvironmentRepositoryIntegrationTests {
@Test
public void update() throws Exception {
String uri = ConfigServerTestUtils.prepareLocalSvnRepo("src/test/resources/svn-config-repo",
"target/config");
String uri = ConfigServerTestUtils.prepareLocalSvnRepo(
"src/test/resources/svn-config-repo", "target/config");
context = new SpringApplicationBuilder(TestConfiguration.class).web(false)
.profiles("subversion").run("--spring.cloud.config.server.svn.uri=" + uri);
.profiles("subversion")
.run("--spring.cloud.config.server.svn.uri=" + uri);
EnvironmentRepository repository = context.getBean(EnvironmentRepository.class);
repository.findOne("bar", "staging", "trunk");
Environment environment = repository.findOne("bar", "staging", "trunk");
assertEquals("bar", environment.getPropertySources().get(0).getSource().get("foo"));
assertEquals("bar", environment.getPropertySources().get(0).getSource()
.get("foo"));
updateRepoForUpdate(uri);
environment = repository.findOne("bar", "staging", "trunk");
assertEquals("foo", environment.getPropertySources().get(0).getSource().get("foo"));
assertEquals("foo", environment.getPropertySources().get(0).getSource()
.get("foo"));
}
private void updateRepoForUpdate(String uri) throws SVNException, FileNotFoundException,
IOException {
private void updateRepoForUpdate(String uri) throws SVNException,
FileNotFoundException, IOException {
SvnOperationFactory svnFactory = new SvnOperationFactory();
final SvnCheckout checkout = svnFactory.createCheckout();
checkout.setSource(SvnTarget.fromURL(SVNURL.parseURIEncoded(uri)));
@@ -104,7 +108,8 @@ public class SVNKitEnvironmentRepositoryIntegrationTests {
// update bar.properties
File barProps = new File(workingDir, "trunk/bar.properties");
StreamUtils.copy("foo: foo", Charset.defaultCharset(), new FileOutputStream(barProps));
StreamUtils.copy("foo: foo", Charset.defaultCharset(), new FileOutputStream(
barProps));
// commit to repo
SvnCommit svnCommit = svnFactory.createCommit();
svnCommit.setCommitMessage("update bar.properties");

View File

@@ -35,14 +35,15 @@ import static org.junit.Assert.assertEquals;
public class SVNKitEnvironmentRepositoryTests {
private StandardEnvironment environment = new StandardEnvironment();
private SVNKitEnvironmentRepository repository = new SVNKitEnvironmentRepository(environment);
private SVNKitEnvironmentRepository repository = new SVNKitEnvironmentRepository(
environment);
private File basedir = new File("target/config");
@Before
public void init() throws Exception {
String uri = ConfigServerTestUtils.prepareLocalSvnRepo("src/test/resources/svn-config-repo",
"target/repos/svn-config-repo");
String uri = ConfigServerTestUtils.prepareLocalSvnRepo(
"src/test/resources/svn-config-repo", "target/repos/svn-config-repo");
repository.setUri(uri);
if (basedir.exists()) {
FileUtils.delete(basedir, FileUtils.RECURSIVE | FileUtils.RETRY);
@@ -53,8 +54,10 @@ public class SVNKitEnvironmentRepositoryTests {
public void vanilla() {
Environment environment = repository.findOne("bar", "staging", "trunk");
assertEquals(2, environment.getPropertySources().size());
assertTrue(environment.getPropertySources().get(0).getName().contains("bar.properties"));
assertTrue(environment.getPropertySources().get(1).getName().contains("application.yml"));
assertTrue(environment.getPropertySources().get(0).getName()
.contains("bar.properties"));
assertTrue(environment.getPropertySources().get(1).getName()
.contains("application.yml"));
}
@Test
@@ -62,15 +65,19 @@ public class SVNKitEnvironmentRepositoryTests {
repository.setBasedir(basedir);
Environment environment = repository.findOne("bar", "staging", "trunk");
assertEquals(2, environment.getPropertySources().size());
assertTrue(environment.getPropertySources().get(0).getName().contains("bar.properties"));
assertTrue(environment.getPropertySources().get(1).getName().contains("application.yml"));
assertTrue(environment.getPropertySources().get(0).getName()
.contains("bar.properties"));
assertTrue(environment.getPropertySources().get(1).getName()
.contains("application.yml"));
}
@Test
public void branch() {
Environment environment = repository.findOne("bar", "staging", "branches/demobranch");
Environment environment = repository.findOne("bar", "staging",
"branches/demobranch");
assertEquals(1, environment.getPropertySources().size());
assertTrue(environment.getPropertySources().get(0).getName().contains("bar.properties"));
assertTrue(environment.getPropertySources().get(0).getName()
.contains("bar.properties"));
}
@Test
@@ -78,8 +85,10 @@ public class SVNKitEnvironmentRepositoryTests {
repository.findOne("bar", "staging", "trunk");
Environment environment = repository.findOne("bar", "staging", "trunk");
assertEquals(2, environment.getPropertySources().size());
assertTrue(environment.getPropertySources().get(0).getName().contains("bar.properties"));
assertTrue(environment.getPropertySources().get(1).getName().contains("application.yml"));
assertTrue(environment.getPropertySources().get(0).getName()
.contains("bar.properties"));
assertTrue(environment.getPropertySources().get(1).getName()
.contains("application.yml"));
}
}

View File

@@ -17,7 +17,8 @@ import static org.junit.Assert.assertFalse;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = ConfigServerApplication.class)
@IntegrationTest({"server.port:0", "spring.config.name:configserver", "spring.cloud.config.server.svn.uri:file:///./target/repos/svn-config-repo"})
@IntegrationTest({ "server.port:0", "spring.config.name:configserver",
"spring.cloud.config.server.svn.uri:file:///./target/repos/svn-config-repo" })
@WebAppConfiguration
@ActiveProfiles("subversion")
public class SubversionConfigServerIntegrationTests {