AnsweredAssumed Answered

Error using JCR API to access documents (ApplicationContext)

Question asked by ndzou on Sep 28, 2009
Latest reply on Oct 14, 2009 by clayton
I'm trying to create a simple Java-backed WebScript that access some documents in the content store, the Java WebScript itself works.  But I can't seem to use the JCR API to access documents in the repository.

Both the WebScript and the JCR code came directly from Alfresco's official developer Wiki examples.

The sample WebScript looks like this:

package org.alfresco.sample;

import java.io.IOException;

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

import org.alfresco.web.scripts.AbstractWebScript;
import org.alfresco.web.scripts.WebScriptRequest;
import org.alfresco.web.scripts.WebScriptResponse;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestWebScript extends AbstractWebScript{
    public void execute(WebScriptRequest request, WebScriptResponse response) throws IOException {
        String result = "";
        try {
            ApplicationContext context = new ClassPathXmlApplicationContext("classpath:alfresco/application-context.xml");
            Repository repository = (Repository)context.getBean("JCR.Repository");

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

            Node rootNode = session.getRootNode();
            result = "Root node: path=" + rootNode.getPath() + ", type=" + rootNode.getPrimaryNodeType().getName();
        } catch (Exception e) {
            System.out.println("Error: " + e.getMessage());
        }

        response.getWriter().write(result);
   }
}
The very first line of trying to get the ApplicationContext fails with the following exception, it seems like it's trying to re-initialize the application context.

16:07:43,827  INFO  [alfresco.config.JndiPropertiesFactoryBean] Loading properties file from class path resource [alfresco/repository.properties]
16:07:43,828  INFO  [alfresco.config.JndiPropertiesFactoryBean] Loading properties file from class path resource [alfresco/domain/transaction.properties]
16:07:43,828  INFO  [alfresco.config.JndiPropertiesFactoryBean] Loading properties file from URL [file:/opt/Alfresco/tomcat/shared/classes/alfresco-global.properties]
16:07:43,868  INFO  [alfresco.config.JndiPropertyPlaceholderConfigurer] Loading properties file from class path resource [alfresco/alfresco-shared.properties]
Error: Error creating bean with name 'workflowScheduler' defined in class path resource [alfresco/bootstrap-context.xml]: Cannot resolve reference to bean 'jbpm_template' while setting bean property 'JBPMTemplate'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jbpm_template' defined in class path resource [alfresco/workflow-context.xml]: Cannot resolve reference to bean 'jbpm_configuration' while setting constructor argument with index 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jbpm_configuration' defined in class path resource [alfresco/workflow-context.xml]: Initialization of bean failed; nested exception is java.lang.IllegalArgumentException: a beanFactoryReference already exists for key jbpm_configuration
How can I get a reference to the ApplicationContext?  I try replacing this line:

            ApplicationContext context = new ClassPathXmlApplicationContext("classpath:alfresco/application-context.xml");
with this:

            ApplicationContext context = ApplicationContextHelper.getApplicationContext();
Still doesn't work.

Am I missing something obvious?  Any help?

Outcomes