openwfe-jcr beancoder

A 'beancoder' is a piece of code that encodes a plain Java bean into something else.

OpenWFE uses beancoders to save its state or to serialize information for transmission.

Those OpenWFE beancoders are meant for simple Java beans (or trees of them), along (or composed with) Maps, Lists and Arrays.

The JcrBeanCoder takes as input a Java bean and stores it in a JCR.

code sample

package acme.com;

/**
 * our NadaObject bean.
 */
public class NadaObject {
    private String nada = null;
    private Color color = null;

    public NadaObject() {
        super();
    }

    public NadaObject(String s, Color c) {
        super();
        this.nada = s;
        this.color = c;
    }

    public void setNada(String s) { this.nada = s; }
    public void setColor(Color c) { this.color = c; }

    public String getNada() { return this.nada; }
    public Color getColor() { return this.color; }
}

public class Color {
    private int code = -1;
    private boolean inverted = false;

    public Color () {
        super();
    }

    public Color (final int i, final boolean inverted) {
        super();

        this.code = i;
        this.inverted = inverted;
    }

    public void setCode (final int i) { this.code = i; }
    public void setInverted (final boolean b) { this.inverted = b; }

    public int getCode () { return this.code; }
    public boolean isInverted () { return this.inverted; }
}
import openwfe.org.jcr.beancoder.JcrBeanCoder;

    ...

    javax.jcr.Node targetNode = whateverNode;
    String beanNodeName = "myBean";

    JcrBeanCoder.encode(targetNode, new NadaObject("some text", new Color(-69, true)));

    targetNode.save();

    ...

will produce such a structure in its JCR repository :

n: 'root'
  p: 'openwfe.org.jcr.beancoder.JcrBeanCoder__instance_class' -> 'acme.com.NadaObject'
  p: 'nada' -> 'some text'
  n: 'color'
    p: 'openwfe.org.jcr.beancoder.JcrBeanCoder__instance_class' -> 'acme.com.Color'
    p: 'code' -> '-69'
    p: 'inverted' -> 'true'

The decoding code would look like :

import openwfe.org.jcr.beancoder.JcrBeanCoder;

    ...

    javax.jcr.Node beanNode = myWhateverNode;

    NadaObject no = (NadaObject)JcrBeanCoder.decode(beanNode);

get the jars

You can download the jars directly from :

http://maven.openwfe.org/preleases - openwfe-applic and openwfe-jcr

get the jars (with Maven)

point your pom.xml to OpenWFE's maven repository :

        <repository>
            <id>org.openwfe.prereleases</id>
            <url>http://maven.openwfe.org/prereleases</url>
        </repository>

and include this dependency :

        <dependency>
            <groupId>org.openwfe</groupId>
            <artifactId>openwfe-jcr</artifactId>
            <version>1.7.2pre14</version>
        </dependency>

For more precisions about OpenWFE's maven repositories : 'doing it almost the maven way'

final notes

Circular dependencies are not (yet) supported by this beancoder implementation.

If you feel like you need it, feel free to drop a mail on our development mailing list.

This beancoder is also meant not to produce too developped JCR trees as it's currently in use within Magnolia 3.0 to store workflow workitems and some configuration information, browsing too deep trees would have been painful.

This code is based on the original OpenWFE XmlBeanCoder, in charge of turning beans into XML documents. It was fairly easy to split and adapt that artefact to make it handle JCR repositories. Persistence targets other than XML and JCRs could also possible.