META-INF/services vs. layer.xml registration

There's been some discussion on the NetBeans Platform mailing list (dev AT openide DOT netbeans DOT org) about the two approaches to registering service providers. Because, there are two approaches that are quite distinct that achieve the same end result. So the question is: which one to use and why? And why are there two approaches to begin with?

To recap, the two approaches are as follows:

  • Create a file in META-INF/services named after the fully qualified name of the interface that you'd like to implement, containing (nothing more than) the fully qualified name of the implementing class. This is the standard JDK 6 ServiceLoader approach. An example, covering both ServiceLoader and Lookup, can be found in John O'Conner's excellent Creating Extensible Applications With the Java Platform.

  • Register the service providers in the layer.xml file and then use Lookups.forPath("path/to/layer/folder") to locate the service provider. Toni Epple's recent concise and thorough How Do NetBeans Extension Points Work? on NetBeans Zone takes this approach.

Note that both John and Toni include compilable sample code with their article. In both cases, they explain the approach very well. Heiko Böck, in "NetBeans Platform 6: Rich-Client-Entwicklung mit Java" discusses service provider registrations too, in section 6.4 of his book, although there the service provider, if registered in the layer.xml must be found in a folder named "Services".

However, during the mailing list discussion, Casper Bang pointed to this helpful exchange in comments on Tim Boudreau's blog:

So, Lookups.forPath() is new. That, together with the other info in Tim's comment, clarifies a lot for me. Also see the related mail thread for some discussion.

Comments:

I never liked the usage of layer.xml files for services. For example the Editors/ folder is a mess with various service registrations you never know where do they come from. I guess it's a matter of style but icons / localization could be nicely done with BeanInfos so it's a bit of reinventing the wheel from this perspective.

I never used Lookups.forPath, but I'd probably pick META-INF/namedservices/ :-)

As for using the layer as a "writeable filesystem" and registering services at runtime (like GSF does for example) it is a nice trick but it looks to me like more of a bug of the Platform than a feature. Having to create a dummy file after I already know what class to add and where seems non-intuitive. Why not Lookup.forPath(whateve).register(Some.class) ?

All this filesystem usage make the code look really ugly sometimes and made me wonder multiple times if the whole layer concept isn't overused or badly used sometimes.

Posted by Emilian Bold on September 04, 2008 at 03:29 AM PDT #

I can't quite put a finger on it, but my intuition and sense of taste prefers the META-INF approach, it just seems more KISS.

Perhaps it's because I am tired/scared of all the XML or because it's actually pretty deeply rooted in Java in many other places as well, i.e: http://java.sun.com/j2se/1.4.2/docs/api/javax/imageio/spi/ServiceRegistry.html

Posted by Casper Bang on September 04, 2008 at 05:34 AM PDT #

I don't really prefer one method over the other. I used the layer for this sometimes since it's easy to order the services.
One benefit of META-INF approach might be that many people are used to it, which is good when providing an extension point.

Posted by Toni Epple on September 04, 2008 at 08:07 PM PDT #

good!

Posted by 一卡多号 on September 09, 2008 at 11:08 PM PDT #

I tend to stick with META-INF/services. The layer.xml seems more convoluted, but they do seem to serve similar function.

I can still easily order the services in META-INF/services via #position=<number> in whichever service.

Posted by Derek on October 20, 2008 at 12:20 AM PDT #

How does Lookups.forPath(...) relate to NamedServicesProvider? I had imagined that if I provided an instance of NamedServicesProvider it would be called when Lookups.forPath(...) but not so.

Posted by Pete on December 11, 2008 at 07:16 PM PST #

Meh the XML standard isn't my cup of tea.

Posted by MMORPG on August 07, 2009 at 01:52 PM PDT #

Post a Comment:
  • HTML Syntax: NOT allowed
About

Geertjan Wielenga (@geertjanw) is a Principal Product Manager in the Oracle Developer Tools group living & working in Amsterdam. He is a Java technology enthusiast, evangelist, trainer, speaker, and writer. He blogs here daily.

The focus of this blog is mostly on NetBeans (a development tool primarily for Java programmers), with an occasional reference to NetBeans, and sometimes diverging to topics relating to NetBeans. And then there are days when NetBeans is mentioned, just for a change.

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
12
13
14
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today