META-INF/services vs. layer.xml registration
By Geertjan-Oracle on Sep 04, 2008
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.