JXSE 2.5 : What's Cool #2 -- META-INF/services
By mduigou on Sep 06, 2007
JXSE (JXTA for Java SE/EE 5.0) 2.5 contains quite a number of exciting changes for JXTA application developers. This little series will look at a few of the important changes in the upcoming release. One thing that's cool about working on an open source project is that every so often a community member you've never heard of will speak up and make a brilliant suggestion that even after years of devoted work on the project everyone else somehow missed. Exactly this happened last fall a few weeks before JXSE 2.4.1 went final and unfortunately too late for it to be included in JXSE 2.4.1 but it is now available in JXSE 2.5. The old jxta.org bugzilla was not preserved when we transfered the JXSE project to Java.net, so I have lost track of the name of the user who suggested the feature. If you're willing to step forward and identify yourself I'd be very happy to acknowledge your contribution. The cool feature which is the subject of this blog is the JAR Service Provider, otherwise known as the META-INF/services system. The META-INF/services system allows for the dynamic registration of instance classes for a particular interface. We are now using it for several of the JXSE class factories and the JXSE Class Loader. To register classes you list the class names implementing an interface in a file named for the interface in the META-INF/services folder of your JAR file. For example, to register new Advertisement types you include a file "net.jxta.document.Advertisement" in the META-INF/services folder of your JAR containing the full names of your Advertisement classes. During startup the JXSE AdvertisementFactory will read class names from that file and register classes. The process is repeated for each net.jxta.document.Advertisement file in all of the other JARs. Use of the META-INF/services system makes it much, much easier to extend JXSE by adding your own components. Adding support for your advertisement type to JXSE now requires only including the class names in the net.jxta.document.Advertisement file of your JAR file. You can also register StructuredDocument types, ID formats, WireMessageFormats and JXTA Services and Modules. We have converted JXSE to use the META-INF/services system for all of the core modules.