Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce GlobalErrorRegistry that registers all DittoRuntimeExceptions
annotated with @JsonParsableException Signed-off-by: Klem Yannic (INST/ECS1) <Yannic.Klem@bosch-si.com>
- Loading branch information
Showing
6 changed files
with
190 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
model/base/src/main/java/org/eclipse/ditto/model/base/json/JsonParsableException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* Copyright (c) 2017-2018 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 | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package org.eclipse.ditto.model.base.json; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
import org.atteo.classindex.IndexAnnotated; | ||
|
||
@Target(ElementType.TYPE) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@IndexAnnotated | ||
public @interface JsonParsableException { | ||
|
||
String errorCode(); | ||
|
||
/** | ||
* The name of the method accepting a {@link org.eclipse.ditto.json.JsonObject} as first argument and | ||
* {@link org.eclipse.ditto.model.base.headers.DittoHeaders} as seconds argument. | ||
* The Method must return an instance of the exception annotated with this annotation. | ||
* | ||
* @return the name of this method. | ||
*/ | ||
String method() default "fromJson"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
signals/base/src/main/java/org/eclipse/ditto/signals/base/GlobalErrorRegistry.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
/* | ||
* Copyright (c) 2017-2018 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 | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package org.eclipse.ditto.signals.base; | ||
|
||
import java.lang.reflect.InvocationTargetException; | ||
import java.lang.reflect.Method; | ||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import javax.annotation.concurrent.Immutable; | ||
|
||
import org.atteo.classindex.ClassIndex; | ||
import org.eclipse.ditto.json.JsonMissingFieldException; | ||
import org.eclipse.ditto.json.JsonObject; | ||
import org.eclipse.ditto.model.base.exceptions.DittoRuntimeException; | ||
import org.eclipse.ditto.model.base.headers.DittoHeaders; | ||
import org.eclipse.ditto.model.base.json.JsonParsableException; | ||
|
||
/** | ||
* Collects | ||
*/ | ||
@Immutable | ||
public final class GlobalErrorRegistry extends AbstractErrorRegistry<DittoRuntimeException> { | ||
|
||
private static final GlobalErrorRegistry instance = new GlobalErrorRegistry(); | ||
|
||
private GlobalErrorRegistry() { | ||
super(getParseRegistries()); | ||
} | ||
|
||
private static Map<String, JsonParsable<DittoRuntimeException>> getParseRegistries() { | ||
return JsonParsableExceptionRegistry.getParseRegistries(); | ||
} | ||
|
||
@Override | ||
protected String resolveType(final JsonObject jsonObject) { | ||
return jsonObject.getValue(DittoRuntimeException.JsonFields.ERROR_CODE) | ||
.orElseThrow(() -> JsonMissingFieldException.newBuilder() | ||
.fieldName(DittoRuntimeException.JsonFields.ERROR_CODE.getPointer().toString()) | ||
.build()); | ||
} | ||
|
||
public static GlobalErrorRegistry getInstance() { | ||
return instance; | ||
} | ||
|
||
private static class JsonParsableExceptionRegistry { | ||
|
||
private static final Class<?> JSON_OBJECT_PARAMETER = JsonObject.class; | ||
private static final Class<?> DITTO_HEADERS_PARAMETER = DittoHeaders.class; | ||
|
||
private static final JsonParsableExceptionRegistry instance = new JsonParsableExceptionRegistry(); | ||
|
||
private final Map<String, JsonParsable<DittoRuntimeException>> parseRegistries = new HashMap<>(); | ||
|
||
|
||
private JsonParsableExceptionRegistry() { | ||
|
||
final List<Class<? extends DittoRuntimeException>> jsonParsableExceptions = getJsonParsableExceptions(); | ||
jsonParsableExceptions.forEach(parsableException -> { | ||
final JsonParsableException fromJsonAnnotation = | ||
parsableException.getAnnotation(JsonParsableException.class); | ||
try { | ||
final String methodName = fromJsonAnnotation.method(); | ||
final String errorCode = fromJsonAnnotation.errorCode(); | ||
final Method method = parsableException | ||
.getMethod(methodName, JSON_OBJECT_PARAMETER, DITTO_HEADERS_PARAMETER); | ||
|
||
appendMethodToParseStrategies(errorCode, method); | ||
|
||
} catch (NoSuchMethodException e) { | ||
// TODO: Log warning or Throw an appropriate exception? | ||
} | ||
}); | ||
} | ||
|
||
private void appendMethodToParseStrategies(final String errorCode, final Method method) { | ||
parseRegistries.put(errorCode, | ||
(jsonObject, dittoHeaders) -> { | ||
try { | ||
return (DittoRuntimeException) method.invoke(null, jsonObject, dittoHeaders); | ||
} catch (IllegalAccessException | InvocationTargetException e) { | ||
throw JsonTypeNotParsableException.newBuilder(errorCode, getClass().getSimpleName()) | ||
.dittoHeaders(dittoHeaders) | ||
.build(); | ||
} | ||
}); | ||
} | ||
|
||
private List<Class<? extends DittoRuntimeException>> getJsonParsableExceptions() { | ||
final Iterable<Class<?>> jsonParsableClasses = ClassIndex.getAnnotated(JsonParsableException.class); | ||
final ArrayList<Class<? extends DittoRuntimeException>> jsonParsableExceptions = new ArrayList<>(); | ||
for (Class<?> classFromJson : jsonParsableClasses) { | ||
if (DittoRuntimeException.class.isAssignableFrom(classFromJson)) { | ||
jsonParsableExceptions.add(classFromJson.asSubclass(DittoRuntimeException.class)); | ||
} | ||
} | ||
return jsonParsableExceptions; | ||
} | ||
|
||
static Map<String, JsonParsable<DittoRuntimeException>> getParseRegistries() { | ||
return instance.parseRegistries; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
signals/base/src/test/java/org/eclipse/ditto/signals/base/GlobalErrorRegistryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package org.eclipse.ditto.signals.base; | ||
|
||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import org.junit.Test; | ||
|
||
/* | ||
* Copyright (c) 2017-2018 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 | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
public class GlobalErrorRegistryTest { | ||
|
||
@Test | ||
public void globalErrorRegistryKnowsJsonTypeNotParsableException() { | ||
final GlobalErrorRegistry instance = GlobalErrorRegistry.getInstance(); | ||
assertThat(instance.getTypes()).contains(JsonTypeNotParsableException.ERROR_CODE); | ||
} | ||
|
||
} |