felix-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Dan Gravell <elstensoftw...@googlemail.com>
Subject Re: Help me understand ClassCastException after update
Date Sat, 30 Jun 2012 16:49:00 GMT
Thanks for answering Neil! I've been referring to your blog a lot the past
few weeks and we also talked on the bndtools group the other day.

The CCE occurs in the activator for Client. It's the line I pasted above:

context.registerService(A.class, (A) serverClass.getA(), new Hashtable());

So it's that cast: (A) - sorry I guess having one letter class names
probably obscured this.

Yes, another bundle uses this service. Let's call this... Client2 (sorry).
That bundle was *not* updated. That uses both a ServiceTracker and in a few
places a simple getService().

I did try to refresh after the update, but got another exception:

    java.lang.IllegalStateException: Invalid BundleContext.
    at
org.apache.felix.framework.BundleContextImpl.checkValidity(BundleContextImpl.java:514)
    at
org.apache.felix.framework.BundleContextImpl.getBundle(BundleContextImpl.java:173)

I am refreshing using a bundle context of one of the bundles that was
updated. This might be a bad thing to do. It throws the ISE I guess because
the of the previous CCE, as the bundle is in STARTING mode, not STARTED. My
code to refresh is:

context.getBundle(0).adapt(FrameworkWiring.class).resolveBundles(null);

In other words: get the system bundle, adapt it and then resolve all. I
didn't know another way of getting the FrameworkBinding instance from just
the BundleContext.

Dan


On Sat, Jun 30, 2012 at 5:39 PM, Neil Bartlett <njbartlett@gmail.com> wrote:

> Where does the ClassCastException happen? You said that Client registers
> the service, does any other bundle access and use the service? If so, what
> kind of method did you use to obtain the service (eg ServiceTracker, DS,
> etc)?
>
> Also did you refresh after updating the bundles? Note that bundles cannot
> really be updated "at the same time", ie there is no atomic multi-bundle
> update. This is why you need to refresh after a series of updates.
>
> Rgds
> Neil
>
> --
> Neil Bartlett
> Sent from a phone
>
>
> On Saturday, 30 June 2012 at 17:09, Dan Gravell wrote:
>
> > My OSGi adventure continues...
> >
> > I have a bundle "Client" and bundle "Server". In Client's activator it
> > invokes code in Server, returning an object from Server, let's call it
> > server.A. A is defined in Server. Client then registers A as a service:
> >
> > context.registerService(A.class, (A) serverClass.getA(), new
> Hashtable());
> >
> > This works when it is first run.
> >
> > If I then update Client and Server bundles, the Client Activator gets run
> > again. This time I get a ClassCastException saying "server.A cannot be
> cast
> > to server.A".
> >
> > So I guess the classloaders are different for the A that is returned by
> > Server to the one Client sees. Given both bundles were updated at the
> same
> > time, why is this?
> >
> > I make use of package versioning, if this is important. A's package,
> > server, was not updated, and its version was not incremented. Was the
> code
> > in Client using the old version of server.A?
> >
> > I guess registering A as a service in Client, rather than Server, is
> > opposed to most sample code I see but is it the cause of the problem? If
> > classloaders work on a per package level then it won't help will it?
> >
> > Thanks,
> > Dan
> >
> >
>
>
>

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message