Skip to content

Commit

Permalink
[aggregate] Changed to not implement Collection
Browse files Browse the repository at this point in the history
The bnd DS Annotations code got confused if
the Aggregate implemented Collection and it
was used as the intended @reference.

So you now need to call getServices() to get
the services.
  • Loading branch information
pkriens committed Oct 12, 2022
1 parent 84c48ad commit 1bb26b4
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
package biz.aQute.aggregate.provider;

import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Collection;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.osgi.framework.Bundle;
import org.osgi.framework.ServiceFactory;
import org.osgi.framework.ServiceRegistration;
import org.osgi.util.tracker.ServiceTracker;

import biz.aQute.aggregate.api.Aggregate;
import biz.aQute.aggregate.provider.TrackedService.ActualType;

/**
Expand All @@ -23,11 +26,12 @@
})
class ActualTypeFactory implements ServiceFactory {
final ActualType atr;
final Map<Bundle, ServiceTracker> trackers = new HashMap<>();
final Map<Bundle, ServiceTracker> trackers = new HashMap<>();
final Class serviceType;
final AggregateState state;
ServiceRegistration reg;
boolean closed;
Method GET_SERVICES = null;

ActualTypeFactory(ActualType registration, AggregateState state, Class serviceType) {
this.atr = registration;
Expand All @@ -47,15 +51,15 @@ public Object getService(Bundle bundle, ServiceRegistration registration) {
if (m.getDeclaringClass() == Object.class) {
return m.invoke(ActualTypeFactory.this, a);
}
if (Collection.class.isAssignableFrom(m.getDeclaringClass())) {
if (m == GET_SERVICES || isGetServices(m)) {
ServiceTracker tracker = getTracked(bundle);
Collection values;
List values;
if (tracker == null)
values = Collections.emptyList();
else
values = tracker.getTracked()
.values();
return m.invoke(values, a);
values = new ArrayList(tracker.getTracked()
.values());
return values;
}
atr.logger.error("invalid invocation for method %s", m);
throw new UnsupportedOperationException("This was registered as an indication of the aggregate state for "
Expand All @@ -64,6 +68,15 @@ public Object getService(Bundle bundle, ServiceRegistration registration) {
return instance;
}

private synchronized boolean isGetServices(Method m) {
if (m.getDeclaringClass() == Aggregate.class && m.getName()
.equals("getServices") && m.getReturnType() == List.class && m.getParameterCount() == 0) {
GET_SERVICES = m;
return true;
}
return false;
}

private ServiceTracker getTracked(Bundle bundle) {
ServiceTracker tracker;
synchronized (this) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ public void straightForwardTest() throws Exception {
assertThat(aggReq1.list).hasSize(3);

int n = 0;
for (IF x : ref1.get().iff) {
for (IF x : ref1.get().iff.getServices()) {
assertThat(x).isNotNull();
n++;
}
Expand All @@ -182,7 +182,7 @@ public void straightForwardTest() throws Exception {
assertThat(ar.isSatisfied()).isTrue();

n = 0;
for (IF x : ref1.get().iff) {
for (IF x : ref1.get().iff.getServices()) {
assertThat(x).isNotNull();
n++;
}
Expand Down Expand Up @@ -215,7 +215,7 @@ public void straightForwardTest() throws Exception {
.getServiceReference(AggIF1.class) != null);

n = 0;
for (IF x : ref1.get().iff) {
for (IF x : ref1.get().iff.getServices()) {
assertThat(x).isNotNull();
n++;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package biz.aQute.aggregate.api;

import java.util.Collection;
import java.util.List;

public interface Aggregate<S> extends Collection<S> {
public interface Aggregate<S> {

List<S> getServices();

}

0 comments on commit 1bb26b4

Please sign in to comment.