2009-10-13 84 views
10

我試圖在我的應用程序中運行嵌入式ApacheDS。在閱讀http://directory.apache.org/apacheds/1.5/41-embedding-apacheds-into-an-application.html我建立這個:運行嵌入在我的應用程序中的Apache DS

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

但我運行它後無法連接到服務器。什麼是默認端口?或者我錯過了什麼?

這裏是解決方案:

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

回答

4

我無法使其運行既不畏縮的,凱文的也不是JörgPfünder的版本。從我的JUnit測試中不斷收到NPE。我已經調試這一點,編譯他們都工作的解決方案:

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

LDAP的默認端口爲389

+0

但它是爲APACH的默認端口eDS呢?並且是使用上面的代碼創建LDAP訪問的ApacheDS ...? – cringe

+0

我使用Apache Directory Studio瀏覽LDAP,但我不熟悉運行嵌入式ApacheDS。剛剛回答了有關LDAP的默認端口的問題。 – JuanZe

+0

我下載了示例代碼和庫,並從Eclipse運行它。輸出顯示: log4j:警告沒有appender可以找到記錄器(org.apache.directory.server.schema.registries.DefaultNormalizerRegistry)。 log4j:WARN請正確初始化log4j系統。 實測值條目:ServerEntry DN [N]:DC =阿帕奇,DC =組織 的objectClass:extensibleObject的 的objectClass:域 objectClass的:頂 DC:阿帕奇 – JuanZe

6

這裏是我們如何使用它的一個簡化版本:

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

謝謝,僅此而已。我不得不改變一些行來匹配我的ApacheDS版本。你可以在問題中看到結果。 – cringe

1

這個項目幫助我: Embedded sample project

我用這種依賴性在pom.xml中:

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

此外,2.0 *工作目錄和其他路徑不DirectoryService中定義了,而是在單獨的類InstanceLayout,你需要實例,然後調用

InstanceLayout il = new InstanceLayout(BASE_PATH); 
directotyService.setInstanceLayout(il); 
相關問題