2009-10-13 8 views
10

Estoy tratando de ejecutar un ApacheDS incrustado en mi aplicación. Después de leer http://directory.apache.org/apacheds/1.5/41-embedding-apacheds-into-an-application.html construyo esto:Ejecución de Apache DS incrustado en mi aplicación

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

Pero no puedo conectar con el servidor después de ejecutar. ¿Cuál es el puerto predeterminado? ¿O me estoy perdiendo algo?

Aquí está la solución:

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

Respuesta

4

yo no era capaz de hacer que se ejecute ni con encogerse de , La versión de Kevin o Jörg Pfünder. Recibí constantes NPE desde mi prueba JUnit. He depurado y compilado que todos ellos a una solución de trabajo:.

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

El puerto predeterminado para LDAP es 389.

+0

Pero es el puerto predeterminado para Apach eDS también? ¿ApacheDS está creando un acceso LDAP con el código anterior ...? – cringe

+0

Uso Apache Directory Studio para explorar LDAP, pero no estoy familiarizado con la ejecución de un ApacheDS incrustado. Acabo de responder su pregunta sobre el puerto predeterminado para LDAP. – JuanZe

+0

He descargado el código de muestra y las bibliotecas y lo ejecuté desde Eclipse. El resultado muestra: log4j: WARN No se encontraron appenders para logger (org.apache.server.server.schema.registries.DefaultNormalizerRegistry). log4j: WARN Inicialice el sistema log4j correctamente. entrada Encontrado: ServerEntry dn [n]: dc = Apache, dc = Org objectClass: extensibleObject objectClass: dominio objectClass: top dc: Apache – JuanZe

6

Aquí es una versión abreviada de cómo la usamos:

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

, gracias, eso es todo. Tuve que cambiar algunas líneas para que coincida con mi versión de ApacheDS. Puedes ver el resultado en la pregunta. – cringe

1

Este proyecto me ayudó: Embedded sample project

utilizo esta dependencia en pom.xml:

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

Además, en 2.0 * el directorio de trabajo y otros caminos no están ya definidas en DirectoryService, sino más bien en clase separada InstanceLayout, que se necesario crear una instancia y luego llamar

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