How to import a package in a OSGi bundle?
Asked by Cristian P?rez Gonz?lez:

I'm developing a plugin for Crowd.

My pom.xml is this
>
> ```
> <?xml version="1.0" encoding="UTF-8"?>
>
> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
>  <modelVersion>4.0.0</modelVersion>
>  <groupId>com.example.crowd</groupId>
>  <artifactId>ldap-crowd-plugin</artifactId>
>  <version>1.0.0-SNAPSHOT</version>
>  <organization>
>  <name>Example Company</name>
>  <url>http://www.example.com/</url>
>  </organization>
>  <name>ldap-crowd-plugin</name>
>  <description>This is the com.example.crowd:ldap-crowd-plugin plugin for Atlassian Crowd.</description>
>  <packaging>atlassian-plugin</packaging>
>  <dependencyManagement>
>  <dependencies>
>  <dependency>
>  <groupId>com.atlassian.crowd</groupId>
>  <artifactId>atlassian-crowd</artifactId>
>  <version>${crowd.version}</version>
>  <type>pom</type>
>  <scope>import</scope>
>  </dependency>
>  </dependencies>
>  </dependencyManagement>
>  <dependencies>
>  <dependency>
>  <groupId>junit</groupId>
>  <artifactId>junit</artifactId>
>  <scope>test</scope>
>  </dependency>
>  <dependency>
>  <groupId>org.hamcrest</groupId>
>  <artifactId>hamcrest-core</artifactId>
>  <scope>test</scope>
>  </dependency>
>  <dependency>
>  <groupId>javax.servlet</groupId>
>  <artifactId>javax.servlet-api</artifactId>
>  <scope>provided</scope>
>  </dependency>
>  <dependency>
>  <groupId>com.atlassian.sal</groupId>
>  <artifactId>sal-api</artifactId>
>  <scope>provided</scope>
>  </dependency>
>  <dependency>
>  <groupId>com.atlassian.plugin</groupId>
>  <artifactId>atlassian-spring-scanner-annotation</artifactId>
>  <scope>compile</scope>
>  </dependency>
>  <dependency>
>  <groupId>com.atlassian.plugin</groupId>
>  <artifactId>atlassian-spring-scanner-runtime</artifactId>
>  <scope>runtime</scope>
>  </dependency>
>  <dependency>
>  <groupId>javax.inject</groupId>
>  <artifactId>javax.inject</artifactId>
>  <scope>provided</scope>
>  </dependency>
>  <dependency>
>  <groupId>org.slf4j</groupId>
>  <artifactId>slf4j-simple</artifactId>
>  </dependency>
>  <!-- WIRED TEST RUNNER DEPENDENCIES -->
>  <dependency>
>  <groupId>com.atlassian.plugins</groupId>
>  <artifactId>atlassian-plugins-osgi-testrunner</artifactId>
>  <version>${plugin.testrunner.version}</version>
>  <scope>test</scope>
>  </dependency>
>  <dependency>
>  <groupId>javax.ws.rs</groupId>
>  <artifactId>jsr311-api</artifactId>
>  <scope>provided</scope>
>  </dependency>
>  <dependency>
>  <groupId>com.google.code.gson</groupId>
>  <artifactId>gson</artifactId>
>  <version>2.2.2-atlassian-1</version>
>  </dependency>
>  <dependency>
>  <groupId>org.mockito</groupId>
>  <artifactId>mockito-all</artifactId>
>  <scope>test</scope>
>  </dependency>
>
>
>
>  <dependency>
>  <groupId>com.atlassian.crowd</groupId>
>  <artifactId>crowd-api</artifactId>
>  <scope>provided</scope>
>  </dependency>
>  <dependency>
>  <groupId>com.atlassian.crowd</groupId>
>  <artifactId>crowd-ldap</artifactId>
>  <scope>provided</scope>
>  </dependency>
>  <dependency>
>  <groupId>com.atlassian.crowd</groupId>
>  <artifactId>crowd-server</artifactId>
>  <scope>provided</scope>
>  </dependency>
>  <dependency>
>  <groupId>com.atlassian.crowd</groupId>
>  <artifactId>crowd-sal</artifactId>
>  <version>${crowd.version}</version>
>  <scope>provided</scope>
>  </dependency>
>  <dependency>
>  <groupId>com.atlassian.templaterenderer</groupId>
>  <artifactId>atlassian-template-renderer-api</artifactId>
>  <scope>provided</scope>
>  </dependency>
>
>  </dependencies>
>  <build>
>  <plugins>
>  <plugin>
>  <groupId>com.atlassian.maven.plugins</groupId>
>  <artifactId>maven-crowd-plugin</artifactId>
>  <version>${amps.version}</version>
>  <extensions>true</extensions>
>  <configuration>
>  <productVersion>${crowd.version}</productVersion>
>  <productDataVersion>${crowd.data.version}</productDataVersion>
>  <jvmArgs>-Xms1g -Xmx2g -XX:MaxPermSize=1g -XX:-UseGCOverheadLimit -server</jvmArgs>
>  <enableQuickReload>true</enableQuickReload>
>  <enableFastdev>false</enableFastdev>
>  <!-- See here for an explanation of default instructions: -->
>  <!-- https://developer.atlassian.com/docs/advanced-topics/configuration-of-instructions-in-atlassian-plugins -->
>  <instructions>
>  <Atlassian-Plugin-Key>${atlassian.plugin.key}</Atlassian-Plugin-Key>
>  <!-- Add package to export here -->
>  <Export-Package>
>  com.example.crowd.api,
>  org.springframework.ldap.core;version=1.0.0,
>  org.springframework.ldap;version=0.0.0
>  </Export-Package>
>  <!-- Add package import here -->
>  <Import-Package>
>  com.atlassian.crowd.directory.*;version=2.11.2;resolution:="optional",
>  org.springframework.ldap.*;resolution:=optional,
>  org.springframework.osgi.*;resolution:="optional",
>  org.eclipse.gemini.blueprint.*;resolution:="optional",*
>
>  </Import-Package>
>  <!-- Ensure plugin is spring powered -->
>  <Spring-Context>*</Spring-Context>
>  </instructions>
>  </configuration>
>  </plugin>
>  <plugin>
>  <groupId>com.atlassian.plugin</groupId>
>  <artifactId>atlassian-spring-scanner-maven-plugin</artifactId>
>  <version>${atlassian.spring.scanner.version}</version>
>  <executions>
>  <execution>
>  <goals>
>  <goal>atlassian-spring-scanner</goal>
>  </goals>
>  <phase>process-classes</phase>
>  </execution>
>  </executions>
>  <configuration>
>  <scannedDependencies>
>  <dependency>
>  <groupId>com.atlassian.plugin</groupId>
>  <artifactId>atlassian-spring-scanner-external-jar</artifactId>
>  </dependency>
>  </scannedDependencies>
>  <verbose>true</verbose>
>  </configuration>
>  </plugin>
>  </plugins>
>  </build>
>  <properties>
>  <crowd.version>2.12.0</crowd.version>
>  <crowd.data.version>2.12.0</crowd.data.version>
>  <amps.version>6.3.6</amps.version>
>  <plugin.testrunner.version>1.2.3</plugin.testrunner.version>
>  <atlassian.spring.scanner.version>1.2.13</atlassian.spring.scanner.version>
>  <!-- This key is used to keep the consistency between the key in atlassian-plugin.xml and the key to generate bundle. -->
>  <atlassian.plugin.key>${project.groupId}.${project.artifactId}</atlassian.plugin.key>
>  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
>  <maven.compiler.source>1.7</maven.compiler.source>
>  <maven.compiler.target>1.7</maven.compiler.target>
>
>  </properties>
> </project>
> ```
>
> And the bundle can see it in Apache Felix Web Management Console
>
> is this
>
> |-------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
> | Exported Packages | com.example.crowd.api,version=1.0.0 org.springframework.ldap,version=0.0.0 org.springframework.ldap.core,version=1.0.0                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 |
> | Imported Packages | com.atlassian.crowd.console.action,version=2.12.0 from org.apache.felix.framework (0) com.atlassian.crowd.directory,version=2.12.0 from org.apache.felix.framework (0) com.atlassian.crowd.directory.ldap,version=2.12.0 from org.apache.felix.framework (0) com.atlassian.crowd.directory.ldap.credential,version=2.12.0 from org.apache.felix.framework (0) com.atlassian.crowd.directory.ldap.name,version=2.12.0 from org.apache.felix.framework (0) com.atlassian.crowd.directory.loader,version=2.12.0 from org.apache.felix.framework (0) com.atlassian.crowd.embedded.api,version=2.12.0 from org.apache.felix.framework (0) com.atlassian.crowd.embedded.impl,version=2.12.0 from org.apache.felix.framework (0) com.atlassian.crowd.exception,version=2.12.0 from org.apache.felix.framework (0) com.atlassian.crowd.manager.directory,version=2.12.0 from org.apache.felix.framework (0) com.atlassian.crowd.model.group,version=2.12.0 from org.apache.felix.framework (0) com.atlassian.crowd.model.user,version=2.12.0 from org.apache.felix.framework (0) com.atlassian.crowd.search.ldap,version=2.12.0 from org.apache.felix.framework (0) com.atlassian.crowd.search.query.membership,version=2.12.0 from org.apache.felix.framework (0) com.atlassian.crowd.util,version=2.12.0 from org.apache.felix.framework (0) com.atlassian.event.api,version=3.1.3 from org.apache.felix.framework (0) com.atlassian.plugin,version=4.4.3 from org.apache.felix.framework (0) com.atlassian.plugin.osgi.factory,version=4.4.3 from org.apache.felix.framework (0) com.atlassian.sal.api,version=3.0.2 from org.apache.felix.framework (0) com.atlassian.sal.api.message,version=3.0.2 from org.apache.felix.framework (0) com.atlassian.sal.api.pluginsettings,version=3.0.2 from org.apache.felix.framework (0) com.atlassian.sal.api.user,version=3.0.2 from org.apache.felix.framework (0) com.google.common.base,version=18.0.0 from org.apache.felix.framework (0) com.google.common.collect,version=18.0.0 from org.apache.felix.framework (0) javax.annotation,version=0.0.0.1_008_JavaSE from org.apache.felix.framework (0) javax.annotation,version=1.0.0 from org.apache.felix.framework (0) javax.inject,version=1.0.0 from org.apache.felix.framework (0) javax.naming,version=0.0.0.1_008_JavaSE from org.apache.felix.framework (0) javax.naming,version=0.0.0 from org.apache.felix.framework (0) javax.naming.directory,version=0.0.0.1_008_JavaSE from org.apache.felix.framework (0) javax.naming.directory,version=0.0.0 from org.apache.felix.framework (0) javax.naming.ldap,version=0.0.0.1_008_JavaSE from org.apache.felix.framework (0) javax.naming.ldap,version=0.0.0 from org.apache.felix.framework (0) javax.servlet.http,version=3.0.0 from org.apache.felix.framework (0) org.apache.commons.lang,version=2.6.0 from org.apache.felix.framework (0) org.apache.commons.lang3,version=3.5.0 from org.apache.felix.framework (0) org.apache.commons.lang3.math,version=3.5.0 from org.apache.felix.framework (0) org.apache.commons.logging,version=1.1.1 from org.apache.felix.framework (0) org.apache.struts2,version=2.3.32 from org.apache.felix.framework (0) org.eclipse.gemini.blueprint.context,version=2.0.5.BUILD-atlassian-m002 from org.eclipse.gemini.blueprint.core (9) org.eclipse.gemini.blueprint.service.exporter,version=2.0.5.BUILD-atlassian-m002 from org.eclipse.gemini.blueprint.core (9) org.eclipse.gemini.blueprint.service.exporter.support,version=2.0.5.BUILD-atlassian-m002 from org.eclipse.gemini.blueprint.core (9) org.eclipse.gemini.blueprint.service.importer.support,version=2.0.5.BUILD-atlassian-m002 from org.eclipse.gemini.blueprint.core (9) org.osgi.framework,version=1.7.0 from org.apache.felix.framework (0) org.osgi.framework,version=1.4.1 from org.apache.felix.framework (0) org.springframework.aop.support,version=4.2.5.RELEASE from org.apache.servicemix.bundles.spring-aop (15) org.springframework.beans,version=4.2.5.RELEASE from org.apache.servicemix.bundles.spring-beans (7) org.springframework.beans.factory,version=4.2.5.RELEASE from org.apache.servicemix.bundles.spring-beans (7) org.springframework.beans.factory.annotation,version=4.2.5.RELEASE from org.apache.servicemix.bundles.spring-beans (7) org.springframework.beans.factory.config,version=4.2.5.RELEASE from org.apache.servicemix.bundles.spring-beans (7) org.springframework.beans.factory.parsing,version=4.2.5.RELEASE from org.apache.servicemix.bundles.spring-beans (7) org.springframework.beans.factory.support,version=4.2.5.RELEASE from org.apache.servicemix.bundles.spring-beans (7) org.springframework.beans.factory.xml,version=4.2.5.RELEASE from org.apache.servicemix.bundles.spring-beans (7) org.springframework.context.annotation,version=4.2.5.RELEASE from org.apache.servicemix.bundles.spring-context (2) org.springframework.core.io,version=4.2.5.RELEASE from org.apache.servicemix.bundles.spring-core (11) org.springframework.osgi.context,version=4.4.3 from org.eclipse.gemini.blueprint.extender (8) org.springframework.stereotype,version=4.2.5.RELEASE from org.apache.servicemix.bundles.spring-context (2) org.springframework.util,version=4.2.5.RELEASE from org.apache.servicemix.bundles.spring-core (11) org.w3c.dom,version=0.0.0.1_008_JavaSE from org.apache.felix.framework (0) org.w3c.dom,version=0.0.0 from org.apache.felix.framework (0) |
> | Importing Bundles | com.example.crowd.ldap-crowd-plugin-tests (70)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         |
>
> In my java class I try to inject via Atlassian Spring Scanner the class LdapTemplate
>
> ```
>  @Inject
>  @ComponentImport
>  private LdapTemplate ldapTemplateCustom;
> ```
>
> but throws me the exception ClassNotFoundException

