Navigation Menu

Skip to content

Commit

Permalink
simplified! mapping and made CommandProcessorActor dynamically load a…
Browse files Browse the repository at this point in the history
… class it interprets from the configured "mappingEngine"

Signed-off-by: Thomas Jaeckle <thomas.jaeckle@bosch-si.com>
  • Loading branch information
thjaeckle committed Feb 5, 2018
1 parent a99347e commit b30d1b5
Show file tree
Hide file tree
Showing 23 changed files with 343 additions and 321 deletions.
Expand Up @@ -64,8 +64,8 @@ public static AmqpConnection connectionFromJson(final JsonObject jsonObject) {
* @throws NullPointerException if any argument is {@code null}.
*/
public static MappingScript newMappingScript(final String contentType, final String mappingEngine,
final String incomingMappingScript, final String outgoingMappingScript, final Map<String, String> options) {
return ImmutableMappingScript.of(contentType, mappingEngine, incomingMappingScript, outgoingMappingScript, options);
final Map<String, String> options) {
return ImmutableMappingScript.of(contentType, mappingEngine, options);
}

/**
Expand Down
Expand Up @@ -38,18 +38,14 @@ final class ImmutableMappingScript implements MappingScript {

private final String contentType;
private final String mappingEngine;
private final String incomingMappingScript;
private final String outgoingMappingScript;
private final Map<String, String> options;


private ImmutableMappingScript(final String contentType, final String mappingEngine,
final String incomingMappingScript, final String outgoingMappingScript, final Map<String, String> options) {
final Map<String, String> options) {

this.contentType = contentType;
this.mappingEngine = mappingEngine;
this.incomingMappingScript = incomingMappingScript;
this.outgoingMappingScript = outgoingMappingScript;
this.options = Collections.unmodifiableMap(new HashMap<>(options));
}

Expand All @@ -58,21 +54,16 @@ private ImmutableMappingScript(final String contentType, final String mappingEng
*
* @param contentType
* @param mappingEngine
* @param incomingMappingScript
* @param outgoingMappingScript
* @param options
* @return
*/
public static ImmutableMappingScript of(final String contentType, final String mappingEngine,
final String incomingMappingScript, final String outgoingMappingScript, final Map<String, String> options) {
final Map<String, String> options) {
checkNotNull(contentType, "content-type");
checkNotNull(mappingEngine, "mapping Engine");
checkNotNull(incomingMappingScript, "incoming MappingScript");
checkNotNull(outgoingMappingScript, "outgoing MappingScript");
checkNotNull(options, "options");

return new ImmutableMappingScript(contentType, mappingEngine, incomingMappingScript, outgoingMappingScript,
options);
return new ImmutableMappingScript(contentType, mappingEngine, options);
}

/**
Expand All @@ -86,12 +77,10 @@ public static ImmutableMappingScript of(final String contentType, final String m
public static MappingScript fromJson(final JsonObject jsonObject) {
final String contentType = jsonObject.getValueOrThrow(JsonFields.CONTENT_TYPE);
final String mappingEngine = jsonObject.getValueOrThrow(JsonFields.MAPPING_ENGINE);
final String incomingMappingScript = jsonObject.getValueOrThrow(JsonFields.INCOMING_MAPPING_SCRIPT);
final String outgoingMappingScript = jsonObject.getValueOrThrow(JsonFields.OUTGOING_MAPPING_SCRIPT);
final Map<String, String> options = jsonObject.getValueOrThrow(JsonFields.OPTIONS).stream()
.collect(Collectors.toMap(e -> e.getKey().toString(), e -> e.getValue().toString()));

return of(contentType, mappingEngine, incomingMappingScript, outgoingMappingScript, options);
return of(contentType, mappingEngine, options);
}

@Override
Expand All @@ -101,8 +90,6 @@ public JsonObject toJson(final JsonSchemaVersion schemaVersion, final Predicate<

jsonObjectBuilder.set(JsonFields.CONTENT_TYPE, contentType, predicate);
jsonObjectBuilder.set(JsonFields.MAPPING_ENGINE, mappingEngine, predicate);
jsonObjectBuilder.set(JsonFields.INCOMING_MAPPING_SCRIPT, incomingMappingScript, predicate);
jsonObjectBuilder.set(JsonFields.OUTGOING_MAPPING_SCRIPT, outgoingMappingScript, predicate);
jsonObjectBuilder.set(JsonFields.OPTIONS, options.entrySet().stream()
.map(e -> JsonField.newInstance(e.getKey(), JsonValue.of(e.getValue())))
.collect(JsonCollectors.fieldsToObject()), predicate);
Expand All @@ -120,16 +107,6 @@ public String getMappingEngine() {
return mappingEngine;
}

@Override
public String getIncomingMappingScript() {
return incomingMappingScript;
}

@Override
public String getOutgoingMappingScript() {
return outgoingMappingScript;
}

@Override
public Map<String, String> getOptions() {
return options;
Expand All @@ -146,23 +123,19 @@ public boolean equals(final Object o) {
final ImmutableMappingScript that = (ImmutableMappingScript) o;
return Objects.equals(contentType, that.contentType) &&
Objects.equals(mappingEngine, that.mappingEngine) &&
Objects.equals(incomingMappingScript, that.incomingMappingScript) &&
Objects.equals(outgoingMappingScript, that.outgoingMappingScript) &&
Objects.equals(options, that.options);
}

@Override
public int hashCode() {
return Objects.hash(contentType, mappingEngine, incomingMappingScript, outgoingMappingScript, options);
return Objects.hash(contentType, mappingEngine, options);
}

@Override
public String toString() {
return getClass().getSimpleName() + " [" +
"contentType=" + contentType +
", mappingEngine=" + mappingEngine +
", incomingMappingScript=" + incomingMappingScript +
", outgoingMappingScript=" + outgoingMappingScript +
", options=" + options +
"]";
}
Expand Down
Expand Up @@ -41,18 +41,6 @@ public interface MappingScript extends Jsonifiable.WithFieldSelectorAndPredicate
*/
String getMappingEngine();

/**
*
* @return
*/
String getIncomingMappingScript();

/**
*
* @return
*/
String getOutgoingMappingScript();

/**
*
* @return
Expand Down Expand Up @@ -94,20 +82,6 @@ final class JsonFields {
JsonFactory.newStringFieldDefinition("mappingEngine", FieldType.REGULAR,
JsonSchemaVersion.V_1, JsonSchemaVersion.V_2);

/**
* JSON field containing the identifier for the script for incoming messages.
*/
public static final JsonFieldDefinition<String> INCOMING_MAPPING_SCRIPT =
JsonFactory.newStringFieldDefinition("incomingMappingScript", FieldType.REGULAR, JsonSchemaVersion.V_1,
JsonSchemaVersion.V_2);

/**
* JSON field containing the identifier for the script for outgoing messages.
*/
public static final JsonFieldDefinition<String> OUTGOING_MAPPING_SCRIPT =
JsonFactory.newStringFieldDefinition("outgoingMappingScript", FieldType.REGULAR, JsonSchemaVersion.V_1,
JsonSchemaVersion.V_2);

/**
* JSON field containing the options for the mapping.
*/
Expand Down
2 changes: 2 additions & 0 deletions services/amqp-bridge/mapping/pom.xml
Expand Up @@ -58,10 +58,12 @@
<dependency>
<groupId>org.webjars.npm</groupId>
<artifactId>mustache</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.webjars.npm</groupId>
<artifactId>bytebuffer</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>

Expand Down

This file was deleted.

Expand Up @@ -21,14 +21,21 @@
/**
* TODO doc
*/
public final class ImmutablePayloadMapperMessage implements PayloadMapperMessage {
final class ImmutablePayloadMapperMessage implements PayloadMapperMessage {

private final String contentType;
private final ByteBuffer rawData;
private final String stringData;
private final Map<String, String> headers;

public ImmutablePayloadMapperMessage(final String contentType, @Nullable final ByteBuffer rawData,
/**
*
* @param contentType
* @param rawData
* @param stringData
* @param headers
*/
ImmutablePayloadMapperMessage(final String contentType, @Nullable final ByteBuffer rawData,
@Nullable final String stringData,
final Map<String, String> headers) {
this.contentType = contentType;
Expand Down
@@ -0,0 +1,73 @@
/*
* Copyright (c) 2017 Bosch Software Innovations GmbH.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/org/documents/epl-2.0/index.php
*
* Contributors:
* Bosch Software Innovations GmbH - initial contribution
*/
package org.eclipse.ditto.services.amqpbridge.mapping.mapper;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
* TODO doc
*/
final class ImmutablePayloadMapperOptions implements PayloadMapperOptions {

private final Map<String, String> optionsMap;

/**
*
* @param optionsMap
*/
ImmutablePayloadMapperOptions(final Map<String, String> optionsMap) {
this.optionsMap = Collections.unmodifiableMap(new HashMap<>(optionsMap));
}

@Override
public Map<String, String> getAsMap() {
return optionsMap;
}

@Override
public boolean equals(final Object o) {
if (this == o) {
return true;
}
if (!(o instanceof ImmutablePayloadMapperOptions)) {
return false;
}
final ImmutablePayloadMapperOptions that = (ImmutablePayloadMapperOptions) o;
return Objects.equals(optionsMap, that.optionsMap);
}

@Override
public int hashCode() {

return Objects.hash(optionsMap);
}

/**
*
*/
static final class Builder implements PayloadMapperOptions.Builder {

private final Map<String, String> options;

Builder(final Map<String, String> options) {
this.options = options;
}

@Override
public PayloadMapperOptions build() {
return new ImmutablePayloadMapperOptions(options);
}
}
}

This file was deleted.

Expand Up @@ -18,7 +18,7 @@
*/
public interface PayloadMapper {

Adaptable mapIncomingMessageToDittoAdaptable(MappingTemplate template, PayloadMapperMessage message);
Adaptable mapIncoming(PayloadMapperMessage message);

PayloadMapperMessage mapOutgoingMessageFromDittoAdaptable(MappingTemplate template, Adaptable dittoProtocolAdaptable);
PayloadMapperMessage mapOutgoing(Adaptable dittoProtocolAdaptable);
}
Expand Up @@ -20,11 +20,27 @@
*/
public interface PayloadMapperMessage {

/**
*
* @return
*/
String getContentType();

/**
*
* @return
*/
Optional<ByteBuffer> getRawData();

/**
*
* @return
*/
Optional<String> getStringData();

/**
*
* @return
*/
Map<String, String> getHeaders();
}

0 comments on commit b30d1b5

Please sign in to comment.