diff --git a/.gitignore b/.gitignore index 039f0ae..6d61356 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,6 @@ +.classpath +.project +.settings target/ pom.xml.tag pom.xml.releaseBackup diff --git a/pom.xml b/pom.xml index 8ee6a42..186d24b 100644 --- a/pom.xml +++ b/pom.xml @@ -9,7 +9,7 @@ http://maven.apache.org 1.7 - 4.1.1.RELEASE + 4.2.9.RELEASE 1.2 4.11 1.0.13 @@ -37,6 +37,12 @@ + + org.springframework.cloud + spring-cloud-config-client + 1.2.0.RELEASE + + org.slf4j jcl-over-slf4j @@ -75,6 +81,14 @@ provided + + HelloWorld @@ -114,6 +128,12 @@ + + org.eclipse.jetty + jetty-maven-plugin + 9.4.3.v20170317 + + diff --git a/src/main/java/com/zenika/controller/BaseController.java b/src/main/java/com/zenika/controller/BaseController.java index 7a9af82..cd960be 100644 --- a/src/main/java/com/zenika/controller/BaseController.java +++ b/src/main/java/com/zenika/controller/BaseController.java @@ -1,6 +1,8 @@ package com.zenika.controller; import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.core.env.Environment; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.PathVariable; @@ -9,31 +11,40 @@ @Controller public class BaseController { - - private static int counter = 0; - private static final String VIEW_INDEX = "index"; - private final static org.slf4j.Logger logger = LoggerFactory.getLogger(BaseController.class); - - @RequestMapping(value = "/", method = RequestMethod.GET) - public String welcome(ModelMap model) { - - model.addAttribute("message", "Welcome"); - model.addAttribute("counter", ++counter); - logger.debug("[welcome] counter : {}", counter); - - // Spring uses InternalResourceViewResolver and return back index.jsp - return VIEW_INDEX; - - } - - @RequestMapping(value = "/{name}", method = RequestMethod.GET) - public String welcomeName(@PathVariable String name, ModelMap model) { - - model.addAttribute("message", "Welcome " + name); - model.addAttribute("counter", ++counter); - logger.debug("[welcomeName] counter : {}", counter); - return VIEW_INDEX; - - } - + + private static int counter = 0; + private static final String VIEW_INDEX = "index"; + private final static org.slf4j.Logger logger = LoggerFactory.getLogger(BaseController.class); + + @Autowired + private GithubProperties properties; + + @Autowired + private MyPropeties myProperties; + + @Autowired + Environment env; + + @RequestMapping(value = "/", method = RequestMethod.GET) + public String welcome(ModelMap model) { + + model.addAttribute("message", "Welcome github prop=" + properties.getCloneBaseDir() + " envProp=" + env.getProperty("github.cloneBaseDir") + " AND MORE MORE MORE beanProp=" + myProperties.getToken()); + model.addAttribute("counter", ++counter); + logger.debug("[welcome] counter : {}", counter); + + // Spring uses InternalResourceViewResolver and return back index.jsp + return VIEW_INDEX; + + } + + @RequestMapping(value = "/{name}", method = RequestMethod.GET) + public String welcomeName(@PathVariable String name, ModelMap model) { + + model.addAttribute("message", "Welcome " + name); + model.addAttribute("counter", ++counter); + logger.debug("[welcomeName] counter : {}", counter); + return VIEW_INDEX; + + } + } diff --git a/src/main/java/com/zenika/controller/GithubProperties.java b/src/main/java/com/zenika/controller/GithubProperties.java new file mode 100644 index 0000000..36ca71c --- /dev/null +++ b/src/main/java/com/zenika/controller/GithubProperties.java @@ -0,0 +1,39 @@ +package com.zenika.controller; + +import javax.annotation.PostConstruct; + +import org.springframework.boot.context.properties.ConfigurationProperties; + +@ConfigurationProperties(prefix = "github") +public class GithubProperties { + + /** + * Oauth token used for authenticating with GitHub + */ + private String token; + /** + * Root dir path to clone the repos + */ + private String cloneBaseDir; + + public String getToken() { + return token; + } + + public void setToken(String token) { + this.token = token; + } + + public String getCloneBaseDir() { + return cloneBaseDir; + } + + public void setCloneBaseDir(String cloneDir) { + this.cloneBaseDir = cloneDir; + } + + @PostConstruct + public void loadedProperties() { + System.out.println("Github Properties are " + token + " with " + cloneBaseDir); + } +} diff --git a/src/main/java/com/zenika/controller/MyPropeties.java b/src/main/java/com/zenika/controller/MyPropeties.java new file mode 100644 index 0000000..df43f76 --- /dev/null +++ b/src/main/java/com/zenika/controller/MyPropeties.java @@ -0,0 +1,42 @@ +package com.zenika.controller; + +import javax.annotation.PostConstruct; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; + +@Component +public class MyPropeties { + + /** + * Oauth token used for authenticating with GitHub + */ + @Value("${github.token}") + private String token; + /** + * Root dir path to clone the repos + */ + @Value("${github.cloneBaseDir}") + private String cloneBaseDir; + + public String getToken() { + return token; + } + + public void setToken(String token) { + this.token = token; + } + + public String getCloneBaseDir() { + return cloneBaseDir; + } + + public void setCloneBaseDir(String cloneDir) { + this.cloneBaseDir = cloneDir; + } + + @PostConstruct + public void loadedProperties() { + System.out.println("My Properties are: " + token + " with " + cloneBaseDir); + } +} diff --git a/src/main/java/com/zenika/controller/config/CloudEnvironment.java b/src/main/java/com/zenika/controller/config/CloudEnvironment.java new file mode 100644 index 0000000..9df1cce --- /dev/null +++ b/src/main/java/com/zenika/controller/config/CloudEnvironment.java @@ -0,0 +1,41 @@ +package com.zenika.controller.config; + +import org.springframework.cloud.config.client.ConfigClientProperties; +import org.springframework.cloud.config.client.ConfigServicePropertySourceLocator; +import org.springframework.core.env.Environment; +import org.springframework.core.env.MutablePropertySources; +import org.springframework.core.env.PropertySource; +import org.springframework.web.context.support.StandardServletEnvironment; + +public class CloudEnvironment extends StandardServletEnvironment { + + @Override + protected void customizePropertySources(MutablePropertySources propertySources) { + super.customizePropertySources(propertySources); + try { + PropertySource source = initConfigServicePropertySourceLocator(this); + propertySources.addLast(source); + + } catch ( + + Exception ex) { + logger.warn("failed to initialize cloud config environment", ex); + } + } + + private PropertySource initConfigServicePropertySourceLocator(Environment environment) { + + ConfigClientProperties configClientProperties = new ConfigClientProperties(environment); + configClientProperties.setUri("http://localhost:8888"); + configClientProperties.setName("publisher"); + configClientProperties.setLabel("master"); + + System.out.println("##################### will load the client configuration"); + System.out.println(configClientProperties); + + ConfigServicePropertySourceLocator configServicePropertySourceLocator = + new ConfigServicePropertySourceLocator(configClientProperties); + + return configServicePropertySourceLocator.locate(environment); + } +} diff --git a/src/main/java/com/zenika/controller/config/PropertiesConfigurer.java b/src/main/java/com/zenika/controller/config/PropertiesConfigurer.java new file mode 100644 index 0000000..b4ccffb --- /dev/null +++ b/src/main/java/com/zenika/controller/config/PropertiesConfigurer.java @@ -0,0 +1,18 @@ +package com.zenika.controller.config; + +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.support.PropertySourcesPlaceholderConfigurer; + +import com.zenika.controller.GithubProperties; + +@Configuration +@EnableConfigurationProperties({GithubProperties.class}) +public class PropertiesConfigurer { + + @Bean + public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() { + return new PropertySourcesPlaceholderConfigurer(); + } +} diff --git a/src/main/java/com/zenika/controller/config/SpringCloudConfigContext.java b/src/main/java/com/zenika/controller/config/SpringCloudConfigContext.java new file mode 100644 index 0000000..e6965c0 --- /dev/null +++ b/src/main/java/com/zenika/controller/config/SpringCloudConfigContext.java @@ -0,0 +1,12 @@ +package com.zenika.controller.config; + +import org.springframework.core.env.ConfigurableEnvironment; +import org.springframework.web.context.support.XmlWebApplicationContext; + +public class SpringCloudConfigContext extends XmlWebApplicationContext { + @Override + protected ConfigurableEnvironment createEnvironment() { + System.out.println("##################### loaded my comfigurable context"); + return new CloudEnvironment(); + } +} diff --git a/src/main/resources/logback.xml b/src/main/resources/logback.xml index a15ae41..786458c 100644 --- a/src/main/resources/logback.xml +++ b/src/main/resources/logback.xml @@ -15,6 +15,11 @@ additivity="false"> + + + + diff --git a/src/main/webapp/WEB-INF/web.xml b/src/main/webapp/WEB-INF/web.xml index 3db2df2..1223169 100644 --- a/src/main/webapp/WEB-INF/web.xml +++ b/src/main/webapp/WEB-INF/web.xml @@ -18,15 +18,21 @@ mvc-dispatcher / + + + org.springframework.web.context.ContextLoaderListener + + + + contextClass + com.zenika.controller.config.SpringCloudConfigContext + + + contextConfigLocation /WEB-INF/mvc-dispatcher-servlet.xml - - - - org.springframework.web.context.ContextLoaderListener - - + \ No newline at end of file