Powered by Blogger.

Wednesday, 27 January 2016

Connect AEM JCR using main() in JAVA



Connect to JCR repository using a simple Java main method.

When using Adobe Experience Manager (CQ) as our content management tool we sometimes need to connect to a AEM JCR repository from our integrated development environment (IDE) like Eclipse or NetBeans.

The following code shows a simple executable Java program for connecting to any AEM JCR Repository using a main() method. This can be used to connect to any remote or local JCR repository.

The Steps to do so are as follows:-

Step1:
Download the Jack Rabbit Jar File form
http://www.apache.org/dyn/closer.cgi/jackrabbit/2.8.1/jackrabbit-standalone-2.8.1.jar
and place the Jack Rabbit jar in build path of your project.


Step 2:

File: ConnectJCR.java

package net.codermag.jcr.test;

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

import org.apache.jackrabbit.commons.JcrUtils;
import org.apache.jackrabbit.core.TransientRepository;

public class ConnectJCR {

public static void main(String[] args) throws Exception {
Repository repository = new TransientRepository();
repository = JcrUtils.getRepository("http://localhost:4502/crx/server");

// Create a Session
Session session = repository.login(new SimpleCredentials("admin", "admin".toCharArray()));


// Create a node that represents the root node
Node root = session.getRootNode();
Node content = root.getNode("content");
Node subContent = content.getNode("dam");


//Getting the iterator over the nodes
NodeIterator childNodeIterator = subContent.getNodes();



//Iterating over the nodes and printing their names
while(childNodeIterator.hasNext()){
Node childNode=(Node) childNodeIterator.next();
System.out.println(childNode.getName());

}
}
}


No comments:

Post a Comment

Coder Magnet
Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt utlaoreet dolore.