GlassFish REST Client Goes to the Flying Circus
Thursday, October 06, 2011 |It happened a bit more quickly than I had planned, and, yes, I know that’s a pretty bad Python joke, but, as promised, I just committed code to add support for generating Python REST clients to the GlassFish RESTful Administration interface. Let’s take a quick look at it.
One easy egg to crack!
Generating the python client looks strangely similar to how it’s done for java:
1
asadmin generate-rest-client --languages python --outputdir tmp
After that is complete, you’ll have a Python egg in tmp/
that you can then install into your Python environment:
1
sudo easy_install tmp/glassfish-rest-client-stubs.zip
If you want both the Java and Python stubs, the command line would look like this:
1
asadmin generate-rest-client --languages java,python --outputdir tmp
Great! How do I use it?
To show how similar the clients are, I’m going to implement here, in Python, the same examples I gave in my last post. First up, then, is creating a JDBC connection pool:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
from glassfish.rest import *
restClient = RestClient()
domain = restClient.getDomain()
resources = domain.getResources();
cp = resources.getJdbcConnectionPool("TestPool")
if cp:
# The get method will return null if the requested resource does not exist
cp.delete()
rr = resources.createJdbcConnectionPool("TestPool", {
"restype": "javax.sql.XADataSource",
"datasourceclassname": "org.apache.derby.jdbc.ClientDataSource",
"property": "portNumber=1527:password=APP:user=APP:serverName=localhost:databaseName=sun-appserv-samples"
})
print "Success!" if rr.isSuccess() else ("Failure: " + rr.getMessage())
Easy! Now let’s deploy an application:
1
2
3
4
5
from glassfish.rest import *
restClient = RestClient()
domain = restClient.getDomain()
rr = domain.getApplications().deploy(open("test.war"), {'force':True})
print "Success!" if rr.isSuccess() else ("Failure: " + rr.getMessage())
Undeploying is just as easy:
1
2
3
app = domain.getApplications().getApplication("test")
if app:
app.delete();
Final Word
In theory, the Python API should look just like the Java API. The goal is to provide, as nearly as possible, identical experiences with the client API, regardless of the target language. That certainly opens the door to complaints that this code isn’t as "pythonic" as it should (which may or may not be the case. I’m not good enough with Python to tell you one way or the other ; ). It also means that any API warts the Java version has will be present in the other languages. The goal, then, is to fix whatever issues the Java version may have and let that trickle down to the other language(s). That means that if you have any issues, now’s the time to speak. Good or bad, I’d love to hear your thoughts.