1 /*********************************************************************************
2 * Copyright © 2015, highstreet technologies GmbH
5 * http://www.highstreet-technologies.com/
7 * The reproduction, transmission or use of this document or its contents is not
8 * permitted without express written authority. Offenders will be liable for
9 * damages. All rights, including rights created by patent grant or registration
10 * of a utility model or design, are reserved. Technical modifications possible.
11 * Technical specifications and features are binding only insofar as they are
12 * specifically and expressly agreed upon in a written contract.
14 * @author: Martin Skorupski [martin@skorupski.de]
15 *********************************************************************************/
16 package org.opendaylight.mwtn.base.database;
18 import java.io.IOException;
19 import java.nio.file.Files;
20 import java.nio.file.Paths;
21 import java.util.List;
23 import org.elasticsearch.common.bytes.BytesReference;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
27 import com.fasterxml.jackson.core.JsonParseException;
28 import com.fasterxml.jackson.databind.JsonMappingException;
29 import com.fasterxml.jackson.databind.JsonNode;
35 public class HtMapper<T> {
37 private static final Logger log = LoggerFactory.getLogger(HtDatabaseClientAbstract.class);
39 private final Class<? extends T> clazz;
41 private final JsonMapperBase objectMapperRead;
42 private final JsonMapperBase objectMapperWrite;
44 private int mappingFailures;
47 public HtMapper(Class<? extends T> clazz) {
49 this.mappingFailures = 0;
52 this.objectMapperRead = new JsonMapperBase();
53 this.objectMapperWrite = this.objectMapperRead;
56 public Class<? extends T> getClazz() {
60 public int getMappingFailures() {
61 return mappingFailures;
64 public String objectToJson( T object ) {
65 return objectMapperWrite.objectToJson(object);
68 public String objectListToJson( List<T> objectList ) {
69 return objectMapperWrite.objectListToJson( objectList );
72 public T readValue( JsonNode node ) {
75 T object = objectMapperRead.readValue(node.traverse(), clazz);
77 } catch (JsonParseException e) {
79 log.warn(e.toString());
80 } catch (JsonMappingException e) {
82 log.warn(e.toString());
83 } catch (IOException e) {
85 log.warn(e.toString());
86 } catch (Exception e) {
88 log.warn(e.toString());
90 log.warn("Can not parse: {} {} ", clazz, node);
96 * Do the mapping from Json to class
97 * Block further mapping if there is are to many failures
98 * @param json String with Objects JSON representation
101 public T getObjectFromJson(byte[] json) {
103 if (json != null && mappingFailures < 10) {
105 T object = objectMapperRead.readValue(json, clazz);
107 } catch (JsonParseException e) {
109 log.warn(e.toString());
110 } catch (JsonMappingException e) {
112 log.warn(e.toString());
113 } catch (IOException e) {
115 log.warn(e.toString());
116 } catch (Exception e) {
118 log.warn(e.toString());
121 log.warn("Problems parsing : {} {}", clazz, json);
126 * Do the mapping from Json to class
127 * Block further mapping if there is are to many failures
128 * @param json Byte array with JSON Object representation
131 public T getObjectFromJson(BytesReference json) {
133 return getObjectFromJson(json.toBytes());
138 * Read json from File.
139 * @param fileName File with JSON text
140 * @return Object Object
142 public T readJsonObjectFromFile( String fileName ) {
143 byte[] content = null;
144 log.debug("Filename readJsonObjectFromFile: {}",fileName);
147 content = Files.readAllBytes(Paths.get(fileName));
148 } catch (IOException e1) {
149 log.warn("IO Problem: {}", e1.getMessage());
152 if (content != null) {
153 return getObjectFromJson(content);