cancel
Showing results for 
Search instead for 
Did you mean: 

Getting Application Context from a webservice

p3d3r0s0
Champ in-the-making
Champ in-the-making
Hey, im using the kofax plugin Ascent, and i need to add a property to a node inside DbNodeServiceImpl. That property is stored in my database. But to access tables in the database i was using this
   protected static ApplicationContext ctx = FacesContextUtils.getWebApplicationContext(FacesContext.getCurrentInstance());
to get the proper context.
Unfortunatly, i cant get the ApplicationContext that way when i use the plugin, i also tried:

   protected static ApplicationContext ctx = ApplicationContextHelper.getApplicationContext();
   protected static ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:alfresco/application-context.xml");
   protected static ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:alfresco/jcr-context.xml");
   protected static ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath*:alfresco/hibernate-context.xml");

Am i going for the wrong approach?
3 REPLIES 3

p3d3r0s0
Champ in-the-making
Champ in-the-making
To anyone with a similar problem, the solution is this:

add a line similar to this to your application-context.xml in the classes directory
   <bean id="appContext" class="your.package.and.class.name.in.repository"></bean>

your.package.and.class.name.in.repository  = something like "org.MyClass"

create that class making sure it implements "ApplicationContextAware"
make a getter, can be static, and use it to get the applicationContext whenever you need it.

Hope it helps someone.
Regards

efycaci_ramesh
Champ in-the-making
Champ in-the-making
hi P3d3r0s0,

i'm also facing same problem?
I can't able to follow ur post,so pls can u provide detail  information.

I'm having
1) when we implement the ApplicationContextAware how will you set content for setApplicationContext(app-content) ?
2)where to place the class file (alfresco/server used for webservice)?
3)How to get the applicationcontent in client side.
4)In your post "your.package.and.class.name.in.repository"  shoulb in which repository?

do u have sample application if? pls provide it..

thanks in advance.

p3d3r0s0
Champ in-the-making
Champ in-the-making
Hey,
I have a class in repository named AppContext, its in a package named myPacket.
I have only 2 methods inside:


package myPacket;

import javax.faces.context.FacesContext;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

public class AppContext implements ApplicationContextAware{
   
   private static ApplicationContext ctx;

   @Override
   public void setApplicationContext(ApplicationContext arg0)
         throws BeansException {

      ctx = arg0;
   }
   
   public static ApplicationContext getApplicationContext(){
      return ctx;
   }
}

Inside the repository project, in config/alfresco, theres a application-context.xml file. In that file i had the line:
   <bean id="appContext" class="myPacket.AppContext"></bean> 
in the end, before the </beans>

After having this done, i can access the applicationContext by using the static method AppContext.getApplicationContext();

Im using in another class in the repository like this:
   protected static ApplicationContext ctx = AppContext.getApplicationContext();

Hope this helps.

ps: i never use the Set method in appContext. Alfresco/Spring injects/sets the applicationContext for you.