Pluggable Applets via ServiceLoaders, Groovy, and Griffon

The first Griffon issue is solved: I made the mistake of including "pack: true", which fails in applets. Now I can start playing a bit with the applet side of Griffon. So, this is the pluggable Griffon application discussed yesterday:

In what way is the application pluggable? Read yesterday's blog entry but, in summary, be aware that none of the items in the list that you see above are defined in the Griffon application. Each item is made available by a different service provider and loaded into the list via the JDK 6 ServiceLoader class. For more details, read yesterday's blog entry. The only change is that I've added a border so that later, when the application is deployed as an applet, I have something to hold on to as I drag the applet out of the browser:

import javax.swing.border.TitledBorder

import javax.swing.border.EtchedBorder

application(title:'Encoder Sales') {

    border = new TitledBorder( new EtchedBorder(), 'Available Encoders')

    panel(border:border) {

        textArea( id:'encoderList', rows:10, columns:30, editable:false )



Also note that above I removed "pack:true" from the application's properties. And here it is deployed to the web as an applet:

That's possible because when Griffon creates the applet (when you do "griffon run-app"), the value of the applet's archive element is a list of all the JARs on the application's classpath. Those JARs include the service providers (and the service itself), hence the resources defined in META-INF/services are made available to the applet. As a result, applets are pluggable too!

And, of course, one can drag the applet out of the browser (Firefox3 with JDK 6 Update 10):

By the way, did you know that you can close the browser... and the applet will still exist, running independently on your desktop?

Summary: I created the applet by first creating a Swing application in Groovy via the Griffon framework. I used the JDK 6 ServiceLoader class to plug three service providers into my application. Then I ran "griffon run-app" and, in addition to a Swing application, an applet (and a JNLP application) was created by Griffon. However, for the applet to work, I needed to remove "pack:true" from the view. When I ran the applet, I had the same result as the Swing application, i.e., the ServiceLoader loaded the service provider interface, thus letting me call the exposed method on each of the implementations registered in their JAR's META-INF/services folder. Hence, I did nothing at all to create the applet, Griffon simply generated it for me from my Groovy (in the Griffon application) and Java (in the service providers) source code. Among many other things, this example therefore shows yet another scenario where Groovy and Java work seamlessly together.


Inside of an application node pack:true wasn't your mistake, it's part of the default templates and is supposed to be benign in applets. Furthermore it is supposed to work just fine. Griffon (in 0.0.2 or 0.1) will address that issue, a patch is in SVN.

Posted by guest on September 21, 2008 at 02:47 AM PDT #

Fair enough. Still it was my mistake not to recognize that as being inapplicable to applets. Thanks for fixing it.

Posted by Geertjan on September 21, 2008 at 04:37 AM PDT #

Post a Comment:
  • HTML Syntax: NOT allowed

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.


« July 2016