How to force my OSGI bundle to ignore a conflicted a bundle while resolving dependencies - osgi

I am using AEM 5.6.1. I am getting an exception with JAXB my bundle when a jax-b api 2.2.12 manually is installed in console. As JAX-b(2.1.0) is already provided internally in AEM by below bundle. This exception doesn't happen when the 2.2.12 is not present. Also, if jax-b 2.2.12 is installed after my bundle, no problems at all.
However this new version is being used by another bundle developed by a different vendor. Hence I can't remove this new bundle from felix console.
The system bundle which provides the original JAX-B 2.1.0
`<dependency>
<groupId>org.apache.sling</groupId>
<artifactId>org.apache.sling.fragment.xml</artifactId>
<version>1.0.2</version>
</dependency>`
I was just wondering how could I force my bundle to ignore looking for jax-b 2.2.12 and take only the system default 2.1.0 version by some configuration using maven pom. Specifically in maven bundle plugin in the POM.
Activate method in my ABC class:
`#Activate
public void activate(ComponentContext ctx) throws JAXBException {
abcContext = JAXBContext.newInstance(ABC.class);
}`
Current Bundle plugin import package definition:
<Import-Package>
*;resolution:=optional
</Import-Package>
Exception:
16.03.2016 03:13:05.709 *ERROR* [Background Update foo.barsupport-bundle (606)] foo.barsupport-bundle [foo.bar.calendar.impl.CalendarEventParserImpl] The activate method has thrown an exception (javax.xml.bind.JAXBException
- with linked exception:
[java.lang.ClassNotFoundException: com.sun.xml.internal.bind.v2.ContextFactory not found by cqse-httpservice [25]]) javax.xml.bind.JAXBException
- with linked exception:
[java.lang.ClassNotFoundException: com.sun.xml.internal.bind.v2.ContextFactory not found by cqse-httpservice [25]]
at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:241)
at javax.xml.bind.ContextFinder.find(ContextFinder.java:477)
at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:656)
at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:599)
at foo.bar.integration.calendar.impl.CalendarEventParserImpl.activate(CalendarEventParserImpl.java:33).
Any help would be really appreciated.

In general, your problem is that your bundle by default accept [2.0, 3.0) which is perfect for the Semantic Versioning (Major.Minor.BugFix). But, if your bundle use the newer version, and if it communicates with some of thoses classes to your framework which use an older version of that bundle, you can get those kind of ClassNotFoundException in Runtime.
The uses directive is created to make sure that if there is multiple compatible versions, and multiple importers, those importers will communicate between them with the same version. It generally occurs when a bundle exports packages where other packages imported by other. This is why a good practice is to have API bundle which doesn't export anything or Impl bundle that exports anything, it free you from the need of uses..

Try to use a version range when importing jaxb in your bundle:
<Import-Package>
javax.xml.bind;version="[2.1,2.2)"
*;resolution:=optional
</Import-Package>
This directive tell the osgi resolver to wire your bundle with the package javax.xml.bind with a version from 2.1 to 2.2, 2.2 excluded.

Related

Maven (Tycho) cannot resolve OSGi Core bundle

I have a dependency on "org.osgi:osgi.core" (7.0.0) in my POM. The reason is that I need access to the "org.osgi.framework" package. I am using Maven (3.6) and Tycho (1.5.1) for building. The build platform runs Debian 10 and Java 11.
I get the following error:
Missing requirement: osgi.core 7.0.0.201802012106 requires 'osgi.unresolvable; (&(!(must.not.resolve=*))(must.not.resolve=*))' but it could not be found
However, if I remove the dependency I get the following error:
Missing requirement: my.bundle 0.0.0.qualifier requires 'java.package; org.osgi.framework 1.7.0' but it could not be found
What is going wrong? How can I resolve this problem?
I get the following error:
Missing requirement: osgi.core 7.0.0.201802012106 requires 'osgi.unresolvable; (&(!(must.not.resolve=*))(must.not.resolve=*))' but it could not be found
The "companion jars" are not meant for runtime and since resolving is a runtime operation (even when performed during build, i.e. for deployment purposes) should not be included (and are therefore marked with the unresolvable requirement).
However, if I remove the dependency I get the following error:
Missing requirement: my.bundle 0.0.0.qualifier requires 'java.package; org.osgi.framework 1.7.0' but it could not be found
This means you have no runtime framework available! Add a runtime dependency on the equinox framework (not intentionally being biased, but since you're using tycho I'm assuming eclipse/equinox landscape. If you have Apache Felix framework available to p2/tycho then use that if you like):
<dependency>
<groupId>org.eclipse.platform</groupId>
<artifactId>org.eclipse.osgi</artifactId>
<version>3.x.0</version>
<scope>runtime</scope>
</dependency>
// of course use tycho mechanism for above.

