cancel
Showing results for 
Search instead for 
Did you mean: 

What's the correct way to reuse LDAP beans/connection settings?

idwright
Star Collaborator
Star Collaborator

I'm trying to create an action which accesses LDAP.

If I define the action bean in /extension/subsystems/Synchronization/ldap/ldap1/my-ldap-context.xml then I would normally expect to be able to access it using org.alfresco.repo.management.subsystems.SubsystemProxyFactory but that doesn't work for the Authentication subsystem.

Cannot convert value of type [org.alfresco.repo.management.subsystems.DefaultChildApplicationContextManager] to required type [org.alfresco.repo.management.subsystems.ApplicationContextFactory] for property 'sourceApplicationContextFactory':

If I pull the bean definitions (<import resource="classpath:alfresco/subsystems/Authentication/common-ldap-context.xml"/>) into my context/actions-context.xml then chaos ensues

What does work is to redefine ldapInitialDirContextFactory in the context file but that's not ideal.

Any thoughts?

Thanks.

1 ACCEPTED ANSWER

kaynezhang
World-Class Innovator
World-Class Innovator

You don't need to define your action bean in /extension/subsystems/Synchronization/ldap/ldap1/my-ldap-context.xml or import common-ldap-context.xml. You can define your action in regular way.

You can try to do it like this:
1.Make your action implements spring InitializingBean and In your action reference interface ChildApplicationContextManager
public class YourAction implements InitializingBean{
   private ChildApplicationContextManager authenticationContextManager;
   private LDAPInitialDirContextFactory ldapInitialDirContextFactory;
   public void setAuthenticationContextManager(ChildApplicationContextManager authenticationContextManager) {
   this.authenticationContextManager = authenticationContextManager;
   }
   @Override
   public void afterPropertiesSet() throws Exception {
   for(String contextName : authenticationContextManager.getInstanceIds()){
      ApplicationContext ctx = authenticationContextManager.getApplicationContext(contextName);
      try{
            ldapInitialDirContextFactory = LDAPInitialDirContextFactory)
            ctx.getBean(LDAPInitialDirContextFactory.class);
         } catch(NoSuchBeanDefinitionException e) {}
      }

   }
}
2.And in your action spring file inject Authentication bean to your action authenticationContextManager property
<property name="authenticationContextManager" ref="Authentication" />
3. override afterPropertiesSet method of your action and get ldapInitialDirContextFactory bean like previous code.

View answer in original post

2 REPLIES 2

kaynezhang
World-Class Innovator
World-Class Innovator

You don't need to define your action bean in /extension/subsystems/Synchronization/ldap/ldap1/my-ldap-context.xml or import common-ldap-context.xml. You can define your action in regular way.

You can try to do it like this:
1.Make your action implements spring InitializingBean and In your action reference interface ChildApplicationContextManager
public class YourAction implements InitializingBean{
   private ChildApplicationContextManager authenticationContextManager;
   private LDAPInitialDirContextFactory ldapInitialDirContextFactory;
   public void setAuthenticationContextManager(ChildApplicationContextManager authenticationContextManager) {
   this.authenticationContextManager = authenticationContextManager;
   }
   @Override
   public void afterPropertiesSet() throws Exception {
   for(String contextName : authenticationContextManager.getInstanceIds()){
      ApplicationContext ctx = authenticationContextManager.getApplicationContext(contextName);
      try{
            ldapInitialDirContextFactory = LDAPInitialDirContextFactory)
            ctx.getBean(LDAPInitialDirContextFactory.class);
         } catch(NoSuchBeanDefinitionException e) {}
      }

   }
}
2.And in your action spring file inject Authentication bean to your action authenticationContextManager property
<property name="authenticationContextManager" ref="Authentication" />
3. override afterPropertiesSet method of your action and get ldapInitialDirContextFactory bean like previous code.

idwright
Star Collaborator
Star Collaborator

I see this has had quite a lot of views recently so thought it might be helpful to add a link to the implementation I ended up with:

cggh-alfresco-extensions/auth-platform-jar at master · cggh/cggh-alfresco-extensions · GitHub