cancel
Showing results for 
Search instead for 
Did you mean: 

Error using JCR API to access documents (ApplicationContext)

ndzou
Champ in-the-making
Champ in-the-making
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?
2 REPLIES 2

clayton
Champ in-the-making
Champ in-the-making
I apologise for not spotting this before posting today, but we share the same error, trying to reload workflow beans.

http://forums.alfresco.com/en/viewtopic.php?f=28&t=22433.

I am using SpringMVC and I had suspected that it might be something to do with the fact that I was using JSPs and not coming through a Faces url.

We have either both fallen into the same trap or there is an issue.

Regards,

Brian

clayton
Champ in-the-making
Champ in-the-making
Aha.  Found the trap.

A web client needs to get a Web application context:

WebApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(servletConfig);

I did check the lists but I was sidetracked by the SpringMVC aspect which in the end doesn't have much to do about the price of fish.

Apologies again.

http://forums.alfresco.com/en/viewtopic.php?f=28&t=20901&p=71891&hilit=application+object#p71891

Clayton