```
[INFO] [talledLocalContainer] Caused by: org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [org.springframework.ldap.core.LdapTemplate] for bean with name 'ldapTemplateCustom' defined in URL [bundle://69.0:0/META-INF/spring/plugin-context.xml]; nested exception is java.lang.ClassNotFoundException: org.springframework.ldap.core.LdapTemplate not found from bundle [com.example.crowd.ldap-crowd-plugin]
[INFO] [talledLocalContainer] at org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1352)
[INFO] [talledLocalContainer] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.determineTargetType(AbstractAutowireCapableBeanFactory.java:628)
[INFO] [talledLocalContainer] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.predictBeanType(AbstractAutowireCapableBeanFactory.java:597)
[INFO] [talledLocalContainer] at org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:1445)
[INFO] [talledLocalContainer] at org.springframework.beans.factory.support.DefaultListableBeanFactory.doGetBeanNamesForType(DefaultListableBeanFactory.java:445)
[INFO] [talledLocalContainer] at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanNamesForType(DefaultListableBeanFactory.java:415)
[INFO] [talledLocalContainer] at org.springframework.beans.factory.BeanFactoryUtils.beanNamesForTypeIncludingAncestors(BeanFactoryUtils.java:220)
[INFO] [talledLocalContainer] at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1177)
[INFO] [talledLocalContainer] at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1116)
[INFO] [talledLocalContainer] at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1014)
[INFO] [talledLocalContainer] at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:813)
[INFO] [talledLocalContainer] at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:741)
[INFO] [talledLocalContainer] ... 22 more
[INFO] [talledLocalContainer] Caused by: java.lang.ClassNotFoundException: org.springframework.ldap.core.LdapTemplate not found from bundle [com.example.crowd.ldap-crowd-plugin]
[INFO] [talledLocalContainer] at org.eclipse.gemini.blueprint.util.BundleDelegatingClassLoader.findClass(BundleDelegatingClassLoader.java:139)
[INFO] [talledLocalContainer] at org.eclipse.gemini.blueprint.util.BundleDelegatingClassLoader.loadClass(BundleDelegatingClassLoader.java:211)
[INFO] [talledLocalContainer] at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
[INFO] [talledLocalContainer] at org.springframework.util.ClassUtils.forName(ClassUtils.java:250)
[INFO] [talledLocalContainer] at org.springframework.beans.factory.support.AbstractBeanDefinition.resolveBeanClass(AbstractBeanDefinition.java:394)
[INFO] [talledLocalContainer] at org.springframework.beans.factory.support.AbstractBeanFactory.doResolveBeanClass(AbstractBeanFactory.java:1397)
[INFO] [talledLocalContainer] at org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1344)
[INFO] [talledLocalContainer] ... 33 more
[INFO] [talledLocalContainer] Caused by: java.lang.ClassNotFoundException: org.springframework.ldap.core.LdapTemplate not found by com.example.crowd.ldap-crowd-plugin [69]
[INFO] [talledLocalContainer] at org.apache.felix.framework.BundleWiringImpl.findClassOrResourceByDelegation(BundleWiringImpl.java:1532)
[INFO] [talledLocalContainer] at org.apache.felix.framework.BundleWiringImpl.access$400(BundleWiringImpl.java:75)
[INFO] [talledLocalContainer] at org.apache.felix.framework.BundleWiringImpl$BundleClassLoader.loadClass(BundleWiringImpl.java:1955)
[INFO] [talledLocalContainer] at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
[INFO] [talledLocalContainer] at org.apache.felix.framework.Felix.loadBundleClass(Felix.java:1844)
[INFO] [talledLocalContainer] at org.apache.felix.framework.BundleImpl.loadClass(BundleImpl.java:937)
[INFO] [talledLocalContainer] at org.eclipse.gemini.blueprint.util.BundleDelegatingClassLoader.findClass(BundleDelegatingClassLoader.java:135)
[INFO] [talledLocalContainer] ... 39 more
```

