writing JAXB extension plugins

Thursday, November 01, 2012

JAXB extension plugins (1) give the user more power than simple binding alterations, plus can encourage clean schemas by keeping the code extensions out of the xsd's. But finding up-to-date documentation on this can be a pain, most predates maven.

In hindsight, the process is actually quite simple and there are some good references, but it still takes collation of quite a few sites to make it work. Here I hope to capture them all for a cohesive approach.

Note that 'plugin' is a ubiquitous term and here can mean many things. For disambiguation I've added a postscript number to each use of the term. See footnotes at the bottom for meanings.

JAXB runtime

This post assumes you're using maven. And if so, the JAXB plugin (2) to use is maven-jaxb2-plugin. Add the latest version to your pom http://mvnrepository.com/artifact/org.jvnet.jaxb2.maven2/maven-jaxb2-plugin. Then get your schema generation working OK as documented plenty of places elsewhere on the web. If you're looking at this page then you're probably well past that step anyway.

Plugin project skeleton

Create a new maven project to hold your plugin (1). For this example we'll call it my-jaxb-plugin.

In the pom add a dependency on jaxb xjc, eg:
<dependency>
<groupid>com.sun.xml.bind</groupid>
<artifactid>jaxb-xjc</artifactid>
<version>2.2.6</version>
</dependency>

Create a new class, call it MyPlugin1 in package com.eg, extending from com.sun.tools.xjc.Plugin. For now just implement the required methods returning "XmyJaxb" from getOptionName(), and implement run() as System.out.println("***working!"); (or similar :).

Now create new folders META-INF/services on the classpath (eg src/main/resources). Add a text file in there called com.sun.tools.xjc.Plugin.In this file is a list of all the plugins that this project provides, one per line. So for now add your new plugin class:
com.eg.MyPlugin1

Run a maven install on the project to make it available (not needed later if you use workspace resolution with eclipse maven plugin (3))

Employ the plugin (1)

Return to the pom of your original JAXB project. For the plugin (2) definition for maven-jaxb2-plugin, add or edit a <configuration> tag. Set <extension> to true, and add an <args> section with <arg>-debug> and <arg>-XmyJaxb>. Then a nested <plugins><plugin> definition pointing to your plugin project. The result should be something like:
<build>
<plugins>
<plugin>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<configuration>
<extension>true</extension>
<args>
<arg>-debug</arg>
<arg>-XmyJaxb</arg>
</args>
<plugins>
<plugin>
<groupId>nz.govt.police</groupId>
<artifactId>esbschema-jaxb-plugin</artifactId>
<version>0.0.1-SNAPSHOT</version>
</plugin>
</plugins>
</configuration>
</plugin>
</plugins>
</build>
Maven build your JAXB project and your should see in the console output your ***working! comment. Congrats
The -debug arg can be removed later as needed. You may also find it useful to use -X after your mvn install command to get verbose output from the maven build.

More

For taking your plugin (1) further, read the key post from 2005, resources incl javadoc for jaxb-xjc, and the sun codemodel javadocs which you'll be using to modify the code generation.

Footnotes

(1) A JAXB plugin - the reason you're reading this!
(2) A plugin to the maven build tool
(3) A plugin to the eclipse IDE

mongodb script types

Monday, April 23, 2012

There's the obvious choice of client drivers in your language of choice for application-based mongodb connections, but for server-side admin and scripting, js is best. But from the shell and cron, often they get wrapped in shell scripts. This table-esque post outlines some of the decision points to use when approaching this, plus some guidance as to how to write them.

Interactive shell

Purpose: ad-hoc queries
Syntax:mongo
About:
The most-used type of query. Not suited to automated tasks. Commands are processed immediately and results returned to the console session. Docs

--eval

Purpose: in-line queries from script or cron
Syntax: mongo dbname --eval "db.coll..."
About:
Great for one-liners, eg cron tasks
  • see printjson

js file

Purpose: call javascript from file because it's long or common to different purposes
Syntax: mongo dbname filename.js
About:
Include all needed code in js file. Usually not suitable when output needed. Best for maintenance tasks longer than one line.

eval + js file

Purpose: call javascript from file when common to different purposes
Syntax: mongo dbname --eval "param1=val1;param2=val2" filename.js
About:
This will execute the mongodb js in filename.js, but will have available to it any values as declared in the eval string. Can be called from a shell script which can modify the eval values and capture / format output

All within one sh script

Purpose: singular, self-contained script, not intended for multi-purposes. Eg monitoring
Syntax:
mongo _dbname_ \-\-eval "param1=val1;param2=val2" <<eof
js here
js here
eof
About:
Allows running map/reduce commands, as it is treated as a standalone js file by the use of HERE Documents. Can also capture the output and format it.
Gotcha Any shell keywords or characters will need to be escaped in the HERE Document otherwise they'll be interpreted and the result passed. Eg "$gt" (mongodb greater-than) otherwise "$gt" will be evaluated and null passsed if the variable isn't set.
map/reduce:
  • Cannot be run inside an --eval
  • From mapreduce docs { inline : 1} - With this option, no collection will be created, and the whole map-reduce operation will happen in RAM. Also, the results of the map-reduce will be returned within the result object. Note that this option is possible only when the result set fits within the 16MB limit of a single document. In v2.0, this is your only available option on a replica set secondary.

  • Queries (such as count(), sort()) cannot be run on inline results (as of mongodb 2.1+ new aggregation features may allow this). For versions <2.1, seeing as cannot run these queries on inline results, and can't have temp collections on secondary servers, have to run these on primary server so can save to a temp collection and run queries against that.

TurnKey Linux

Saturday, February 18, 2012

TurnKey is yet another Linux distro, but it's come a long way since I last checked it out. Initially providing images for LAMP and 2x CMS stacks only, it now boasts nearly 50 different appliances, with an impending launch of up to 150. There are now commercial options for managed cloud deployments as well. Nice offerring


Check out this Sourceforge podcast for the details