2009-10-13 12 views
10

Próbuję uruchomić osadzone ApacheDS w mojej aplikacji. Po przeczytaniu http://directory.apache.org/apacheds/1.5/41-embedding-apacheds-into-an-application.html buduję to:Uruchamianie Apache DS osadzone w mojej aplikacji

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

Ale nie mogę połączyć się z serwerem po uruchomieniu go. Jaki jest domyślny port? Czy może czegoś brakuje?

Oto rozwiązanie:

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

Odpowiedz

4

I nie był w stanie uczynić go uruchomić ani z warować na , Wersja Kevina i Jörga Pfündera. Otrzymałem stale NPE z mojego testu JUnit. Mam debugowany że skompilowany i wszystkie z nich do roztworu roboczego.

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

Domyślnym portem LDAP jest 389.

+0

Ale jest to domyślny port dla Apach eDS też? A czy ApacheDS tworzy dostęp do LDAP z powyższym kodem ...? – cringe

+0

Używam Apache Directory Studio do przeglądania LDAP, ale nie jestem zaznajomiony z uruchamianiem wbudowanego ApacheDS. Właśnie odpowiedziałem na twoje pytanie o domyślny port dla LDAP. – JuanZe

+0

Pobrałem próbny kod i biblioteki i uruchomiłem je z Eclipse. Dane wyjściowe pokazują: log4j: WARN Nie znaleziono aplikacji dla rejestratora (org.apache.directory.server.schema.registries.DefaultNormalizerRegistry). log4j: WARN Proszę zainicjować system log4j poprawnie. Znaleziono wejście: ServerEntry DN [n] dc = Apache dc = Org objectClass: extensibleObject objectClass: domena objectClass: góra stałego: Apache – JuanZe

6

Oto skrócona wersja jak używamy go:

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

dzięki, że to. Musiałem zmienić kilka linii, aby pasowały do ​​mojej wersji ApacheDS. Możesz zobaczyć wynik w pytaniu. – cringe

1

Ten projekt pomógł mi: Embedded sample project

używam tej zależności w pom.xml:

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

Ponadto w 2,0 * dir pracę i inne ścieżki nie są już zdefiniowane w DirectoryService, ale raczej w osobnej klasy InstanceLayout, który cię trzeba instancji, a następnie zadzwonić

InstanceLayout il = new InstanceLayout(BASE_PATH); 
directotyService.setInstanceLayout(il); 
Powiązane problemy