You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tamaya.apache.org by an...@apache.org on 2017/11/20 09:40:41 UTC

incubator-tamaya-extensions git commit: TAMAYA-318 Added config demo feature.

Repository: incubator-tamaya-extensions
Updated Branches:
  refs/heads/master 668053c7c -> d503ef491


TAMAYA-318 Added config demo feature.


Project: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/commit/d503ef49
Tree: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/tree/d503ef49
Diff: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/diff/d503ef49

Branch: refs/heads/master
Commit: d503ef491289cd2a3a0b7841dc6415703ce80cfc
Parents: 668053c
Author: Anatole Tresch <an...@apache.org>
Authored: Mon Nov 20 10:40:30 2017 +0100
Committer: Anatole Tresch <an...@apache.org>
Committed: Mon Nov 20 10:40:30 2017 +0100

----------------------------------------------------------------------
 .../tamaya/springexample/ColorConverter.java    | 41 ++++++++++++
 .../tamaya/springexample/WelcomeController.java | 43 +++++++++++-
 .../org.apache.tamaya.spi.PropertyConverter     | 19 ++++++
 .../src/main/resources/templates/config.ftl     | 29 ++++++++
 .../src/main/resources/templates/welcome.ftl    | 69 ++++++++++++++++++--
 5 files changed, 196 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/d503ef49/examples/05-spring-example/src/main/java/org/apache/tamaya/springexample/ColorConverter.java
