views:

196

answers:

2

I have some legacy code that was used to monitor my applications cpu,memory etc that I want to convert to a bundle. Now when i start this bundle its complaining

Missing Constraint: Import-Package: com.sun.management; version="0.0.0"

I had used the OperatingSystemMXBean to get access to stats on the JVM.

My question is can I use this class inside an OSGI container and if so how? Or should I use some other way to monitor my application. I was making an RMI call to the application from a web frontend to get the nodes performance figures pre OSGi.

+1  A: 

Could you try to install it in an interactive OSGi session?
See this article for example.

osgi> ss

Framework is launched.

id State       Bundle
0 ACTIVE      org.eclipse.osgi_3.4.0.v20080605-1900

osgi>  install file:bundles/FirstBundle-1.0.0.jar
Bundle id is 1

//Try starting 
osgi> start 1
org.osgi.framework.BundleException: The bundle could not be resolved. 
  Reason: Missing Constraint: Import-Package: com.so.samples.osgi.second; 
                                              version="0.0.0"
 at org.eclipse.osgi.framework.internal.core.BundleHost.startWorker
    (BundleHost.java:305)

You can diagnostic the issue:

osgi> diag 1
file:bundles/FirstBundle-1.0.0.jar [1]
  Direct constraints which are unresolved:
    Missing imported package com.so.samples.osgi.second_0.0.0.

And install the missing dependency, provided you know where to fetch the jar
(which might very well be the crux of your question, and for which I have no exact answer, except to convert a legacy jar in an OSGi bundle, like a wrap protocol or an extension of an OSGi framework):

osgi> install file:bundles/SecondBundle-1.0.0.jar
Bundle id is 2
VonC
Thanks VonC yes sorry I am trying to resolve the dependency issue. The package is in rt.jar which is part of the jre lib so I was wary of wrapping it but I'll give it a go and report back here.
Paul Whelan
@Paul: perhaps this thread can also help, before wrapping rt.jar: http://www.mail-archive.com/[email protected]/msg00518.html
VonC
Great thanks for the link pointed me in the correct direction I added the detail below on the reasons for the com.sun packages not being included by default.
Paul Whelan
+2  A: 

The following is what I had to do in order to get this working.

I had to add com.sun.management to the systemProperties value for the system bundle, as I was new to OSGI this took me a while to figure out. I use the maven-pax-plugin so i needed to add the following property. The reason it didn't work by default was equinox my osgi container of choice does not include the com.sun.* packages in the system bundle by default.

This was obvious by looking at the system bundle with the bundle 0 command as bundle 0 is always the system bundle which was something new to me.

<param>--sp=com.sun.management</param>

after adding this command the system bundle includes com.sun.management and my bundle deployed with no issues.

The reason that equinox doesn't include the com.sun packages in the systemProperties by default see here. (A Java program that directly calls into sun.* packages is not guaranteed to work on all Java-compatible platforms. In fact, such a program is not guaranteed to work even in future versions on the same platform.)

So you have two options for adding com.sun to the osgi container.

Solution A': Extension Bundles

These act as fragments; they are not bundles of their own but rather are attached to a host. Extension bundles are a special kind of fragments that get attached only to the System bundle in order to deliver optional parts of the Framework. One can use this mechanism to create an empty extension that just declares the needed packages, leaving the loading to its hosting bundle (in this case the Framework). I didn't go for this route as the second option was quicker to implement.

Solution B: Boot Delegation

The option I went for in the the end was boot delegation. This allows the user to create 'implied' packages that will always be loaded by the framework parent class loader, even if the bundles do not provide the proper imports. I achieved by setting the system packages to include com.sun.management.

See the following excellent link that describes the whole issue in more detail.

Paul Whelan
+1. Thank you for this very interesting feedback. Could you detail the solution B (in order to not depend on an internet link which might broke one day)?
VonC
done hopefully some day will save someone some time:)
Paul Whelan