EmailLinkedInGoogle+TwitterFacebook

Say hello to Jackrabbit

Source code available @
svn checkout https://whiteboardjunkie.googlecode.com/svn/trunk/jackrabbittutorial jackrabbittutorial

I believe in show and tell. So here is an introductory post that shows you how simple it is to start using a powerful content management system called Jackrabbit using a JCP standardized API called JCR API. It is setup as a maven 2 project and I will be progressively building on this project to take you through steps to build powerful software systems that uses JCR API and Jackrabbit implementation in particular to manage content. Here the Key word is content and in my definition content covers the complex world of applications that require to manage relational data that can traditionally be stored in relational databases and binary content such as video, pictures and audio.

package org.boni.jrtutorial.util;

import static org.junit.Assert.assertNotNull;

import javax.jcr.Node;
import javax.jcr.Repository;
import javax.jcr.Session;
import javax.jcr.SimpleCredentials;

import org.apache.jackrabbit.core.TransientRepository;
import org.junit.Test;
public class JackrabbitConnectionTest {
	
	
	@Test
	public void testCanConnectToJackrabbit(){
		try {
			//Creates a transient repository at the default 
			Repository repository = new TransientRepository(
										"classpath:repository.xml",
										"target/repository");
			Session aSession = repository.login(
									new SimpleCredentials(
											"username",
											"password".toCharArray()));
			Node rootNode =  aSession.getRootNode();
			assertNotNull(rootNode);
			Node aNode = rootNode.addNode("firstChildnode");
			assertNotNull(aNode);
			aNode.remove();
			aSession.save();
			aSession.logout();
			
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
	
}

JackrabbitConnectionTest.java

<project 	xmlns="http://maven.apache.org/POM/4.0.0" 
			xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
			xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 

http://maven.apache.org/maven-v4_0_0.xsd">

  <modelVersion>4.0.0</modelVersion>
  <groupId>org.boni</groupId>
  <artifactId>jackrabbittutorial</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <dependencies>
  	<dependency>
  		<groupId>javax.jcr</groupId>
  		<artifactId>jcr</artifactId>
  		<version>1.0.1</version>
  	</dependency>
  	<dependency>
  		<groupId>org.apache.jackrabbit</groupId>
  		<artifactId>jackrabbit-core</artifactId>
  		<version>1.4.5</version>
  	</dependency>
  	<dependency>
  		<groupId>junit</groupId>
  		<artifactId>junit</artifactId>
  		<version>4.4</version>
  		<scope>test</scope>
  	</dependency>
  	<dependency>
  		<groupId>org.slf4j</groupId>
  		<artifactId>slf4j-jdk14</artifactId>
  		<version>1.5.2</version>
  		<scope>provided</scope>
  	</dependency>
  	<dependency>
  		<groupId>log4j</groupId>
  		<artifactId>log4j</artifactId>
  		<version>1.2.9</version>
  		<scope>provided</scope>
  	</dependency>
  </dependencies>
  <build>
  	<plugins>
  		<plugin>
  			<groupId>org.apache.maven.plugins</groupId>
  			<artifactId>maven-compiler-plugin</artifactId>
  			<version>2.0.2</version>
			<configuration>
	          <source>1.5</source>
	          <target>1.5</target>
			</configuration>
  		</plugin>
  	</plugins>
  </build>
</project>

pom.xml

The simple testcase above creates a content repository under your target/repository folder. It creates a ‘Node’ and then removes it. While we have not added any meaningful data through this code please be rest assured that you can store giga bytes of data efficiently to that single node. You could add files, attributes or more nodes of some other kind. You have the flexibility of choosing to store data on a single file system or multiple file systems depending upon your requirements. Furthermore you can search the data very easily and efficiently using a pluggable search engine. For most of us the default search engine , Apache Lucene is powerful enough and for the greedy there definitely is a way to choose your pick. The data elements can be connected and referential integrity could be enforced. These is a powerful caching system purrring silently under the hood making your fetches as efficient as it should be. The icing on the cake is that there is so little to learn to start using this baby and there already is a powerful ORM layer. Talking about ORM layers there are Options too. I mean to cover all these and more in the forthcoming posts. So stay tuned. Ciao!1. 1.

One Thought on “Jackrabbit – Introduction [Part 1]

  1. murali on May 15, 2013 at 9:27 am said:

    good morning BONI ….. this is murali..
    i need a help …. i.e i need to create a repository or i need to create a new work space ….through programatically…
    i done the work space creating but i unable to access the created workspaces.. if u know just help me…
    thanks…

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Post Navigation