JSON to Java with JDK6

December 26th, 2011

There are multiple frameworks for transforming a JSON expression to a Java object tree, but the Java Platform does not come out of the box with some javax.json package (I heard there are plans for that).

Today I will show that you can easily transform a JSON expression to a simple Java object tree (map, arrays, primitive wrappers and string) with Java 6 and no additional framework.

Obviously, it may not be the best way to proceed, specially when it comes to speed, security or java mapping. However this solution is appropriate if those are not a concern and you don’t want to add a dependency to an external library for parsing JSON expressions.

The solution relies on the javax.script package and its Javascript binding. Indeed what’s best than Javascript to parse JavaScript Object Notation ?

The scripting API provides evaluation of any Javascript which is nice, unfortunately native Javascript objects are not usable from Java, but the magic of Javascript will come to the rescue!

Javascript is a dynamic language which allows to modify its meta object protocol (MOP) to add new functions on native Javascript objects, through the concept of prototypes. We can modify each of the base Javascript type’s prototypes to add a toJava function that will return a Java version of the object:

Now you probably get the idea, each toJava function returns the Java counterpart of the Javascript object. Initially I wanted to iterate the object tree, but extending the types thanks to the prototype is a much better solution.

We also need to add the missing Java part that loads this JS and uses it to create our Java objects. This is trivial to do thanks to the scripting API, here is how to load the Javascript and setup the engine:

Now we need to use the engine to convert JSON expressions:

Here there is a little trick: you can remark the usage of AtomicReference and it may look inappropriate since we are not dealing with concurrent programming. The trick is that if we return a String from the engine to Java, the engine will wrap it as a Javascript string and we want to avoid that. The AtomicReference makes the value opaque to Javascript and the Java code will unwrap it.

Those three pieces together makes a lightweight solution to transform JSON expressions to Java objects. There are certainly corner case not handled in this code, but it’s a solid basis to start with, I saved the code in a GitHub gist there.

Juzu 0.4.2

December 12th, 2011

We have been working hard to get this release done with a great set of new features!!!

First and foremost, I’m very proud of the level support we have now reached for Eclipse. As you may know, Juzu relies much on Annotation Processing Tools (APT) since the beginning to bring exclusive features. Making Juzu work with Eclipse incremental compiler was very challenging because APT simply works differently. Among those features, the type safe template parameters with Eclipse is showed in this screencast

The dev mode feature has been improved a lot, specially now the error reporting is more accurate and sexy, this screencast will teach you everything about switching an application to dev mode in Liferay

Juzu comes as a package to download on this page. Alternatively you can also use the maven archetype to bootstrap an application

Finally, the Dependency Injection has been improved in order to allow the injection of external providers, in particular this is useful if you are using Juzu in the GateIn server and you want to inject GateIn services in a Juzu application, this is now trivial to do and is explained in the wiki.

For the future we plan to develop new features and improve step by step the project, on top of my mind the integration with Bean Validation would be a good thing to do. If you are interested to follow the Juzu project development you can join our Juzu group.

Juzu Web Framework

October 31st, 2011

Juzu is an open source web framework I started to work on recently, for several reasons, the most important one is that there is no decent framework for developing portlet application in a simple and productive manner. There are other good and valid reasons.

Juzu deploys on the GateIn Portal and on the Liferay Portal. Perhaps it also deploys on other portals, there aren’t any good reason why Juzu would not work on other portal (except bugs).

Juzu integrates with dependency injection frameworks such as Weld (CDI) and Spring, theoretically it should work with any injection framework providing a good support of the JSR-330 specification (however it requires some custom integration work, as JSR-330 is really lightweight…)

Juzu is a work in progress, however it is advanced enough to deliver the infamous Booking application.

If you are interested in Juzu (using or helping), you can start to read this page.

CRaSH gets an installer!

August 16th, 2011

CRaSH 1.0.0-beta22 release features an IZPack installer that will let you configure a CRaSH runtime throughout a serie of screens, here is a short video showing an installation:

You can download them from the usual page.

GateIn 3.2 M1

June 27th, 2011

We released last week the first milestone of GateIn 3.2, it was a long time since the 3.1 release and both Red Hat and eXo teams were quite busy working on the product and project side of GateIn.

Among the noticeable changes, you will find now a support for Jetty 6.1/Tomcat 7/JBoss AS 6 web containers, a improved Maven build compatible with Maven 3, a few UI improvements and many bug fixes. You can read more about it there.

I’m proud now to work with Bolek that will replace Thomas Heute as GateIn technical lead. Bolek was involved in the effort since the JBoss Portal projects and earned his credits by providing an Hibernate implementation of the now dead Slide project (JCR ancestor) and implementing much of the Portlet 1 TCK assertions in our testsuite (that proved to be invaluable when JSR 286 was implemented). He blogged about his new role in the project here.

If you are a fan of GateIn, the CRaSH shell is a must have companion for GateIn. CRaSH is a shell that provides a JCR access and allows to do stuff and hack GateIn. Use it at your own risks of course ;-) . Download of the latest beta-21 is here.

