cancel
Showing results for 
Search instead for 
Did you mean: 

Acquiring ApplicationContext inside ListOfValuesConstraint

robertito
Champ in-the-making
Champ in-the-making
Hi!

I was trying to add a constraint type programmatically, by extending it from ListOfValuesConstraint. I want to fill a listbox with values extracted from the alfresco repository (defined in a particular space), but when I try to acquire the ApplicationContext, it looks like it tries to initialize the Repository again, and therefore throws an exception. Does anybody know if there is any way to solve this?

Thank you very much!
Robert

The piece of code that performs this, and the exception follow:

The variable allowedValues is filled with the values to display in the list box.

public List<String> getAllowedValues() {
      
  ApplicationContext context =  ApplicationContextHelper.getApplicationContext(); // EXCEPTION!!!
         
  Repository repository = (Repository) context.getBean("JCR.Repository");
         
  Session session = null;
         
  try {

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

    Node root = session.getRootNode();
    Node node = root.getNode("app:company_home/cm:Applications/cm:Values");

    allowedValues = new ArrayList<String>();
            
    // Fill allowedValues with the names of the nodes…
   
    return allowedValues;
            
  } catch (Exception e) {
    // …handle exceptions…
  }
  finally {
    session.logout();
  }
}

Exception:

18:21:37,584 ERROR [org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/alfresco].[jsp]] Servlet.service() for servlet jsp threw exception
java.lang.IllegalArgumentException: a beanFactoryReference already exists for key jbpm_configuration
at org.springmodules.workflow.jbpm31.JbpmFactoryLocator.addToMap(JbpmFactoryLocator.java:108)
at org.springmodules.workflow.jbpm31.JbpmFactoryLocator.setBeanFactory(JbpmFactoryLocator.java:76)
at org.alfresco.repo.workflow.jbpm.AlfrescoJbpmConfigurationFactoryBean.setBeanFactory(AlfrescoJbpmConfigurationFactoryBean.java:68)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1076)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:429)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:250)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:141)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:247)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:161)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:245)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:124)
at org.springframework.beans.factory.support.ConstructorResolver.resolveConstructorArguments(ConstructorResolver.java:316)
at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:91)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:745)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:683)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:388)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:250)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:141)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:247)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:161)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:245)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:124)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1019)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:809)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:425)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:250)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:141)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:247)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:161)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeansOfType(DefaultListableBeanFactory.java:236)
at org.springframework.context.support.AbstractApplicationContext.getBeansOfType(AbstractApplicationContext.java:701)
at org.springframework.context.support.AbstractApplicationContext.registerListeners(AbstractApplicationContext.java:519)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:343)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:92)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:77)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:68)
at lu.intrasoft.alfresco.tools.constraint.ListOfValuesFromRepositoryConstraint.getAllowedValues(ListOfValuesFromRepositoryConstraint.java:101)
at org.alfresco.web.bean.generator.TextFieldGenerator.createComponent(TextFieldGenerator.java:141)
at org.alfresco.web.bean.generator.BaseComponentGenerator.generateAndAdd(BaseComponentGenerator.java:87)
at org.alfresco.web.ui.repo.component.property.UIProperty.generateControl(UIProperty.java:185)
at org.alfresco.web.ui.repo.component.property.UIProperty.generateItem(UIProperty.java:126)
at org.alfresco.web.ui.repo.component.property.PropertySheetItem.encodeBegin(PropertySheetItem.java:74)
at org.apache.myfaces.shared_impl.renderkit.RendererUtils.renderChild(RendererUtils.java:412)

at java.lang.Thread.run(Thread.java:595)
5 REPLIES 5

derek
Star Contributor
Star Contributor
Hi,

The application context is being initialized somewhere else - this depends on how you are starting the application in the first place.  You cannot start it twice and if the first context initialization didn't use ApplicationContextHelper, then you can't use it either.

You will need some support to give the constraint implementation access to the context.  Create a bean that is ApplicationContextAware and use that to push the context (not a specific bean) onto a static member.  You can then retrieve it statically from the constraint implementation at runtime.

Regards

jharrop
Champ in-the-making
Champ in-the-making
f the first context initialization didn't use ApplicationContextHelper, then you can't use it either.

Excuse me if this is too simple minded, but this implies that Alfresco doesn't use ApplicationContextHelper when starting up.  Why doesn't it?

thanks

Jason

lvs
Champ in-the-making
Champ in-the-making
Hi Jason,

Did you manage to do this? I am having exactly the same problem but I don't understand exactly what I should do to solve it.

Thanks
Laura

kirin
Champ in-the-making
Champ in-the-making
The application context is being initialized somewhere else - this depends on how you are starting the application in the first place. You cannot start it twice and if the first context initialization didn't use ApplicationContextHelper, then you can't use it either.
I've got the same problem. And I didn't understand how to sovle it too   :cry:  For me it's very strange that Alfresco platform has ApplicationContextHelper class which doesn't return application context  :?

enrico_it
Champ in-the-making
Champ in-the-making
Create a bean that is ApplicationContextAware and use that to push the context (not a specific bean) onto a static member.  You can then retrieve it statically from the constraint implementation at runtime.

I post just to say, that I have implemented this solution and it works.
I have created a ApplicationContextAware bean in this way:

public class MyApplicationContextHelper implements ApplicationContextAware {

   private static ApplicationContext applicationContext;
   
   public void setApplicationContext(ApplicationContext arg0)
         throws BeansException {
      applicationContext = arg0;
   }
   
   public static ApplicationContext getApplicationContext()
   {
      return applicationContext;
   }
   
   public static Object getBean(String beanName)
   {
      if(beanName==null||beanName.trim().length()==0)
      {
         return null;
      }
      else{
         return applicationContext.getBean(beanName);
      }
   }
}
Spring creates an instance of this bean, and then I can access the ApplicationContext in any class doing:
ByBean myBean = (MyBean)MyApplicationContextHelper.getBean("MyBean");

Obviously, you use this way only when you don't have access to the Spring injection (in the evaluators for instance).
Getting started

Tags


Find what you came for

We want to make your experience in Hyland Connect as valuable as possible, so we put together some helpful links.