cancel
Showing results for 
Search instead for 
Did you mean: 

How to acess repository using JCR and a Session Bean

pinkman
Champ in-the-making
Champ in-the-making
In a nutshell, I want to connect to an Alfresco repository from a session bean. It will be running on the same JBoss instance as Alfresco (tho that could change in the future).

I would prefer JCR as I am a standards freak, tho I can't get my head around how to connect to a pre-existing repository. The JNDI project http://issues.alfresco.com/browse/AR-1131 looks useful but maybe a little immature? I have a feeling I am going in the wrong direction with it anyway.

I have read the "Simple Example", and understand most things AFTER the repository is accessed, but how it accessed the repository instance is still a bit of a mystery to me. At the end of the day I am just trying to get an instance of a JCR "Repository" object. What is the best way to get started?
13 REPLIES 13

davidc
Star Contributor
Star Contributor
I'm not (yet) familiar with the jcr jndi project, but I can tell you about the canonical (in-process) way to get a reference to the Alfresco JCR Repository interface.

Alfresco's JCR Repository is a Spring Bean called 'JCR.Repository'.  So, you write:

Repository repository = (Repository)context.getBean("JCR.Repository");

where context is the Spring Application Context.

How to the get the Spring context?

Either create explicitly using:

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

or hook into a web application configured one:

a) web.xml:

   <listener>
      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
   </listener>

   <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>
         classpath:alfresco/application-context.xml
      </param-value>
      <description>Spring config file locations</description>
   </context-param>
  

b) Accessed using:

WebApplicationContextUtils.getWebApplicationContext(ServletContext sc);

pinkman
Champ in-the-making
Champ in-the-making
Thanks for the prompt reply David, that has helped but I am still a bit confused about accessing it via spring.

It sounds like I need to bootstrap an instance of JCR.Repository via spring, rather than hook into the existing instance in the Alfresco spring context. Does that sound correct?

If I do need to bootstrap it myself, I will obviously need to copy the application-context.xml into my classpath. I guess I will also need my config files from "default\conf\alfresco\extension" in my class path for the session bean. Do I need any other spring config files?

Am I on the right track here? I have a feeling I have lost the plot somewhere along the line…

davidc
Star Contributor
Star Contributor
I'm not sure of how you've deployed Alfresco - you say its running in the same instance as JBoss.  In that case, I'd go for option b), rather than instantiate a new repository.

pinkman
Champ in-the-making
Champ in-the-making
Alfresco is deployed as a war on JBoss. I have manually configured it, ie its not the pre bundled version. It is connecting to a Oracle database. Not sure if that helps, but it's a bit of background at least.

How would I go about connecting to the pre-existing repository bean? doesn't it exist in a different Spring application context?

If I run:

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

Won't it startup it's own context, and need all of the Alfresco XML files?

Sorry, the lightbulb hasn't quite gone off above my head yet Smiley Happy.

pinkman
Champ in-the-making
Champ in-the-making
The JCR examples use an embedded Alfresco instance, and bind to that URL using RMI. However, Starting an embedded Alfresco instance is pretty heavy weight, and I would need to connect to it over RMI even though it is on the same JBoss instance.

The other options are to grab the JCR instance from Spring, or to manually create another JCR connection using a JCR implementation (JackRabbit or Alfresco).

It doesn't look like I can inject the spring bean into my project, or at least I can't figure out how to do it and can't find any examples of this for the JCR. So I guess I need to create another JCR connection?

I am sure lots of people have done this before, I just wish it was clearer in the wiki. We will be using a plain file system for a large project if I can't get this to work soon Smiley Sad.

hbf
Champ on-the-rise
Champ on-the-rise
I've run into the same problem.

If I use, from my Tomcat webapp,

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

on a Tomcat installation with Alfresco & Web Client running next to by webapp, then I get


WARN http-8080-Processor25 org.alfresco.repo.content.RoutingContentService - Property 'transactionService' has been replaced by 'retryingTransactionHelper'.
Error creating bean with name 'serverConnector' defined in class path resource [alfresco/core-services-context.xml]: Invocation of init method failed; nested exception is java.io.IOException: Cannot bind to URL [rmi://localhost:50500/alfresco/jmxrmi]: javax.naming.NameAlreadyBoundException: alfresco/jmxrmi [Root exception is java.rmi.AlreadyBoundException: alfresco/jmxrmi]

I understand this as follows: When Alfresco starts up, it creates the beans in its Spring application context. In my webapp, I tell it to create the context again but now of course, it cannot bind once more.

We need a way to get a handle to the spring context without recreating it, right?

This article points into this direction but I cannot get it to work.

Can an Alfresco engineer help here, please?

This issue has popped up several times in the past (this thread, this post, this one on the Sakai forum with a recent follow up, and this one on the Spring forum) and no solution seems to be publicly available. If we find a solution, I volunter to document it on the Alfresco wiki.

hbf
Champ on-the-rise
Champ on-the-rise
I've detailed the steps proposed in this thred on Alfresco's wiki.

I hope this helps others that need to do a similar thing.

Regarding the errors that show up in my log (see previous post), I think they are always output but not shown due to a different logging level configuration (I might be wrong, though).

srikanth
Champ in-the-making
Champ in-the-making
Hello…
i want to know that how to use oracle for alfresco…

morpheus
Champ in-the-making
Champ in-the-making
yes the same old story, but i also can´t get the App context.
I wrote an Junit test to figure out what it could be. And also added the dependcy to the AlfrescoEmbedded Project, so all things needed should be there , right  ?
But when i´m  running the test im getting this Exception

org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [opt/alfresco/tomcat/webapps/WEB-INF/classes/alfresco/application-context.xml]; nested exception is java.io.FileNotFoundException: class path resource [opt/alfresco/tomcat/webapps/WEB-INF/classes/alfresco/application-context.xml] cannot be opened because it does not exist
Caused by: java.io.FileNotFoundException: class path resource [opt/alfresco/tomcat/webapps/WEB-INF/classes/alfresco/application-context.xml] cannot be opened because it does not exist

I checked the location and there is an application-context.xml file. So did ich forget configurating an varible ? Where/Which  ?

code of the Test

   public void testGettingSpringApplicationContext(){
         // Setup Spring
        ApplicationContext context = new ClassPathXmlApplicationContext("/opt/alfresco/tomcat/webapps/WEB-INF/classes/alfresco/application-context.xml");
        assertNotNull(context);
   }