【问题标题】:Running Apache DS embedded in my application运行嵌入在我的应用程序中的 Apache DS
【发布时间】:2009-10-13 13:24:48
【问题描述】:

我正在尝试在我的应用程序中运行嵌入式 ApacheDS。在阅读http://directory.apache.org/apacheds/1.5/41-embedding-apacheds-into-an-application.html 之后,我构建了这个:

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

【问题讨论】:

    标签: java apache ldap apacheds


    【解决方案1】:

    这是我们如何使用它的缩写版本:

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

    【讨论】:

    • 谢谢,就是这样。我不得不更改一些行以匹配我的 ApacheDS 版本。你可以在问题中看到结果。
    【解决方案2】:
    【解决方案3】:

    无论是 cringe、Kevin 还是 Jörg Pfü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();
      }
    
    }
    

    【讨论】:

      【解决方案4】:

      LDAP 的默认端口是 389。

      【讨论】:

      • 但它也是 ApacheDS 的默认端口吗? ApacheDS 是否使用上述代码创建 LDAP 访问...?
      • 我使用 Apache Directory Studio 浏览 LDAP,但我不熟悉运行嵌入式 ApacheDS。刚刚回答了关于 LDAP 默认端口的问题。
      • 我下载了示例代码和库并从 Eclipse 运行它。输出显示: log4j:WARN No appenders could be found for logger (org.apache.directory.server.schema.registries.DefaultNormalizerRegistry)。 log4j:WARN 请正确初始化 log4j 系统。找到条目:ServerEntry dn[n]: dc=Apache,dc=Org objectClass: extensibleObject objectClass: domain objectClass: top dc: Apache
      【解决方案5】:

      从 ApacheDS 1.5.7 开始,您将收到 NullpointerException。请使用本教程 http://svn.apache.org/repos/asf/directory/documentation/samples/trunk/embedded-sample

      【讨论】:

        【解决方案6】:

        这个项目帮助了我: 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>
        

        【讨论】:

          【解决方案7】:

          此外,在 2.0.* 中,工作目录和其他路径不再在 DirectoryService 中定义,而是在单独的类 InstanceLayout 中定义,您需要对其进行实例化然后调用

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

          【讨论】:

            猜你喜欢
            • 2023-02-01
            • 2018-09-09
            • 2014-08-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2013-06-15
            • 1970-01-01
            相关资源
            最近更新 更多