Tuesday, May 15, 2012

Java 8: extensible module definitions with JSON

While the Java Modularity JSR [1] is still AWOL (come on, Oracle, I thought that this JSR was planned to be launched by September 2011!), I did some work in OpenJDK Penrose around changing the Java SE 8 module metadata from the .java/.class format into JSON.

JSON is quite nice because
with JSON, the SE 8 Quick Start module org.astro module definition looks like this:

{
  "module" :
  {
    "name" : "org.astro",
    "version" : "1.2",
    "exports" : [ {"name" : "org.astro"} ]
  }
}

and the same module info file can be expanded to contain OSGi metadata, for example to add a version to the exported package like this:

{
  "module" :
  {
    "name" : "org.astro",
    "version" : "1.2",
    "exports" : 
    [
      { 
        "name" : "org.astro", 
        "org.osgi.version" : "1.2.3"
      }
    ]
  }
}

Any OSGi metadata can be added to the JSON module descriptor. I've added a simple test to the Penrose codebase to give an idea what this looks like: hello-json-custom.sh.

This works today in OpenJDK project Penrose. The codebase still supports module-info.java, to allow a gradual migration to JSON-based metadata, however I'd like to see the module-info.java/.class support go away ultimately.

While Jigsaw/Penrose are two variants on a module system for Java SE, other ones are clearly possible. Once the modularity JSR is up in action, it should seriously consider OSGi as a module system for Java SE 8. Apache Harmony has clearly demonstrated in the past that modularizing the Java platform using OSGi metadata works quite well. OSGi has proven to be an excellent module system for Java for the last 12 or so years, so it would be foolish not to take that experience on board...

On the lack of Modularity JSR, I think at that at this stage we can expect that it will delay the release of Java 8, given that modularity is such an important building block for Java, and is certainly not something that can be rushed through.

[1] 'Java Platform Module System' JSR as mentioned in the Java SE 8 umbrella JSR.

3 comments:

  1. I noticed that the project pages from OpenJDK project Penrose are a bit empty at the moment. If you're interested in playing around with Penrose, the Mercurial forest can be obtained from here: http://hg.openjdk.java.net/penrose/jigsaw/

    (In the mean time, I hope that the Penrose pages will get some proper content soon)

    ReplyDelete
  2. Thanks for the interesting work.
    Just let me ask you one thing ;-)

    Is it possible, you are a bit in love with braces and quotation marks...
    I'd prefer simple notation along the lines of
    module=org.astro:1.2
    anytime over indented open/closed braces etc.

    Anyway, keep it up, David.

    Cheers.
    Jörg

    ReplyDelete
  3. Hi Jörg, however then it would not be JSON any more. See http://json.org/example.html. I did propose XML as an initial suggestion, but the general consensus seemed to be more in favour of JSON.

    I'd like to stick with a well-known format because it means that parsers are readily available. Otherwise everyone who wants to read this data has to write their own parser...

    ReplyDelete