Getting started with Java Content Repository API

With the growth in industry the need for the managing the different types of contents has been increased.A content can be a piece of text to a video file or some executable files. Starting from the first hour of office , most of the IT users uses a lot of different types content repositories. Most common content repositories that we use are filesystem information maintained by OS, code repositories like Subversion and websites like wiki. These repositories maintains the content in a hierarchial manner.

The Content Repository specifications for Java Technology was developed under the Java Community Process as JSR-170. The API is called as Java Content Repository API. These provides a common interface for the developers to develop different content centric applications in a vendor-neutral manner.But still there are some venodr specific features like connecting and shutting down the repositories.

There are many other advantages other than standard API. On the backend JCR can work with any database. And its easy to move contents from one repository to other using export / import feature of the JCR. Here we will see different steps in working with content repositories using jackrabbit API. Its an implementation of JSR-170 specifcations by Apache.

We will go through each step from creating the repository , maintaining it and shutting it down. Some of the steps like configuring , creating and starting the repository ,shuttingdown are still
implementation specific.

Configure repository : In the configuration file we need to mention different properties like
backend database details, home folder of repository. The default configuration file can be created using TransientRepository class.

Start or create repository :

Once we have the configuration file has been created we can create repository by using following code

TransientRepository repository = new TransientRepository(String config, String home);

It Creates a transient repository proxy that will use the given repository configuration file and home directory paths to initialize the underlying repository instances. If use the empty constructor it will create a default config file and repository which is based on file system persistence manager.

Log in into repository :

To access repository we need authorize first. The default configuration allows anonymous access to repositories. We can change the configuration to have authorization details.

Session session = repository.login();

We can use other overloaded method login(Credentials credentials,String workspaceName) to specify the workspace and login credentials. With this session object we can do different operations on repository. The repository can be visulaized like a tree. So we can start with root node of the tree to do different operations. Root node can be obtained using session object.

Node root = session.getRootNode();

Here Node is a generic type representing different nodes in the tree. We can create custom node types by defining them in a configuration file first and then registering them with repository (It was not discussed here).

Accessing nodes :

There are three ways to access a node
Direct access : Give the path to node starting from the node object we are using. Here we can use the UUID ( which will be assigned by repository for each node at the time of creation) to access it.

Node node = root.getNode("hello/world");


Node node = root.getNode(UUID);

Traversal from another node : We can get the children of node. So starting from root node we can traverse the tree using some tree traversal algorithm to find the node.

From the result of a query : We can query JCR repositories using SQL or XPath.First we need to build a query and we can specify whether it is a XPath query or SQL query. Following are the steps to query the repository.

WorkSpace ws = session.getWorkspace();
QueryManager qm = ws.getQueryManager();
String xPathQuery = "//*[@name='Eelco']";
Query query = qm.createQuery(xPathQuery,Query.XPATH);
QueryResult result = query.execute();
NodeIterator iterator = result.getNodes();

This querying is almost similar to JDBC. We can use the node objects to retrieve the data needed.

Work with content or data ( add / delete / update)
Once we have the node object its easy to play with them. We can use the different methods provided by Node class to add , delete and update data.

Node child = parent.addNode("newChild"); //adding node
child.setProperty(propertyName, propertyValue); //creating property

There are different basic property types we can use String , Binary ,Date , Long ,Double , Reference ,Path

child.setProperty(propertyName,newValue) // updates the property
child.remove() // removes the node along with its children // committs the changes to repository

Shut down repository :
Once we are done with using repository it needs to be shutdown properly. It can be done using


Using above steps you can build your first java application which can maintain hierarchical data effectively.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s