felix-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Tim McIver <tmci...@verizon.net>
Subject Re: RepositoryAdmin ClassCastException
Date Wed, 06 Aug 2014 15:58:52 GMT
Neil,

Thanks for your reply.  Here are the OSGi/Felix -related imports I have
in the class that contains the code that I included in the original
email (below):

import org.apache.felix.bundlerepository.Resource;
import org.apache.felix.framework.Felix;
import org.apache.felix.framework.util.FelixConstants;
import org.apache.felix.bundlerepository.RepositoryAdmin;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleException;
import org.osgi.framework.BundleContext;
import org.osgi.framework.Constants;
import org.osgi.framework.ServiceReference;
import org.osgi.util.tracker.ServiceTracker;

Regardless of which import I'm using I'm still confused as to how this
discrepancy can exist since I'm requesting an implementation of the
RepositoryAdmin I'm importing.

Thanks for the tip about how to open the ServiceTracker.  We are indeed
using open() without an arg.  I was not aware of that gotcha.

Tim

On 08/06/2014 11:18 AM, Neil Bartlett wrote:
> Tim, you don’t really need to know anything about classloaders! The important part
from David’s email was the second paragraph. Make sure you import the same package that
the provider of the service uses, and you will be okay.
>
> By the way, how did you open your ServiceTracker? Did you call open(true)? If so, this
exposes you to incompatible services and potential classloading issues… you should nearly
always use open(false) or the no-arg open() method, which is equivalent. Then OSGi protects
you from seeing service instances that are incompatible with your bundle.
>
> Regards,
> Neil
>
>
> On 6 August 2014 at 16:04:32, Tim McIver (tmciver@verizon.net) wrote:
>
> David,  
>
> Thanks for the reply. I figured it had to do with Class loaders but I'm  
> at a loss as to how this is happening; I don't know a lot about class  
> loaders.  
>
> Here's some more detail on my situation: as I said we're using Felix in  
> an embedded fashion; we instantiate a Felix object and start it from  
> within a JavaFX app (we're using IntelliJ). I suppose this is the  
> source of the different classloaders. The reason I'm confused is that  
> we have been successfully using a ServiceTracker to get references to  
> our plugins without any classloading issues. So how can I fix this? Do  
> I need access Felix's classloader and use it to load the  
> RepositoryAdmin? Any pointers on how to do this?  
>
> Thanks,  
> Tim  
>
> On 08/06/2014 03:15 AM, David Bosschaert wrote:  
>> Hi Tim,  
>>  
>>> java.lang.ClassCastException:  
>>> org.apache.felix.bundlerepository.impl.RepositoryAdminImpl cannot be  
>>> cast to org.apache.felix.bundlerepository.RepositoryAdmin  
>> This happens when the RepositoryAdmin interface is loaded from a  
>> different classloader than where the RepositoryAdminImpl loads it  
>> from.  
>>  
>> You need to check that the bundle that runs your code has an import on  
>> the org.apache.felix.bundlerepository package like this:  
>> Import-Package: org.apache.felix.bundlerepository;version="[1.0,2)"  
>> and is wired to the same bundle where you get the RepositoryAdminImpl from.  
>>  
>>> By the way, is there javadoc for Felix? I can't seem to find any.  
>> If you're looking for documentation on Felix, it might be best to look  
>> for general OSGi Framework documentation. You can find information on  
>> the Felix Framework in the OSGi Core R5 specs [1] or in one of the  
>> many books and web articles about the OSGi framework.  
>>  
>> For documentation about the Bundle Repository, it might be worth  
>> looking at the OSGi R5 Enterprise Specs (also at [1]). Chapter 132  
>> describes the OSGi Repository Service. Since version 2.0.x this is now  
>> also implemented by Felix OBR so using that gives you a standard API  
>> to work with the Repository.  
>>  
>> Cheers,  
>>  
>> David  
>>  
>> [1] http://www.osgi.org/Download/Release5  
>>  
>> On 5 August 2014 20:38, Tim McIver <tmciver@verizon.net> wrote:  
>>> Hello,  
>>>  
>>> I'm using Apache Felix in an embedded fashion. I'm attempting to access  
>>> my own bundle repository so I install and start the bundle repository  
>>> bundle like so:  
>>>  
>>> // install the Felix OBR bundle  
>>> Bundle obrBundle = null;  
>>> try {  
>>> obrBundle =  
>>> m_felix.getBundleContext().installBundle("file:/home/tim/workspace/clojure/lambdaprime/metascrape-ui/lib/org.apache.felix.bundlerepository-2.0.2.jar");
 
>>> } catch (BundleException e) {  
>>> System.err.println("Error installing Felix OBR bundle.");  
>>> e.printStackTrace();  
>>> }  
>>>  
>>> // start the Felix OBR bundle  
>>> if (obrBundle != null) {  
>>> try {  
>>> obrBundle.start();  
>>> } catch (BundleException e) {  
>>> System.err.println("Could not start Felix OBR bundle.");  
>>> e.printStackTrace();  
>>> }  
>>> }  
>>>  
>>> I have a private method to get a RepositoryAdmin:  
>>>  
>>> private static RepositoryAdmin getRepositoryAdmin(BundleContext ctx) {  
>>> ServiceReference ref =  
>>> ctx.getServiceReference(RepositoryAdmin.class.getName());  
>>> if (ref != null) {  
>>> return (RepositoryAdmin)ctx.getService(ref);  
>>> }  
>>> return null;  
>>> }  
>>>  
>>> But when this method is called, I get the following exception:  
>>>  
>>> java.lang.ClassCastException:  
>>> org.apache.felix.bundlerepository.impl.RepositoryAdminImpl cannot be  
>>> cast to org.apache.felix.bundlerepository.RepositoryAdmin  
>>>  
>>> I'm using felix.jar from the bin directory of of the 4.4.1 distribution  
>>> and using org.apache.felix.bundlerepository-2.0.2.jar for the bundle  
>>> repository. Any ideas what my issue is?  
>>>  
>>> By the way, is there javadoc for Felix? I can't seem to find any.  
>>>  
>>> Thanks,  
>>> Tim  
>>>  
>>> ---------------------------------------------------------------------  
>>> 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  
>>  
>
> ---------------------------------------------------------------------  
> 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


Mime
View raw message