javaapache-karafaries

Karaf/OSGi and AriesCDI ClassCastException JDK16


Having this java-code:

package a;
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.context.Initialized;
import javax.enterprise.event.Observes;
import org.osgi.service.cdi.annotations.Service;
@Service
@ApplicationScoped
public class Controller {
    public void sayHello(@Observes @Initialized(ApplicationScoped.class) Object o) {
        System.out.println("Hello");
    }
}

I try to start karaf but this exception occourse:

00:07:04.906 INFO [fileinstall-C:\Users\Guest\Desktop\apache-karaf-4.3.2/deploy] Updating bundle a.b / 0.0.1.SNAPSHOT
00:07:04.915 WARN [fileinstall-C:\Users\Guest\Desktop\apache-karaf-4.3.2/deploy] Error while creating extension
java.lang.ClassCastException: java.lang.String incompatible with java.util.List
        at org.apache.aries.cdi.container.internal.Activator.requiresCDIExtender(Activator.java:278) ~[!/:1.1.3]
        at org.apache.aries.cdi.container.internal.Activator.doCreateExtension(Activator.java:194) [!/:1.1.3]
        at org.apache.felix.utils.extender.AbstractExtender.createExtension(AbstractExtender.java:242) [!/:1.1.3]
        at org.apache.felix.utils.extender.AbstractExtender.modifiedBundle(AbstractExtender.java:227) [!/:1.1.3]
        at org.osgi.util.tracker.BundleTracker$Tracked.customizerModified(BundleTracker.java:488) [osgi.core-7.0.0.jar:?]
        at org.osgi.util.tracker.BundleTracker$Tracked.customizerModified(BundleTracker.java:420) [osgi.core-7.0.0.jar:?]
        at org.osgi.util.tracker.AbstractTracked.track(AbstractTracked.java:232) [osgi.core-7.0.0.jar:?]
        at org.osgi.util.tracker.BundleTracker$Tracked.bundleChanged(BundleTracker.java:450) [osgi.core-7.0.0.jar:?]
        at org.apache.felix.framework.EventDispatcher.invokeBundleListenerCallback(EventDispatcher.java:915) [org.apache.felix.framework-6.0.4.jar:?]
        at org.apache.felix.framework.EventDispatcher.fireEventImmediately(EventDispatcher.java:834) [org.apache.felix.framework-6.0.4.jar:?]
        at org.apache.felix.framework.EventDispatcher.fireBundleEvent(EventDispatcher.java:516) [org.apache.felix.framework-6.0.4.jar:?]
        at org.apache.felix.framework.Felix.fireBundleEvent(Felix.java:4817) [org.apache.felix.framework-6.0.4.jar:?]
        at org.apache.felix.framework.Felix.startBundle(Felix.java:2336) [org.apache.felix.framework-6.0.4.jar:?]
        at org.apache.felix.framework.BundleImpl.start(BundleImpl.java:998) [org.apache.felix.framework-6.0.4.jar:?]
        at org.apache.felix.fileinstall.internal.DirectoryWatcher.startBundle(DirectoryWatcher.java:1260) [!/:3.6.8]
        at org.apache.felix.fileinstall.internal.DirectoryWatcher.startBundles(DirectoryWatcher.java:1233) [!/:3.6.8]
        at org.apache.felix.fileinstall.internal.DirectoryWatcher.startAllBundles(DirectoryWatcher.java:1221) [!/:3.6.8]
        at org.apache.felix.fileinstall.internal.DirectoryWatcher.doProcess(DirectoryWatcher.java:515) [!/:3.6.8]
        at org.apache.felix.fileinstall.internal.DirectoryWatcher.process(DirectoryWatcher.java:365) [!/:3.6.8]
        at org.apache.felix.fileinstall.internal.DirectoryWatcher.run(DirectoryWatcher.java:316) [!/:3.6.8]
00:07:04.915 INFO [fileinstall-C:\Users\Guest\Desktop\apache-karaf-4.3.2/deploy] Started bundle: file:/C:/Users/Guest/Desktop/apache-karaf-4.3.2/deploy/b-0.0.1-SNAPSHOT.jar

I found the cause of the exception, an unchecked cast.

Any idea what to change?

This is the content of the manifest.mf:

Manifest-Version: 1.0
Bnd-LastModified: 1650924413134
Build-Jdk-Spec: 16
Bundle-ManifestVersion: 2
Bundle-Name: b
Bundle-SymbolicName: a.b
Bundle-Version: 0.0.1.SNAPSHOT
Created-By: Apache Maven Bundle Plugin
Export-Package: a;uses:="javax.enterprise.context,org.osgi.service.cdi
 .annotations";version="0.0.1"
Import-Package: javax.enterprise.context;version="[2.0,3)",org.osgi.se
 rvice.cdi.annotations;version="[1.0,2)"
Provide-Capability: osgi.service;objectClass="a.Controller"
Require-Capability: osgi.ee;filter:="(&(osgi.ee=JavaSE)(version=1.8))"
 ,osgi.extender;filter:="(&(osgi.extender=osgi.cdi)(version>=1.0.0)(!(
 version>=2.0.0)))";beans="a.Controller"
Tool: Bnd-5.1.1.202006162103

Solution

  • I guess, I found the problem.

    I noticed the manifest should have the OSGi7 notation what is a little bit different:

    Instead of

    Require-Capability: osgi.ee;filter:="(&(osgi.ee=JavaSE)(version=1.8))"
     ,osgi.extender;filter:="(&(osgi.extender=osgi.cdi)(version>=1.0.0)(!(
     version>=2.0.0)))";beans="a.Controller"
    

    I must use

    Require-Capability: osgi.ee;filter:="(&(osgi.ee=JavaSE)(version=1.8))"
     ,osgi.extender;filter:="(&(osgi.extender=osgi.cdi)(version>=1.0.0)(!(
     version>=2.0.0)))";beans:List<String>="a.Controller"
    

    Yes, I moved to 5.1.4 and it worked as expected.