As we transition to the new Hyland Community, some post links may be temporarily disrupted as we finalize redirects. We apologize for the inconvenience and are working to resolve this as quickly as possible. Details.
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(); } }
f the first context initialization didn't use ApplicationContextHelper, then you can't use it either.
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.
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.
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); } } }