Fork me on GitHub

Repository Construction

Oak comes with a simple and flexible mechanism for constructing content repositories for use in embedded deployments and test cases. This article describes this mechanism. Deployments in managed environments like OSGi should use the native construction/configuration mechanism of the environment.

First, we construct a Repository instance. Both the Oak and the Jcr classes support with() methods, so you can easily extend the repository with custom functionality if you like. To construct an in-memory repository, use:

    Repository repo = new Jcr(new Oak()).createRepository();

To use a tar file based Segment NodeStore backend, use:

    FileStore fs = FileStoreBuilder.fileStoreBuilder(new File("repository")).build();
    SegmentNodeStore ns = SegmentNodeStoreBuilders.builder(fs).build();
    Repository repo = new Jcr(new Oak(ns)).createRepository();

To use a MongoDB backend, use:

    DB db = new MongoClient("", 27017).getDB("test2");
    DocumentNodeStore ns = new DocumentMK.Builder().
    Repository repo = new Jcr(new Oak(ns)).createRepository();

To login to the repository and do some work (using the default username/password combination), use:

    Session session = repo.login(
            new SimpleCredentials("admin", "admin".toCharArray()));
    Node root = session.getRootNode();
    if (root.hasNode("hello")) {
        Node hello = root.getNode("hello");
        long count = hello.getProperty("count").getLong();
        hello.setProperty("count", count + 1);
        System.out.println("found the hello node, count = " + count);
    } else {
        System.out.println("creating the hello node");
        root.addNode("hello").setProperty("count", 1);

To logout and close the backend store, use:

    // depending on NodeStore implementation either:
    // close FileStore
    // or close DocumentNodeStore