You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@felix.apache.org by Roland <wg...@ids.de> on 2013/05/07 17:17:15 UTC

ClassCastException after isAssignableTo()

ClassCastException - not again!

Hello everyone,
after hours of investigation of my problem i resign.

I embedded Apache Felix 4.3 to my host application, i use the maven blugin
to build my project and i have 3 bundles.

Bundle A (systembundle)
Bundle B (exports a interface)
Bundle C (implements this interface)

Bundle C imports the interface and registers the implementation of the
interface as a service.
Bundle A imports the interface and uses this service.

In the activator method of the systembundle (A) i have following code
snippets.



I get a ClassCastException after isAssignableTo().

I checked if the interface and the implementation are fully separated.
The imports and exports of (A,B,C) are correct.
The classloaders are the same, namely the classloader of the bundle which
exports the interface (B).
I checked if the interface-package is imbedded to other bundles, but it
exist only one version in (B).
I checked if i'm inside or outside of OSGi. I'm definitly inside of OSGi.

Have someone an idea whats going wrong? What else can I check?
Maybe a switch to Equinox might help. Should I?


Thank you so much so far.
regards
Roland



--
View this message in context: http://apache-felix.18485.x6.nabble.com/ClassCastException-after-isAssignableTo-tp5003252.html
Sent from the Apache Felix - Users mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
For additional commands, e-mail: users-help@felix.apache.org


Re: ClassCastException after isAssignableTo()

Posted by Roland <wg...@ids.de>.
appendix...

g! inspect capability service 19                 (19 = id of Bundle C)
BundleC [19] provides:
-----------------------------------------------------------------------------------------
service; PackageOfBundleB.IMyService with properties:
   service.id = 43
   Used by:
      org.apache.felix.framework [0]

g! inspect requirement service 0              (0 = id of Systembundle A)
org.apache.felix.framework [0] requires:
----------------------------------------
service; PackageOfBundleB.IMyService provided by:
   BundleC [19]



--
View this message in context: http://apache-felix.18485.x6.nabble.com/ClassCastException-after-isAssignableTo-tp5003252p5003253.html
Sent from the Apache Felix - Users mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
For additional commands, e-mail: users-help@felix.apache.org


Re: ClassCastException after isAssignableTo()

Posted by Neil Bartlett <nj...@gmail.com>.
Your code samples have been removed from your email again, and I'm not
sure what else might be missing.

However, I notice one obvious error. The package is exported by Bundle
B, and you are trying to import it in Bundle A and Bundle C....
however, Bundle A is the system bundle. It is impossible for the
system bundle to import packages from ordinary bundles. Instead, you
need to export the package from the system bundle using the
"org.osgi.framework.system.packages.extra" configuration property. At
that going, Bundle B becomes redundant (unless it has other useful
exports).

You could try switching to Equinox; you'll get exactly the same
problem of course :-)

Neil


On Tue, May 7, 2013 at 4:17 PM, Roland <wg...@ids.de> wrote:
> ClassCastException - not again!
>
> Hello everyone,
> after hours of investigation of my problem i resign.
>
> I embedded Apache Felix 4.3 to my host application, i use the maven blugin
> to build my project and i have 3 bundles.
>
> Bundle A (systembundle)
> Bundle B (exports a interface)
> Bundle C (implements this interface)
>
> Bundle C imports the interface and registers the implementation of the
> interface as a service.
> Bundle A imports the interface and uses this service.
>
> In the activator method of the systembundle (A) i have following code
> snippets.
>
>
>
> I get a ClassCastException after isAssignableTo().
>
> I checked if the interface and the implementation are fully separated.
> The imports and exports of (A,B,C) are correct.
> The classloaders are the same, namely the classloader of the bundle which
> exports the interface (B).
> I checked if the interface-package is imbedded to other bundles, but it
> exist only one version in (B).
> I checked if i'm inside or outside of OSGi. I'm definitly inside of OSGi.
>
> Have someone an idea whats going wrong? What else can I check?
> Maybe a switch to Equinox might help. Should I?
>
>
> Thank you so much so far.
> regards
> Roland
>
>
>
> --
> View this message in context: http://apache-felix.18485.x6.nabble.com/ClassCastException-after-isAssignableTo-tp5003252.html
> Sent from the Apache Felix - Users mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@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: ClassCastException after isAssignableTo()

