JCR API is a well known API among content repository vendors. Content Repository API for Java (JCR) is a specification for a Java platform API for accessing content repositories in an uniform manner and it defines an abstract model and a Java API for data storage and related services commonly used by content-oriented applications. WSO2 Governance Registry JCR API is built on top of the repository side of Governance Registry.
A JCR repository is composed of one or more persistent workspaces, each consisting of a directed acyclic graph of items where the edges represent the parent-child relation. Here a JCR Item is either a Node or a Property.
A Node can have zero or more child items. A property cannot have child items but can hold zero or more values.
So in other words a workspace is a tree of Nodes and Properties. The nodes of a workspace form the structure of the stored data while the actual content is stored in the values of the properties.
In JCR, you can store many types of data such as String, javax.jcr.Value, Boolean, Long, Double, Integer, InputStream, etc. into node properties.
For a sample on JCR, see JCR Sample.
Usages of JCR API
This page provides some samples which illustrate how to use WSO2 Governance Registry through JCR layer. Before directly going in to samples of using the JCR API, let's first take a look at how to make the connection with a remote Registry instance and access the JCR Registry Repository.
A user can simply login to a repository by passing some credentials. Then the repository returns a JCR session instance with a persistent workspace.
First of all, you have to set up a client to access and use the JCR API. In the Governance Registry distribution, you will find a sample
The following steps will illustrate how to set up and run your client.
1. Before getting the sample to work, you have to run
GREG_HOME/bin. This copies all of the necessary libraries to their respective locations
2. Start the WSO2 Governance Registry by running the respective script (
Steps for running this sample
1. Execute the ant command (
ant run) from the sample directory.
2. Run the
SampleJCRRegistryClient.java which contains a main method and login to the Registry and see how we can access registry through JCR layer.
3. Login to the repository and get a session instance.
For all the following samples, we use the session instance which was acquired when during repository login as shown before.
Sample 1 - Basic JCR operations, adding and removing nodes
This sample shows how to add a node and remove any item (a node or a property) by using the session instance (acquired after repository login as shown above).
Sample 2 - How to register a node type in JCR
This sample shows how we can register a node type in JCR. Each node in a workspace tree has a node type that defines the child nodes and properties it may (or must) have. There are two categories of node types, primary and mixin. Every node has a primary node type assigned to it upon creation (
Node.addNode in the JCR API).
In addition, a mixin node type may be added to a node later in its lifecycle (
Node.addMixin). The following code block shows how we can simply register our own custom JCR node type and use it later (for more information please refer to JCR API description or the spec).
Sample 3 - Adding various types of properties to nodes and getting those nodes
Here, in this sample, we can see how to save our various types of resources inside JCR properties.
You can save many types of data inside properties such as
Also you can directly acquire those types of properties by giving the exact absolute path (if you access it through a session), or you can simply ask the property by name directly from a particular node.
Sample 4 - Using ValueFactory to add several values to a property at once
This sample shows how to use
ValueFactory when you have to store a set of values which may bound to a particular context. In such a case, you can set all those values under unique property name. Also, JCR provides the facility to iterate all the values directly at once.
Sample 5 - Using JCR versioning concept
In JCR, versioning is only valid for the nodes with the type
ix:simpleVersionable. Here, we still support simple visioning in JCR.
Sample 6 - Using JCR QOM (Query Object Model) for querying repository content
This shows a simple way of querying repository content in JCR. Here, other than typical SQL kind of querying, JCR also provides a Query Object Model (QOM) to deal with repository content. This is a very simple, user friendly and less complex way of dealing with our repository as this is an object model. So everything related to querying is handled by a
QueryManager which can be acquired through the workspace.