DZone Forums
Go Back   DZone Forums > Community > Tools & IDEs > Eclipse
Reload this Page Access OSGi servicees outside OSGi environment
Notices
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  (#1 (permalink)) Old
Member
 
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Join Date: Feb 2008
Default Access OSGi servicees outside OSGi environment - 02-28-2008, 03:13 AM

Hi

I am trying to figure out how to use OSGi to add module capabilities to an existing Java application. I am using equinox 3.3.1.1 and I can start the framework and install bundels:

Code:
BundleContext bc = EclipseStarter.startup(new String[] {}, null);
bc.installBundle("file:TestBundle_1.0.0.jar");
I can start and stop bundles, find services etc. But when I actually try and get a service (bc.getService(serviceRef)) and cast it to its interface I get a ClassCastException. The class of the service is correct and I have checked it using :
Code:
serviceRef.getProperty("objectClass")
So far, the only services I have managed to use are the ones that are published by the org.eclipse.osgi bundle. Every other service gives me the same problem.

Does anybody have any ideas how I could make this work?
Reply With Quote
  (#2 (permalink)) Old
Member
 
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Join Date: Feb 2008
Default 02-28-2008, 07:30 AM

It seems this isn't possible with Eclipse Equinox :EclipseZone - Programmatically start Equinox in ...

but it should be with Felix: Apache Felix - Launching and Embedding Apache Felix
Reply With Quote
  (#3 (permalink)) Old
Member
 
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Join Date: Feb 2008
Default 02-28-2008, 10:09 AM

[QUOTE=simongdkelly;360]It seems this isn't possible with Eclipse Equinox :EclipseZone - Programmatically start Equinox in ...

Urm. Why do you say that? The link you posted doesn't say anything about what you can/can't do.

It is possible to do this in Equinox; however, you need to be careful that the classes (particularly the interfaces) are ones that you have defined in your application. OSGi throws in a bunch of classloaders that need due and careful attention to expose them, so if you're not dealing with interfaces and implementations at the main Java level, it won't work.

You also assume, naively, that the name of the class is unique. It's not. You can have many classes of the same name loaded by many classloaders; and the classloader doesn't need to make that class visible to others. So the fact you're getting a 'ClassCastException' indicates that the class is not visible from your calling code, not that it doesn't work.
Reply With Quote
  (#4 (permalink)) Old
Member
 
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Join Date: Feb 2008
Default 02-29-2008, 08:43 AM

The problem is very likely that you try to work with bundles while you are not a bundle because you start the framework. This means your classloader is not an OSGi class loader but the normal Java application class loader. This loader will load the class for your cast operator while the bundle loads it through an OSGi class loader.

If you really need to make this work, export the package from the class path (set a property called org.osgi.framework.systempackages with the class names). Then the bundle will also load the class from the same source.

However, the way you use OSGi is for very special applications that want to embed a framework. These apps must be extremely careful with the classloading because nobody is paying attention. Class loading with OSGi is quite straightforward but if you don't play by its rules, you get this result :-)

Therfore, always test with bundle-bundle operations and not system-bundle.

Hope this helps, kind regards,

Peter Kriens



[QUOTE=alblue;363]
Quote:
Originally Posted by simongdkelly View Post
It seems this isn't possible with Eclipse Equinox :EclipseZone - Programmatically start Equinox in ...

Urm. Why do you say that? The link you posted doesn't say anything about what you can/can't do.

It is possible to do this in Equinox; however, you need to be careful that the classes (particularly the interfaces) are ones that you have defined in your application. OSGi throws in a bunch of classloaders that need due and careful attention to expose them, so if you're not dealing with interfaces and implementations at the main Java level, it won't work.

You also assume, naively, that the name of the class is unique. It's not. You can have many classes of the same name loaded by many classloaders; and the classloader doesn't need to make that class visible to others. So the fact you're getting a 'ClassCastException' indicates that the class is not visible from your calling code, not that it doesn't work.
Reply With Quote
Reply

Tags
eclipse, equinox, osgi

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
change osgi.nl programatically joesusai Eclipse 0 12-08-2008 10:56 PM
How to use URLClassLoader in a OSGi Bundle? raja00cs Eclipse 0 11-19-2008 11:04 AM
A Conceptual Problem with OSGi AlexandreSH Java 0 10-30-2008 05:23 AM
changing osgi prompt text AlexandreSH Java 0 06-26-2008 01:21 AM
conceptual trouble with OSGi AlexandreSH Java 4 05-31-2008 06:12 AM


Copyright 1997-2009, DZone, Inc.
vBulletin Skin developed by: vBStyles.com