OSGI bundle dependency issue

I have got the project for migration. Currently, version is CQ5.6. Maven build is deploying successfully. However, after build, bundles in osgi show in installed state. Two dependencies causing the issue.
org.apache.felix.shell,version=[1.0,2) -- Cannot be resolved
I was getting an error earlier as below during build.
ERROR
[INFO] --- maven-bundle-plugin:2.3.4:bundle (default-bundle) # myPRJ-taglib ---
[ERROR] Error building bundle com.mypack.deewealth:myPRJ-taglib:bundle:1.0.0-SNAPSHOT : Unresolved references to [org.apache.felix.shell] by class
(es) on the Bundle-Classpath[Jar:dot, Jar:OSGI-INF/lib/recaptcha4j-0.0.8-kohsuke-1.jar, Jar:OSGI-INF/lib/commons-io-2.1.jar, Jar:OSGI-INF/lib/commons-
lang-2.4.jar, Jar:OSGI-INF/lib/crx-packagemgr-1.0.22.jar, Jar:OSGI-INF/lib/squeakysand-osgi-0.4.0.jar, Jar:OSGI-INF/lib/jsoup-1.6.1.jar, Jar:OSGI-INF/
lib/stax-api-1.0-2.jar, Jar:OSGI-INF/lib/org.apache.sling.settings-1.1.0.jar, Jar:OSGI-INF/lib/cq-compat-runmode-0.2.0.jar, Jar:OSGI-INF/lib/commons-c
ollections-3.2.1.jar, Jar:OSGI-INF/lib/squeakysand-jsp-0.4.0.jar, Jar:OSGI-INF/lib/squeakysand-commons-0.4.0.jar]: [org/apache/sling/settings/impl/Run
ModeCommand.class]
To solve this I added below dependancy in pom.xml, as we added in
Dependency
<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.apache.felix.shell</artifactId>
<version>1.4.2</version>
<scope>provided</scope>
</dependency>
Under import statement.
<Import-Package>
....
....
org.apache.felix.shell
</Import-Package>
After that, build was successful, but bundle was in resolved state because of
org.apache.felix.shell,version=[1.0,2) -- Cannot be resolved
Any suggestion, why this is causing the problem.
org.apache.felix.shell,version=[1.0,2) -- Cannot be resolved
mean that you are trying to use these felix packages from within AEM. However - there are no OSGi bundles in AEM that exports these packages.
You can download the bundle from maven repo and upload it into the AEM Felix console and use it.
OR you can create it as your parent pom dependency as one of module and wrap along with your project jar and use it.
To veify the availability of any package and dependencies use AEM Dependency finder.
I don't know CQ but I can explain why this happens in general. You had the error during build time because of missing dependency. You correctly solved it by adding the dependency which fixed your build. What happened behind the scenes is your bundle was updated with information that it needs (imports) org.apache.felix.shell,version=[1.0,2) package and that package will be provided by some other bundle at runtime.
Then at runtime the resolver got that information and tried to find a bundle that provides (exports) org.apache.felix.shell,version=[1.0,2) package. It couldn't find one and so the resolution failed and your bundle was left in installed (I believe that's what you meant to write instaed of resolved in your last sentence) state!
To solve that, you need to make sure org.apache.felix.shell,version=[1.0,2) package is available at runtime. I don't know CQ and how to install bundles in it but since you can install your own bundle I assume you can also install org.apache.felix.shell bundle the same way.
The shell jar you have installed is still unresolved as it depends on some jars that might not be present. You need to find the depth of all dependencies and resolve them.

OSGi errors when running jackrabbit 2.11 in Karaf 2.4 / Felix 4.x

