GlassFish Administration: The REST of the Story Part I
Of the many great things about GlassFish, one that is often mentioned most (and is, in fact, what got me involved with GlassFish as an end user years ago) is the Administration Console. It’s an extremely powerful and capable interface, and is, if I may be so bold, orders of magnitudes better than its open source competition (it may even beat commercial competitors, but I have no experience with those). Another powerful tool in GlassFish administration is the asadmin CLI utility, which allows for quick and easy scripting of server provisioning, etc. Did you know, though, that GlassFish has a third administration interface? As of GlassFish v3, we offer a RESTful administration API, based on Jersey, to allow non-Java clients to configure the app server easily. For GlassFish 3.1, one of my main responsibilities, with the help, I should add of my coworkers Ludovic Champenois and Mitesh Meswani, has been to help improve upon the great start we had in in v3. In this entry, we’ll take a look at the current state of the interface and learn the basics of using.
I don’t want to go too far into the weeds explaining what REST is, so for those of you new to the concept, you can get a high level overview at Wikipedia. Go ahead and read it. We’ll wait. Everyone back? Great! :P
The GlassFish RESTful Administration API (aka, the REST API/interface) can be found at http://localhost:4848/management/domain for configuration-related activities, and http://localhost:4848/monitoring/domain for monitoring-related activities. For this entry, we’ll not do anything with the monitoring side other than mention that it’s there. The REST API supports three different response encodings (yes, I’m aware of how dangerously overloaded that term is : ): HTML, JSON, and XML. Which type to use is determined using the Accept header, but can be overridden by using an extension on the URL (.html, .json, or .xml). Let’s take a quick look at what the return looks like.
The request payload is typically what you would see with an HTML form submission. For example, this curl example sets the port for http-listener-1, which is the listener that serves your applications:
There are places where the payload is a bit more complex of necessity (such as properties), but I’ll cover those in another post.
Early on, the structure of the document the REST interface could vary depending on how the endpoint resource was implemented (more on the later). In this cycle, we’ve done a lot of work to standardize on one format, which I’ll describe here, but be aware you may still see non-compliant response documents. If you do, it’s a bug and we’d appreciate it if you could open an issue. :)
The structure, then, is an object with the following fields: message, command, exit_code, properties, and extraProperties. An example is often best, so here’s the three representations of the endpoint http://localhost:4848/management/domain/:
As you can see from these trimmed down version, there’s quite a bit of data there. For the most part (though we still have areas we need to clean up), the data you will be most interested in as an end user will be under 'extraProperties'. This property is an object that lists the various HTTP methods the endpoint supports, giving information on parameters it supports; the entity’s state, if there is any (more on that later); any commands nested under this endpoint (more on that later as well); and any child resources this resource may have.
It’s worth noting that the documents you see above were pretty-printed by the server, a feature that is off by default. To enable this feature, the HTTP Accept header __debug must be set to true. If this header is not present, or if the value is not 'true', the server will not format the document. In this case, the unformatted JSON document is just over half the size of the pretty-printed one, resulting in much less going over the wire, an important production consideration.
Those of that have been following GlassFish for a while may remember the big deal we made about the dynamic nature of v3. Jerome Dochez stood on the stage at JavaOne and showed an EJB-less GlassFish container start up, and then refuse to deploy an EJB app, because it didn’t support them. He then added the require jars, and with the black magic help of OSGi, the server suddenly supported EJB deployments. Really slick. One of the issues we tackled on the REST side, though, was how to handle the addition and removal of these modules. Making matters more difficult, we didn’t want to require third party module developers to worry about REST when writing their add-ons. Enter asm, stage right.
Thanks to hard work of Mitesh and Ludo, the REST endpoints you see in the server are completely dynamic. The REST module itself is lazily loaded, so you don’t have to pay that penalty as the server starts, but, once it starts, it analyzes what is in memory (i.e., the HK2 DOM hierarchy) and generates and registers REST endpoints on the fly. What this means for third party developers is that as long as they’re using HK2 and domain.xml to manage their config, they get REST endpoints for free. It also means that a web profile GlassFish installation isn’t exposing useless endpoints.
This ended up being much longer than I had intended, but here’s the take away. GlassFish offers three ways to administer the server: the web-based console, the command line-based asadmin utility, and HTTP-based REST interface. Using one or more of those means you will likely never have to look at an XML file, and that ain’t bad. :)
In future posts in what I intend to be a series, we’ll take a look at specific use cases using the REST interface.