Posted by "Richard S. Hall" <he...@ungoverned.org>.
On 5/7/13 11:17 , Roland wrote:
> ClassCastException - not again!
>
> Hello everyone,
> after hours of investigation of my problem i resign.
>
> I embedded Apache Felix 4.3 to my host application, i use the maven blugin
> to build my project and i have 3 bundles.
>
> Bundle A (systembundle)
> Bundle B (exports a interface)
> Bundle C (implements this interface)
>
> Bundle C imports the interface and registers the implementation of the
> interface as a service.
> Bundle A imports the interface and uses this service.
>
> In the activator method of the systembundle (A) i have following code
> snippets.

You cannot use a package exported by a bundle from the system bundle. If 
you need to use a service from the system bundle (i.e., in the host 
application) then the service interface must be exported by the system 
bundle (i.e., provided by the host application)...unless you're willing 
to use reflection to access the service (pretty ugly).

At the Gogo prompt you can use "which" to see where each bundle gets a 
specific class. If you test the service interface in this case, you 
should see that A gets it from a different place than B and C.

>
>
> I get a ClassCastException after isAssignableTo().
>
> I checked if the interface and the implementation are fully separated.
> The imports and exports of (A,B,C) are correct.
> The classloaders are the same, namely the classloader of the bundle which
> exports the interface (B).
> I checked if the interface-package is imbedded to other bundles, but it
> exist only one version in (B).
> I checked if i'm inside or outside of OSGi. I'm definitly inside of OSGi.
>
> Have someone an idea whats going wrong? What else can I check?
> Maybe a switch to Equinox might help. Should I?

Perhaps read some more. :-)

-> richard

>
>
> Thank you so much so far.
> regards
> Roland
>
>
>
> --
> View this message in context: http://apache-felix.18485.x6.nabble.com/ClassCastException-after-isAssignableTo-tp5003252.html
> Sent from the Apache Felix - Users mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@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: Experiences with ClassCastException

Posted by Roland <wg...@ids.de>.
Hello Jan,
I introduce the framework into an existing project. Certain requirements
make it necessary to use the low-level APIs. In principle, you're right.



--
View this message in context: http://apache-felix.18485.x6.nabble.com/ClassCastException-after-isAssignableTo-tp5003252p5004275.html
Sent from the Apache Felix - Users mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
For additional commands, e-mail: users-help@felix.apache.org


Re: Experiences with ClassCastException