I'm trying to use JackRabbit 2.11.1 to connect to a remote repo (using jackrabbit-jcr-rmi). The bundles run in JBoss Fuse 6.2, which has Apache Karaf 2.4 / Felix 4.4 under the hood. On startup i get the exception below.
If i try to use jackrabbit-bundle i get
"Missing Constraint: Import-Package: com.ibm.db2.jcc; version="0.0.0""
So i'm confused, is JackRabbit 2.x OSGi ready? or do i need to use Sling or Oak , or .... ?
Caused by: org.osgi.framework.BundleException: Uses constraint violation. Unable to resolve bundle revision wrap_mvn_org.apache.jackrabbit_jackrabbit-core_2.11.1 [270.0] because it exports package 'org.apache.jackrabbit.core.config' and is also exposed to it from bundle revision org.apache.jackrabbit.jackrabbit-data [276.0] via the following dependency chain:
wrap_mvn_org.apache.jackrabbit_jackrabbit-core_2.11.1 [270.0]
import: (osgi.wiring.package=org.apache.jackrabbit.core.data.db)
export: osgi.wiring.package=org.apache.jackrabbit.core.data.db; uses:=org.apache.jackrabbit.core.config
export: osgi.wiring.package=org.apache.jackrabbit.core.config
org.apache.jackrabbit.jackrabbit-data [276.0]
at org.apache.felix.framework.Felix.resolveBundleRevision(Felix.java:4006)[org.apache.felix.framework-4.4.1.jar:]
at org.apache.felix.framework.Felix.startBundle(Felix.java:2045)[org.apache.felix.framework-4.4.1.jar:]
at org.apache.felix.framework.BundleImpl.start(BundleImpl.java:976)[org.apache.felix.framework-4.4.1.jar:]
at org.apache.felix.framework.BundleImpl.start(BundleImpl.java:963)[org.apache.felix.framework-4.4.1.jar:]
at org.apache.karaf.features.internal.FeaturesServiceImpl.doInstallFeatures(FeaturesServiceImpl.java:546)[9:org.apache.karaf.features.core:2.4.0.redhat-620133]
See also https://issues.apache.org/jira/browse/JCR-3917
I solved it with a horrible hack.
Embed the dependencies i need into my own jar.
Set the ContextClassLoader to the classloader of the providing class (what the SPI thing was supposed to do in the first place but did not work, probably because i needed to wrap more jars than the one i did ?).
So, in the maven-bundle-plugin i did:
<Embed-Dependency>jackrabbit-jcr2dav*,jackrabbit-jcr2spi*,jackrabbit-jcr-commons*;scope=compile;inline=false</Embed-Dependency>
And in my consuming code:
ClassLoader originalContextClassLoader = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(Jcr2davRepositoryFactory.class.getClassLoader());
//
repository = JcrUtils.getRepository(uri);
session = getSession();
// restore original classloader
Thread.currentThread().setContextClassLoader(originalContextClassLoader);

Felix bundles failng to load

