You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tamaya.apache.org by pl...@apache.org on 2015/01/06 07:37:42 UTC
incubator-tamaya git commit: TAMAYA-39 First early steps for the JSON
integration.
Repository: incubator-tamaya
Updated Branches:
refs/heads/master b2f764f9c -> a83ba18f1
TAMAYA-39 First early steps for the JSON integration.
Project: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/commit/a83ba18f
Tree: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/tree/a83ba18f
Diff: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/diff/a83ba18f
Branch: refs/heads/master
Commit: a83ba18f101dde79745ded3112f3ba24c8f4024a
Parents: b2f764f
Author: Oliver B. Fischer <pl...@apache.org>
Authored: Tue Jan 6 07:32:01 2015 +0100
Committer: Oliver B. Fischer <pl...@apache.org>
Committed: Tue Jan 6 07:32:01 2015 +0100
----------------------------------------------------------------------
extras/json/pom.xml | 63 +++++++++++
.../tamaya/extras/json/FileBasedResource.java | 50 +++++++++
.../tamaya/extras/json/InputResource.java | 27 +++++
.../tamaya/extras/json/JSONPropertySource.java | 104 +++++++++++++++++++
.../apache/tamaya/extras/json/JSONVisitor.java | 76 ++++++++++++++
.../resources/configs/valid/simple-config.json | 5 +
extras/pom.xml | 41 ++++++++
pom.xml | 8 ++
8 files changed, 374 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/a83ba18f/extras/json/pom.xml
----------------------------------------------------------------------
diff --git a/extras/json/pom.xml b/extras/json/pom.xml
new file mode 100644
index 0000000..7dd3298
--- /dev/null
+++ b/extras/json/pom.xml
@@ -0,0 +1,63 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+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 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.
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+
+ <parent>
+ <groupId>org.apache.tamaya</groupId>
+ <artifactId>extras</artifactId>
+ <version>0.2-SNAPSHOT</version>
+ </parent>
+
+ <artifactId>json</artifactId>
+ <name>Apache Tamaya JSON Support</name>
+ <inceptionYear>2015</inceptionYear>
+
+
+ <dependencies>
+ <dependency>
+ <groupId>org.apache.tamaya</groupId>
+ <artifactId>tamaya-api</artifactId>
+ <version>${project.version}</version>
+ </dependency>
+
+ <dependency>
+ <groupId>com.fasterxml.jackson.core</groupId>
+ <artifactId>jackson-databind</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.hamcrest</groupId>
+ <artifactId>hamcrest-library</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.hamcrest</groupId>
+ <artifactId>hamcrest-core</artifactId>
+ </dependency>
+
+
+ </dependencies>
+
+</project>
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/a83ba18f/extras/json/src/main/java/org/apache/tamaya/extras/json/FileBasedResource.java
----------------------------------------------------------------------
diff --git a/extras/json/src/main/java/org/apache/tamaya/extras/json/FileBasedResource.java b/extras/json/src/main/java/org/apache/tamaya/extras/json/FileBasedResource.java
new file mode 100644
index 0000000..1ba1306
--- /dev/null
+++ b/extras/json/src/main/java/org/apache/tamaya/extras/json/FileBasedResource.java
@@ -0,0 +1,50 @@
+/*
+ * 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 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.extras.json;
+
+import org.apache.tamaya.ConfigException;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+
+public class FileBasedResource implements InputResource {
+ private final File sourceFile;
+
+ public FileBasedResource(File file) {
+ sourceFile = file;
+ }
+
+
+ @Override
+ public InputStream getInputStream() {
+ try {
+ return new FileInputStream(sourceFile);
+ } catch (IOException ioe) {
+ String msg = String.format("Failed to open file %s", getDescription());
+ throw new ConfigException(msg, ioe);
+ }
+ }
+
+ @Override
+ public CharSequence getDescription() {
+ return sourceFile.toURI().toString();
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/a83ba18f/extras/json/src/main/java/org/apache/tamaya/extras/json/InputResource.java
----------------------------------------------------------------------
diff --git a/extras/json/src/main/java/org/apache/tamaya/extras/json/InputResource.java b/extras/json/src/main/java/org/apache/tamaya/extras/json/InputResource.java
new file mode 100644
index 0000000..4868689
--- /dev/null
+++ b/extras/json/src/main/java/org/apache/tamaya/extras/json/InputResource.java
@@ -0,0 +1,27 @@
+/*
+ * 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 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.extras.json;
+
+import java.io.InputStream;
+
+public interface InputResource {
+ InputStream getInputStream();
+
+ CharSequence getDescription();
+}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/a83ba18f/extras/json/src/main/java/org/apache/tamaya/extras/json/JSONPropertySource.java
----------------------------------------------------------------------
diff --git a/extras/json/src/main/java/org/apache/tamaya/extras/json/JSONPropertySource.java b/extras/json/src/main/java/org/apache/tamaya/extras/json/JSONPropertySource.java
new file mode 100644
index 0000000..6950f2c
--- /dev/null
+++ b/extras/json/src/main/java/org/apache/tamaya/extras/json/JSONPropertySource.java
@@ -0,0 +1,104 @@
+/*
+ * 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 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.extras.json;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import org.apache.tamaya.ConfigException;
+import org.apache.tamaya.spi.PropertySource;
+
+import java.io.File;
+import java.io.InputStream;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Optional;
+
+import static java.lang.String.format;
+
+public class JSONPropertySource
+ implements PropertySource {
+ private int priority = 0;
+ private InputResource source;
+ private HashMap<String, String> values;
+
+ public JSONPropertySource(File file, int priority) {
+ this.priority = priority;
+ source = new FileBasedResource(file);
+ }
+
+ @Override
+ public int getOrdinal() {
+ return priority;
+ }
+
+ @Override
+ public String getName() {
+ // @todo Implement me
+ throw new RuntimeException("Not implemented yet.");
+ }
+
+ @Override
+ public Optional<String> get(String key) {
+ Objects.requireNonNull(key, "Key must not be null");
+
+ return Optional.ofNullable(getProperties().get(key));
+ }
+
+ @Override
+ public Map<String, String> getProperties() {
+ synchronized (this) {
+ if (values == null) {
+ readSource();
+ }
+ }
+
+ return Collections.unmodifiableMap(values);
+ }
+
+ protected void readSource() {
+ try (InputStream is = source.getInputStream()) {
+ ObjectMapper mapper = new ObjectMapper();
+ JsonNode root = mapper.readTree(is);
+
+ // @todo Add test for this. Oliver B. Fischer, 5. Jan. 2015
+ if (!(root instanceof ObjectNode)) {
+ throw new ConfigException("Currently only JSON objects are supported");
+ }
+
+ HashMap<String, String> values = new HashMap<>();
+ JSONVisitor visitor = new JSONVisitor((ObjectNode) root, values);
+ visitor.run();
+
+ this.values = values;
+ }
+ catch (Throwable t) {
+ throw new ConfigException(format("Failed to read properties from %s", source.getDescription()));
+ }
+
+ }
+
+ @Override
+ public boolean isScannable() {
+ // @todo Implement me
+ throw new RuntimeException("Not implemented yet.");
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/a83ba18f/extras/json/src/main/java/org/apache/tamaya/extras/json/JSONVisitor.java
----------------------------------------------------------------------
diff --git a/extras/json/src/main/java/org/apache/tamaya/extras/json/JSONVisitor.java b/extras/json/src/main/java/org/apache/tamaya/extras/json/JSONVisitor.java
new file mode 100644
index 0000000..fc929e1
--- /dev/null
+++ b/extras/json/src/main/java/org/apache/tamaya/extras/json/JSONVisitor.java
@@ -0,0 +1,76 @@
+/*
+ * 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 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.extras.json;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import com.fasterxml.jackson.databind.node.ValueNode;
+import org.apache.tamaya.ConfigException;
+
+import java.util.*;
+
+public class JSONVisitor {
+ private final ObjectNode rootNode;
+ private final HashMap<String, String> targetStore;
+
+ public JSONVisitor(ObjectNode startNode, HashMap<String, String> target) {
+ rootNode = startNode;
+ targetStore = target;
+ }
+
+ public void run() {
+ Queue<VisitingContext> stack = new ArrayDeque<>();
+
+ stack.add(new VisitingContext(rootNode));
+
+ while(!stack.peek().completed()) {
+ Map.Entry<String, JsonNode> current = stack.peek().nextElement();
+
+ if (current.getValue() instanceof ValueNode) {
+ System.out.println("I:" + current.getValue().asText());
+ } else {
+ // @todo
+ throw new ConfigException("");
+ }
+
+ System.out.println(current);
+ }
+
+
+ }
+
+ private class VisitingContext {
+ private final ObjectNode node;
+ private final Iterator<Map.Entry<String, JsonNode>> elements;
+
+ public VisitingContext(ObjectNode rootNode) {
+ node = rootNode;
+ elements = node.fields();
+ }
+
+ public Map.Entry<String, JsonNode> nextElement() {
+ return elements.next();
+ }
+
+
+ public boolean completed() {
+ return elements.hasNext() == false;
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/a83ba18f/extras/json/src/test/resources/configs/valid/simple-config.json
----------------------------------------------------------------------
diff --git a/extras/json/src/test/resources/configs/valid/simple-config.json b/extras/json/src/test/resources/configs/valid/simple-config.json
new file mode 100644
index 0000000..c8ea179
--- /dev/null
+++ b/extras/json/src/test/resources/configs/valid/simple-config.json
@@ -0,0 +1,5 @@
+{
+ "a" : "A",
+ "b" : "B",
+ "c" : "C"
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/a83ba18f/extras/pom.xml
----------------------------------------------------------------------
diff --git a/extras/pom.xml b/extras/pom.xml
new file mode 100644
index 0000000..ff81857
--- /dev/null
+++ b/extras/pom.xml
@@ -0,0 +1,41 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+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 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.
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+
+ <parent>
+ <groupId>org.apache.tamaya</groupId>
+ <artifactId>tamaya-all</artifactId>
+ <version>0.2-SNAPSHOT</version>
+ </parent>
+
+ <artifactId>extras</artifactId>
+ <name>Apache Tamaya Extras Parent</name>
+
+ <packaging>pom</packaging>
+ <inceptionYear>2015</inceptionYear>
+
+ <modules>
+ <module>json</module>
+ </modules>
+
+</project>
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/a83ba18f/pom.xml
----------------------------------------------------------------------
diff --git a/pom.xml b/pom.xml
index 82c41bb..40e7f0f 100644
--- a/pom.xml
+++ b/pom.xml
@@ -51,6 +51,7 @@ under the License.
<maven.javadoc.skip>true</maven.javadoc.skip>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
+ <jackson.version>2.5.0</jackson.version>
<junit.version>4.12</junit.version>
<!-- Dependency and plugin relate version properties go here -->
@@ -191,6 +192,7 @@ under the License.
<module>api</module>
<module>core</module>
<module>modules</module>
+ <module>extras</module>
</modules>
<dependencyManagement>
@@ -221,6 +223,12 @@ under the License.
<version>${asciidoctor-diagramm.version}</version>
<type>gem</type>
</dependency>
+
+ <dependency>
+ <groupId>com.fasterxml.jackson.core</groupId>
+ <artifactId>jackson-databind</artifactId>
+ <version>${jackson.version}</version>
+ </dependency>
</dependencies>
</dependencyManagement>