jackrabbit-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Fabián Mandelbaum <fmandelb...@gmail.com>
Subject Re: Problem with OCM: Cannot write at /
Date Wed, 06 Aug 2008 17:29:33 GMT
Christophe Lombart escribió:
> Did you review the first (and the only one :-( ) tutorial :
> http://jackrabbit.apache.org/5-with-jackrabbit-ocm.html
> http://jackrabbit.apache.org/a-simple-ocm-project-with-maven-eclipse.html
>
>   

Yes, I did. My test classes are based on (small) modifications of the
classes the "5 mins with OCM" tutorial presents.

>
>   
>> However, when I try to store an entity (using ocm.insert(obj);
>> ocm.save();) I get an exception stating that I cannot write at the
>> repository root (/), what's going on? Thanks in advance...
>>
>>
>>     
>
> can you send more information ? What is the name of the exception ? please,
> send us a code example.
>
>   

Sure, here you have it. I've made a GenericJcrDAO class modelled after
the GenericHibernateDAO on Hibernate's book, Here you have its code:

-------------------------------------------------- GenericJcrDAO.java
-----------------------------------------------------
package repository;

import java.lang.reflect.ParameterizedType;
import java.util.ArrayList;
import java.util.List;

import javax.jcr.Session;

import org.apache.jackrabbit.ocm.exception.ObjectContentManagerException;
import org.apache.jackrabbit.ocm.manager.ObjectContentManager;
import org.apache.jackrabbit.ocm.manager.impl.ObjectContentManagerImpl;
import
org.apache.jackrabbit.ocm.mapper.impl.annotation.AnnotationMapperImpl;

public abstract class GenericJcrDAO<T> {
    private Class<T> clazz;
    protected ObjectContentManager ocm;
    protected Session session;
   
    @SuppressWarnings("unchecked")
    public GenericJcrDAO(Session session) {
        this.session = session;
        ParameterizedType pt = (ParameterizedType)
getClass().getGenericSuperclass();
        clazz = (Class<T>)pt.getActualTypeArguments()[0];
        List<Class> classes = new ArrayList<Class>();
        classes.add(clazz);
        ocm = new ObjectContentManagerImpl(session, new
AnnotationMapperImpl(classes));
    }
   
    public void store(T entity) throws RepositoryException {
        try {
            ocm.insert(entity);
            ocm.save();
        }
        catch (ObjectContentManagerException ocme) {
            throw new RepositoryException(ocme);
        }
    }
   
    public T retrieve(String xpath) throws RepositoryException {
        try {
            T entity = clazz.cast(ocm.getObject(xpath));
            return entity;
        }
        catch (ObjectContentManagerException ocme) {
            throw new RepositoryException(ocme);
        }
    }
   
    public T delete(String xpath) throws RepositoryException {
        try {
            T entity = clazz.cast(ocm.getObject(xpath));
            ocm.remove(entity);
            ocm.save();
            return entity;
        }
        catch (ObjectContentManagerException ocme) {
            throw new RepositoryException(ocme);
        }
    }
}

I've made a PressReleaseDAO class to store objects of class PressRelease
(the one of the "5 mins" tutorial), which is very simple (no
PressRelease-specific code there yet):

-------------------------------------------------- PressReleaseDAO.java
-----------------------------------------------------
package repository;

import javax.jcr.Session;

import com.calenco.ocm.model.PressRelease;

public class PressReleaseDAO extends GenericJcrDAO<PressRelease> {

    public PressReleaseDAO(Session session) {
        super(session);
    }
}

I've also made a RepositoryManager class to encapsulate OCM repo
creation/management which closely resembles the example of the "5 mins"
tutorial.

The JUnit test case for the store() method is:

@Test
    public void testStore() throws Exception {
        PressRelease pr = new PressRelease();
        pr.setTitle("Test Press Release");
        pr.setPubDate(new Date());
        pr.setPath("/pr1");
        pr.setContent("Just a test press release");
        PressReleaseDAO prdao = new PressReleaseDAO(session);
        prdao.store(pr);
    }

And the exception stack trace is:

repository.RepositoryException:
org.apache.jackrabbit.ocm.exception.ObjectContentManagerException:
Cannot persist current session changes.; nested exception is
javax.jcr.AccessDeniedException: /: not allowed to modify item
    at repository.GenericJcrDAO.store(GenericJcrDAO.java:35)
    at
com.calenco.jcr.RepositoryManagerTest.testStore(RepositoryManagerTest.java:94)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at
org.junit.internal.runners.TestMethodRunner.executeMethodBody(TestMethodRunner.java:99)
    at
org.junit.internal.runners.TestMethodRunner.runUnprotected(TestMethodRunner.java:81)
    at
org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:34)
    at
org.junit.internal.runners.TestMethodRunner.runMethod(TestMethodRunner.java:75)
    at
org.junit.internal.runners.TestMethodRunner.run(TestMethodRunner.java:45)
    at
org.junit.internal.runners.TestClassMethodsRunner.invokeTestMethod(TestClassMethodsRunner.java:66)
    at
org.junit.internal.runners.TestClassMethodsRunner.run(TestClassMethodsRunner.java:35)
    at
org.junit.internal.runners.TestClassRunner$1.runUnprotected(TestClassRunner.java:42)
    at
org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:34)
    at
org.junit.internal.runners.TestClassRunner.run(TestClassRunner.java:52)
    at
org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:38)
    at
org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
    at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
    at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
    at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
Caused by:
org.apache.jackrabbit.ocm.exception.ObjectContentManagerException:
Cannot persist current session changes.; nested exception is
javax.jcr.AccessDeniedException: /: not allowed to modify item
    at
org.apache.jackrabbit.ocm.manager.impl.ObjectContentManagerImpl.save(ObjectContentManagerImpl.java:1076)
    at repository.GenericJcrDAO.store(GenericJcrDAO.java:32)
    ... 21 more
Caused by: javax.jcr.AccessDeniedException: /: not allowed to modify item
    at
org.apache.jackrabbit.core.ItemImpl.validateTransientItems(ItemImpl.java:486)
    at org.apache.jackrabbit.core.ItemImpl.save(ItemImpl.java:1208)
    at org.apache.jackrabbit.core.SessionImpl.save(SessionImpl.java:897)
    at
org.apache.jackrabbit.ocm.manager.impl.ObjectContentManagerImpl.save(ObjectContentManagerImpl.java:1068)
    ... 22 more

I hope this helps, please let me know if you need more information
(Using latest JackRabbit jars available on jackrabbit.apache.org).
Thanks in advance.

Mime
View raw message