What have I done wrong? How to import package org.springframework.ldap.core in my bundle?


Answers:
author: Marcin Kempa
body: Hi [@Cristian P?rez Gonz?lez](/t5/user/viewprofilepage/user-id/1178311)

From what I can see you are trying to build some directory integration that would allow you connect to LDAP directory. You are trying to do this via a spring bean that would be started in your plugin context. As currently the way you define a custom directory in Crowd is not via spring bean but through a class name defined in the ui, which class is then instantiated through a Crowd server class loader. This means that you have access to the classes and packages that are used be Crowd itself.

Here you can find out more about implementing a custom directory in Crowd plugin <https://developer.atlassian.com/server/crowd/creating-a-custom-directory-connector/>

In your case since you are trying to connect to LDAP, I think you could try to experiment with the com.atlassian.crowd.directory.SpringLDAPConnector as a base class. Please note that this class might change in future releases of Crowd.

Hope that helps,

Marcin Kempa

comments: author: Cristian P?rez Gonz?lez
body: Hi [@Marcin Kempa](/t5/user/viewprofilepage/user-id/671367),

<br />

I want to avoid having to implement a custom directory because it would imply the following:

1. Create a JAR of the `MyCustomDirectory` and any supporting class files.
2. Shut down Crowd.
3. ++**Place the newly-created JAR from step one in the `CROWD/crowd-webapp/WEB-INF/lib` folder.**++
4. Start Crowd.

