Skip to content

Commit

Permalink
Use @EnableConfigServer
Browse files Browse the repository at this point in the history
  • Loading branch information
Dave Syer committed Aug 1, 2014
1 parent 2b2cbf2 commit b0c3642
Show file tree
Hide file tree
Showing 6 changed files with 182 additions and 118 deletions.
Expand Up @@ -94,8 +94,11 @@ public void initialize(ConfigurableApplicationContext applicationContext) {
public ConfigServicePropertySourceLocator configServicePropertySource(
ConfigurableEnvironment environment) {
ConfigServicePropertySourceLocator locator = new ConfigServicePropertySourceLocator();
locator.setEnv(StringUtils.arrayToCommaDelimitedString(environment
.getActiveProfiles()));
String[] profiles = environment.getActiveProfiles();
if (profiles.length==0) {
profiles = environment.getDefaultProfiles();
}
locator.setEnv(StringUtils.arrayToCommaDelimitedString(profiles));
return locator;
}

Expand Down
Expand Up @@ -35,7 +35,7 @@
@ConfigurationProperties("spring.platform.config")
public class ConfigServicePropertySourceLocator implements PropertySourceLocator {

private String env = "development";
private String env = "default";

@Value("${spring.application.name:'application'}")
private String name;
Expand Down
@@ -1,122 +1,11 @@
package org.springframework.platform.config.server;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.io.Resource;
import org.springframework.security.rsa.crypto.KeyStoreKeyFactory;
import org.springframework.security.rsa.crypto.RsaSecretEncryptor;

@Configuration
@ComponentScan
@EnableAutoConfiguration
@EnableConfigServer
public class Application {

public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}

@Configuration
@ConfigurationProperties("encrypt")
protected static class KeyConfiguration {
@Autowired
private EncryptionController controller;

private String key;

private KeyStore keyStore = new KeyStore();

public String getKey() {
return key;
}

public void setKey(String key) {
this.key = key;
}

public KeyStore getKeyStore() {
return keyStore;
}

public void setKeyStore(KeyStore keyStore) {
this.keyStore = keyStore;
}

@PostConstruct
public void init() {
if (keyStore.getLocation() != null) {
controller.setEncryptor(new RsaSecretEncryptor(new KeyStoreKeyFactory(
keyStore.getLocation(), keyStore.getPassword().toCharArray())
.getKeyPair(keyStore.getAlias())));
}
if (key != null) {
controller.uploadKey(key);
}
}

public static class KeyStore {

private Resource location;
private String password;
private String alias;

public String getAlias() {
return alias;
}

public void setAlias(String alias) {
this.alias = alias;
}

public Resource getLocation() {
return location;
}

public void setLocation(Resource location) {
this.location = location;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

}
}

@Configuration
@Profile("native")
protected static class NativeRepositoryConfiguration {
@Autowired
private ConfigurableEnvironment environment;

@Bean
public NativeEnvironmentRepository repository() {
return new NativeEnvironmentRepository(environment);
}
}

@Configuration
@Profile("!native")
protected static class GitRepositoryConfiguration {
@Autowired
private ConfigurableEnvironment environment;

@Bean
@ConfigurationProperties("spring.platform.config.server")
public JGitEnvironmentRepository repository() {
return new JGitEnvironmentRepository(environment);
}
}
}
@@ -0,0 +1,134 @@
/*
* Copyright 2013-2014 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.platform.config.server;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.io.Resource;
import org.springframework.security.rsa.crypto.KeyStoreKeyFactory;
import org.springframework.security.rsa.crypto.RsaSecretEncryptor;

/**
* @author Dave Syer
*
*/
@Configuration
@ComponentScan
public class ConfigServerConfiguration {

@Configuration
@ConfigurationProperties("encrypt")
protected static class KeyConfiguration {
@Autowired
private EncryptionController controller;

private String key;

private KeyStore keyStore = new KeyStore();

public String getKey() {
return key;
}

public void setKey(String key) {
this.key = key;
}

public KeyStore getKeyStore() {
return keyStore;
}

public void setKeyStore(KeyStore keyStore) {
this.keyStore = keyStore;
}

@PostConstruct
public void init() {
if (keyStore.getLocation() != null) {
controller.setEncryptor(new RsaSecretEncryptor(new KeyStoreKeyFactory(
keyStore.getLocation(), keyStore.getPassword().toCharArray())
.getKeyPair(keyStore.getAlias())));
}
if (key != null) {
controller.uploadKey(key);
}
}

public static class KeyStore {

private Resource location;
private String password;
private String alias;

public String getAlias() {
return alias;
}

public void setAlias(String alias) {
this.alias = alias;
}

public Resource getLocation() {
return location;
}

public void setLocation(Resource location) {
this.location = location;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

}
}

@Configuration
@Profile("native")
protected static class NativeRepositoryConfiguration {
@Autowired
private ConfigurableEnvironment environment;

@Bean
public NativeEnvironmentRepository repository() {
return new NativeEnvironmentRepository(environment);
}
}

@Configuration
@Profile("!native")
protected static class GitRepositoryConfiguration {
@Autowired
private ConfigurableEnvironment environment;

@Bean
@ConfigurationProperties("spring.platform.config.server")
public JGitEnvironmentRepository repository() {
return new JGitEnvironmentRepository(environment);
}
}
}
@@ -0,0 +1,36 @@
/*
* Copyright 2013-2014 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.platform.config.server;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.springframework.context.annotation.Import;

/**
* @author Dave Syer
*
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(ConfigServerConfiguration.class)
public @interface EnableConfigServer {

}
Expand Up @@ -30,7 +30,6 @@
import org.eclipse.jgit.api.ListBranchCommand.ListMode;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.transport.TagOpt;
import org.eclipse.jgit.util.FileUtils;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.platform.config.Environment;
Expand Down Expand Up @@ -130,14 +129,17 @@ public Environment findOne(String application, String profile, String label) {
trackBranch(git, checkout, label);
}
else {
git.fetch().setTagOpt(TagOpt.FETCH_TAGS).call();
// works for tags and local branches
checkout.setName(label);
}
Ref ref = checkout.call();
if (git.status().call().isClean() && ref != null) {
// Assumes we are on a tracking branch (should be safe)
git.pull().call();
try {
git.pull().call();
} catch (Exception e) {
logger.warn("Could not pull remote for " + label + " (current ref=" + ref + ")");
}
}
String search = git.getRepository().getDirectory().getParent();
environment.setSearchLocations(search);
Expand Down

0 comments on commit b0c3642

Please sign in to comment.