Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Equinox » ClassCastException when using BundleContext.getService()
ClassCastException when using BundleContext.getService() [message #107584] Mon, 07 April 2008 03:09 Go to next message
Eclipse UserFriend
Originally posted by: pillii.gmx.de

Hello,

im pretty new to eclipse RCP and OSGi.
I have two Projects. The first is an RCP application and the second is a
pure Java application.
I try to get an Object that i succesfully registered as OSGi Service
from outside the OSGi Framework with getService. This doesnt work.

Here is what i do:

Register the service inside my RCP application:
Hashtable props = new Hashtable();
props.put(Constants.SERVICE_DESCRIPTION, "QualiGUI Service");
props.put("description", "This is a QualiGUI Test");
ServiceRegistration sr = bc.registerService(ComTest.class.getName(), new
ComTest(), props);

This seems to work. The service is listed in the Array i get from
EclipseStarter.getSystemBundleContext().getAllServiceReferen ces(null, null)
from inside my Plugin.

In my Java application is set some Frameworksettings and then
start the Framwork using
EclipseStarter.run(args);
This works fine. My RCP Application is running.

Now im trying to get the Service that i registered above.
for (Bundle b : EclipseStarter.getSystemBundleContext().getBundles())
{
if (b.getSymbolicName().equalsIgnoreCase("qualishipgui"))
{

for (ServiceReference sr : b.getRegisteredServices())
{
ComTest ct = (ComTest)b.getBundleContext().getService(sr);
}
}
}
In my Java project i added my RCP project to the classpath so i get
access to the class "ComTest" that is located inside my RCP app.

When the Cast runs i get this error:
Exception in thread "main" java.lang.ClassCastException:
de.cemaris.qualiship.qualishipgui.communication.ComTest cannot be cast
to de.cemaris.qualiship.qualishipgui.communication.ComTest

This looks kind of funny to me..
I googled a lot and search Forums and i figured it has to do something
with the different Classloaders being used.
But i didnt find a solution on how to fix this.

Can you help me, please?
regards,
Gordian
Re: ClassCastException when using BundleContext.getService() [message #107609 is a reply to message #107584] Mon, 07 April 2008 13:07 Go to previous messageGo to next message
Danail Nachev is currently offline Danail NachevFriend
Messages: 110
Registered: July 2009
Senior Member
Indeed this is a clear symptom that you are using different class
loaders. The bundle which registers the service is using the its bundle
class loader. However, your code trying to access the service is using
most probably VM's Application Class loader, which is the very base
class loader.

You need to guarantee that the class is loaded from the same place for
both your non-OSGi code and your OSGi code. This can be most easily done
by packing the class in your non-OSGi part of the application and making
it available to the OSGi part via system packages
(org.osgi.framework.system.packages) and importing the package in the
bundle's manifest.

Have fun,
Danail

gordian wrote:
> Hello,
>
> im pretty new to eclipse RCP and OSGi.
> I have two Projects. The first is an RCP application and the second is a
> pure Java application.
> I try to get an Object that i succesfully registered as OSGi Service
> from outside the OSGi Framework with getService. This doesnt work.
>
> Here is what i do:
>
> Register the service inside my RCP application:
> Hashtable props = new Hashtable();
> props.put(Constants.SERVICE_DESCRIPTION, "QualiGUI Service");
> props.put("description", "This is a QualiGUI Test");
> ServiceRegistration sr = bc.registerService(ComTest.class.getName(), new
> ComTest(), props);
>
> This seems to work. The service is listed in the Array i get from
> EclipseStarter.getSystemBundleContext().getAllServiceReferen ces(null, null)
> from inside my Plugin.
>
> In my Java application is set some Frameworksettings and then
> start the Framwork using
> EclipseStarter.run(args);
> This works fine. My RCP Application is running.
>
> Now im trying to get the Service that i registered above.
> for (Bundle b : EclipseStarter.getSystemBundleContext().getBundles())
> {
> if (b.getSymbolicName().equalsIgnoreCase("qualishipgui"))
> {
>
> for (ServiceReference sr : b.getRegisteredServices())
> {
> ComTest ct = (ComTest)b.getBundleContext().getService(sr);
> }
> }
> }
> In my Java project i added my RCP project to the classpath so i get
> access to the class "ComTest" that is located inside my RCP app.
>
> When the Cast runs i get this error:
> Exception in thread "main" java.lang.ClassCastException:
> de.cemaris.qualiship.qualishipgui.communication.ComTest cannot be cast
> to de.cemaris.qualiship.qualishipgui.communication.ComTest
>
> This looks kind of funny to me..
> I googled a lot and search Forums and i figured it has to do something
> with the different Classloaders being used.
> But i didnt find a solution on how to fix this.
>
> Can you help me, please?
> regards,
> Gordian
Re: ClassCastException when using BundleContext.getService() [message #107637 is a reply to message #107609] Mon, 07 April 2008 15:10 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: pillii.gmx.de

Hello,

thank you for your answer.
How do i make a class available to OSGi via system packages?

Regards,
Gordian

Danail Nachev schrieb:
> Indeed this is a clear symptom that you are using different class
> loaders. The bundle which registers the service is using the its bundle
> class loader. However, your code trying to access the service is using
> most probably VM's Application Class loader, which is the very base
> class loader.
>
> You need to guarantee that the class is loaded from the same place for
> both your non-OSGi code and your OSGi code. This can be most easily done
> by packing the class in your non-OSGi part of the application and making
> it available to the OSGi part via system packages
> (org.osgi.framework.system.packages) and importing the package in the
> bundle's manifest.
>
> Have fun,
> Danail
>
> gordian wrote:
>> Hello,
>>
>> im pretty new to eclipse RCP and OSGi.
>> I have two Projects. The first is an RCP application and the second is a
>> pure Java application.
>> I try to get an Object that i succesfully registered as OSGi Service
>> from outside the OSGi Framework with getService. This doesnt work.
>>
>> Here is what i do:
>>
>> Register the service inside my RCP application:
>> Hashtable props = new Hashtable();
>> props.put(Constants.SERVICE_DESCRIPTION, "QualiGUI Service");
>> props.put("description", "This is a QualiGUI Test");
>> ServiceRegistration sr = bc.registerService(ComTest.class.getName(), new
>> ComTest(), props);
>>
>> This seems to work. The service is listed in the Array i get from
>> EclipseStarter.getSystemBundleContext().getAllServiceReferen ces(null, null)
>> from inside my Plugin.
>>
>> In my Java application is set some Frameworksettings and then
>> start the Framwork using
>> EclipseStarter.run(args);
>> This works fine. My RCP Application is running.
>>
>> Now im trying to get the Service that i registered above.
>> for (Bundle b : EclipseStarter.getSystemBundleContext().getBundles())
>> {
>> if (b.getSymbolicName().equalsIgnoreCase("qualishipgui"))
>> {
>>
>> for (ServiceReference sr : b.getRegisteredServices())
>> {
>> ComTest ct = (ComTest)b.getBundleContext().getService(sr);
>> }
>> }
>> }
>> In my Java project i added my RCP project to the classpath so i get
>> access to the class "ComTest" that is located inside my RCP app.
>>
>> When the Cast runs i get this error:
>> Exception in thread "main" java.lang.ClassCastException:
>> de.cemaris.qualiship.qualishipgui.communication.ComTest cannot be cast
>> to de.cemaris.qualiship.qualishipgui.communication.ComTest
>>
>> This looks kind of funny to me..
>> I googled a lot and search Forums and i figured it has to do something
>> with the different Classloaders being used.
>> But i didnt find a solution on how to fix this.
>>
>> Can you help me, please?
>> regards,
>> Gordian
Re: ClassCastException when using BundleContext.getService() [message #107703 is a reply to message #107637] Tue, 08 April 2008 06:36 Go to previous messageGo to next message
Danail Nachev is currently offline Danail NachevFriend
Messages: 110
Registered: July 2009
Senior Member
The best approach is to create framework extension bundle. This is a
fragment bundle of the system.bundle:

Fragment-Host: system.bundle; extension:=framework

Additionally, in the fragment bundle's manifest, you need to export the
packages you are interested in:

Export-Package: com.acme.pack; version="1.0"

In your bundle, you need to import it:

Import-Package: com.acme.pack

The other approach is by setting the org.osgi.framework.system.packages
property, before launching the OSGi container. However, this may lead to
other problems if you are using samo JDK packages in your bundles, so it
is not advisable to use it, unless you know what you are doing. More
information about both concepts can be found in the OSGi R4.1 specification:

http://www.osgi.org/Download/Release4V41

gordian wrote:
> Hello,
>
> thank you for your answer.
> How do i make a class available to OSGi via system packages?
>
> Regards,
> Gordian
>
> Danail Nachev schrieb:
>> Indeed this is a clear symptom that you are using different class
>> loaders. The bundle which registers the service is using the its bundle
>> class loader. However, your code trying to access the service is using
>> most probably VM's Application Class loader, which is the very base
>> class loader.
>>
>> You need to guarantee that the class is loaded from the same place for
>> both your non-OSGi code and your OSGi code. This can be most easily done
>> by packing the class in your non-OSGi part of the application and making
>> it available to the OSGi part via system packages
>> (org.osgi.framework.system.packages) and importing the package in the
>> bundle's manifest.
>>
>> Have fun,
>> Danail
>>
>> gordian wrote:
>>> Hello,
>>>
>>> im pretty new to eclipse RCP and OSGi.
>>> I have two Projects. The first is an RCP application and the second is a
>>> pure Java application.
>>> I try to get an Object that i succesfully registered as OSGi Service
>>> from outside the OSGi Framework with getService. This doesnt work.
>>>
>>> Here is what i do:
>>>
>>> Register the service inside my RCP application:
>>> Hashtable props = new Hashtable();
>>> props.put(Constants.SERVICE_DESCRIPTION, "QualiGUI Service");
>>> props.put("description", "This is a QualiGUI Test");
>>> ServiceRegistration sr = bc.registerService(ComTest.class.getName(), new
>>> ComTest(), props);
>>>
>>> This seems to work. The service is listed in the Array i get from
>>> EclipseStarter.getSystemBundleContext().getAllServiceReferen ces(null,
>>> null)
>>> from inside my Plugin.
>>>
>>> In my Java application is set some Frameworksettings and then
>>> start the Framwork using
>>> EclipseStarter.run(args);
>>> This works fine. My RCP Application is running.
>>>
>>> Now im trying to get the Service that i registered above.
>>> for (Bundle b : EclipseStarter.getSystemBundleContext().getBundles())
>>> {
>>> if (b.getSymbolicName().equalsIgnoreCase("qualishipgui"))
>>> {
>>> for (ServiceReference sr : b.getRegisteredServices())
>>> {
>>> ComTest ct = (ComTest)b.getBundleContext().getService(sr);
>>> }
>>> }
>>> }
>>> In my Java project i added my RCP project to the classpath so i get
>>> access to the class "ComTest" that is located inside my RCP app.
>>>
>>> When the Cast runs i get this error:
>>> Exception in thread "main" java.lang.ClassCastException:
>>> de.cemaris.qualiship.qualishipgui.communication.ComTest cannot be cast
>>> to de.cemaris.qualiship.qualishipgui.communication.ComTest
>>>
>>> This looks kind of funny to me..
>>> I googled a lot and search Forums and i figured it has to do something
>>> with the different Classloaders being used.
>>> But i didnt find a solution on how to fix this.
>>>
>>> Can you help me, please?
>>> regards,
>>> Gordian
Re: ClassCastException when using BundleContext.getService() [message #107728 is a reply to message #107703] Tue, 08 April 2008 11:02 Go to previous message
Eclipse UserFriend
Originally posted by: pillii.gmx.de

It works. Thank you so much!

I used the way of creating a fragment bundle of system.bundle.

The other way of setting org.osgi.framework.system.packages did indeed
lead to problems. My Application threw alot of errors because it didnt
find stuff. I guess setting up org.osgi.framework.system.packages
manually to all the right settings would work.? But thats alot of work.

Again, Thank you!

Regards,
Gordian


Danail Nachev schrieb:
> The best approach is to create framework extension bundle. This is a
> fragment bundle of the system.bundle:
>
> Fragment-Host: system.bundle; extension:=framework
>
> Additionally, in the fragment bundle's manifest, you need to export the
> packages you are interested in:
>
> Export-Package: com.acme.pack; version="1.0"
>
> In your bundle, you need to import it:
>
> Import-Package: com.acme.pack
>
> The other approach is by setting the org.osgi.framework.system.packages
> property, before launching the OSGi container. However, this may lead to
> other problems if you are using samo JDK packages in your bundles, so it
> is not advisable to use it, unless you know what you are doing. More
> information about both concepts can be found in the OSGi R4.1 specification:
>
> http://www.osgi.org/Download/Release4V41
>
> gordian wrote:
>> Hello,
>>
>> thank you for your answer.
>> How do i make a class available to OSGi via system packages?
>>
>> Regards,
>> Gordian
>>
>> Danail Nachev schrieb:
>>> Indeed this is a clear symptom that you are using different class
>>> loaders. The bundle which registers the service is using the its bundle
>>> class loader. However, your code trying to access the service is using
>>> most probably VM's Application Class loader, which is the very base
>>> class loader.
>>>
>>> You need to guarantee that the class is loaded from the same place for
>>> both your non-OSGi code and your OSGi code. This can be most easily done
>>> by packing the class in your non-OSGi part of the application and making
>>> it available to the OSGi part via system packages
>>> (org.osgi.framework.system.packages) and importing the package in the
>>> bundle's manifest.
>>>
>>> Have fun,
>>> Danail
>>>
>>> gordian wrote:
>>>> Hello,
>>>>
>>>> im pretty new to eclipse RCP and OSGi.
>>>> I have two Projects. The first is an RCP application and the second is a
>>>> pure Java application.
>>>> I try to get an Object that i succesfully registered as OSGi Service
>>>> from outside the OSGi Framework with getService. This doesnt work.
>>>>
>>>> Here is what i do:
>>>>
>>>> Register the service inside my RCP application:
>>>> Hashtable props = new Hashtable();
>>>> props.put(Constants.SERVICE_DESCRIPTION, "QualiGUI Service");
>>>> props.put("description", "This is a QualiGUI Test");
>>>> ServiceRegistration sr = bc.registerService(ComTest.class.getName(), new
>>>> ComTest(), props);
>>>>
>>>> This seems to work. The service is listed in the Array i get from
>>>> EclipseStarter.getSystemBundleContext().getAllServiceReferen ces(null,
>>>> null)
>>>> from inside my Plugin.
>>>>
>>>> In my Java application is set some Frameworksettings and then
>>>> start the Framwork using
>>>> EclipseStarter.run(args);
>>>> This works fine. My RCP Application is running.
>>>>
>>>> Now im trying to get the Service that i registered above.
>>>> for (Bundle b : EclipseStarter.getSystemBundleContext().getBundles())
>>>> {
>>>> if (b.getSymbolicName().equalsIgnoreCase("qualishipgui"))
>>>> {
>>>> for (ServiceReference sr : b.getRegisteredServices())
>>>> {
>>>> ComTest ct = (ComTest)b.getBundleContext().getService(sr);
>>>> }
>>>> }
>>>> }
>>>> In my Java project i added my RCP project to the classpath so i get
>>>> access to the class "ComTest" that is located inside my RCP app.
>>>>
>>>> When the Cast runs i get this error:
>>>> Exception in thread "main" java.lang.ClassCastException:
>>>> de.cemaris.qualiship.qualishipgui.communication.ComTest cannot be cast
>>>> to de.cemaris.qualiship.qualishipgui.communication.ComTest
>>>>
>>>> This looks kind of funny to me..
>>>> I googled a lot and search Forums and i figured it has to do something
>>>> with the different Classloaders being used.
>>>> But i didnt find a solution on how to fix this.
>>>>
>>>> Can you help me, please?
>>>> regards,
>>>> Gordian
Previous Topic:Equinox Transforms Hook bundle missing on CVS?
Next Topic:3.4m6 and readonly directories.
Goto Forum:
  


Current Time: Fri Apr 26 10:28:30 GMT 2024

Powered by FUDForum. Page generated in 0.03185 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top