So, what I intend to do is:

* Overwrite the addUser action
* Get the instance of the directory that the user has chosen
* Use my own addUser method to add more ldap schemes ("posixAccount", "top", "inetOrgPerson", "shadowAccount"), more attributes.
* Implement a new user interface where the user can choose these ldap schemes

com.atlassian.crowd.directory.SpringLDAPConnector has protected fields and methods so I use the wrapper pattern to try to access certain protected fields. The fact is that I need the ldapTemplate field.

Here I show the class where I intend to implement this (the method where I have problems with osgi is geLdapTemplate)

```
@ExportAsService
@Named("springLdapConnectorCustom")
public class SpringLdapConnectorCustomImpl extends SpringLDAPConnector implements SpringLdapConnectorCustom {

 
 SpringLDAPConnector connector;
 private final InstanceFactory instanceFactory;
 @Inject
 @ComponentImport
 private LdapTemplate ldapTemplateCustom;
 
 
 
 private static final Logger logger = LoggerFactory.getLogger(SpringLdapConnectorCustomImpl.class);
 
 

 @Inject
 public SpringLdapConnectorCustomImpl(LDAPQueryTranslater ldapQueryTranslater,@ComponentImport EventPublisher eventPublisher,
 InstanceFactory instanceFactory) {
 super(ldapQueryTranslater, eventPublisher, instanceFactory);
 this.instanceFactory = instanceFactory;
 }

 @Override
 public String getDescriptiveName() {
 return this.getDescriptiveName();
 }




 @Override
 public boolean isUserDirectGroupMember(String username, String groupName) throws OperationFailedException {
 return this.isUserDirectGroupMember(username, groupName);
 }




 @Override
 public boolean isGroupDirectGroupMember(String childGroup, String parentGroup) throws OperationFailedException {
 return this.isGroupDirectGroupMember(childGroup, parentGroup);
 }




 @Override
 public void addUserToGroup(String username, String groupName) throws GroupNotFoundException, UserNotFoundException,
 ReadOnlyGroupException, OperationFailedException, MembershipAlreadyExistsException {
 this.addUserToGroup(username, groupName);
 }




 @Override
 public void addGroupToGroup(String childGroup, String parentGroup)
 throws GroupNotFoundException, InvalidMembershipException, ReadOnlyGroupException, OperationFailedException,
 MembershipAlreadyExistsException {
 this.addGroupToGroup(childGroup, parentGroup);
 }




 @Override
 public void removeUserFromGroup(String username, String groupName) throws GroupNotFoundException,
 UserNotFoundException, MembershipNotFoundException, ReadOnlyGroupException, OperationFailedException {
 this.removeUserFromGroup(username, groupName);
 }




 @Override
 public void removeGroupFromGroup(String childGroup, String parentGroup) throws GroupNotFoundException,
 InvalidMembershipException, MembershipNotFoundException, ReadOnlyGroupException, OperationFailedException {
 this.removeGroupFromGroup(childGroup, parentGroup);
 }




 @Override
 public Iterable<Membership> getMemberships() throws OperationFailedException {
 return this.getMemberships();
 }

 @Override
 protected <T> Iterable<T> searchGroupRelationshipsWithGroupTypeSpecified(MembershipQuery<T> query)
 throws OperationFailedException {
 return this.searchGroupRelationshipsWithGroupTypeSpecified(query);
 }

 @Override
 protected LDAPCredentialEncoder getCredentialEncoder() {
 return this.getCredentialEncoder();
 }

 
 @Override
 public SpringLdapTemplateWrapper geLdapTemplate() {
 ldapTemplateCustom.setContextSource(connector.getContextSource());
 
 ldapTemplate = new SpringLdapTemplateWrapper(ldapTemplateCustom);

 return this.ldapTemplate;
 }

 public LDAPPropertiesMapper getLdapPropertiesMapper() {
 this.ldapPropertiesMapper = connector.getLdapPropertiesMapper();
 return this.ldapPropertiesMapper;
 }
 

 public Converter getNameConverter() {
 nameConverter = new GenericConverter();
 return nameConverter;
 }
 
 public SearchDN getSearchDN() {
 return connector.getSearchDN();
 }

 public void setConnector(SpringLDAPConnector connector) {
 this.connector = connector;
 
 }
 
 @Override
 public void setDirectoryId(long id) {
 super.setDirectoryId(id);
 }
 @Override
 public LDAPUserWithAttributes addUser(UserTemplateWithAttributes user, PasswordCredential credential)
 throws InvalidUserException, InvalidCredentialException, OperationFailedException {

 Validate.notNull(user, "user cannot be null");
 Validate.notNull(user.getName(), "user.name cannot be null");
 
 
 try {
 SpringLdapTemplateWrapper ldapTemplate = geLdapTemplate();
 LDAPPropertiesMapper ldapPropertiesMapper = getLdapPropertiesMapper();

 Converter nameConverter = getNameConverter();
 SearchDN searchDN = getSearchDN();
 LdapName dn;
 
 dn = nameConverter.getName(ldapPropertiesMapper.getUserNameRdnAttribute(), user.getName(), searchDN.getUser());

 UserLdap person = new UserLdap();
 
 mapUserTemplateToUserLdap(user,person,credential);
 
 DirContextAdapter context = new DirContextAdapter(dn);
 
 String loginShell = "/bin/false";
 String homeDirectory = "/home/";
 String gidNumber = "100";
 
 context.setAttributeValue("gidNumber", gidNumber);
 context.setAttributeValue("homeDirectory", homeDirectory + person.getNombreUsuario());
 context.setAttributeValue("loginShell", loginShell);
 context.setAttributeValue("shadowInactive", "0");
 context.setAttributeValue("sn", person.getApellidos());
 context.setAttributeValue("givenName", person.getNombre());
 context.setAttributeValues("objectclass",
 new String[] { "posixAccount", "top", "inetOrgPerson", "shadowAccount" });
 context.setAttributeValue("uidNumber", "1050");
 context.setAttributeValue("userPassword", person.getPassword());
 context.setAttributeValue("cn", person.getNombreUsuario());
 context.setAttributeValue("displayName", person.getNombre() + person.getApellidos());
 context.setAttributeValue("uid", person.getNombreUsuario());
 context.setAttributeValue("mail", person.getEmail());

 
 
 ldapTemplate.bind(dn, context, null);
 } catch (InvalidNameException e) {
 throw new InvalidUserException(user, e.getMessage(), e);
 }

 
 return null;
 }
 
 private void mapUserTemplateToUserLdap(UserTemplate user, UserLdap person, PasswordCredential credential) {
 user.getDirectoryId();
 person.setEmail(user.getEmailAddress());
 person.setNombre(user.getFirstName());
 person.setApellidos(user.getLastName());
 person.setNombreUsuario(user.getName());
 person.setPassword(credential.getCredential());
 
 }

}
```


