-
Notifications
You must be signed in to change notification settings - Fork 4
/
Event.java
119 lines (99 loc) · 3.77 KB
/
Event.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/**
* Copyright 2012 Rackspace
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package com.rackspacecloud.client.service_registry.objects;
import com.google.gson.*;
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;
public class Event implements
InstanceCreator<Event>,
JsonDeserializer<Event> {
private String id;
private Long timestamp;
private String type;
private EventPayload payload;
public Event() {}
public Event(String id, Long timestamp, String type, EventPayload payload) {
this.id = id;
this.timestamp = timestamp;
this.type = type;
this.payload = payload;
}
public String getId() {
return id;
}
public Long getTimestamp() {
return timestamp;
}
public String getType() {
return type;
}
public EventPayload getPayload() {
return payload;
}
@Override
public Event createInstance(Type type) {
return this;
}
@Override
public Event deserialize(JsonElement json, Type typeOfT,
JsonDeserializationContext context) {
JsonObject jsonObject = json.getAsJsonObject();
String id = jsonObject.get("id").getAsString();
String eventType = jsonObject.get("type").getAsString();
JsonObject payloadJsonObject = jsonObject.get("payload").getAsJsonObject();
Long timestamp = jsonObject.get("timestamp").getAsLong();
EventPayload payload = null;
if (eventType.equals("configuration_value.update") ||
eventType.equals("configuration_value.remove")) {
String configurationId = payloadJsonObject.get("configuration_value_id").getAsString();
String oldValue, newValue;
if (payloadJsonObject.get("old_value").isJsonNull()) {
oldValue = null;
}
else {
oldValue = payloadJsonObject.get("old_value").getAsString();
}
if (payloadJsonObject.has("new_value")) {
newValue = payloadJsonObject.get("new_value").getAsString();
payload = new ConfigurationValueUpdatedEventPayload(configurationId, oldValue, newValue);
}
else {
payload = new ConfigurationValueRemovedEventPayload(configurationId, oldValue);
}
}
else if (eventType.equals("service.join") || eventType.equals("service.timeout") ||
eventType.equals("service.remove")) {
Service service = new Gson().fromJson(jsonObject,
new TypeToken<Service>() {}.getType());
if (eventType.equals("service.join")) {
payload = new ServiceJoinEventPayload(service);
}
else if (eventType.equals("service.timeout")) {
payload = new ServiceTimeoutEventPayload(service);
}
else if (eventType.equals("service.remove")) {
payload = new ServiceRemoveEventPayload(service);
}
}
else {
throw new Error("Unrecognized event type: " + eventType);
}
return new Event(id, timestamp, eventType, payload);
}
}