Permalink
Browse files

Merge pull request #16 from racker/events_client

Events client
  • Loading branch information...
2 parents fd58af2 + 8700e1f commit c9c78291c23a11df226151b5e91e06781067f0af @Kami Kami committed Feb 24, 2013
Showing with 537 additions and 23 deletions.
  1. +2 −0 .gitignore
  2. +3 −0 src/main/java/com/rackspacecloud/client/service_registry/Client.java
  3. +6 −1 src/main/java/com/rackspacecloud/client/service_registry/ClientResponse.java
  4. +3 −3 src/main/java/com/rackspacecloud/client/service_registry/HeartBeater.java
  5. +3 −3 src/main/java/com/rackspacecloud/client/service_registry/clients/BaseClient.java
  6. +116 −0 src/main/java/com/rackspacecloud/client/service_registry/clients/EventsClient.java
  7. +31 −0 src/main/java/com/rackspacecloud/client/service_registry/containers/EventsContainer.java
  8. +1 −1 src/main/java/com/rackspacecloud/client/service_registry/events/{ → client}/ClientEvent.java
  9. +1 −1 src/main/java/com/rackspacecloud/client/service_registry/events/{ → client}/ClientEventListener.java
  10. +1 −1 src/main/java/com/rackspacecloud/client/service_registry/events/{ → client}/ClientEventThread.java
  11. +1 −1 src/main/java/com/rackspacecloud/client/service_registry/events/{ → client}/HeartbeatAckEvent.java
  12. +1 −1 src/main/java/com/rackspacecloud/client/service_registry/events/{ → client}/HeartbeatErrorEvent.java
  13. +1 −1 ...in/java/com/rackspacecloud/client/service_registry/events/{ → client}/HeartbeatEventListener.java
  14. +1 −1 ...ain/java/com/rackspacecloud/client/service_registry/events/{ → client}/HeartbeatStoppedEvent.java
  15. +21 −0 src/main/java/com/rackspacecloud/client/service_registry/events/server/BaseEvent.java
  16. +25 −0 ...java/com/rackspacecloud/client/service_registry/events/server/ConfigurationValueRemovedEvent.java
  17. +33 −0 ...java/com/rackspacecloud/client/service_registry/events/server/ConfigurationValueUpdatedEvent.java
  18. +20 −0 src/main/java/com/rackspacecloud/client/service_registry/events/server/ServiceJoinEvent.java
  19. +20 −0 src/main/java/com/rackspacecloud/client/service_registry/events/server/ServiceTimeoutEvent.java
  20. +37 −0 ...ava/com/rackspacecloud/client/service_registry/objects/ConfigurationValueRemovedEventPayload.java
  21. +42 −0 ...ava/com/rackspacecloud/client/service_registry/objects/ConfigurationValueUpdatedEventPayload.java
  22. +19 −3 src/main/java/com/rackspacecloud/client/service_registry/objects/Event.java
  23. +80 −0 src/main/java/com/rackspacecloud/client/service_registry/objects/EventPayload.java
  24. +1 −0 src/main/java/com/rackspacecloud/client/service_registry/objects/Service.java
  25. +30 −0 src/main/java/com/rackspacecloud/client/service_registry/objects/ServiceJoinEventPayload.java
  26. +32 −0 src/main/java/com/rackspacecloud/client/service_registry/objects/ServicesTimeoutEventPayload.java
  27. +6 −6 src/test/java/com/rackspacecloud/client/service_registry/clients/HeartbeatEventTest.java
