2009-10-13 14 views
10

J'essaie d'exécuter un ApacheDS intégré dans mon application. Après avoir lu http://directory.apache.org/apacheds/1.5/41-embedding-apacheds-into-an-application.html Je construis ceci:Exécution d'Apache DS intégré dans mon application

public void startDirectoryService() throws Exception { 
    service = new DefaultDirectoryService(); 
    service.getChangeLog().setEnabled(false); 

    Partition apachePartition = addPartition("apache", "dc=apache,dc=org"); 
    addIndex(apachePartition, "objectClass", "ou", "uid"); 

    service.startup(); 

    // Inject the apache root entry if it does not already exist 
    try 
    { 
     service.getAdminSession().lookup(apachePartition.getSuffixDn()); 
    } 
    catch (LdapNameNotFoundException lnnfe) 
    { 
     LdapDN dnApache = new LdapDN("dc=Apache,dc=Org"); 
     ServerEntry entryApache = service.newEntry(dnApache); 
     entryApache.add("objectClass", "top", "domain", "extensibleObject"); 
     entryApache.add("dc", "Apache"); 
     service.getAdminSession().add(entryApache); 
    } 
} 

Mais je ne peux pas se connecter au serveur après l'exécuter. Quel est le port par défaut? Ou est-ce que je manque quelque chose?

Voici la solution:

service = new DefaultDirectoryService(); 
    service.getChangeLog().setEnabled(false); 

    Partition apachePartition = addPartition("apache", "dc=apache,dc=org"); 

    LdapServer ldapService = new LdapServer(); 
    ldapService.setTransports(new TcpTransport(389)); 
    ldapService.setDirectoryService(service); 

    service.startup(); 
    ldapService.start(); 

Répondre

4

je n'étais pas en mesure de le faire tourner ni avec un mouvement de recul de , La version de Kevin ou de Jörg Pfünder. Reçu constamment NPE à partir de mon test JUnit. Je déboguée que et compilé tous à une solution de travail.

public class DirContextSourceAnonAuthTest { 

    private static DirectoryService directoryService; 
    private static LdapServer ldapServer; 

    @BeforeClass 
    public static void startApacheDs() throws Exception { 
    String buildDirectory = System.getProperty("buildDirectory"); 
    File workingDirectory = new File(buildDirectory, "apacheds-work"); 
    workingDirectory.mkdir(); 

    directoryService = new DefaultDirectoryService(); 
    directoryService.setWorkingDirectory(workingDirectory); 

    SchemaPartition schemaPartition = directoryService.getSchemaService() 
     .getSchemaPartition(); 

    LdifPartition ldifPartition = new LdifPartition(); 
    String workingDirectoryPath = directoryService.getWorkingDirectory() 
     .getPath(); 
    ldifPartition.setWorkingDirectory(workingDirectoryPath + "/schema"); 

    File schemaRepository = new File(workingDirectory, "schema"); 
    SchemaLdifExtractor extractor = new DefaultSchemaLdifExtractor(
     workingDirectory); 
    extractor.extractOrCopy(true); 

    schemaPartition.setWrappedPartition(ldifPartition); 

    SchemaLoader loader = new LdifSchemaLoader(schemaRepository); 
    SchemaManager schemaManager = new DefaultSchemaManager(loader); 
    directoryService.setSchemaManager(schemaManager); 

    schemaManager.loadAllEnabled(); 

    schemaPartition.setSchemaManager(schemaManager); 

    List<Throwable> errors = schemaManager.getErrors(); 

    if (!errors.isEmpty()) 
     throw new Exception("Schema load failed : " + errors); 

    JdbmPartition systemPartition = new JdbmPartition(); 
    systemPartition.setId("system"); 
    systemPartition.setPartitionDir(new File(directoryService 
     .getWorkingDirectory(), "system")); 
    systemPartition.setSuffix(ServerDNConstants.SYSTEM_DN); 
    systemPartition.setSchemaManager(schemaManager); 
    directoryService.setSystemPartition(systemPartition); 

    directoryService.setShutdownHookEnabled(false); 
    directoryService.getChangeLog().setEnabled(false); 

    ldapServer = new LdapServer(); 
    ldapServer.setTransports(new TcpTransport(11389)); 
    ldapServer.setDirectoryService(directoryService); 

    directoryService.startup(); 
    ldapServer.start(); 
    } 

    @AfterClass 
    public static void stopApacheDs() throws Exception { 
    ldapServer.stop(); 
    directoryService.shutdown(); 
    directoryService.getWorkingDirectory().delete(); 
    } 

    @Test 
    public void anonAuth() throws NamingException { 
    DirContextSource.Builder builder = new DirContextSource.Builder(
     "ldap://localhost:11389"); 
    DirContextSource contextSource = builder.build(); 

    DirContext context = contextSource.getDirContext(); 
    assertNotNull(context.getNameInNamespace()); 
    context.close(); 
    } 

} 
1

Le port par défaut pour LDAP est 389.

+0

Mais est-ce le port par défaut pour Apach eDS aussi? Et ApacheDS crée-t-il un accès LDAP avec le code ci-dessus ...? – cringe

+0

J'utilise Apache Directory Studio pour parcourir LDAP, mais je ne suis pas familier avec l'exécution d'un ApacheDS intégré. Vous venez de répondre à votre question sur le port par défaut de LDAP. – JuanZe

+0

J'ai téléchargé l'exemple de code et les bibliothèques et je l'ai exécuté à partir d'Eclipse. La sortie affiche: log4j: WARN Aucun appender n'a pu être trouvé pour logger (org.apache.directory.server.schema.registries.DefaultNormalizerRegistry). log4j: WARN Veuillez initialiser correctement le système log4j. entrée Trouvé: ServerEntry dn [n]: dc = Apache, dc = org objectClass: extensibleObject objectClass: Domaine objectClass: top dc: Apache – JuanZe

6

Voici une version abrégée de la façon dont nous utilisons:

File workingDirectory = ...; 

Partition partition = new JdbmPartition(); 
partition.setId(...); 
partition.setSuffix(...); 

DirectoryService directoryService = new DefaultDirectoryService(); 
directoryService.setWorkingDirectory(workingDirectory); 
directoryService.addPartition(partition); 

LdapService ldapService = new LdapService(); 
ldapService.setSocketAcceptor(new SocketAcceptor(null)); 
ldapService.setIpPort(...); 
ldapService.setDirectoryService(directoryService); 

directoryService.startup(); 
ldapService.start(); 
+0

Merci, c'est tout. J'ai dû changer certaines lignes pour correspondre à ma version d'ApacheDS. Vous pouvez voir le résultat dans la question. – cringe

1

Ce projet m'a aidé: Embedded sample project

J'utilise cette dépendance dans pom.xml:

<dependency> 
    <groupId>org.apache.directory.server</groupId> 
    <artifactId>apacheds-server-integ</artifactId> 
    <version>1.5.7</version> 
    <scope>test</scope> 
</dependency> 
0

De plus, dans la version 2.0 * le répertoire et d'autres chemins de travail ne sont pas définis plus en DirectoryService, mais plutôt dans la classe séparée InstanceLayout, que vous besoin d'instancier puis appelez

InstanceLayout il = new InstanceLayout(BASE_PATH); 
directotyService.setInstanceLayout(il);