Skip to content

Commit

Permalink
CAMEL-10842: Adjust connector to new JSon schema
Browse files Browse the repository at this point in the history
  • Loading branch information
davsclaus committed Feb 16, 2017
1 parent c4607e6 commit aea6e22
Show file tree
Hide file tree
Showing 17 changed files with 608 additions and 354 deletions.
@@ -0,0 +1,58 @@
/**
* 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.camel.maven.connector;

/**
* A little helper class for converting a collection of values to a (usually comma separated) string.
*/
public class CollectionStringBuffer {

private final StringBuilder buffer = new StringBuilder();
private String separator;
private boolean first = true;

public CollectionStringBuffer() {
this(", ");
}

public CollectionStringBuffer(String separator) {
this.separator = separator;
}

@Override
public String toString() {
return buffer.toString();
}

public void append(Object value) {
if (first) {
first = false;
} else {
buffer.append(separator);
}
buffer.append(value);
}

public String getSeparator() {
return separator;
}

public void setSeparator(String separator) {
this.separator = separator;
}

}
Expand Up @@ -23,8 +23,11 @@
import java.io.InputStream; import java.io.InputStream;
import java.net.URL; import java.net.URL;
import java.net.URLClassLoader; import java.net.URLClassLoader;
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors; import java.util.stream.Collectors;


import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.JsonProcessingException;
Expand Down Expand Up @@ -211,26 +214,24 @@ private String buildComponentOptionsSchema(List<Map<String, String>> rows, Map d
Map values = (Map) dto.get("componentValues"); Map values = (Map) dto.get("componentValues");
Map overrides = (Map) dto.get("componentOverrides"); Map overrides = (Map) dto.get("componentOverrides");


ObjectMapper mapper = new ObjectMapper();

StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
sb.append(" \"componentProperties\": {\n"); sb.append(" \"componentProperties\": {\n");


boolean first = true; boolean first = true;
for (int i = 0; i < rows.size(); i++) { for (int i = 0; i < rows.size(); i++) {
Map<String, String> row = rows.get(i); Map<String, String> row = rows.get(i);
String key = row.get("name"); String key = row.get("name");
row.remove("name");


if (options == null || !options.contains(key)) { if (options == null || !options.contains(key)) {
continue; continue;
} }


// do we have a new default value for this row? // do we have a new default value for this row?
if (values != null && values.containsKey(key)) { if (values != null && values.containsKey(key)) {
String newDefaultValue = (String) values.get(key); // the value may be an integer so we need to use Object and toString when putting back in row
Object newDefaultValue = values.get(key);
if (newDefaultValue != null) { if (newDefaultValue != null) {
row.put("defaultValue", newDefaultValue); row.put("defaultValue", newDefaultValue.toString());
} }
} }


Expand All @@ -244,13 +245,12 @@ private String buildComponentOptionsSchema(List<Map<String, String>> rows, Map d


// we should build the json as one-line which is how Camel does it today // we should build the json as one-line which is how Camel does it today
// which makes its internal json parser support loading our generated schema file // which makes its internal json parser support loading our generated schema file
String line = mapper.writeValueAsString(row); String line = buildJSonLineFromRow(row);


if (!first) { if (!first) {
sb.append(",\n"); sb.append(",\n");
} }
sb.append(" \"" + key + "\": "); sb.append(" ").append(line);
sb.append(line);


first = false; first = false;
} }
Expand All @@ -277,17 +277,17 @@ private String buildEndpointOptionsSchema(List<Map<String, String>> rows, Map dt
for (int i = 0; i < rows.size(); i++) { for (int i = 0; i < rows.size(); i++) {
Map<String, String> row = rows.get(i); Map<String, String> row = rows.get(i);
String key = row.get("name"); String key = row.get("name");
row.remove("name");


if (options == null || !options.contains(key)) { if (options == null || !options.contains(key)) {
continue; continue;
} }


// do we have a new default value for this row? // do we have a new default value for this row?
if (values != null && values.containsKey(key)) { if (values != null && values.containsKey(key)) {
String newDefaultValue = (String) values.get(key); // the value may be an integer so we need to use Object and toString when putting back in row
Object newDefaultValue = values.get(key);
if (newDefaultValue != null) { if (newDefaultValue != null) {
row.put("defaultValue", newDefaultValue); row.put("defaultValue", newDefaultValue.toString());
} }
} }


Expand All @@ -301,13 +301,12 @@ private String buildEndpointOptionsSchema(List<Map<String, String>> rows, Map dt


// we should build the json as one-line which is how Camel does it today // we should build the json as one-line which is how Camel does it today
// which makes its internal json parser support loading our generated schema file // which makes its internal json parser support loading our generated schema file
String line = mapper.writeValueAsString(row); String line = buildJSonLineFromRow(row);


if (!first) { if (!first) {
sb.append(",\n"); sb.append(",\n");
} }
sb.append(" \"" + key + "\": "); sb.append(" ").append(line);
sb.append(line);


first = false; first = false;
} }
Expand Down Expand Up @@ -447,7 +446,40 @@ private File embedCamelComponentSchema(File file) throws MojoExecutionException
return null; return null;
} }


private String extractClass(List<String> lines) { /**
* Builds a JSon line of the given row
*/
private static String buildJSonLineFromRow(Map<String, String> row) {
String name = row.get("name");
String kind = row.get("kind");
boolean required = Boolean.valueOf(row.getOrDefault("required", "false"));
String type = row.get("type");
String defaultValue = row.get("defaultValue");
String description = row.get("description");
boolean deprecated = Boolean.valueOf(row.getOrDefault("deprecated", "false"));
boolean secret = Boolean.valueOf(row.getOrDefault("secret", "false"));
String group = row.get("group");
String label = row.get("label");
// for enum we need to build it back as a set
Set<String> enums = null;
// the enum can either be a List or String
Object value = row.get("enum");
if (value != null && value instanceof List) {
enums = new LinkedHashSet<String>((List)value);
} else if (value != null && value instanceof String) {
String[] array = value.toString().split(",");
enums = Arrays.stream(array).collect(Collectors.toSet());
}
boolean enumType = enums != null;
String optionalPrefix = row.get("optionalPrefix");
String prefix = row.get("prefix");
boolean multiValue = Boolean.valueOf(row.getOrDefault("multiValue", "false"));

return JSonSchemaHelper.toJson(name, kind, required, type, defaultValue, description, deprecated, secret, group, label,
enumType, enums, false, null, false, optionalPrefix, prefix, multiValue);
}

private static String extractClass(List<String> lines) {
for (String line : lines) { for (String line : lines) {
line = line.trim(); line = line.trim();
if (line.startsWith("class=")) { if (line.startsWith("class=")) {
Expand All @@ -457,19 +489,19 @@ private String extractClass(List<String> lines) {
return null; return null;
} }


private String extractScheme(Map map) { private static String extractScheme(Map map) {
return (String) map.get("baseScheme"); return (String) map.get("baseScheme");
} }


private String extractGroupId(Map map) { private static String extractGroupId(Map map) {
return (String) map.get("baseGroupId"); return (String) map.get("baseGroupId");
} }


private String extractArtifactId(Map map) { private static String extractArtifactId(Map map) {
return (String) map.get("baseArtifactId"); return (String) map.get("baseArtifactId");
} }


private String extractVersion(Map map) { private static String extractVersion(Map map) {
return (String) map.get("baseVersion"); return (String) map.get("baseVersion");
} }


Expand Down

0 comments on commit aea6e22

Please sign in to comment.