You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@felix.apache.org by Imóveis Nacionais <im...@gmail.com> on 2013/02/12 19:56:28 UTC
Console calling OSGi bundle returns “cannot be cast to…”
I have 2 bundles, one that supplys a service and the other that calls the
service. When both are installed and started in felix framework everything
works.
Now, I would like to drop/forget the client bundle and create a console app
to launch felix, install server bundle and call the service.
Here the structure of server bundle (MyBundleServer):
Src
com.mycompany.mybundleserver package
Activator.java
upnpImpl.java
com.mycompany.mybundleserver.serviceinterface
upnp.java -> the interface
For the console client, I created a java application, I added the
org.apache.felix.framework-2.0.0.jar and wrote the following code in main()
function
FrameworkFactory frameworkFactory =
ServiceLoader.load(FrameworkFactory.class).iterator().next();
Map<String, String> config = new HashMap<String, String>();
Framework framework = frameworkFactory.newFramework(config);
framework.start();
BundleContext context = framework.getBundleContext();
Bundle y=
context.installBundle("file:/C:/OSGi/felix-framework-4.0.3/MyBundleServer-1.0-SNAPSHOT.jar"
); //the server bundle
y.start();
ServiceReference sr = context.getServiceReference(upnp.class.getName());
if(sr != null)
{
upnp x = (upnp)context.getService(sr); //interface type
if(x != null)
{
int n=x.GetNumber();
context.ungetService(sr);
}
}
When running, line
upnp x = (upnp)context.getService(sr);
generates:
Exception in thread "main" java.lang.ClassCastException:
com.mycompany.mybundleserver.upnpImpl cannot be cast to
com.mycompany.mybundleserver.serviceinterface.upnp
at mystandaloneclient.MyStandAloneClient.main(MyStandAloneClient.java:65)
So, if upnpImpl implements upnp interface and upnp is the interface why can
not JVM cast this stuff?
Thanks a lot
Alex
Re: Console calling OSGi bundle returns “cannot be cast to…”
Posted by Dhiego Abrantes de Oliveira Martins <dh...@dhiegoabrantes.com>.
Thanks!
Re: Console calling OSGi bundle returns “cannot be cast to…”
Posted by "Richard S. Hall" <he...@ungoverned.org>.
On 2/16/13 22:02, Dhiego Abrantes de Oliveira Martins wrote:
> What do you mean with 'framework' ?
OSGi framework...in this case Felix framework.
-> richard
>
> regards,
>
> Dhiego.
>
>
> 2013/2/12 Richard S. Hall <he...@ungoverned.org>
>
>> You are loading the class from two different class loaders, thus they are
>> not compatible with each other.
>>
>> You cannot "import" packages from bundles when you are on the outside of
>> the framework, such as you are trying to do by moving your client to the
>> outside.
>>
>> The only way you can get this to work is to put the packages you want to
>> share on the outside too and then export them into the framework via the
>> system bundle (see org.osgi.framework.system.**packages.extra config
>> property).
>>
>> Any classes you want to share between bundles and outside code must be on
>> the class path and shared into the framework...we have a whole chapter on
>> launching and embedding frameworks in the OSGi in Action book if you want a
>> more detailed explanation...
>>
>> -> richard
>>
>> p.s. Technically, you could also use reflection in the client instead of
>> using the classes directly, but I assume you don't want to do that.
>>
>>
>> On 2/12/13 13:56 , Imóveis Nacionais wrote:
>>
>>> I have 2 bundles, one that supplys a service and the other that calls the
>>> service. When both are installed and started in felix framework everything
>>> works.
>>>
>>> Now, I would like to drop/forget the client bundle and create a console
>>> app
>>> to launch felix, install server bundle and call the service.
>>>
>>>
>>>
>>> Here the structure of server bundle (MyBundleServer):
>>>
>>> Src
>>>
>>> com.mycompany.mybundleserver package
>>>
>>> Activator.java
>>>
>>> upnpImpl.java
>>>
>>> com.mycompany.mybundleserver.**serviceinterface
>>>
>>> upnp.java -> the interface
>>>
>>>
>>>
>>> For the console client, I created a java application, I added the
>>> org.apache.felix.framework-2.**0.0.jar and wrote the following code in
>>> main()
>>> function
>>>
>>>
>>> FrameworkFactory frameworkFactory =
>>> ServiceLoader.load(**FrameworkFactory.class).**iterator().next();
>>>
>>> Map<String, String> config = new HashMap<String, String>();
>>>
>>> Framework framework = frameworkFactory.newFramework(**config);
>>>
>>> framework.start();
>>>
>>>
>>> BundleContext context = framework.getBundleContext();
>>>
>>> Bundle y=
>>> context.installBundle("file:/**C:/OSGi/felix-framework-4.0.3/**
>>> MyBundleServer-1.0-SNAPSHOT.**jar"
>>> ); //the server bundle
>>>
>>> y.start();
>>>
>>>
>>>
>>> ServiceReference sr = context.getServiceReference(**
>>> upnp.class.getName());
>>>
>>> if(sr != null)
>>>
>>> {
>>>
>>> upnp x = (upnp)context.getService(sr); //interface type
>>>
>>> if(x != null)
>>>
>>> {
>>>
>>> int n=x.GetNumber();
>>>
>>> context.ungetService(sr);
>>>
>>> }
>>>
>>> }
>>>
>>>
>>>
>>> When running, line
>>>
>>> upnp x = (upnp)context.getService(sr);
>>>
>>> generates:
>>>
>>> Exception in thread "main" java.lang.ClassCastException:
>>> com.mycompany.mybundleserver.**upnpImpl cannot be cast to
>>> com.mycompany.mybundleserver.**serviceinterface.upnp
>>>
>>> at mystandaloneclient.**MyStandAloneClient.main(**
>>> MyStandAloneClient.java:65)
>>>
>>>
>>>
>>> So, if upnpImpl implements upnp interface and upnp is the interface why
>>> can
>>> not JVM cast this stuff?
>>>
>>> Thanks a lot
>>>
>>>
>>>
>>> Alex
>>>
>>>
>> ------------------------------**------------------------------**---------
>> To unsubscribe, e-mail: users-unsubscribe@felix.**apache.org<us...@felix.apache.org>
>> For additional commands, e-mail: users-help@felix.apache.org
>>
>>
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
For additional commands, e-mail: users-help@felix.apache.org
Re: Console calling OSGi bundle returns “cannot be cast to…”
Posted by Dhiego Abrantes de Oliveira Martins <dh...@dhiegoabrantes.com>.
What do you mean with 'framework' ?
regards,
Dhiego.
2013/2/12 Richard S. Hall <he...@ungoverned.org>
> You are loading the class from two different class loaders, thus they are
> not compatible with each other.
>
> You cannot "import" packages from bundles when you are on the outside of
> the framework, such as you are trying to do by moving your client to the
> outside.
>
> The only way you can get this to work is to put the packages you want to
> share on the outside too and then export them into the framework via the
> system bundle (see org.osgi.framework.system.**packages.extra config
> property).
>
> Any classes you want to share between bundles and outside code must be on
> the class path and shared into the framework...we have a whole chapter on
> launching and embedding frameworks in the OSGi in Action book if you want a
> more detailed explanation...
>
> -> richard
>
> p.s. Technically, you could also use reflection in the client instead of
> using the classes directly, but I assume you don't want to do that.
>
>
> On 2/12/13 13:56 , Imóveis Nacionais wrote:
>
>> I have 2 bundles, one that supplys a service and the other that calls the
>> service. When both are installed and started in felix framework everything
>> works.
>>
>> Now, I would like to drop/forget the client bundle and create a console
>> app
>> to launch felix, install server bundle and call the service.
>>
>>
>>
>> Here the structure of server bundle (MyBundleServer):
>>
>> Src
>>
>> com.mycompany.mybundleserver package
>>
>> Activator.java
>>
>> upnpImpl.java
>>
>> com.mycompany.mybundleserver.**serviceinterface
>>
>> upnp.java -> the interface
>>
>>
>>
>> For the console client, I created a java application, I added the
>> org.apache.felix.framework-2.**0.0.jar and wrote the following code in
>> main()
>> function
>>
>>
>> FrameworkFactory frameworkFactory =
>> ServiceLoader.load(**FrameworkFactory.class).**iterator().next();
>>
>> Map<String, String> config = new HashMap<String, String>();
>>
>> Framework framework = frameworkFactory.newFramework(**config);
>>
>> framework.start();
>>
>>
>> BundleContext context = framework.getBundleContext();
>>
>> Bundle y=
>> context.installBundle("file:/**C:/OSGi/felix-framework-4.0.3/**
>> MyBundleServer-1.0-SNAPSHOT.**jar"
>> ); //the server bundle
>>
>> y.start();
>>
>>
>>
>> ServiceReference sr = context.getServiceReference(**
>> upnp.class.getName());
>>
>> if(sr != null)
>>
>> {
>>
>> upnp x = (upnp)context.getService(sr); //interface type
>>
>> if(x != null)
>>
>> {
>>
>> int n=x.GetNumber();
>>
>> context.ungetService(sr);
>>
>> }
>>
>> }
>>
>>
>>
>> When running, line
>>
>> upnp x = (upnp)context.getService(sr);
>>
>> generates:
>>
>> Exception in thread "main" java.lang.ClassCastException:
>> com.mycompany.mybundleserver.**upnpImpl cannot be cast to
>> com.mycompany.mybundleserver.**serviceinterface.upnp
>>
>> at mystandaloneclient.**MyStandAloneClient.main(**
>> MyStandAloneClient.java:65)
>>
>>
>>
>> So, if upnpImpl implements upnp interface and upnp is the interface why
>> can
>> not JVM cast this stuff?
>>
>> Thanks a lot
>>
>>
>>
>> Alex
>>
>>
>
> ------------------------------**------------------------------**---------
> To unsubscribe, e-mail: users-unsubscribe@felix.**apache.org<us...@felix.apache.org>
> For additional commands, e-mail: users-help@felix.apache.org
>
>
Re: Console calling OSGi bundle returns “cannot be cast to…”
Posted by "Richard S. Hall" <he...@ungoverned.org>.
You are loading the class from two different class loaders, thus they
are not compatible with each other.
You cannot "import" packages from bundles when you are on the outside of
the framework, such as you are trying to do by moving your client to the
outside.
The only way you can get this to work is to put the packages you want to
share on the outside too and then export them into the framework via the
system bundle (see org.osgi.framework.system.packages.extra config
property).
Any classes you want to share between bundles and outside code must be
on the class path and shared into the framework...we have a whole
chapter on launching and embedding frameworks in the OSGi in Action book
if you want a more detailed explanation...
-> richard
p.s. Technically, you could also use reflection in the client instead of
using the classes directly, but I assume you don't want to do that.
On 2/12/13 13:56 , Imóveis Nacionais wrote:
> I have 2 bundles, one that supplys a service and the other that calls the
> service. When both are installed and started in felix framework everything
> works.
>
> Now, I would like to drop/forget the client bundle and create a console app
> to launch felix, install server bundle and call the service.
>
>
>
> Here the structure of server bundle (MyBundleServer):
>
> Src
>
> com.mycompany.mybundleserver package
>
> Activator.java
>
> upnpImpl.java
>
> com.mycompany.mybundleserver.serviceinterface
>
> upnp.java -> the interface
>
>
>
> For the console client, I created a java application, I added the
> org.apache.felix.framework-2.0.0.jar and wrote the following code in main()
> function
>
>
> FrameworkFactory frameworkFactory =
> ServiceLoader.load(FrameworkFactory.class).iterator().next();
>
> Map<String, String> config = new HashMap<String, String>();
>
> Framework framework = frameworkFactory.newFramework(config);
>
> framework.start();
>
>
> BundleContext context = framework.getBundleContext();
>
> Bundle y=
> context.installBundle("file:/C:/OSGi/felix-framework-4.0.3/MyBundleServer-1.0-SNAPSHOT.jar"
> ); //the server bundle
>
> y.start();
>
>
>
> ServiceReference sr = context.getServiceReference(upnp.class.getName());
>
> if(sr != null)
>
> {
>
> upnp x = (upnp)context.getService(sr); //interface type
>
> if(x != null)
>
> {
>
> int n=x.GetNumber();
>
> context.ungetService(sr);
>
> }
>
> }
>
>
>
> When running, line
>
> upnp x = (upnp)context.getService(sr);
>
> generates:
>
> Exception in thread "main" java.lang.ClassCastException:
> com.mycompany.mybundleserver.upnpImpl cannot be cast to
> com.mycompany.mybundleserver.serviceinterface.upnp
>
> at mystandaloneclient.MyStandAloneClient.main(MyStandAloneClient.java:65)
>
>
>
> So, if upnpImpl implements upnp interface and upnp is the interface why can
> not JVM cast this stuff?
>
> Thanks a lot
>
>
>
> Alex
>
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
For additional commands, e-mail: users-help@felix.apache.org