cancel
Showing results for 
Search instead for 
Did you mean: 

How to treat JSR-170 Repository and Session

alexeyg
Champ in-the-making
Champ in-the-making
Hello,

Quick question regarding JSR-170 Repository and Session objects… 

It takes a long time to instantiate a Repository object programmatically, and in the end it says "repository started." 

I need to get (and keep) JSR-170 level 1 access to Alfresco from my web app.  Login with Admin account and go from there.  What is the preferred way to handle this situation?  I tried loggin in once and keeping Session as a static variable but that throws security exceptions on refresh.

All examples seem to use the:
ApplicationContext context = new ClassPathXmlApplicationContext("classpathSmiley Surprisedrg/alfresco/jcr-context.xml");
doesn't that start up the repository (which takes a while)?

Thanks!
7 REPLIES 7

davidc
Star Contributor
Star Contributor
You only need to start the Repository once within your web application. This is achieved by adding the following to web.xml:

   <listener>
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
   </listener>
   …
   <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>
         classpath:alfresco/jcr-context.xml
      </param-value>
      <description>Spring config file locations</description>
   </context-param>

To get hold of the Spring root Web Application Context which in turn gives you access to Alfresco's JCR Repository bean, you can use:

context = WebApplicationContextUtils.getRequiredApplicationContext(ServletContext)

Then, any time you want access to the Repository, use:

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

alexeyg
Champ in-the-making
Champ in-the-making
Everything works, thank you so much!  It's so nice to leave for the weekend with working code!  :lol:

One point of clarification… I am running Alfresco Web Client (AWC) and myApp in the same Tomcat installation.  AWC starts up the repository and so does my client.  Could this potentially be a problem?

If so, can I add the code you posted to AWC's web.xml and pull Sprint's WebApplicationContext accross Tomcat contexts?

alexeyg
Champ in-the-making
Champ in-the-making
hmm I tried adding that classpath:alfresco/jcr-context.xml line to existing

   <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>

location, but Tomcat throws a bunch of exceptions on startup:

Exception sending context initialized event to listener instance of class org.alfresco.web.app.ContextListener
….
Cannot register alias 'TransactionService' for bean name 'transactionComponent': it's already registered for bean name 'transactionComponent'

This sounds like a simple Spring issue but unfortunately I do not know Spring that well.  I'll read up on it over the weekend.

Sounds like you guys are already planning to take a look at this:
http://www.alfresco.org/forums/viewtopic.php?t=917

Thanks again.

alexeyg
Champ in-the-making
Champ in-the-making
and another interesting thing - when I have Web Client and myApp open at the same time, content updated via Web Client does not get reflected in myApp unless it is restarted (it seems).  Is there some kind of a Session for Web Client that it commits at some point?  If so, can I force a commit?  Thanks.

alexeyg
Champ in-the-making
Champ in-the-making
alright, everything is resolved and working!! 

Here is what I did (Tomcat 5.5.12 + Alfresco):

Move everything from WEB-INF/lib and WEB-INF/classes to Shared folder in Tomcat (to allow Shared classloder to load it).

Remove all spring/alfresco/jcr jars from my app

Build Alfresco, get alfresco-api.jar, jcr-context.xml and /model/jcrModel.xml, put it into appropriate place in Shared

Add jcr-context.xml to Alfresco's web.xml (do not include it in application-context.xml):

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

In servlet:

// getting Alfresco context and Spring WebAppContext
ServletContext clientContext = this.getServletContext().getContext("/alfresco");
WebApplicationContext context = WebApplicationContextUtils.getRequiredWebApplicationContext(clientContext);

// pulling JCR Spring Bean from Alfresco
Repository repository = (Repository)context.getBean("JCR.Repository");

YAY!! thanks everyone for your help.  I finally managed to gain access to JCR from another context in Tomcat while also running Alfresco Web Client.

I spent good 3 weeks (on and off) tryin to figure this out.  Somebody put this on Wiki please Smiley Happy

davidc
Star Contributor
Star Contributor
Cool - btw, you are free to add this kind of information to the WIKI yourself.  Just register and that's it.

nico
Champ in-the-making
Champ in-the-making
Does this also apply to 1.3 ?
I'm having some trouble doing this…

BTW, I'm trying to get the bean via :

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