I am attempting to run an application on Apache Felix, using the Felix Dependency bundles. The container is, for some reason, not loading my bundles.
I am doing my development using Bndtools in Eclipse. I am using the Amdatu bundles.
The bundles I am concerned about are the bundles that are in my "Run Requirements":
org.apache.felix.dependencymanager
org.apache.felix.dependencymanager.shell
org.apache.felix.dependencymanager.runtime
osgi.core
osgi.cmpn
All of the above bundles are taken from the Amdatu repository.
I have two bundles which I created:
com.test.demo.reader
com.test.demo.listener
And these bundles are built using org.apache.felix.dependencymanager and osgi.core.
When I attempt to run OSGi, I am getting the following failures:
! could not resolve the bundles: [com.test.demo.reader-1.0.0 Unresolved constraint in bundle
com.test.demo.reader [8]: Unable to resolve 8.0: missing requirement
[8.0] osgi.wiring.package; (&(osgi.wiring.package=org.apache.felix.dm)(version>=3.0.0)(!(version>=4.0.0))),
com.test.demo.listener-1.0.0 Unresolved constraint in bundle com.test.demo.listener [9]:
Unable to resolve 9.0: missing requirement [9.0] osgi.wiring.package;
(&(osgi.wiring.package=com.test.demo.reader)(version>=1.0.0)(!(version>=2.0.0)))
[caused by: Unable to resolve 8.0: missing requirement [8.0] osgi.wiring.package;
(&(osgi.wiring.package=org.apache.felix.dm)(version>=3.0.0)(!(version>=4.0.0)))]]
and
! Failed to start bundle com.test.demo.reader-1.0.0, exception Unresolved constraint in
bundle com.test.demo.reader [8]: Unable to resolve 8.0: missing requirement [8.0]
osgi.wiring.package; (&(osgi.wiring.package=org.apache.felix.dm)(version>=3.0.0)
(!(version>=4.0.0)))
and more
! Failed to start bundle com.test.demo.listener-1.0.0, exception Unresolved constraint in
bundle com.test.demo.listener [9]: Unable to resolve 9.0:
missing requirement [9.0] osgi.wiring.package; (&(osgi.wiring.package=com.test.demo.reader)
(version>=1.0.0)(!(version>=2.0.0))) [caused by: Unable to resolve 8.0: missing requirement
[8.0] osgi.wiring.package; (&(osgi.wiring.package=org.apache.felix.dm)(version>=3.0.0)
(!(version>=4.0.0)))]
I think I understand what is happening: apparently the org.apache.dm package being used is a version that is somehow incompatible with my bundles. But that doesn't make any sense, because according to the Felix documentation the org.apache.dm package is in the org.apache.felix.dependencymanager bundle. That bundle is from the Amdatu repository and it is the only version of the bundle I am using with my application! How is the version incompatible with my bundles when theirs is the only version that my bundles know about???
Someone please advise. What is causing this failure an how can I fix it?
The problem was the runtime I was running and what appears to be a bug in the Amdatu plugin.
I would not advise using the Amdatu configuration in your OSGi project. Not in its current form.
The various Felix Dependency bundles that I have been using from Amdatu are apparently intended to run under Felix version 5. Unfortunately, the Amdatu environment (the "Amdatu Configuration" that you can select when you choose a configuration template for your project) does not load Version 5. When you select the "Bundle- Hub configuration", your choice of runtime environments includes all Felix environments from Version 4.0.2 to Version 5.2, as well as several versions of Equinox. When you select the "Amdatu Configuration" you only runtime choices are Felix 4.0.2 and Felix 4.2.0.
I was using 4.2.0 Felix, and as a result the dependency bundles were not working.
The Amdatu environment in its current form is unusable in Bndtools. It apparently supplies the right bundles for Version 5 but does not provide that environment for the user.
I did try manually changing the runtime environment io Version 5.2. This did not work because apparently when you select a template, Bndtools puts together whatever runtimes are associated with the template. Because the Amdatu template does not include Felix version 5, the runtime environment is unavailable.
In order to get my application to run, I had to create a new project that used the "Bundle- Hub" template and manually download the dependency bundles from the Felix website and include them in my buil and runtime environments. Unfortunately, neither Bndtools nor Amdatu provides any way to add the Amdatu bundles to project after a template is selected. This means that instead of using the ready- made bundles from Amdatu, you have to go out to the source sites and manually include them.
I will probably ty and find a way to create a new repository that can be included in projects and contains all the Amdatu bundles, then include that repository in the "Bundle- Hub" template. I will also report the problems with environments (as well as seversl other problems I've seen) to the Amdatu folks.
In the meantime, my application runs smoothly now that I have set up the right runtime environment.
Special thanks to earcam, whose questions in the comment above gave me a clue as to what to start looking at to solve this problem...

Beginner starting large OSGi migration - osgi.wiring.package=android.dalvik?

I have a large Java application that's mostly networking and file processing with lots of DB access. There's no UI. We expose a few web services (embedded Netty) and call some external rest web services. The project is built with Ant. It's about 10 different jars plus maybe 30-40 libraries.
My current task is to move the project to the OSGi framework. I am startling slowly.
Following the examples in "OSGi In Action" chapter 6, I have used the BND ant task to put the entire project into one huge jar file. This worked. I am able to run the program using java -jar. Here's my current .bnd file:
-output: bundle/MerchantServicesBundle.jar
-include: manifest/merchantservices.manifest
Bundle-Name: MerchantServices
Bundle-SymbolicName: com.shopping.services.merchant
Bundle-Version: 4.1
Main-Class: com.shopping.merchant.services.netty.MerchantServices
Class-Path: /home/ppantera/repositories/MerchantJava/modules/MerchantServices/conf/
Private-Package: *
I am using Apache Felix 4.0.3. From the Gogo shell I can install the bundle but when I start it I get this:
org.osgi.framework.BundleException: Unresolved constraint in bundle com.shopping.services.merchant [8]: Unable to resolve 8.0: missing requirement [8.0] osgi.wiring.package; (osgi.wiring.package=android.dalvik)
Why does Felix think this is an Android project?
There doesn't seem to be much about this on the 'net. Would you recommend using an older version of Felix so I'm sheltered from the newer OSGi features that could cause me confusion?
I tried adding this to my .bnd file:
Require-Capability: osgi.ee; filter:="(&(osgi.ee=JavaSE)(version>=1.7))"
That didn't help. What am I doing wrong? Any other pointers?
It looks like somehow bnd detected a requirement to an Android package, and added that to the MANIFEST.MF, it could be in your code, but could also be in one of your 3rd party libraries.
Check your manifest to be sure, I guess you'll find something like
Import-Package:android.dalvik.
If that's the case you can test the bundle by manually removing that header and see if that helps. When you've got that clear, you can resolve it for example by making that import optional in bnd.
One of easy solution is:
Go to FuseESB console:
Type the command:
osgi:install mvn:commons-io/commons-io/2.1
Replace 'common's-io' with your your dependency's group id and artifact id (Maven)
e.g my dependency was:
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.1</version>
<scope>provided</scope>
</dependency>
Cheers

Resources