2009-10-13 11 views
10

Sto provando a eseguire un ApacheDS incorporato nella mia applicazione. Dopo aver letto http://directory.apache.org/apacheds/1.5/41-embedding-apacheds-into-an-application.html costruisco questo:Esecuzione di Apache DS incorporato nella mia applicazione

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); 
    } 
} 

Ma non riesco a connettersi al server dopo l'esecuzione. Qual è la porta di default? O mi sta sfuggendo qualcosa?

ecco la soluzione:

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(); 

risposta

4

non ero in grado di farlo funzionare né con rabbrividire di , La versione di Kevin's o di Jörg Pfünder. Ricevuto costantemente NPE dal mio test JUnit. Ho debug che e compilato tutti loro ad una soluzione di lavoro:.

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

La porta di default per LDAP è 389.

+0

Ma è la porta predefinita per Apach eDS anche? E ApacheDS sta creando un accesso LDAP con il codice sopra ...? – cringe

+0

Io uso Apache Directory Studio per navigare su LDAP, ma non ho familiarità con l'esecuzione di un ApacheDS incorporato. Ho appena risposto alla tua domanda sulla porta predefinita per LDAP. – JuanZe

+0

Ho scaricato il codice di esempio e le librerie ed eseguito da Eclipse. L'output mostra: log4j: WARN Non è stato possibile trovare appendici per il logger (org.apache.directory.server.schema.registries.DefaultNormalizerRegistry). log4j: WARN Si prega di inizializzare correttamente il sistema log4j. ingresso Trovato: ServerEntry dn [n]: dc = Apache, dc = Org objectClass: extensibleObject objectClass: dominio objectClass: top dc: Apache – JuanZe

6

Ecco una versione abbreviata di come le utilizziamo:

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

Grazie, questo è tutto. Ho dovuto modificare alcune righe per abbinare la mia versione di ApacheDS. Puoi vedere il risultato nella domanda. – cringe

1

Questo progetto mi ha aiutato: Embedded sample project

Io uso questa dipendenza in pom.xml:

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

Inoltre, nella versione 2.0 * dir di lavoro e altri percorsi non sono più definiti in DirectoryService, ma piuttosto in classe separata InstanceLayout, che si bisogno di istanziare e quindi chiamare

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