Thursday Sep 04, 2008

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.

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
« September 2008 »
SunMonTueWedThuFriSat
 
3
6
9
13
14
23
30
    
       
Today