The Source for Java Technology Collaboration
User: Password:



Ethan Nicholas

Ethan Nicholas's Blog

Creating a Service Provider Interface

Posted by enicholas on April 07, 2006 at 09:04 AM | Comments (7)

While working on my XML user interface language, JAXX, I wanted to provide a mechanism for users to add support for additional tags and data types. As JAXX is a command-line compiler, a run-time API to add additional features would be very awkward to access, so a Service Provider Interface (SPI) seemed like a natural fit.

In case you aren't familiar with SPIs, they have a tantalizing proposition: just put a special JAR file somewhere on your class path, and the features in the JAR file will be found and made available. Many of Java's internal APIs, such as JNDI and javax.imageio, have SPIs, which makes it a snap to plug in new features.

So, great, I needed to write an SPI. I spent some time wrestling with search engines to find documentation on how to actually create an SPI, and found nothing. Tons of information about how to interact with the SPIs built into Java, but absolutely nothing about how to create one. There was nothing left to do but dig through Java's source code to figure out what Sun was doing, so I could shamelessly copy it.

The answer turned out to be embarrassingly simple. The method ClassLoader.getResources() returns an Enumeration of all resources with a given name, like myspi.properties. To implement an SPI, you have each SPI JAR contain (say) a properties file with the same name in the same location. When you use ClassLoader.getResources() to get a list of all such properties files, you will find one properties file for each JAR on the class path which implements your SPI.

The properties file should contain whatever information is necessary for you to make use of the features in the JAR file. This typically takes the form of properties set to the names of classes found in the JAR file, which can then be loaded and used.

In the case of JAXX, the properties file is named jaxx.properties and is located at the root of the JAR file. It contains a single property jaxx.initializer, which should be set to the name of a class in the JAR file. This class must implement the interface jaxx.spi.Initializer. JAXX can then, via the magic of ClassLoader.getResources(), easily find all such initializer classes and load the support they contain.

Presto! A fully-functioning SPI, just like the ones Sun uses.


