Skip to content
Permalink
Browse files
Initial support for remote events using OSGi Remote Services
  • Loading branch information
timothyjward committed Oct 1, 2020
1 parent c9f16c1 commit 07b5f41a8e5d8459801d7703a71f887a659ac55c
Showing 28 changed files with 2,382 additions and 54 deletions.
@@ -19,6 +19,11 @@

import static java.util.Collections.emptyMap;
import static java.util.stream.Collectors.toList;
import static org.osgi.namespace.implementation.ImplementationNamespace.IMPLEMENTATION_NAMESPACE;
import static org.osgi.namespace.service.ServiceNamespace.SERVICE_NAMESPACE;
import static org.osgi.service.typedevent.TypedEventConstants.TYPED_EVENT_FILTER;
import static org.osgi.service.typedevent.TypedEventConstants.TYPED_EVENT_IMPLEMENTATION;
import static org.osgi.service.typedevent.TypedEventConstants.TYPED_EVENT_SPECIFICATION_VERSION;
import static org.osgi.util.converter.Converters.standardConverter;

import java.lang.reflect.ParameterizedType;
@@ -34,6 +39,7 @@
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Stream;

import org.osgi.annotation.bundle.Capability;
import org.osgi.framework.Constants;
import org.osgi.framework.Filter;
import org.osgi.framework.FrameworkUtil;
@@ -45,6 +51,8 @@
import org.osgi.service.typedevent.UntypedEventHandler;
import org.osgi.util.converter.TypeReference;

