Skip to content

Commit

Permalink
[aggregate] Immediate
Browse files Browse the repository at this point in the history
The service needs to be immediate
  • Loading branch information
pkriens committed Oct 13, 2022
1 parent 1bb26b4 commit 80ca439
Show file tree
Hide file tree
Showing 22 changed files with 287 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
@SuppressWarnings({
"rawtypes", "unchecked"
})
@Component(property = "condition=true", service = AggregateState.class)
@Component(property = "condition=true", service = AggregateState.class, immediate = true)
@AggregateImplementation
public class AggregateState {
final static Class ARCHETYPE = Aggregate.class;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import java.util.stream.Collectors;

import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.FrameworkUtil;
import org.osgi.framework.ServiceReference;
import org.osgi.framework.ServiceRegistration;
import org.osgi.util.tracker.ServiceTracker;
Expand All @@ -23,15 +25,15 @@
})
class TrackedService {

final AggregateState state;
final HLogger logger;
final Map<Class, ActualType> actualTypes = new HashMap<>();
final Map<Bundle, BundleInfo> bundleInfos = new HashMap<>();
final Class serviceType;
final AggregateState state;
final HLogger logger;
final Map<Class, ActualType> actualTypes = new HashMap<>();
final Map<Bundle, BundleInfo> bundleInfos = new HashMap<>();
final Class serviceType;
final ServiceTracker<Object, ServiceReference<Object>> tracker;
final int override;
final int override;

int registeredServices = 0;
int registeredServices = 0;

class BundleInfo {
final Bundle bundle;
Expand Down Expand Up @@ -116,7 +118,9 @@ private void register() {
logger.debug("registering %s", actualType);
try {
ActualTypeFactory instance = new ActualTypeFactory(this, state, serviceType);
ServiceRegistration reg = state.context.registerService(actualType.getName(), instance, null);
Bundle bundle = FrameworkUtil.getBundle(actualType);
BundleContext context = bundle == null ? state.context : bundle.getBundleContext();
ServiceRegistration reg = context.registerService(actualType.getName(), instance, null);
synchronized (state) {
instance.reg = reg;
if (!state.closed) {
Expand Down
11 changes: 11 additions & 0 deletions biz.aQute.aggregate.test/.classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry kind="con" path="aQute.bnd.classpath.container"/>
<classpathentry kind="src" output="target/classes" path="src/main/java"/>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
</classpath>
23 changes: 23 additions & 0 deletions biz.aQute.aggregate.test/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>biz.aQute.aggregate.test</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>bndtools.core.bndbuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>bndtools.core.bndnature</nature>
</natures>
</projectDescription>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
eclipse.preferences.version=1
encoding/.classpath=UTF-8
encoding//src/test/java/biz/aQute/aggregate/test/AggregateTest.java=UTF-8
encoding/<project>=UTF-8
encoding/bnd.bnd=UTF-8
12 changes: 12 additions & 0 deletions biz.aQute.aggregate.test/.settings/org.eclipse.jdt.core.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.release=enabled
org.eclipse.jdt.core.compiler.source=1.8
1 change: 1 addition & 0 deletions biz.aQute.aggregate.test/b1.bnd
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-includepackage: biz.aQute.aggregate.test.b1
1 change: 1 addition & 0 deletions biz.aQute.aggregate.test/b2.bnd
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-includepackage: biz.aQute.aggregate.test.b2
1 change: 1 addition & 0 deletions biz.aQute.aggregate.test/b3.bnd
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-includepackage: biz.aQute.aggregate.test.b3
24 changes: 24 additions & 0 deletions biz.aQute.aggregate.test/bnd.bnd
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#
# biz.aQute.aggregate.test PROVIDER BUNDLE
#

-buildpath: \
osgi.annotation,\
org.osgi.service.component.annotations,\
org.osgi.framework,\
org.osgi.resource,\
slf4j.api,\
biz.aQute.api.aggregate,\
biz.aQute.osgi.diverse.util;version=snapshot

-testpath: \
biz.aQute.wrapper.junit,\
biz.aQute.wrapper.hamcrest,\
org.assertj.core,\
biz.aQute.launchpad,\
org.osgi.util.tracker,\
org.osgi.dto,\
org.awaitility,\
slf4j.simple

-sub: *.bnd
1 change: 1 addition & 0 deletions biz.aQute.aggregate.test/main.bnd
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-includepackage: biz.aQute.aggregate.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package biz.aQute.aggregate.test;

public interface Foo {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package biz.aQute.aggregate.test;

import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Deactivate;
import org.osgi.service.component.annotations.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import biz.aQute.aggregate.api.Aggregate;
import biz.aQute.osgi.service.util.ObjectClass;

@Component
public class Server {
Logger logger = LoggerFactory.getLogger(Server.class);

@Activate
public Server() {
logger.info("activated {}", guard.getServices());
}

@Deactivate
public void deactivate() {
logger.info("deactivated {}", guard.getServices());
}

@ObjectClass
interface AgFoo extends Aggregate<Foo> {
}

@Reference
AgFoo guard;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package biz.aQute.aggregate.test.b1;

import org.osgi.service.component.annotations.Component;

import biz.aQute.aggregate.test.Foo;

@Component(immediate=true)
public class B1 implements Foo {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package biz.aQute.aggregate.test.b2;

import org.osgi.service.component.annotations.Component;

import biz.aQute.aggregate.test.Foo;

@Component(immediate=true)
public class B3 implements Foo {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package biz.aQute.aggregate.test.b3;

import org.osgi.service.component.annotations.Component;

import biz.aQute.aggregate.test.Foo;

@Component(immediate=true)
public class B2 implements Foo {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
@org.osgi.annotation.bundle.Export
@Version("1.0.0")
package biz.aQute.aggregate.test;

import org.osgi.annotation.versioning.Version;

/**
* An API to do thread based stuff
*/
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package biz.aQute.aggregate.test;

import org.junit.Ignore;
import org.junit.Test;
import org.osgi.framework.Constants;

import aQute.launchpad.Launchpad;
import aQute.launchpad.LaunchpadBuilder;
import biz.aQute.aggregate.api.Aggregate;

public class AggregateTest {
static {
System.setProperty(org.slf4j.impl.SimpleLogger.DEFAULT_LOG_LEVEL_KEY, "debug");
}

static LaunchpadBuilder builder = new LaunchpadBuilder().nostart()
.bndrun("test.bndrun")
.set(Constants.FRAMEWORK_BEGINNING_STARTLEVEL, "2");

interface T1Agg extends Aggregate<String> {}


@Ignore
@Test
public void simple() throws Exception {
try (Launchpad lp = builder.create()) {
lp.start();

System.out.println();
}

}

}
37 changes: 37 additions & 0 deletions biz.aQute.aggregate.test/test.bndrun
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
-runpath: slf4j.simple, slf4j.api
-resolve: cache
-runfw: org.apache.felix.framework;version='[6.0.2,6.0.2]'
-runee: JavaSE-1.8
-runrequires: \
osgi.identity;filter:='(osgi.identity=org.apache.felix.scr)',\
osgi.identity;filter:='(osgi.identity=org.apache.felix.webconsole.plugins.scriptconsole)',\
osgi.identity;filter:='(osgi.identity=org.apache.felix.log)',\
osgi.identity;filter:='(osgi.identity=biz.aQute.aggregate.provider)',\
osgi.identity;filter:='(osgi.identity=biz.aQute.aggregate.test.b1)',\
osgi.identity;filter:='(osgi.identity=biz.aQute.aggregate.test.b2)',\
osgi.identity;filter:='(osgi.identity=biz.aQute.aggregate.test.b3)',\
osgi.identity;filter:='(osgi.identity=biz.aQute.aggregate.test.main)',\
osgi.identity;filter:='(osgi.identity=biz.aQute.gogo.commands.provider)'
-runbundles: \
org.apache.commons.fileupload;version='[1.3.2,1.3.3)',\
org.apache.commons.io;version='[2.5.0,2.5.1)',\
org.apache.felix.configadmin;version='[1.9.12,1.9.13)',\
org.apache.felix.gogo.command;version='[1.1.0,1.1.1)',\
org.apache.felix.http.servlet-api;version='[1.1.2,1.1.3)',\
org.apache.felix.log;version='[1.2.4,1.2.5)',\
org.apache.felix.scr;version='[2.1.30,2.1.31)',\
org.apache.felix.webconsole;version='[4.3.8,4.3.9)',\
org.apache.felix.webconsole.plugins.scriptconsole;version='[1.0.2,1.0.3)',\
org.json;version='[1.0.0,1.0.1)',\
org.osgi.service.http;version='[1.2.1,1.2.2)',\
org.osgi.util.function;version='[1.1.0,1.1.1)',\
org.osgi.util.promise;version='[1.1.1,1.1.2)',\
biz.aQute.aggregate.provider;version=snapshot,\
biz.aQute.aggregate.test.b1;version=snapshot,\
biz.aQute.aggregate.test.b2;version=snapshot,\
biz.aQute.aggregate.test.b3;version=snapshot,\
biz.aQute.aggregate.test.main;version=snapshot,\
biz.aQute.api.aggregate;version=snapshot,\
biz.aQute.osgi.diverse.util;version=snapshot,\
org.apache.felix.gogo.runtime;version='[1.1.2,1.1.3)',\
org.apache.felix.gogo.shell;version='[1.1.2,1.1.3)'
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package biz.aQute.osgi.service.util;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.osgi.annotation.bundle.Attribute;
import org.osgi.annotation.bundle.Capability;

/**
* Add a service capability to the bundle
*
*/
@Capability(namespace = "osgi.service", effective = "active")
@Retention(RetentionPolicy.CLASS)
@Target(ElementType.TYPE)
public @interface AddServiceCapability {
@Attribute("objectClass")
Class<?>[] value();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package biz.aQute.osgi.service.util;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.osgi.annotation.bundle.Capability;

/**
* Add the marked type as a service capability
*
*/
@Capability(namespace = "osgi.service", effective = "active", attribute = "objectClass=${@class}")
@Retention(RetentionPolicy.CLASS)
@Target(ElementType.TYPE)
public @interface ObjectClass {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
@org.osgi.annotation.bundle.Export
@Version("1.0.0")
package biz.aQute.osgi.service.util;

import org.osgi.annotation.versioning.Version;

/**
* An API to do thread based stuff
*/

0 comments on commit 80ca439

Please sign in to comment.