Bookmark blog post: del.icio.us del.icio.us Digg Digg DZone DZone Furl Furl Reddit Reddit
Comments
Comments are listed in date ascending order (oldest first) | Post Comment

  • Much like what laf-plugin does for look-and-feels. Looks up LAF-specific XML descriptors in the classpath, which allows providing jars for third-party components by simple dropping them anywhere on the classpath. The only drawback i found with this approach - since the resource name is hardcoded, you can't put more than one descriptor in the same jar.

    Posted by: kirillcool on April 07, 2006 at 10:20 AM

  • The conventional approach is to have providers JARs include files named, in your case,

    META-INF/services/jaxx.spi.Initializer

    which can include impl class names, one per line (usually just one per JAR). This convention is used in various places in the JDK, documented here:

    http://java.sun.com/j2se/1.5.0/docs/guide/jar/jar.html#Service%20Provider

    As of Mustang you can access provider impls following this format quite easily, without manually writing the retrieval code:

    http://download.java.net/jdk6/docs/api/java/util/Service.html

    Posted by: jglick on April 07, 2006 at 05:14 PM

  • Aha! Thank you. Evidently the JDK uses at least two different patterns for SPIs, and I stumbled upon the older of the two. Now to see which version of Java introduced the new interface, and therefore whether I can actually use it...

    Posted by: enicholas on April 07, 2006 at 06:08 PM

  • I don't like this approach. It boils down to gambling which implementation you'll get. Typical symptoms: "seems like you have a xercesImpl.jar somewhere in your classpath." or "try listing xx.jar first".

    Posted by: mernst on April 08, 2006 at 01:45 AM

  • See also the Java Module System, which looks like it will obsolete the approach above.

    Posted by: coxcu on April 10, 2006 at 02:35 PM

  • Isn't this exactly how the OGSi framework discovers services described in the manifests?

    Posted by: phonetech on April 11, 2006 at 09:11 AM

  • Greetings to all.


    aaliyah cds
    adriana lima
    adriana sklenarikova
    adrianne curry
    ainett stephens
    aishwarya rai
    alanis morissette
    alessandra ambrosio
    alexandra paul
    alexis bledel
    ali landry
    alicia key
    alicia silverstone
    alarm beyonce lyric ring
    angel locsin
    billy gilman
    bob esponja
    bobs furniture
    bounty dog hunter
    bridge fergie london lyric
    brinkley christie
    bronx zoo
    brookfield zoo
    gina lee nolin
    giorgia palmas .htm
    gretchen mol
    grubman lizzie
    guelcan karahanci
    harring laura naomi
    heidi klum
    heidi muller
    holly brisley
    holly mcguire
    holly valance
    ines rivero
    ines sastre
    alizee
    alley baggett
    alyssa milano
    amber tamblyn
    amy smart
    ana beatriz barros
    anastacia
    anastasia myskina
    andrea corr
    angelica bridges
    angie everhart
    anna faris
    anna kournikova
    anna paquin
    anne hathaway
    ashanti
    ashlee simpson
    ashley judd
    ashley olsen
    ashley scott
    asia argento
    atomic kitten
    bai ling
    basinger kim
    irina voronina
    isabeli fontana
    isabella hervey
    izabella miko
    izabella scorupco
    jacki o
    jaime pressly
    jakki degg
    jamelia
    jamie lynn sigler
    janelle perzina
    jeanette biedermann
    jelena dokic
    jenna elfman
    jennifer esposito
    jeri ryan
    jolene blalock
    josie maran
    julie benz
    kaley cuoco
    kari wuhrer
    karolina kurkova
    kasem kerri
    kate bosworth
    katherine heigl
    beverly mitchell
    beyonce knowles
    billie piper
    bjork
    blu cantrell
    bourret caprice
    brande roderick
    brandy norwood
    bridget moynahan
    brigitte bardot
    brittany murphy
    calista flockhart
    carla bruni
    carla gugino
    carly pope
    carmella decesare
    carolina ardohain
    carrie westcott
    cat deeley
    chabert lacey
    charisma carpenter
    cheryl tweedy
    chris daughtry
    christine lakin
    katie lohmann
    kelly rowland
    kerry katona
    kristy swanson
    leila arcieri
    leonor varela
    leslie bibb
    linda evangelista
    lisa kudrow
    lisa snowdon
    lucy lawless
    lucy pinder
    lucy woodward
    marlene dietrich
    mathilda may
    matt dillon
    matt leblanc
    matthew mcconaughey
    michael bolton
    michael douglas
    michael keaton
    mike myers
    miner rachel
    mylene farmer
    nastassja kinski
    nick nolte
    nicolas cage
    nicole kidman
    noemie lenoir
    olivia newton john
    paris latsis
    patsy kensit
    paul newman
    penelope cruz
    pete doherty
    richard gere
    robert de niro
    claire forlani
    claudia schiffer
    daisy fuentes
    daniela hantuchova
    daniela pestova
    dannii minogue
    dannii minogue
    daryl hannah
    davis geena
    debra messing
    destinys child
    devon aoki
    diane kruger
    dierks bentley
    elisabeth harnois
    elisabetta canalis
    elle macpherson
    elsa benitez
    emilie de ravin
    emily mortimer
    emma bunton
    emmanuelle chriqui
    emmanuelle seigner
    emmy rossum
    erica durance
    estella warren
    eva padberg
    famous daves
    rupert everett
    sandrine kiberlain
    santa barbara hotel
    sarah michelle gellar
    sean connery
    shannen doherty
    sheryl crow
    shirley maclaine
    sophie marceau
    stavros niarchos
    steffi graf
    steve mcqueen
    tea leoni
    tina arena
    tobey maguire
    tommy mottola
    tony danza
    ursula andress
    val kilmer
    vin diesel
    vivien leigh
    whoopi goldberg
    federica fontana
    foxy brown
    fran drescher
    francesca lodo
    frederique van der wal
    gabriela sabatini
    gabrielle union
    garth jennie
    gemma ward
    gena lee nolin
    kiele sanchez
    kim cattrall
    kirsty gallagher
    kristanna loken
    kristy hinze
    ronnie coleman
    snow patrol lyric
    tony matterhorn
    weider

    Today fine day.

    Posted by: skwer on November 03, 2007 at 04:27 PM



Only logged in users may post comments. Login Here.


Powered by
Movable Type 3.01D
 Feed java.net RSS Feeds