Search in sources :

Example 6 with DirectoryService

use of org.apache.directory.server.core.api.DirectoryService in project keycloak by keycloak.

the class LDAPEmbeddedServer method createDirectoryService.

protected DirectoryService createDirectoryService() throws Exception {
    // Parse "keycloak" from "dc=keycloak,dc=org"
    String dcName = baseDN.split(",")[0];
    dcName = dcName.substring(dcName.indexOf("=") + 1);
    if (this.directoryServiceFactory.equals(DSF_INMEMORY)) {
        System.setProperty("apacheds.partition.factory", AvlPartitionFactory.class.getName());
    } else if (this.directoryServiceFactory.equals(DSF_FILE)) {
        System.setProperty("apacheds.partition.factory", JdbmPartitionFactory.class.getName());
    } else {
        throw new IllegalStateException("Unknown value of directoryServiceFactory: " + this.directoryServiceFactory);
    }
    DefaultDirectoryServiceFactory dsf = new DefaultDirectoryServiceFactory();
    DirectoryService service = dsf.getDirectoryService();
    service.setAccessControlEnabled(enableAccessControl);
    service.setAllowAnonymousAccess(enableAnonymousAccess);
    service.getChangeLog().setEnabled(false);
    dsf.init(dcName + "DS");
    Partition partition = dsf.getPartitionFactory().createPartition(service.getSchemaManager(), service.getDnFactory(), dcName, this.baseDN, 1000, new File(service.getInstanceLayout().getPartitionsDirectory(), dcName));
    partition.initialize();
    partition.setSchemaManager(service.getSchemaManager());
    // Inject the partition into the DirectoryService
    service.addPartition(partition);
    // Last, process the context entry
    String entryLdif = "dn: " + baseDN + "\n" + "dc: " + dcName + "\n" + "objectClass: top\n" + "objectClass: domain\n\n";
    importLdifContent(service, entryLdif);
    if (this.directoryServiceFactory.equals(DSF_INMEMORY)) {
        // Find Normalization interceptor in chain and add our range emulated interceptor
        List<Interceptor> interceptors = service.getInterceptors();
        int insertionPosition = -1;
        for (int pos = 0; pos < interceptors.size(); ++pos) {
            Interceptor interceptor = interceptors.get(pos);
            if (interceptor instanceof NormalizationInterceptor) {
                insertionPosition = pos;
            }
        }
        RangedAttributeInterceptor interceptor = new RangedAttributeInterceptor("member", PAGE_SIZE);
        interceptor.init(service);
        interceptors.add(insertionPosition + 1, interceptor);
        service.setInterceptors(interceptors);
    }
    return service;
}
Also used : Partition(org.apache.directory.server.core.api.partition.Partition) AvlPartitionFactory(org.apache.directory.server.core.factory.AvlPartitionFactory) NormalizationInterceptor(org.apache.directory.server.core.normalization.NormalizationInterceptor) DefaultDirectoryServiceFactory(org.apache.directory.server.core.factory.DefaultDirectoryServiceFactory) DirectoryService(org.apache.directory.server.core.api.DirectoryService) File(java.io.File) FindFile(org.keycloak.common.util.FindFile) Interceptor(org.apache.directory.server.core.api.interceptor.Interceptor) NormalizationInterceptor(org.apache.directory.server.core.normalization.NormalizationInterceptor)

Aggregations

DirectoryService (org.apache.directory.server.core.api.DirectoryService)6 DefaultEntry (org.apache.directory.api.ldap.model.entry.DefaultEntry)2 Entry (org.apache.directory.api.ldap.model.entry.Entry)2 Partition (org.apache.directory.server.core.api.partition.Partition)2 File (java.io.File)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Properties (java.util.Properties)1 JarEntry (java.util.jar.JarEntry)1 ZipEntry (java.util.zip.ZipEntry)1 ServletContext (javax.servlet.ServletContext)1 LdapException (org.apache.directory.api.ldap.model.exception.LdapException)1 LdifEntry (org.apache.directory.api.ldap.model.ldif.LdifEntry)1 Dn (org.apache.directory.api.ldap.model.name.Dn)1 SchemaManager (org.apache.directory.api.ldap.model.schema.SchemaManager)1 DefaultSchemaManager (org.apache.directory.api.ldap.schema.manager.impl.DefaultSchemaManager)1 DefaultDirectoryService (org.apache.directory.server.core.DefaultDirectoryService)1 ChangeLog (org.apache.directory.server.core.api.changelog.ChangeLog)1 Interceptor (org.apache.directory.server.core.api.interceptor.Interceptor)1 SchemaPartition (org.apache.directory.server.core.api.schema.SchemaPartition)1