Permalink
Browse files

Add JsonEventSerializer for external use

  • Loading branch information...
1 parent 2da9f1f commit 7d239617f6400d25e78dd5805ca9ba58ba4b7cba @electrum electrum committed Apr 11, 2012
@@ -0,0 +1,20 @@
+package com.proofpoint.event.client;
+
+import com.google.inject.Binder;
+import com.google.inject.Module;
+import com.google.inject.Scopes;
+import com.google.inject.TypeLiteral;
+import com.google.inject.multibindings.Multibinder;
+
+public class JsonEventModule
+ implements Module
+{
+ @Override
+ public void configure(Binder binder)
+ {
+ binder.bind(JsonEventSerializer.class).in(Scopes.SINGLETON);
+
+ // Kick off the binding of Set<EventTypeMetadata> in case no events are bound
+ Multibinder.newSetBinder(binder, new TypeLiteral<EventTypeMetadata<?>>() {});
+ }
+}
@@ -0,0 +1,56 @@
+package com.proofpoint.event.client;
+
+import com.google.common.collect.ImmutableMap;
+import org.codehaus.jackson.JsonGenerator;
+import org.codehaus.jackson.map.JsonSerializer;
+
+import javax.inject.Inject;
+import java.io.IOException;
+import java.util.Map;
+import java.util.Set;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+import static com.proofpoint.event.client.EventJsonSerializerV2.createEventJsonSerializer;
+import static com.proofpoint.event.client.EventTypeMetadata.getValidEventTypeMetaDataSet;
+
+public class JsonEventSerializer
+{
+ private final Map<Class<?>, JsonSerializer<?>> serializers;
+
+ @Inject
+ public JsonEventSerializer(Set<EventTypeMetadata<?>> eventTypes)
+ {
+ checkNotNull(eventTypes, "eventTypes is null");
+
+ ImmutableMap.Builder<Class<?>, JsonSerializer<?>> map = ImmutableMap.builder();
+ for (EventTypeMetadata<?> eventType : eventTypes) {
+ map.put(eventType.getEventClass(), createEventJsonSerializer(eventType));
+ }
+ this.serializers = map.build();
+ }
+
+ public JsonEventSerializer(Class<?>... eventClasses)
+ {
+ this(getValidEventTypeMetaDataSet(eventClasses));
+ }
+
+ public <T> void serialize(T event, JsonGenerator jsonGenerator)
+ throws IOException
+ {
+ checkNotNull(event, "event is null");
+ checkNotNull(jsonGenerator, "jsonGenerator is null");
+
+ JsonSerializer<T> serializer = getSerializer(event);
+ if (serializer == null) {
+ throw new InvalidEventException("Event class [%s] has not been registered as an event", event.getClass().getName());
+ }
+
+ serializer.serialize(event, jsonGenerator, null);
+ }
+
+ @SuppressWarnings("unchecked")
+ private <T> JsonSerializer<T> getSerializer(T event)
+ {
+ return (JsonSerializer<T>) serializers.get(event.getClass());
+ }
+}
@@ -0,0 +1,30 @@
+package com.proofpoint.event.client;
+
+import com.google.common.base.Charsets;
+import org.codehaus.jackson.JsonEncoding;
+import org.codehaus.jackson.JsonFactory;
+import org.codehaus.jackson.JsonGenerator;
+import org.testng.annotations.Test;
+
+import java.io.ByteArrayOutputStream;
+
+import static org.testng.Assert.assertEquals;
+
+public class TestJsonEventSerializer
+{
+ @Test
+ public void testEventSerializer()
+ throws Exception
+ {
+ JsonEventSerializer eventSerializer = new JsonEventSerializer(FixedDummyEventClass.class);
+
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ JsonGenerator jsonGenerator = new JsonFactory().createJsonGenerator(out, JsonEncoding.UTF8);
+
+ FixedDummyEventClass event = TestingUtils.getEvents().get(0);
+ eventSerializer.serialize(event, jsonGenerator);
+
+ String json = out.toString(Charsets.UTF_8.name());
+ assertEquals(json, TestingUtils.getNormalizedJson("event.json"));
+ }
+}
@@ -0,0 +1,10 @@
+{
+ "type":"FixedDummy",
+ "uuid":"8e248a16-da86-11e0-9e77-9fc96e21a396",
+ "host":"localhost",
+ "timestamp":"2011-09-09T01:35:28.333Z",
+ "data":{
+ "intValue":5678,
+ "stringValue":"foo"
+ }
+}

5 comments on commit 7d23961

Contributor

dain replied Apr 13, 2012

Don't we already have this code somewhere?

Contributor

electrum replied Apr 13, 2012

We have JsonEventWriter, but it's purpose is different:

  • it is constructed using HttpEventClientConfig, allowing either v1 or v2 format
  • it uses an EventGenerator to serialize multiple events to an OutputStream with an array container

JsonEventSerializer serializes a single event in v2 format using a JsonGenerator. I use this in the event collector with a TokenBuffer to deserialize back into an Event, which is then sent to all the EventWriters: electrum/event-collector@51f59e9

Contributor

dain replied Apr 13, 2012

That make since. Can this be moved to event-collector?

Contributor

electrum replied Apr 13, 2012

It's not possible to make it external without exposing the guts of the event client (EventTypeMetadata, EventJsonSerializerV2). It seems reasonable to have the building blocks for a custom client in the platform event client. Do you have an alternative abstraction in mind?

Contributor

electrum replied Apr 13, 2012

I think JsonEventWriter should actually be private, since it's only externally instantiable via Guice and is specific to the HttpEventClient.

Please sign in to comment.