Posted by Jan Willem Janssen <ja...@luminis.eu>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 7/11/13 11:12 AM, Roland wrote:
> Hi, I would like to tell you about my experiences that I made
> yesterday. I had a error that occurred during integration testing
> and which is difficult to understand. But I was able to fix the
> error. Maybe I can prevent someone making the same mistake.
> 
> I have a Utility-Class which is used by the Felix-Launcher
> (systembundle) as well as by some other bundles. This Utility-Class
> provides some basic functionality like catching services with
> interface-casting and error handling. Since the systembundle can
> not import packages the Utility-package has to be embedded to the
> systembundle. So far so good. At any point of time I register a
> number of services by a Bundle A. And at a later point of time I
> catch the services by calling ServiceTracker::getServices() in a
> Bundle B.
> 
> public <T> T[] getServices(BundleContext bundlecontext, Class<T>
> clazz) { ServiceTracker st = new ServiceTracker(bundlecontext,
> clazz.getName(), null); st.open(); Object[] objs =
> st.getServices(); st.close(); try{ return (T[]) objs; 
> }catch(LinkageError e){ ... }catch(ClassCastException e){ ... }
> 
> The service-objects are instanciated by the bundle-ClassLoader of
> Bundle A. The array "Object[] objs" (see above) holding the
> service-instances is instanciated by the bootstrap-ClassLoader but
> not by the ClassLoader of Bundle A. This causes a
> ClassCastException because of two different ClassLoaders. Very
> nasty!
> 
> The solution is to use Collection<T> instead of T[].

tl;drc> Why don't you use frameworks that can manage the dependencies
for you, like DS or Felix DependencyManager, instead of fiddling with
the low-level APIs?

- -- 
Met vriendelijke groeten | Kind regards

Jan Willem Janssen | Software Architect
+31 631 765 814

/My world is revolving around PulseOn and Amdatu/

Luminis Technologies B.V.
J.C. Wilslaan 29
7313 HK   Apeldoorn
+31 88 586 46 30

http://www.luminis-technologies.com
http://www.luminis.eu

KvK (CoC) 09 16 28 93
BTW (VAT) NL8169.78.566.B.01
-----BEGIN PGP SIGNATURE-----
Version: GnuPG/MacGPG2 v2.0.17 (Darwin)
Comment: GPGTools - http://gpgtools.org
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/

iQIcBAEBAgAGBQJR3nv2AAoJEKF/mP2eHDc4cXUP/30bgJdUSbU4PKaV9iAJcdTs
J5sZvSI7NCkwwU7lhZFmURqyqxarKRwv/FQObvI87nRu4S/Q/8x9sU4cSIuXXsGj
PKIV+yYVH1XD01x/LDyXgyuFsMqfqHT8MObV8pSyFuNs2gnnJDd11Z492ZYn56H6
nbIL2RCMUCs5clJLpwfrAGS6y2835ZtuDn0TDHub6UOZ5sexfHKKsWH0UhYused8
vsmdLGlIw3V9W1XfQQhOosAbbUFzRHGIa6OflcBMiMZiixZZzR1ysmIr0DCAVXzU
2QL1q9or+94eVbPhZuOUsJgZLeHxOtjJpzguTP2ofM5801/1GKlxOuAYI78Wqi5d
qYZ8DExwf1Y/9s9ESQa6QcAq6uOFpDAGV8OO8KAFj4usKTP3uaK0wrGH1sg+AmBC
vw0jXNFTuTDW8JmC4MfDjqAx9WnS2+1yIZLS4zaYjJ3iVanJAJSkAUCGHeFhgeZ1
K1KwJtnySxhP01K4li/uOA9U8jEr/dbMtq0+Xgp4fQLJvDbMiy4reddhUn/H6Y4w
+xv4sW6h48OkeA8kxSK60zA0cp3EpPgYK1rUicpl03k9zt435aB1teMdo3RKdQ+i
QC6GlIOvjZppv3gOIN+QAtltL3Iog64kw6WqvrfSvTP7Gdo1HaK/+OR6wM7XMZIm
J28WFlt0M2xm8u8xHzpT
=Vdw/
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
For additional commands, e-mail: users-help@felix.apache.org


Experiences with ClassCastException

Posted by Roland <wg...@ids.de>.
Hi,
I would like to tell you about my experiences that I made yesterday. I had a
error that occurred during integration testing and which is difficult to
understand. But I was able to fix the error. Maybe I can prevent someone
making the same mistake.

I have a Utility-Class which is used by the Felix-Launcher (systembundle) as
well as by some other bundles. This Utility-Class provides some basic
functionality like catching services with interface-casting and error
handling. Since the systembundle can not import packages the Utility-package
has to be embedded to the systembundle. So far so good. At any point of time
I register a number of services by a Bundle A. And at a later point of time
I catch the services by calling ServiceTracker::getServices() in a Bundle B.

  public <T> T[] getServices(BundleContext bundlecontext, Class<T> clazz)
  {
    ServiceTracker st = new ServiceTracker(bundlecontext, clazz.getName(),
null);
    st.open();
    Object[] objs = st.getServices();
    st.close();
    try{
        return (T[]) objs;
      }catch(LinkageError e){
        ...
      }catch(ClassCastException e){
        ...
      }

The service-objects are instanciated by the bundle-ClassLoader of Bundle A.
The array "Object[] objs" (see above) holding the service-instances is
instanciated by the bootstrap-ClassLoader but not by the ClassLoader of
Bundle A. This causes a ClassCastException because of two different
ClassLoaders. Very nasty!

The solution is to use Collection<T> instead of T[].

Regards
Roland




--
View this message in context: http://apache-felix.18485.x6.nabble.com/ClassCastException-after-isAssignableTo-tp5003252p5004270.html
Sent from the Apache Felix - Users mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
For additional commands, e-mail: users-help@felix.apache.org


Re: ClassCastException after isAssignableTo()

Posted by Roland <wg...@ids.de>.
Thank you very much for your replys! 



--
View this message in context: http://apache-felix.18485.x6.nabble.com/ClassCastException-after-isAssignableTo-tp5003252p5003256.html
Sent from the Apache Felix - Users mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
For additional commands, e-mail: users-help@felix.apache.org