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