Did you read about embedding Felix in here:
http://felix.apache.org/site/apache-felix-framework-launching-and-embedding.html
It talks about this very issue in "Using Services Provided by Bundles"...
-> richard
On 8/14/09 11:28, Henrik Niehaus wrote:
Hi Felix users,
I'm trying to use the OBR service in an application, which embeds felix.
This is what I'm doing:
1. Start the framework in the host application:
private void startOsgiFramework() {
// Create a case-insensitive configuration property map.
Map<String, Object> configMap = new HashMap<String, Object>();
// Add the bundle provided service interface package and the
core OSGi
// packages to be exported from the class path via the system
bundle.
configMap.put(BundleCache.CACHE_ROOTDIR_PROP, "cache");
configMap.put(Constants.FRAMEWORK_SYSTEMPACKAGES_EXTRA,
"net.sf.jmp3renamer," +
"net.sf.jmp3renamer.gui," +
"net.sf.jmp3renamer.util," +
"net.sf.jmp3renamer.plugins," +
"net.sf.jmp3renamer.gui.components.historycombobox," +
"org.slf4j;version=\"1.5\"");
// add the mandatory bundles
StringBuffer sb = new StringBuffer();
File[] bundles = new File("../../bundle").listFiles();
for (int i = 0; i< bundles.length; i++) {
File bundle = bundles[i];
sb.append("file:").append(bundle.getAbsolutePath()).append(' ');
}
String autoStart = sb.toString();
logger.debug("Auto start packages: {}", autoStart);
configMap.put(AutoActivator.AUTO_START_PROP + ".1", autoStart);
configMap.put(FelixConstants.LOG_LEVEL_PROP, "3");
// Create list to hold custom framework activators.
List<BundleActivator> list = new ArrayList<BundleActivator>();
// Add activator to process auto-start/install properties.
list.add(new AutoActivator(configMap));
list.add(this);
// Add activators to the configuration map.
configMap.put(FelixConstants.SYSTEMBUNDLE_ACTIVATORS_PROP, list);
try {
// Now create an instance of the framework with
// our configuration properties and activator.
m_felix = new Felix(configMap);
// Now start Felix instance.
m_felix.start();
m_felix.getBundleContext();
filePluginTracker = new FilePluginTracker(bndCtx,
FilePlugin.class.getName(), null);
filePluginTracker.open();
dataPluginTracker = new DataPluginTracker(bndCtx,
DataPlugin.class.getName(), null);
dataPluginTracker.open();
generalPluginTracker = new GeneralPluginTracker(bndCtx,
GeneralPlugin.class.getName(), null);
generalPluginTracker.open();
} catch (Exception ex) {
logger.error("Could not start osgi framework", ex);
System.exit(1);
}
}
2. Try to get the OBR service:
BundleContext ctx = Main.getBundleContext();
ServiceReference sr =
ctx.getServiceReference(RepositoryAdmin.class.getName());
RepositoryAdmin adm = (RepositoryAdmin) ctx.getService(sr);
I'm getting a ClassCastException, when I try to get the service from the
bundle context:
java.lang.ClassCastException:
org.apache.felix.bundlerepository.RepositoryAdminImpl cannot be cast to
org.osgi.service.obr.RepositoryAdmin.
This sounds reasonable to me, because the interface class
(RepositoryAdmin) gets loaded by the application's loader and the
RepositoryAdminImpl comes from the obr bundle loader, which has also
loaded the interface. So there are two "versions" of the interface and
the cast doesn't work.
But what can I do in such a situation? What's best practice for using
bundle services in the host application?
Regards
Henrik
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]