运行嵌入在我的应用程序中的Apache DS

cri*_*nge 12 java apache ldap apacheds

我正在尝试在我的应用程序中运行嵌入式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 );
    }
}
Run Code Online (Sandbox Code Playgroud)

但运行后我无法连接到服务器.什么是默认端口?或者我错过了什么?

这是解决方案:

    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();
Run Code Online (Sandbox Code Playgroud)

Kev*_*vin 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();
Run Code Online (Sandbox Code Playgroud)


Mic*_*l-O 5

我无法让它在 Cringe、Kevin 和 J\xc3\xb6rg Pf\xc3\xbcnder\ 的版本中运行。我的 JUnit 测试中不断收到 NPE。我已经对其进行了调试并将它们全部编译为工作解决方案:

\n\n
public class DirContextSourceAnonAuthTest {\n\n  private static DirectoryService directoryService;\n  private static LdapServer ldapServer;\n\n  @BeforeClass\n  public static void startApacheDs() throws Exception {\n    String buildDirectory = System.getProperty("buildDirectory");\n    File workingDirectory = new File(buildDirectory, "apacheds-work");\n    workingDirectory.mkdir();\n\n    directoryService = new DefaultDirectoryService();\n    directoryService.setWorkingDirectory(workingDirectory);\n\n    SchemaPartition schemaPartition = directoryService.getSchemaService()\n        .getSchemaPartition();\n\n    LdifPartition ldifPartition = new LdifPartition();\n    String workingDirectoryPath = directoryService.getWorkingDirectory()\n        .getPath();\n    ldifPartition.setWorkingDirectory(workingDirectoryPath + "/schema");\n\n    File schemaRepository = new File(workingDirectory, "schema");\n    SchemaLdifExtractor extractor = new DefaultSchemaLdifExtractor(\n        workingDirectory);\n    extractor.extractOrCopy(true);\n\n    schemaPartition.setWrappedPartition(ldifPartition);\n\n    SchemaLoader loader = new LdifSchemaLoader(schemaRepository);\n    SchemaManager schemaManager = new DefaultSchemaManager(loader);\n    directoryService.setSchemaManager(schemaManager);\n\n    schemaManager.loadAllEnabled();\n\n    schemaPartition.setSchemaManager(schemaManager);\n\n    List<Throwable> errors = schemaManager.getErrors();\n\n    if (!errors.isEmpty())\n      throw new Exception("Schema load failed : " + errors);\n\n    JdbmPartition systemPartition = new JdbmPartition();\n    systemPartition.setId("system");\n    systemPartition.setPartitionDir(new File(directoryService\n        .getWorkingDirectory(), "system"));\n    systemPartition.setSuffix(ServerDNConstants.SYSTEM_DN);\n    systemPartition.setSchemaManager(schemaManager);\n    directoryService.setSystemPartition(systemPartition);\n\n    directoryService.setShutdownHookEnabled(false);\n    directoryService.getChangeLog().setEnabled(false);\n\n    ldapServer = new LdapServer();\n    ldapServer.setTransports(new TcpTransport(11389));\n    ldapServer.setDirectoryService(directoryService);\n\n    directoryService.startup();\n    ldapServer.start();\n  }\n\n  @AfterClass\n  public static void stopApacheDs() throws Exception {\n    ldapServer.stop();\n    directoryService.shutdown();\n    directoryService.getWorkingDirectory().delete();\n  }\n\n  @Test\n  public void anonAuth() throws NamingException {\n    DirContextSource.Builder builder = new DirContextSource.Builder(\n        "ldap://localhost:11389");\n    DirContextSource contextSource = builder.build();\n\n    DirContext context = contextSource.getDirContext();\n    assertNotNull(context.getNameInNamespace());\n    context.close();\n  }\n\n}\n
Run Code Online (Sandbox Code Playgroud)\n