Skip to content

Commit

Permalink
fix unnamed service callback with second name argument accepts any se…
Browse files Browse the repository at this point in the history
…rvice, for multiple services in a class
  • Loading branch information
greg-higgins committed Aug 28, 2024
1 parent 779d179 commit 63030ef
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,24 @@ public void svcNamedCallbackRegisterShortcutTest() {

}

@Test
public void multiServicesTest() {
sep(c -> {
c.addNode(new MultiServiceListenerNode(), "myListener");
});

MyServiceImpl svcA = new MyServiceImpl("svc_A");
sep.registerService(svcA, MyService.class, "svc_A");

MyService2Impl svc2 = new MyService2Impl();
sep.registerService(svc2, MyService2.class, "svc_2");

MultiServiceListenerNode node = getField("myListener");

Assert.assertEquals("svc_A", node.serviceName);
Assert.assertEquals("svc_2", node.serviceName2);
}

public static class ServiceListenerNode {

private String name;
Expand Down Expand Up @@ -205,6 +223,21 @@ public boolean onMyEvent(String event) {
}
}

@Data
public static class MultiServiceListenerNode {
private String serviceName;
private String serviceName2;

@ServiceRegistered
public void registerMyService(MyService service, String serviceName) {
this.serviceName = serviceName;
}

@ServiceRegistered
public void registerMyService2(MyService2 service, String serviceName) {
this.serviceName2 = serviceName;
}
}

public interface MyService {
String getName();
Expand All @@ -214,4 +247,10 @@ public interface MyService {
public static class MyServiceImpl implements MyService {
private final String name;
}

public interface MyService2 {
}

public static class MyService2Impl implements MyService2 {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@

import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;

/**
* Manages service registrations and de-registrations pushing services into nodes that have methods annotated with:
Expand All @@ -40,9 +37,9 @@ public class ServiceRegistryNode
@FluxtionIgnore
private final Map<RegistrationKey, List<Callback>> serviceDeregisterCallbackMap = new HashMap<>();
@FluxtionIgnore
private final List<Callback> serviceWithNameCallbacks = new ArrayList<>();
private final Map<Class<?>, List<Callback>> serviceWithNameCallbacks = new HashMap<>();
@FluxtionIgnore
private final List<Callback> serviceDeregisterWithNameCallbacks = new ArrayList<>();
private final Map<Class<?>, List<Callback>> serviceDeregisterWithNameCallbacks = new HashMap<>();
@FluxtionIgnore
private final RegistrationKey tempKey = new RegistrationKey();

Expand All @@ -63,7 +60,7 @@ public void registerService(Service<?> service) {
}
}

for (Callback nameCallback : serviceWithNameCallbacks) {
for (Callback nameCallback : serviceWithNameCallbacks.getOrDefault(service.serviceClass(), Collections.emptyList())) {
nameCallback.invoke(service.instance(), service.serviceName());
}
}
Expand All @@ -81,7 +78,7 @@ public void deRegisterService(Service<?> service) {
}
}

for (Callback nameCallback : serviceDeregisterWithNameCallbacks) {
for (Callback nameCallback : serviceDeregisterWithNameCallbacks.getOrDefault(service.serviceClass(), Collections.emptyList())) {
nameCallback.invoke(service.instance(), service.serviceName());
}

Expand Down Expand Up @@ -115,7 +112,12 @@ public void nodeRegistered(Object node, String nodeName) {
parameterType,
registerAnnotation.value().isEmpty() ? parameterType.getCanonicalName() : registerAnnotation.value());
if (namedService) {
serviceWithNameCallbacks.add(new Callback(method, node, true));
serviceWithNameCallbacks.compute(parameterType,
(k, v) -> {
List<Callback> list = v == null ? new ArrayList<>() : v;
list.add(new Callback(method, node, namedService));
return list;
});
} else {
serviceCallbackMap.compute(key,
(k, v) -> {
Expand All @@ -137,7 +139,13 @@ public void nodeRegistered(Object node, String nodeName) {
deregisterAnnotation.value().isEmpty() ? parameterType.getCanonicalName() : deregisterAnnotation.value());

if (namedService) {
serviceDeregisterWithNameCallbacks.add(new Callback(method, node, true));
// serviceDeregisterWithNameCallbacks.add(new Callback(method, node, true));
serviceDeregisterWithNameCallbacks.compute(parameterType,
(k, v) -> {
List<Callback> list = v == null ? new ArrayList<>() : v;
list.add(new Callback(method, node, namedService));
return list;
});
} else {
serviceDeregisterCallbackMap.compute(key,
(k, v) -> {
Expand Down

0 comments on commit 63030ef

Please sign in to comment.