Skip to content

Commit

Permalink
feat(event): Add initial implementation of the internal event bus
Browse files Browse the repository at this point in the history
  • Loading branch information
brasseld committed Jun 15, 2015
1 parent c409f52 commit 0796924
Show file tree
Hide file tree
Showing 27 changed files with 593 additions and 171 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
@Singleton
@Produces(MediaType.APPLICATION_JSON)
@Path("/apis")
// TODO handle messages responses
// TODO doNext messages responses
public class ApiResource extends AbstractResource {

@GET
Expand Down
4 changes: 2 additions & 2 deletions gateway/api/src/main/java/io/gravitee/gateway/api/Policy.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
*/
public interface Policy {

void onRequest(final Request request, final Response response, final PolicyHandler handler);
void onRequest(final Request request, final Response response, final PolicyChain handler);

void onResponse(final Request request, final Response response, final PolicyHandler handler);
void onResponse(final Request request, final Response response, final PolicyChain handler);

String name();
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@
*
* @author David BRASSELY (brasseld at gmail.com)
*/
public interface PolicyHandler {
public interface PolicyChain {

void handle(Request request, Response response);

void fail(Request request, Response response, Throwable throwable);
void doNext(Request request, Response response);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* Copyright (C) 2015 The Gravitee team (http://gravitee.io)
*
* 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 io.gravitee.gateway.api;

/**
* @author David BRASSELY (brasseld at gmail.com)
*/
public interface PolicyResult {
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
*
* @author David BRASSELY (brasseld at gmail.com)
*/
public interface Reactor {
public interface Reactor<T> {

Observable<Response> process(Request request);
T process(Request request);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* Copyright (C) 2015 The Gravitee team (http://gravitee.io)
*
* 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 io.gravitee.gateway.core.event;

/**
* @author David BRASSELY (brasseld at gmail.com)
*/
public interface Event<T extends Enum, S> {

S content();

T type();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* Copyright (C) 2015 The Gravitee team (http://gravitee.io)
*
* 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 io.gravitee.gateway.core.event;

/**
* @author David BRASSELY (brasseld at gmail.com)
*/
public interface EventListener<T extends Enum, S> {

void onEvent(Event<T, S> event);
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,18 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.gravitee.gateway.core.policy;

import io.gravitee.gateway.api.Policy;
import io.gravitee.gateway.api.Request;
import io.gravitee.gateway.api.Response;

import java.util.Set;
package io.gravitee.gateway.core.event;

/**
* @author David BRASSELY (brasseld at gmail.com)
*/
public class RequestPolicyHandler extends AbstractPolicyHandler {
public interface EventManager {

void publishEvent(Enum type, Object content);

void publishEvent(Event event);

public RequestPolicyHandler(Set<Policy> policies) {
super(policies);
}
<T extends Enum> void subscribeForEvents(EventListener<T, ?> eventListener, Class<T> events);

@Override
public void handle(Request request, Response response) {
if (iterator().hasNext()) {
Policy first = iterator().next();
first.onRequest(request, response, this);
}
}
<T extends Enum> void subscribeForEvents(EventListener<T, ?> eventListener, T... events);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/**
* Copyright (C) 2015 The Gravitee team (http://gravitee.io)
*
* 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 io.gravitee.gateway.core.event.impl;

import io.gravitee.gateway.core.event.Event;
import io.gravitee.gateway.core.event.EventListener;
import io.gravitee.gateway.core.event.EventManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;

/**
* @author David BRASSELY (brasseld at gmail.com)
*/
public class EventManagerImpl implements EventManager {

private static final Logger LOGGER = LoggerFactory.getLogger(EventManagerImpl.class);

private Map<ComparableEventType, Set<EventListenerWrapper>> listenersMap = new TreeMap();

public void publishEvent(Enum type, Object content) {
this.publishEvent(new SimpleEvent(type, content));
}

public void publishEvent(Event event) {
LOGGER.debug("Publish event {} - {}", event.type(), event.content());

Set<EventListenerWrapper> listeners = getEventListeners(event.type().getClass());
for(EventListenerWrapper listener : listeners) {
listener.eventListener().onEvent(event);
}
}

public <T extends Enum> void subscribeForEvents(EventListener<T, ?> eventListener, T... events) {
for( T event : events) {
addEventListener(eventListener, (Class<T>) event.getClass(), Arrays.asList(events));
}
}

public <T extends Enum> void subscribeForEvents(EventListener<T, ?> eventListener, Class<T> events) {
addEventListener(eventListener, events, EnumSet.allOf(events));
}

private <T extends Enum> void addEventListener(EventListener<T, ?> eventListener, Class<T> enumClass, Collection<T> events) {
LOGGER.info("Register new listener {} for event type {}", eventListener.getClass().getSimpleName(), enumClass);

Set<EventListenerWrapper> listeners = getEventListeners(enumClass);
listeners.add(new EventListenerWrapper(eventListener, events));
}

private <T extends Enum> Set<EventListenerWrapper> getEventListeners(Class<T> eventType) {
Set<EventListenerWrapper> listeners = this.listenersMap.get(new ComparableEventType(eventType));

if (listeners == null) {
listeners = new HashSet();
this.listenersMap.put(new ComparableEventType(eventType), listeners);
}

return listeners;
}

private class EventListenerWrapper<T extends Enum> {

private final EventListener<T, ?> eventListener;
private final Set<T> events;

public EventListenerWrapper(EventListener<T, ?> eventListener, Collection<T> events) {
this.eventListener = eventListener;
this.events = new HashSet(events);
}

public EventListener<T, ?> eventListener() {
return eventListener;
}

public Set<T> events() {
return events;
}
}

private class ComparableEventType<T> implements Comparable<ComparableEventType<T>> {

private static final int HASH = 7 * 89;
private final Class<? extends T> wrappedClass;

public ComparableEventType(Class<? extends T> wrappedClass) {
this.wrappedClass = wrappedClass;
}

@Override
public int compareTo(ComparableEventType<T> o) {
return wrappedClass.getCanonicalName().compareTo(o.wrappedClass.getCanonicalName());
}

@Override
public int hashCode() {
return HASH + (this.wrappedClass != null ? this.wrappedClass.hashCode() : 0);
}

@Override
public boolean equals(Object o) {
if (!(o instanceof ComparableEventType)) {
return false;
}

return compareTo((ComparableEventType<T>) o) == 0;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,28 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.gravitee.gateway.core.policy;
package io.gravitee.gateway.core.event.impl;

import io.gravitee.gateway.api.Policy;
import io.gravitee.gateway.api.Request;
import io.gravitee.gateway.api.Response;

import java.util.Set;
import io.gravitee.gateway.core.event.Event;

/**
* @author David BRASSELY (brasseld at gmail.com)
*/
public class ResponsePolicyHandler extends AbstractPolicyHandler {
public class SimpleEvent<T extends Enum, S> implements Event<T, S> {

private final T type;
private final S content;

public SimpleEvent(T type, S content) {
this.type = type;
this.content = content;
}

public ResponsePolicyHandler(Set<Policy> policies) {
super(policies);
}
@Override public S content() {
return this.content;
}

@Override
public void handle(Request request, Response response) {
if (iterator().hasNext()) {
Policy first = iterator().next();
first.onResponse(request, response, this);
}
}
@Override public T type() {
return this.type;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package io.gravitee.gateway.core.handler;

import io.gravitee.gateway.api.Request;
import io.gravitee.gateway.api.Response;

/**
* @author David BRASSELY (brasseld at gmail.com)
*/
public class ContextHandler implements Handler {

@Override
public void handle(Request request, Response response) {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package io.gravitee.gateway.core.handler;

import io.gravitee.gateway.api.Request;
import io.gravitee.gateway.api.Response;

/**
* @author David BRASSELY (brasseld at gmail.com)
*/
public interface Handler {

void handle(Request request, Response response);
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@
package io.gravitee.gateway.core.policy;

import io.gravitee.gateway.api.Policy;
import io.gravitee.gateway.api.PolicyHandler;
import io.gravitee.gateway.api.Request;
import io.gravitee.gateway.api.Response;
import io.gravitee.gateway.api.PolicyChain;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -28,23 +26,18 @@
/**
* @author David BRASSELY (brasseld at gmail.com)
*/
public abstract class AbstractPolicyHandler implements PolicyHandler {
public abstract class AbstractPolicyChain implements PolicyChain {

private final Logger LOGGER = LoggerFactory.getLogger(getClass());

private Set<Policy> policies;
private Iterator<Policy> policyIterator;

protected AbstractPolicyHandler(final Set<Policy> policies) {
protected AbstractPolicyChain(final Set<Policy> policies) {
this.policies = policies;
this.policyIterator = policies.iterator();
}

@Override
public void fail(Request request, Response response, Throwable throwable) {
LOGGER.info("Exit policy chain...");
}

public Set<Policy> getPolicies() {
return this.policies;
}
Expand Down
Loading

0 comments on commit 0796924

Please sign in to comment.