3
votes

I am having some issues connecting to the JCR repository within AEM 6.0. When I get to the point of creating a session on the repostory I get a javax.jcr.lock.LockException: Precondition Failed.

I have been using this tutorial to get started.

Here is my very simple code sample:

import java.io.FileNotFoundException;
import java.io.FileReader;

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

import org.apache.jackrabbit.commons.JcrUtils;

import com.opencsv.CSVReader;


public class Main { 

    public static void main(String[] args) throws FileNotFoundException {
        Repository repository;
        FileReader fileReader;
        CSVReader csvReader;

        try {
            System.out.println("connecting to repository");
            repository = JcrUtils.getRepository("http://localhost:4502/crx/server");

            Session session = repository.login( new SimpleCredentials("admin", "admin".toCharArray())); // throws javax.jcr.lock.LockException: Precondition Failed

        }
        catch(Exception e) {
            System.out.println(e);
        }
    }

}

Any guidance would be greatly appreciated.

2

2 Answers

6
votes

Inside a JCR repository, content is organized into one or more workspaces, each of which holds of a hierarchical structure of nodes and properties. So to create a jcr session & access node and properties you have to pass workspace with credentials, Default AEM workspace is crx.default

Instead of :

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

Use :

Session session = repository.login( new SimpleCredentials("admin", "admin".toCharArray()),"crx.default");

Please check the below link

javax.jcr.lock.LockException:Precondition Failed

1
votes

The Obvious first: Is the AEM server running?

Secondly: Maybe your build environment is not set up correctly I was able to set up a working project using your code and this maven file:

<?xml version="1.0" encoding="UTF-8"?> 
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.stackoverflow.test</groupId>
<artifactId>access_crx_from_outside</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
    <dependency>
        <groupId>javax.jcr</groupId>
        <artifactId>jcr</artifactId>
        <version>2.0</version>
    </dependency>
    <dependency>
        <groupId>org.apache.jackrabbit</groupId>
        <artifactId>jackrabbit-jcr-commons</artifactId>
        <version>2.7.4</version>
    </dependency>

    <dependency>
        <groupId>org.apache.jackrabbit</groupId>
        <artifactId>jackrabbit-jcr2dav</artifactId>
        <version>2.6.0</version>
    </dependency>
</dependencies>