author: Marcin Kempa
body: Hi,

So I understand that you would like to be able to extend the user creation so that admins would be able to choose of different schemes for objectclass when they are adding new users into the LDAP directory. The way you would like to achieve this is to provide a plugin (so you do not need to restart Crowd) and add another / replace 'add User' option so that you are able to provide new UI for creating a user where selecting such a scheme would be possible. Is that a fair understanding of the issue you are trying to solve?

Currently I see the problem that Crowd does not export the

```
org.springframework.ldap.core
```

nor it exports the LdapTemplate as spring bean (or maybe you export it by yourself?).

Are you trying to replace the current SpringLDAPConnector with your custom implementation? At the moment I don't see the possibility to do so from plugin.

What you could potentially do is to create a new UI so once users enter all required data you will trigger directoryManage.addUser (as the DirectoryManager is visible to plugins) so it would do what normally Crowd does and later on you could update the user with a scheme that was selected. For the latter you will have to get information about the directory that was selected and get the URL, BaseDN, username and password in order to make a connection to the LDAP for update. In order to use LdapTemplate you will have to include this spring module as part of your plugin.

At the moment I think that there are no better options to provide an attribute mapper to the SpringLDAPConnector so that it would do all the magic in one go.

Hope that helps,

Marcin Kempa


author: Cristian P?rez Gonz?lez
body: What you are saying in the penultimate section is what I am doing now.

Thanks anyway!


Tags: crowd-development

developer