package org.onap.policy.apex.plugins.context.schema.avro;
+import com.google.gson.Gson;
+import com.google.gson.GsonBuilder;
+import com.google.gson.JsonElement;
+
import java.io.ByteArrayOutputStream;
import org.apache.avro.Schema;
import org.slf4j.ext.XLogger;
import org.slf4j.ext.XLoggerFactory;
-import com.google.gson.Gson;
-import com.google.gson.GsonBuilder;
-import com.google.gson.JsonElement;
-
/**
- * This class is the implementation of the {@link org.onap.policy.apex.context.SchemaHelper}
- * interface for Avro schemas.
+ * This class is the implementation of the {@link org.onap.policy.apex.context.SchemaHelper} interface for Avro schemas.
*
* @author Liam Fallon (liam.fallon@ericsson.com)
*/
// Get a reference to the logger
private static final XLogger LOGGER = XLoggerFactory.getXLogger(AvroSchemaHelper.class);
+ // Recurring string constants
+ private static final String OBJECT_TAG = ": object \"";
+
// The Avro schema for this context schema
private Schema avroSchema;
private AvroObjectMapper avroObjectMapper;
@Override
- public void init(final AxKey userKey, final AxContextSchema schema) throws ContextRuntimeException {
+ public void init(final AxKey userKey, final AxContextSchema schema) {
super.init(userKey, schema);
// Configure the Avro schema
avroSchema = new Schema.Parser().parse(schema.getSchema());
} catch (final Exception e) {
final String resultSting = userKey.getId() + ": avro context schema \"" + schema.getId()
- + "\" schema is invalid: " + e.getMessage() + ", schema: " + schema.getSchema();
+ + "\" schema is invalid: " + e.getMessage() + ", schema: " + schema.getSchema();
LOGGER.warn(resultSting);
throw new ContextRuntimeException(resultSting);
}
@Override
public Object getSchemaObject() {
- return avroSchema;
+ return getAvroSchema();
}
@Override
final String elementJsonString = gson.toJson((JsonElement) incomingObject);
return createNewInstance(elementJsonString);
- }
- else {
+ } else {
final String returnString = getUserKey().getId() + ": the object \"" + incomingObject
- + "\" is not an instance of JsonObject";
+ + "\" is not an instance of JsonObject";
LOGGER.warn(returnString);
throw new ContextRuntimeException(returnString);
}
final JsonDecoder jsonDecoder = DecoderFactory.get().jsonDecoder(avroSchema, objectString);
decodedObject = new GenericDatumReader<GenericRecord>(avroSchema).read(null, jsonDecoder);
} catch (final Exception e) {
- final String returnString = getUserKey().getId() + ": object \"" + objectString
- + "\" Avro unmarshalling failed: " + e.getMessage();
+ final String returnString = getUserKey().getId() + OBJECT_TAG + objectString
+ + "\" Avro unmarshalling failed: " + e.getMessage();
LOGGER.warn(returnString, e);
throw new ContextRuntimeException(returnString, e);
}
}
/**
- * Check that the incoming object is a string, the incoming object must be a string containing
- * Json
+ * Check that the incoming object is a string, the incoming object must be a string containing Json.
*
* @param object incoming object
* @return object as String
return (String) object;
}
} catch (final ClassCastException e) {
- final String returnString = getUserKey().getId() + ": object \"" + object + "\" of type \""
- + (object != null ? object.getClass().getCanonicalName() : "null") + "\" must be assignable to \""
- + getSchemaClass().getCanonicalName()
- + "\" or be a Json string representation of it for Avro unmarshalling";
+ final String returnString = getUserKey().getId() + OBJECT_TAG + object + "\" of type \""
+ + (object != null ? object.getClass().getCanonicalName() : "null")
+ + "\" must be assignable to \"" + getSchemaClass().getCanonicalName()
+ + "\" or be a Json string representation of it for Avro unmarshalling";
LOGGER.warn(returnString);
throw new ContextRuntimeException(returnString);
}
jsonEncoder.flush();
return new String(output.toByteArray());
} catch (final Exception e) {
- final String returnString =
- getUserKey().getId() + ": object \"" + object + "\" Avro marshalling failed: " + e.getMessage();
+ final String returnString = getUserKey().getId() + OBJECT_TAG + object + "\" Avro marshalling failed: "
+ + e.getMessage();
LOGGER.warn(returnString);
throw new ContextRuntimeException(returnString, e);
}
}
/**
- * Check if we can pass this object straight through encoding or decoding, is it an object
- * native to the schema.
+ * Check if we can pass this object straight through encoding or decoding, is it an object native to the schema.
*
* @param object the object to check
* @return true if it's a straight pass through