View
@@ -1,2 +1,4 @@
target/
out/
+lib/slf4j-simple-1.7.2.jar
+src/main/java/com/rackspacecloud/client/service_registry/Main.java
@@ -33,6 +33,7 @@
private final ServicesClient services;
private final ConfigurationClient configuration;
private final ViewsClient views;
+ private final EventsClient events;
private final AccountClient account;
public static final String VERSION = "java-service-registry-client/v1.0.0-SNAPSHOT";
@@ -61,12 +62,14 @@ protected ClientConnectionManager createClientConnectionManager() {
this.services = new ServicesClient(authClient);
this.configuration = new ConfigurationClient(authClient);
this.views = new ViewsClient(authClient);
+ this.events = new EventsClient(authClient);
this.account = new AccountClient(authClient);
}
public SessionsClient getSessionsClient() { return this.sessions; }
public ServicesClient getServicesClient() { return this.services; }
public ConfigurationClient getConfigurationClient() { return this.configuration; }
public ViewsClient getViewsClient() { return this.views; }
+ public EventsClient getEventsClient () { return this.events; }
public AccountClient getAccountClient() { return this.account; }
}
@@ -18,7 +18,9 @@
package com.rackspacecloud.client.service_registry;
import com.google.gson.Gson;
+import com.google.gson.GsonBuilder;
import com.rackspacecloud.client.service_registry.exceptions.ValidationException;
+import com.rackspacecloud.client.service_registry.objects.EventPayload;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
@@ -61,7 +63,10 @@ private Object processResponse() throws IOException, ValidationException {
data = EntityUtils.toString(entity);
if (this.parseAsJson && this.responseType != null) {
- data = new Gson().fromJson(data.toString(), this.responseType);
+ GsonBuilder builder = new GsonBuilder();
+ Gson gson = builder.registerTypeAdapter(EventPayload.class,
+ new EventPayload()).create();
+ data = gson.fromJson(data.toString(), this.responseType);
}
}
@@ -19,9 +19,9 @@
import com.rackspacecloud.client.service_registry.clients.AuthClient;
import com.rackspacecloud.client.service_registry.clients.BaseClient;
-import com.rackspacecloud.client.service_registry.events.HeartbeatAckEvent;
-import com.rackspacecloud.client.service_registry.events.HeartbeatErrorEvent;
-import com.rackspacecloud.client.service_registry.events.HeartbeatStoppedEvent;
+import com.rackspacecloud.client.service_registry.events.client.HeartbeatAckEvent;
+import com.rackspacecloud.client.service_registry.events.client.HeartbeatErrorEvent;
+import com.rackspacecloud.client.service_registry.events.client.HeartbeatStoppedEvent;
import com.rackspacecloud.client.service_registry.objects.HeartbeatToken;
import org.apache.http.client.methods.HttpPost;
import org.slf4j.Logger;
@@ -31,9 +31,9 @@
import com.google.gson.GsonBuilder;
import com.rackspacecloud.client.service_registry.Client;
import com.rackspacecloud.client.service_registry.ClientResponse;
-import com.rackspacecloud.client.service_registry.events.ClientEvent;
-import com.rackspacecloud.client.service_registry.events.ClientEventListener;
-import com.rackspacecloud.client.service_registry.events.ClientEventThread;
+import com.rackspacecloud.client.service_registry.events.client.ClientEvent;
+import com.rackspacecloud.client.service_registry.events.client.ClientEventListener;
+import com.rackspacecloud.client.service_registry.events.client.ClientEventThread;
import org.apache.http.NameValuePair;
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
import org.apache.http.client.methods.HttpRequestBase;
@@ -0,0 +1,116 @@
+/**
+ * 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.clients;
+
+import com.google.gson.reflect.TypeToken;
+import com.rackspacecloud.client.service_registry.events.server.*;
+import com.rackspacecloud.client.service_registry.objects.*;
+import org.apache.http.client.methods.HttpGet;
+import com.rackspacecloud.client.service_registry.ClientResponse;
+import com.rackspacecloud.client.service_registry.containers.EventsContainer;
+
+import java.lang.reflect.Type;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Map;
+
+public class EventsClient extends BaseClient {
+ private static final List VALID_EVENT_TYPES = new ArrayList<String>(Arrays.asList(new String[]{
+ "service.join",
+ "services.timeout",
+ "configuration_value.update",
+ "configuration_value.remove"}));
+
+ public EventsClient(AuthClient authClient) {
+ super(authClient);
+ }
+
+ public List<BaseEvent> list(Map<String, String> options) throws Exception {
+ String url = "/events";
+ Type type = new TypeToken<EventsContainer>() {}.getType();
+ ClientResponse response = this.performRequest(url, null, new HttpGet(), true, type);
+
+ EventsContainer container = (EventsContainer)response.getBody();
+ List<BaseEvent> events = this.parseEvents(container.getValues());
+ return events;
+ }
+
+ /**
+ * Takes a list of raw Event objects and convert into a List of BaseEvent
+ * objects.
+ * @param events List of Event objects.
+ * @return List of BaseEvent objects.
+ */
+ private List<BaseEvent> parseEvents(List<Event> events) throws Exception {
+ String type;
+ EventPayload payload;
+ BaseEvent event;
+
+ String configurationValueId;
+ ConfigurationValue oldValue, newValue;
+
+ List<BaseEvent> result = new ArrayList<BaseEvent>();
+
+ for (Event rawEvent : events) {
+ type = rawEvent.getType();
+ payload = rawEvent.getPayload();
+
+ if (!VALID_EVENT_TYPES.contains(type)) {
+ throw new Exception("Unrecognized event type: " + type);
+ }
+
+ if (type.compareTo("service.join") == 0) {
+ ServiceJoinEventPayload eventPayload = ((ServiceJoinEventPayload)payload);
+
+ event = new ServiceJoinEvent(eventPayload.getService());
+ result.add(event);
+ }
+ else if (type.compareTo("services.timeout") == 0) {
+ ServicesTimeoutEventPayload eventPayload = ((ServicesTimeoutEventPayload)payload);
+
+ for (Service service : eventPayload.getServices()) {
+ event = new ServiceTimeoutEvent(service);
+ result.add(event);
+ }
+ }
+ else if (type.compareTo("configuration_value.update") == 0) {
+ ConfigurationValueUpdatedEventPayload eventPayload = ((ConfigurationValueUpdatedEventPayload)payload);
+
+ configurationValueId = eventPayload.getConfigurationId();
+ oldValue = (eventPayload.getOldValue() == null) ? null : new ConfigurationValue(configurationValueId, eventPayload.getOldValue());
+ newValue = new ConfigurationValue(configurationValueId,eventPayload.getNewValue());
+
+ event = new ConfigurationValueUpdatedEvent(oldValue, newValue);
+ result.add(event);
+ }
+ else if (type.compareTo("configuration_value.remove") == 0) {
+
+ ConfigurationValueRemovedEventPayload eventPayload = ((ConfigurationValueRemovedEventPayload)payload);
+
+ configurationValueId = eventPayload.getConfigurationId();
+ oldValue = (eventPayload.getOldValue() == null) ? null : new ConfigurationValue(configurationValueId, eventPayload.getOldValue());
+
+ event = new ConfigurationValueRemovedEvent(oldValue);
+ result.add(event);
+ }
+ }
+
+ return result;
+ }
+}
@@ -0,0 +1,31 @@
+/**
+ * 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.containers;
+
+
+import com.rackspacecloud.client.service_registry.objects.Event;
+
+import java.util.List;
+
+public class EventsContainer {
+ private List<Event> values;
+
+ public List<Event> getValues() {
+ return values;
+ }
+}
@@ -1,4 +1,4 @@
-package com.rackspacecloud.client.service_registry.events;
+package com.rackspacecloud.client.service_registry.events.client;
import java.util.EventObject;
@@ -1,4 +1,4 @@
-package com.rackspacecloud.client.service_registry.events;
+package com.rackspacecloud.client.service_registry.events.client;
import java.util.EventListener;
@@ -1,4 +1,4 @@
-package com.rackspacecloud.client.service_registry.events;
+package com.rackspacecloud.client.service_registry.events.client;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
@@ -1,4 +1,4 @@
-package com.rackspacecloud.client.service_registry.events;
+package com.rackspacecloud.client.service_registry.events.client;
import com.rackspacecloud.client.service_registry.ClientResponse;
import com.rackspacecloud.client.service_registry.HeartBeater;
@@ -1,4 +1,4 @@
-package com.rackspacecloud.client.service_registry.events;
+package com.rackspacecloud.client.service_registry.events.client;
public class HeartbeatErrorEvent extends ClientEvent {
@@ -1,4 +1,4 @@
-package com.rackspacecloud.client.service_registry.events;
+package com.rackspacecloud.client.service_registry.events.client;
public abstract class HeartbeatEventListener implements ClientEventListener {
@@ -1,4 +1,4 @@
-package com.rackspacecloud.client.service_registry.events;
+package com.rackspacecloud.client.service_registry.events.client;
import com.rackspacecloud.client.service_registry.HeartBeater;
@@ -0,0 +1,21 @@
+package com.rackspacecloud.client.service_registry.events.server;
+
+public class BaseEvent {
+ private String id;
+ private Long timestamp;
+
+ public BaseEvent(String id, Long timestamp) {
+ this.id = id;
+ this.timestamp = timestamp;
+ }
+
+ public BaseEvent() {}
+
+ public String getId() {
+ return id;
+ }
+
+ public Long getTimestamp() {
+ return timestamp;
+ }
+}
@@ -0,0 +1,25 @@
+package com.rackspacecloud.client.service_registry.events.server;
+
+import com.rackspacecloud.client.service_registry.objects.ConfigurationValue;
+
+public class ConfigurationValueRemovedEvent extends BaseEvent {
+ private ConfigurationValue oldValue;
+
+ public ConfigurationValueRemovedEvent(ConfigurationValue oldValue) {
+ this.oldValue = oldValue;
+ }
+
+ public ConfigurationValue getOldValue() {
+ return oldValue;
+ }
+
+ @Override
+ public String toString() {
+ String id, oldValue;
+
+ id = this.oldValue.getId();
+ oldValue = this.oldValue.getValue();
+ return String.format("[ConfigurationValueRemovedEvent id=%s, old_value=%s",
+ id, oldValue);
+ }
+}
@@ -0,0 +1,33 @@
+package com.rackspacecloud.client.service_registry.events.server;
+
+import com.rackspacecloud.client.service_registry.objects.ConfigurationValue;
+
+public class ConfigurationValueUpdatedEvent extends BaseEvent {
+ private ConfigurationValue oldValue;
+ private ConfigurationValue newValue;
+
+ public ConfigurationValueUpdatedEvent(ConfigurationValue oldValue, ConfigurationValue newValue) {
+ this.oldValue = oldValue;
+ this.newValue = newValue;
+ }
+
+ public ConfigurationValue getOldValue() {
+ return oldValue;
+ }
+
+ public ConfigurationValue getNewValue() {
+ return newValue;
+ }
+
+ @Override
+ public String toString() {
+ String id, oldValue, newValue;
+
+ id = this.newValue.getId();
+ oldValue = (this.oldValue == null) ? "null" : this.oldValue.getValue();
+ newValue = this.newValue.getValue();
+
+ return String.format("[ConfigurationValueUpdatedEvent id=%s, old_value=%s, new_value=%s]",
+ id, oldValue, newValue);
+ }
+}
@@ -0,0 +1,20 @@
+package com.rackspacecloud.client.service_registry.events.server;
+
+import com.rackspacecloud.client.service_registry.objects.Service;
+
+public class ServiceJoinEvent extends BaseEvent {
+ private Service service;
+
+ public ServiceJoinEvent(Service service) {
+ this.service = service;
+ }
+
+ public Service getService() {
+ return service;
+ }
+
+ @Override
+ public String toString() {
+ return String.format("[ServiceJoinEvent id=%s]", this.service.getId());
+ }
+}
@@ -0,0 +1,20 @@
+package com.rackspacecloud.client.service_registry.events.server;
+
+import com.rackspacecloud.client.service_registry.objects.Service;
+
+public class ServiceTimeoutEvent extends BaseEvent {
+ private Service service;
+
+ public ServiceTimeoutEvent(Service service) {
+ this.service = service;
+ }
+
+ public Service getService() {
+ return service;
+ }
+
+ @Override
+ public String toString() {
+ return String.format("[ServiceTimeoutEvent id=%s]", this.service.getId());
+ }
+}
Oops, something went wrong.

0 comments on commit c9c7829

Please sign in to comment.