@Capability(namespace=SERVICE_NAMESPACE, attribute="objectClass:List<String>=org.osgi.service.typedevent.TypedEventBus", uses=TypedEventBus.class)
@Capability(namespace=IMPLEMENTATION_NAMESPACE, name=TYPED_EVENT_IMPLEMENTATION, version=TYPED_EVENT_SPECIFICATION_VERSION)
public class TypedEventBusImpl implements TypedEventBus {

private static final TypeReference<List<String>> LIST_OF_STRINGS = new TypeReference<List<String>>() {
@@ -248,7 +256,7 @@ private Long getServiceId(Map<String, Object> properties) {
}

private Filter getFilter(Long serviceId, Map<String, Object> properties) throws IllegalArgumentException {
String key = "event.filter";
String key = TYPED_EVENT_FILTER;
return getFilter(serviceId, key, properties.get(key));
}

@@ -21,6 +21,11 @@
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.argThat;
import static org.mockito.ArgumentMatchers.eq;
import static org.osgi.framework.Constants.SERVICE_ID;
import static org.osgi.service.typedevent.TypedEventConstants.TYPED_EVENT_FILTER;
import static org.osgi.service.typedevent.TypedEventConstants.TYPED_EVENT_TOPICS;
import static org.osgi.service.typedevent.TypedEventConstants.TYPED_EVENT_TYPE;
import static org.osgi.util.converter.Converters.standardConverter;

import java.util.HashMap;
import java.util.Map;
@@ -35,11 +40,9 @@
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.osgi.framework.Constants;
import org.osgi.service.typedevent.TypedEventConstants;
import org.osgi.service.typedevent.TypedEventHandler;
import org.osgi.service.typedevent.UnhandledEventHandler;
import org.osgi.service.typedevent.UntypedEventHandler;
import org.osgi.util.converter.Converters;

public class TypedEventBusImplTest {

@@ -132,31 +135,31 @@ public void testEventSending() throws InterruptedException {

Map<String, Object> serviceProperties = new HashMap<>();

serviceProperties.put(TypedEventConstants.TYPED_EVENT_TOPICS, TEST_EVENT_TOPIC);
serviceProperties.put(TypedEventConstants.TYPED_EVENT_TYPE, TestEvent.class.getName());
serviceProperties.put(Constants.SERVICE_ID, 42L);
serviceProperties.put(TYPED_EVENT_TOPICS, TEST_EVENT_TOPIC);
serviceProperties.put(TYPED_EVENT_TYPE, TestEvent.class.getName());
serviceProperties.put(SERVICE_ID, 42L);

impl.addTypedEventHandler(handlerA, serviceProperties);

serviceProperties = new HashMap<>();

serviceProperties.put(TypedEventConstants.TYPED_EVENT_TOPICS, TestEvent2.class.getName().replace(".", "/"));
serviceProperties.put(TypedEventConstants.TYPED_EVENT_TYPE, TestEvent2.class.getName());
serviceProperties.put(Constants.SERVICE_ID, 43L);
serviceProperties.put(TYPED_EVENT_TOPICS, TestEvent2.class.getName().replace(".", "/"));
serviceProperties.put(TYPED_EVENT_TYPE, TestEvent2.class.getName());
serviceProperties.put(SERVICE_ID, 43L);

impl.addTypedEventHandler(handlerB, serviceProperties);

serviceProperties = new HashMap<>();

serviceProperties.put(TypedEventConstants.TYPED_EVENT_TOPICS, TEST_EVENT_TOPIC);
serviceProperties.put(Constants.SERVICE_ID, 44L);
serviceProperties.put(TYPED_EVENT_TOPICS, TEST_EVENT_TOPIC);
serviceProperties.put(SERVICE_ID, 44L);

impl.addUntypedEventHandler(untypedHandlerA, serviceProperties);

serviceProperties = new HashMap<>();

serviceProperties.put(TypedEventConstants.TYPED_EVENT_TOPICS, TestEvent2.class.getName().replace(".", "/"));
serviceProperties.put(Constants.SERVICE_ID, 45L);
serviceProperties.put(TYPED_EVENT_TOPICS, TestEvent2.class.getName().replace(".", "/"));
serviceProperties.put(SERVICE_ID, 45L);

impl.addUntypedEventHandler(untypedHandlerB, serviceProperties);

@@ -206,20 +209,20 @@ public void testGenericTypeInference() throws InterruptedException {
event.message = "boo";

Map<String, Object> serviceProperties = new HashMap<>();
serviceProperties.put(Constants.SERVICE_ID, 42L);
serviceProperties.put(SERVICE_ID, 42L);

impl.addTypedEventHandler(handler, serviceProperties);

serviceProperties = new HashMap<>();

serviceProperties.put(TypedEventConstants.TYPED_EVENT_TYPE, SpecialTestEvent.class.getName());
serviceProperties.put(Constants.SERVICE_ID, 43L);
serviceProperties.put(TYPED_EVENT_TYPE, SpecialTestEvent.class.getName());
serviceProperties.put(SERVICE_ID, 43L);

impl.addTypedEventHandler(handler2, serviceProperties);

serviceProperties = new HashMap<>();

serviceProperties.put(Constants.SERVICE_ID, 44L);
serviceProperties.put(SERVICE_ID, 44L);

impl.addTypedEventHandler(handler3, serviceProperties);

@@ -258,35 +261,35 @@ public void testUntypedEventSending() throws InterruptedException {

Map<String, Object> serviceProperties = new HashMap<>();

serviceProperties.put(TypedEventConstants.TYPED_EVENT_TOPICS, TestEvent.class.getName());
serviceProperties.put(TypedEventConstants.TYPED_EVENT_TYPE, TestEvent.class.getName());
serviceProperties.put(Constants.SERVICE_ID, 42L);
serviceProperties.put(TYPED_EVENT_TOPICS, TestEvent.class.getName());
serviceProperties.put(TYPED_EVENT_TYPE, TestEvent.class.getName());
serviceProperties.put(SERVICE_ID, 42L);

impl.addTypedEventHandler(handlerA, serviceProperties);

serviceProperties = new HashMap<>();

serviceProperties.put(TypedEventConstants.TYPED_EVENT_TOPICS, TestEvent2.class.getName());
serviceProperties.put(TypedEventConstants.TYPED_EVENT_TYPE, TestEvent2.class.getName());
serviceProperties.put(Constants.SERVICE_ID, 43L);
serviceProperties.put(TYPED_EVENT_TOPICS, TestEvent2.class.getName());
serviceProperties.put(TYPED_EVENT_TYPE, TestEvent2.class.getName());
serviceProperties.put(SERVICE_ID, 43L);

impl.addTypedEventHandler(handlerB, serviceProperties);

serviceProperties = new HashMap<>();

serviceProperties.put(TypedEventConstants.TYPED_EVENT_TOPICS, TestEvent.class.getName());
serviceProperties.put(Constants.SERVICE_ID, 44L);
serviceProperties.put(TYPED_EVENT_TOPICS, TestEvent.class.getName());
serviceProperties.put(SERVICE_ID, 44L);

impl.addUntypedEventHandler(untypedHandlerA, serviceProperties);

serviceProperties = new HashMap<>();

serviceProperties.put(TypedEventConstants.TYPED_EVENT_TOPICS, TestEvent2.class.getName());
serviceProperties.put(Constants.SERVICE_ID, 45L);
serviceProperties.put(TYPED_EVENT_TOPICS, TestEvent2.class.getName());
serviceProperties.put(SERVICE_ID, 45L);

impl.addUntypedEventHandler(untypedHandlerB, serviceProperties);

impl.deliver(event.getClass().getName(), Converters.standardConverter().convert(event).to(Map.class));
impl.deliver(event.getClass().getName(), standardConverter().convert(event).to(Map.class));

assertTrue(semA.tryAcquire(1, TimeUnit.SECONDS));

@@ -314,35 +317,35 @@ public void testEventFiltering() throws InterruptedException {

Map<String, Object> serviceProperties = new HashMap<>();

serviceProperties.put(TypedEventConstants.TYPED_EVENT_TOPICS, TEST_EVENT_TOPIC);
serviceProperties.put(TypedEventConstants.TYPED_EVENT_TYPE, TestEvent.class.getName());
serviceProperties.put("event.filter", "(message=foo)");
serviceProperties.put(Constants.SERVICE_ID, 42L);
serviceProperties.put(TYPED_EVENT_TOPICS, TEST_EVENT_TOPIC);
serviceProperties.put(TYPED_EVENT_TYPE, TestEvent.class.getName());
serviceProperties.put(TYPED_EVENT_FILTER, "(message=foo)");
serviceProperties.put(SERVICE_ID, 42L);

impl.addTypedEventHandler(handlerA, serviceProperties);

serviceProperties = new HashMap<>();

serviceProperties.put(TypedEventConstants.TYPED_EVENT_TOPICS, TEST_EVENT_TOPIC);
serviceProperties.put(TypedEventConstants.TYPED_EVENT_TYPE, TestEvent.class.getName());
serviceProperties.put("event.filter", "(message=bar)");
serviceProperties.put(Constants.SERVICE_ID, 43L);
serviceProperties.put(TYPED_EVENT_TOPICS, TEST_EVENT_TOPIC);
serviceProperties.put(TYPED_EVENT_TYPE, TestEvent.class.getName());
serviceProperties.put(TYPED_EVENT_FILTER, "(message=bar)");
serviceProperties.put(SERVICE_ID, 43L);

impl.addTypedEventHandler(handlerB, serviceProperties);

serviceProperties = new HashMap<>();

serviceProperties.put(TypedEventConstants.TYPED_EVENT_TOPICS, TEST_EVENT_TOPIC);
serviceProperties.put("event.filter", "(message=foo)");
serviceProperties.put(Constants.SERVICE_ID, 44L);
serviceProperties.put(TYPED_EVENT_TOPICS, TEST_EVENT_TOPIC);
serviceProperties.put(TYPED_EVENT_FILTER, "(message=foo)");
serviceProperties.put(SERVICE_ID, 44L);

impl.addUntypedEventHandler(untypedHandlerA, serviceProperties);

serviceProperties = new HashMap<>();

serviceProperties.put(TypedEventConstants.TYPED_EVENT_TOPICS, TEST_EVENT_TOPIC);
serviceProperties.put("event.filter", "(message=bar)");
serviceProperties.put(Constants.SERVICE_ID, 45L);
serviceProperties.put(TYPED_EVENT_TOPICS, TEST_EVENT_TOPIC);
serviceProperties.put(TYPED_EVENT_FILTER, "(message=bar)");
serviceProperties.put(SERVICE_ID, 45L);

impl.addUntypedEventHandler(untypedHandlerB, serviceProperties);

@@ -395,10 +398,10 @@ public void testEventFilteringWithEmptyStringFilter() throws InterruptedExceptio

Map<String, Object> serviceProperties = new HashMap<>();

serviceProperties.put(TypedEventConstants.TYPED_EVENT_TOPICS, TEST_EVENT_TOPIC);
serviceProperties.put(TypedEventConstants.TYPED_EVENT_TYPE, TestEvent.class.getName());
serviceProperties.put("event.filter", "");
serviceProperties.put(Constants.SERVICE_ID, 42L);
serviceProperties.put(TYPED_EVENT_TOPICS, TEST_EVENT_TOPIC);
serviceProperties.put(TYPED_EVENT_TYPE, TestEvent.class.getName());
serviceProperties.put(TYPED_EVENT_FILTER, "");
serviceProperties.put(SERVICE_ID, 42L);

impl.addTypedEventHandler(handlerA, serviceProperties);

@@ -420,10 +423,10 @@ public void testUnhandledEventHandlers() throws InterruptedException {

Map<String, Object> serviceProperties = new HashMap<>();

serviceProperties.put(TypedEventConstants.TYPED_EVENT_TOPICS, TEST_EVENT_TOPIC);
serviceProperties.put(TypedEventConstants.TYPED_EVENT_TYPE, TestEvent.class.getName());
serviceProperties.put("event.filter", "(message=foo)");
serviceProperties.put(Constants.SERVICE_ID, 42L);
serviceProperties.put(TYPED_EVENT_TOPICS, TEST_EVENT_TOPIC);
serviceProperties.put(TYPED_EVENT_TYPE, TestEvent.class.getName());
serviceProperties.put(TYPED_EVENT_FILTER, "(message=foo)");
serviceProperties.put(SERVICE_ID, 42L);

impl.addTypedEventHandler(handlerA, serviceProperties);

@@ -16,6 +16,8 @@
*/
package org.apache.aries.typedevent.bus.osgi;

import static org.osgi.service.typedevent.TypedEventConstants.TYPED_EVENT_FILTER;

import java.util.Dictionary;
import java.util.Hashtable;

@@ -71,12 +73,12 @@ public void stop() throws Exception {
@Test
public void testFilteredListener() throws Exception {
Dictionary<String, Object> props = new Hashtable<>();
props.put("event.filter", "(message=foo)");
props.put(TYPED_EVENT_FILTER, "(message=foo)");

regs.add(context.registerService(TypedEventHandler.class, typedEventHandler, props));

props = new Hashtable<>();
props.put("event.filter", "(message=bar)");
props.put(TYPED_EVENT_FILTER, "(message=bar)");

regs.add(context.registerService(TypedEventHandler.class, typedEventHandlerB, props));

@@ -107,7 +109,7 @@ public void testFilteredListener() throws Exception {
@Test
public void testFilteredListenerEmptyString() throws Exception {
Dictionary<String, Object> props = new Hashtable<>();
props.put("event.filter", "");
props.put(TYPED_EVENT_FILTER, "");


regs.add(context.registerService(TypedEventHandler.class, typedEventHandler, props));
@@ -24,6 +24,7 @@
import static org.mockito.Mockito.after;
import static org.mockito.Mockito.timeout;
import static org.mockito.Mockito.verify;
import static org.osgi.service.typedevent.TypedEventConstants.TYPED_EVENT_FILTER;

import java.util.Dictionary;
import java.util.Hashtable;
@@ -128,7 +129,7 @@ public void testUnhandledDueToTopic() throws InterruptedException {
public void testUnhandledDueToFilter() throws InterruptedException {

Dictionary<String, Object> props = new Hashtable<>();
props.put("event.filter", "(message=foo)");
props.put(TYPED_EVENT_FILTER, "(message=foo)");

regs.add(context.registerService(TypedEventHandler.class, typedEventHandler, props));

@@ -0,0 +1,41 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.apache.aries.typedevent</groupId>
<artifactId>typedevent-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>

<groupId>org.apache.aries.typedevent.remote</groupId>
<artifactId>org.apache.aries.typedevent.remote.api</artifactId>

<dependencies>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.core</artifactId>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.service.typedevent</artifactId>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>osgi.annotation</artifactId>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.annotation.bundle</artifactId>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>biz.aQute.bnd</groupId>
<artifactId>bnd-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
@@ -0,0 +1,33 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.aries.typedevent.remote.api;

import org.osgi.annotation.versioning.ProviderType;
import org.osgi.dto.DTO;

/**
* A monitoring event filter.
*
* If both LDAP and regular expressions are supplied, then both must match.
*/
@ProviderType
public class FilterDTO extends DTO {

public String ldapExpression;

public String regularExpression;
}

0 comments on commit 07b5f41

Please sign in to comment.