----------------------------------------------------------------------
diff --git a/examples/05-spring-example/src/main/java/org/apache/tamaya/springexample/ColorConverter.java b/examples/05-spring-example/src/main/java/org/apache/tamaya/springexample/ColorConverter.java
new file mode 100644
index 0000000..3ec3977
--- /dev/null
+++ b/examples/05-spring-example/src/main/java/org/apache/tamaya/springexample/ColorConverter.java
@@ -0,0 +1,41 @@
+/*
+ * Copyright 2012-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.apache.tamaya.springexample;
+
+import org.apache.tamaya.spi.ConversionContext;
+import org.apache.tamaya.spi.PropertyConverter;
+
+import java.awt.*;
+
+/**
+ * Simple demo converter for Color.
+ */
+public class ColorConverter implements PropertyConverter<Color>{
+
+    @Override
+    public Color convert(String value, ConversionContext context) {
+        if(value.length()<7){
+            return null;
+        }
+        if(!value.startsWith("#")){
+            throw new IllegalArgumentException("Invalid color, format is: #RRGGBB");
+        }
+        int r = Integer.parseInt(value.substring(1,3),8);
+        int g = Integer.parseInt(value.substring(3,5),8);
+        int b = Integer.parseInt(value.substring(5,7),8);
+        return new Color(r, g, b);
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/d503ef49/examples/05-spring-example/src/main/java/org/apache/tamaya/springexample/WelcomeController.java
----------------------------------------------------------------------
diff --git a/examples/05-spring-example/src/main/java/org/apache/tamaya/springexample/WelcomeController.java b/examples/05-spring-example/src/main/java/org/apache/tamaya/springexample/WelcomeController.java
index bbe921f..172c4d5 100644
--- a/examples/05-spring-example/src/main/java/org/apache/tamaya/springexample/WelcomeController.java
+++ b/examples/05-spring-example/src/main/java/org/apache/tamaya/springexample/WelcomeController.java
@@ -15,15 +15,21 @@
  */
 package org.apache.tamaya.springexample;
 
+import java.awt.*;
 import java.util.Date;
 import java.util.Map;
 
+import org.apache.tamaya.Configuration;
+import org.apache.tamaya.ConfigurationProvider;
+import org.apache.tamaya.functions.ConfigurationFunctions;
 import org.apache.tamaya.inject.api.Config;
 import org.apache.tamaya.inject.api.DynamicValue;
 import org.apache.tamaya.inject.api.UpdatePolicy;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Controller;
-import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.*;
+
+import javax.websocket.server.PathParam;
 
 @Controller
 public class WelcomeController {
@@ -37,6 +43,9 @@ public class WelcomeController {
 	@Config(value = "foreground.color", required = false, defaultValue = "#DDDDDD")
 	private DynamicValue<String> foregroundColor;
 
+	@Config(value = "background.color", required = false)
+	private Color bgColor;
+
 	@GetMapping("/")
 	public String welcome(Map<String, Object> model) {
 		foregroundColor.setUpdatePolicy(UpdatePolicy.IMMEDIATE);
@@ -47,4 +56,36 @@ public class WelcomeController {
 		return "welcome";
 	}
 
+	@GetMapping("/update")
+	public String update(@RequestParam("foreground") String newForeground, Map<String, Object> model) {
+		foregroundColor.setUpdatePolicy(UpdatePolicy.IMMEDIATE);
+		if(newForeground!=null){
+			System.out.println("Setting new foreground: " + newForeground+"...");
+			System.setProperty("foreground.color", newForeground);
+		}
+		model.put("time", new Date());
+		model.put("message", this.message);
+		model.put("background", this.backgroundColor);
+		model.put("foreground", this.foregroundColor.get());
+		return "welcome";
+	}
+
+    @GetMapping("/config")
+    public String config(Map<String, Object> model) {
+        Configuration config = ConfigurationProvider.getConfiguration();
+        model.put("filter", "NO FILTER");
+        model.put("config", config
+                    .query(ConfigurationFunctions.textInfo()));
+        return "config";
+    }
+
+    @GetMapping(value="/config/{path}")
+	public String config(@PathVariable("path") String path, Map<String, Object> model) {
+        Configuration config = ConfigurationProvider.getConfiguration();
+        model.put("filter", path);
+        model.put("config", config.with(ConfigurationFunctions.section(path))
+					.query(ConfigurationFunctions.textInfo()));
+		return "config";
+	}
+
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/d503ef49/examples/05-spring-example/src/main/resources/META-INF/services/org.apache.tamaya.spi.PropertyConverter
----------------------------------------------------------------------
diff --git a/examples/05-spring-example/src/main/resources/META-INF/services/org.apache.tamaya.spi.PropertyConverter b/examples/05-spring-example/src/main/resources/META-INF/services/org.apache.tamaya.spi.PropertyConverter
new file mode 100644
index 0000000..f1a29ee
--- /dev/null
+++ b/examples/05-spring-example/src/main/resources/META-INF/services/org.apache.tamaya.spi.PropertyConverter
@@ -0,0 +1,19 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you 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 current 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.
+#
+org.apache.tamaya.springexample.ColorConverter
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/d503ef49/examples/05-spring-example/src/main/resources/templates/config.ftl
----------------------------------------------------------------------
diff --git a/examples/05-spring-example/src/main/resources/templates/config.ftl b/examples/05-spring-example/src/main/resources/templates/config.ftl
new file mode 100644
index 0000000..26c0e03
--- /dev/null
+++ b/examples/05-spring-example/src/main/resources/templates/config.ftl
@@ -0,0 +1,29 @@
+<!DOCTYPE html>
+<!--
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you 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 current 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.
+-->
+<html lang="en">
+
+<body>
+    <h1>Tamaya - Current Configuration</h1>
+    Current Tamaya Configuration, filter applied: ${filter}.<br/>
+    <a href="/">Go back</a><br/>
+    <pre>${config}</pre>
+</body>
+
+</html>
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/d503ef49/examples/05-spring-example/src/main/resources/templates/welcome.ftl
----------------------------------------------------------------------
diff --git a/examples/05-spring-example/src/main/resources/templates/welcome.ftl b/examples/05-spring-example/src/main/resources/templates/welcome.ftl
index 696e4bc..da57937 100644
--- a/examples/05-spring-example/src/main/resources/templates/welcome.ftl
+++ b/examples/05-spring-example/src/main/resources/templates/welcome.ftl
@@ -19,14 +19,75 @@
 -->
 <html lang="en">
 
-<body bgcolor="${background}">
-    <font color="${foreground}}">
+<body>
+    <h1>Tamaya - Spring Boot Example</h1>
+    This example show how Tamaya Configuration can be used with Spring Boot to enable Spring with Tamaya
+    dynamic configuration features and Tamaya Configuration Injection.
+    <h2>Accessing properties programmatically</h2>
+    Configuration properties can be easily accessed with Tamaya's Java API:<br/>
+<pre>
+Configuration config = ConfigurationProvider.getConfiguration();
+String value = config.get("foreground.color");
+</pre>
+   Hereby Tamaya also offers type safe access:<br/>
+<pre>
+Color color = config.get("foreground.color", Color.class);
+</pre>
+    <h2>Annotating properties</h2>
+    Configuration properties on beans can be easily annotated with Tamaya.
+    <ul>
+    <li>You can use default Spring mechanism, e.g.:<br/>
+<pre>
+@Value("$\{application.message\:Hello World}")
+private String message = "Hello World";
+</pre></li>
+    <li>You can use the Tamaya injection API, e.g.<br/>
+<pre>
+@Config(value = "background.color", required = false)
+private String backgroundColor = "#BBBBBB";
+</pre></li>
+    <li>You can also use Tamaya's dynmic configuration features:<br/>
+<pre>
+@Config(value = "foreground.color", required = false, defaultValue = "#DDDDDD")
+private DynamicValue<String> foregroundColor;
+</pre>
+    </li>
+    <li>As with the programmatic API, type safe configuration is supported similarly:<br/>
+<pre>
+@Config(value = "background.color", required = false)<
+<b>private</b> Color bgColor = "#BBBBBB";
+</pre></li>
+    </ul>
+    <h2>Demo: setting the foreground color</h2>
+    This small form allows you to set the foreground color of the following box. For simplicity it
+    uses standard system property, but configuration could also be accessed from any kind of remote or local
+    resource:
+    <p/>
+    <form action="/update">
+       <input type="text" name="foreground" value="${foreground}"/>
+       <input type="submit" name="action-update"/>
+    </form>
+    <br/>
+    </p>
+    <table>
+    <tr><td>
+    <p>
+    Foreground Color: ${foreground}<br/>
+    Background Color: ${background}</p>
+    </td>
+    <td bgcolor="${background}">
+    <font color="${foreground}">
 	Date: ${time?date}
-	<br>
+	<br/>
 	Time: ${time?time}
-	<br>
+	<br/>
 	Message: ${message}
 	</font>
+	</p>
+	</td></tr></table>
+	<h2>Configuration Data</h2>
+	Cou can access the current configuration using the following URL: <a href="/config">/config</a>. <br/>
+	Add	additional path parameter for filtering...
 </body>
 
 </html>
\ No newline at end of file