Just in case anyone is interested: I've written a little bundle to work
around that problem and to get Apache FOP and Batik working in an OSGi
environment. The Apache XML Graphics project makes heavy use of the
META-INF/services mechanism for plug-ins.

My bundle provides code to transparently provide plug-in classes, be
that in an OSGi environment or in a plain Java environment. And it works
for Java >= 1.4 (i.e. doesn't use ServiceLoader). It seems to work quite
nicely for me so far, but I haven't taken the time to clean up
everything and publish it. And I'm using my own Ant-based
Bundle-Manifest creator (i.e. not BND) which is equally unpublished. But
if there's interest, I could do that work and publish it (be that on my
own website, as part of Apache XML Graphics or Apache Felix) in the next
few weeks (when my immediate deadlines are past).

Here's how some client code would look like:

        Services services = Services.getInstance();

        //Dynamic registration of ImagePreloaders
        Iterator iter = 
services.getServiceProvider(ImagePreloader.class).getServiceFactories();
        while (iter.hasNext()) {
            ServiceFactory factory = (ServiceFactory)iter.next();
            registerPreloader((ImagePreloader)factory.createInstance());
        }

Sorry, no generics, since I have to support Java 1.4 for the time being.

On 01.10.2009 11:48:16 Richard S. Hall wrote:
> Did this issue ever get resolved?
> 
> As far as I can see, you are trying to find a service provider from the 
> SPI bundle, but the SPI bundle doesn't have any visibility to the 
> service provider, so it cannot find it. You cannot use ServiceLoader in 
> the normal way in OSGi.
> 
> -> richard
> 
> On 5/18/09 22:04, Moloney, Tim M wrote:
> > I've attached my example project.  To build it just run 'mvn install'.
> > To run it from the command line, run 'RunFooFromOsgi'.  To run it within
> > Eclipse, import the project, then run fooOsgi.  RunFooFromMain (command
> > line) and fooMain (Eclipse) run the example service provider
> > successfully outside of Felix.
> >
> > If I run my example code from the command line (RunFooFromOsgi), no
> > service providers are found.
> >
> > If I run my example code from within Eclipse, I get the following:
> >
> > Welcome to Felix.
> > =================
> >
> > ->  In com.example.foo.osgi.Activator.start():
> > ERROR: Error starting file:target/com.example.foo.osgi-0.1.0.jar
> > (org.osgi.framework.BundleException: Activator start error in bundle
> > com.example.foo.osgi [5].)
> > java.util.ServiceConfigurationError: com.example.foo.spi.Foo: Provider
> > com.example.foo.impl.FooImpl could not be instantiated:
> > java.lang.ClassCastException
> >     at java.util.ServiceLoader.fail(ServiceLoader.java:207)
> >     at java.util.ServiceLoader.access$100(ServiceLoader.java:164)
> >     at
> > java.util.ServiceLoader$LazyIterator.next(ServiceLoader.java:353)
> >     at java.util.ServiceLoader$1.next(ServiceLoader.java:421)
> >     at com.example.foo.spi.FooFactory.getFoo(FooFactory.java:17)
> >     at com.example.foo.osgi.Activator.start(Activator.java:17)
> >     at
> > org.apache.felix.framework.util.SecureAction.startActivator(SecureAction
> > .java:589)
> >     at org.apache.felix.framework.Felix.startBundle(Felix.java:1458)
> >     at
> > org.apache.felix.framework.Felix.setActiveStartLevel(Felix.java:984)
> >     at
> > org.apache.felix.framework.StartLevelImpl.run(StartLevelImpl.java:263)
> >     at java.lang.Thread.run(Thread.java:619)
> > Caused by: java.lang.ClassCastException
> >     at java.lang.Class.cast(Class.java:2990)
> >     at
> > java.util.ServiceLoader$LazyIterator.next(ServiceLoader.java:345)
> >     ... 8 more
> >
> >
> > Tim Moloney                            The reasonable man adapts himself
> > to
> > ManTech Real-time Systems Laboratory   the world; the unreasonable one
> > persists
> > 2015 Cattlemen Road                    in trying to adapt the world to
> > himself.
> > Sarasota, FL  34232                    Therefore all progress depends on
> > the
> > (941) 377-6775 x208                    unreasonable man. - George
> > Bernard Shaw
> >
> >
> >    
> >> -----Original Message-----
> >> From: Richard S. Hall [mailto:he...@ungoverned.org]
> >> Sent: Monday, May 18, 2009 10:11
> >> To: users@felix.apache.org
> >> Subject: Re: ServiceLoader and OSGi?
> >>
> >> On 5/18/09 9:31 AM, Moloney, Tim M wrote:
> >>      
> >>> I have a very simple service provider that works outside of OSGi.
> >>>
> >>> I tried using it in Felix but it fails with a ClassCastException.
> >>>
> >>> Is this somehow related to the different class loaders in
> >>>        
> >> the different
> >>      
> >>> bundles?
> >>>
> >>>        
> >> Probably.
> >>
> >>      
> >>> Any suggestions on how to get past this?
> >>>
> >>>        
> >> Not enough information to say.
> >>
> >> ->  richard
> >>
> >>      
> >>> Thanks.
> >>>
> >>>
> >>> Tim Moloney             The  reasonable  man adapts  himself  to
> >>> MRSL                    the world; the unreasonable one persists
> >>> 2015 Cattlemen Road     in trying to adapt the world to himself.
> >>> Sarasota, FL  34232     Therefore  all progress  depends on  the
> >>> (941) 377-6775 x208     unreasonable man.    George Bernard Shaw
> >>>
> >>>



Jeremias Maerki


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

Reply via email to