Finally it’s time of a few words about GateIn stuff that is another GateIn companion I started a few months ago on GitHub. It’s something more informal than a real project, it’s like a bunch of stuff that could be useful if you’re a GateIn user, at least it’s useful to me, it contains so far

  • a CRaSH folder with command for adding users to GateIn
  • gen : an XML generator for generating large navigations
  • sqlman : a tool based on the great JBoss Byteman project that measures the access to resources, very valuable for monitoring database activity. Note that this tool is generic and does not depend on GateIn itself. We could extract it and find a better name (containing “spy”)

Yet another CRaSH beta (21)

June 8th, 2011

Nothing fancy, in it, mostly bug fixes (in particular for SCP CRASH-23 and CRASH-24).

I updated also the Google Code page to add links to GitHub where the project is now hosted and the online docs now have the PDF and the Javadoc.

Finally the project CI is now Jenkins hosted by my friends at Cloudbees. A special thanks to Arnaud for helping me setting this up.

The release is available on Google Code as usual.

CRaSH update

April 29th, 2011

A long time since I haven’t blogged and released a CRaSH version (beta15), here are the the new stuff in CRaSH since the beta15 release.

First and foremost, the beta 20 was just released, so grab it, it’s very hot!

One of the most important feature is a new set of annotations for writing commands, that provides contextual completion! I think it was one of the most awaited CRaSH feature and now it is there, allowing for instance JCR path auto completion, but it works also based on the context and is able to provide completion for various part of the command line

  • command: “sy” -> “system”
  • option names: “–fo” -> “–force”
  • enumerated values: “deb” -> “debug”
  • custom handlers, for instance JCR path: “/fo” -> “/foo”

Sub command is another neat new feature that allows to group several commands inside the same class, inspired from git:

  • log ls: list all loggers
  • log send: send a message to a logger
  • etc…

Here is an overview of how now CRaSh commands are written

New documentation system to provide usage and man for a command, further more now the command reference is included in the documentation

A few new based commands were added

  • log: hot configure or send message to the logging system
  • jdbc: an out of the box SQL console for JNDI based datasources
  • system: interact with the VM properties

Finally we relocated CRaSH code on GitHub, the project page remains for now on Google Code along with the hosted documentation and downloads. It will allow to better follow the project status and contribute.

Uploading a file on Cloudreader iPad application from shell

November 22nd, 2010

I’ve discovered the great Cloudreader application for iPad. Since I don’t have my books on the same computer than synchronize with my iPad, I’ve been using Cloudreader builtin capabilities for uploading my books. Unfortunately the web interface is rather poor and each file needs to be added separately.

So I went to write a shell script based on the curl command to perform a bulk upload to the Cloudreader server and I wanted to share it with other, here is the script:

#!/bin/bash

# usage
if [ $# -lt 2 ]
then
  echo "Usage: $0 server_adress file1 ... fileN"
  exit 1
fi

# Save server and shift
UPLOAD_SERVER=$1
shift;

# Upload each file
while (( "$#" ))
do
if [ ! -f "$1" ]
then
  echo "The file $1 does not exist"
  exit 1
fi
echo "Uploading $1 on $UPLOAD_SERVER"
curl --form datafile=@"$1" --form title= --form author= http://$UPLOAD_SERVER/post >/dev/null
shift
done

CRaSH beta gets flexible command composition

November 9th, 2010

CRaSH 1.0.0-beta15 is just fresh out of the mill and gets an important new feature inspired from shell pipes.

We have added the capability to compose commands through the pipe (|) and the distribution (+) operators (if you find a more appropriate name, don’t hesitate to tell me).

I won’t detail the whole thing but instead give a few explanations, the whole thing is described in the documentation.

Each command is able to consume and produce a stream of node and this capability is described by a Java generic like notation : <C,P> with <C> for consumed and <P> for produced. By default a command that does not specify anything is considered as <Void,Void>, i.e it does not consume and produce anything.

The pipe operator (|) allows to take the stream produced by a command and pipe it to a command that will consume it (<Void, X> | <X, Void>).

The distribution operator (+) is useful with the pipe operator and it allows to distribute a stream to two commands (<Void, X> | <X, Void> + <X, Void>) or to take the output stream of two commands and combine it in a single output stream (<Void, X> + <Void, X> | <X, Void>).

So after the nice theory, let’s see a few examples:

// delete all nt:file nodes
select * from nt:file | rm

// add the read permission to anyone on all nt:file or nt:resource nodes
select * from nt:file + select * from nt:resource | setperm -i any -a read

// change the mime type and encoding of all nt:resource nodes
select * from nt:resource | set jcr:mimeType text/html + set jcr:encoding UTF-8

In addition of this very nice feature, the war file packaging has been reworked to include less jar than before, those that were already bundled by the server runtime.

CRaSH 1.0.0-beta12 released

October 6th, 2010

We just release the 1.0.0-beta12 of CRaSH that add two new commands:

  1. The addnode command adds one or several JCR nodes
  2. The setperm command provides security configuration of JCR nodes
The rm command has also be improved and provides the capability to remove several nodes at the same time.