本文介绍了运行Apache DS嵌在我的应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的应用程序中运行的嵌入式ApacheDS中。看完http://directory.apache.org/apacheds/1.5/41-embedding-apacheds-into-an-application.html我建立这个:

 公共无效startDirectoryService()抛出异常{
    服务=新DefaultDirectoryService();
    service.getChangeLog()的setEnabled(假)。    分区apachePartition = addPartition(阿帕奇,DC =阿帕奇,DC =组织);
    addIndex(apachePartition,对象类,OU,UID);    service.startup();    如果它不存在//注入apache的根条目
    尝试
    {
        。service.getAdminSession()查找(apachePartition.getSuffixDn());
    }
    赶上(LdapNameNotFoundException lnnfe)
    {
        LDAPDN dnApache =新LDAPDN(DC =阿帕奇,DC =组织);
        ServerEntry entryApache = service.newEntry(dnApache);
        entryApache.add(对象类,顶,域,extensibleObject的);
        entryApache.add(DC,阿帕奇);
        。service.getAdminSession()加(entryApache);
    }
}

但我不能在运行后连接到服务器。什么是默认端口?还是我失去了一些东西?

这里是解决方案:

  =服务新DefaultDirectoryService();
    service.getChangeLog()的setEnabled(假)。    分区apachePartition = addPartition(阿帕奇,DC =阿帕奇,DC =组织);    LdapServer ldapService =新LdapServer();
    ldapService.setTransports(新TcpTransport(389));
    ldapService.setDirectoryService(服务);    service.startup();
    ldapService.start();


解决方案

我无法使其运行既不畏缩的,凯文也不约尔格Pfünder的版本。不断收到的NPE从我的JUnit测试中。我已经调试了和编译他们都工作的解决方案:

 公共类DirContextSourceAnonAuthTest {  私有静态DirectoryService中DirectoryService中;
  私有静态LdapServer ldapServer;  @BeforeClass
  公共静态无效startApacheDs()抛出异常{
    串buildDirectory = System.getProperty(buildDirectory);
    文件工作目录=新的文件(buildDirectoryApacheDS的工作);
    workingDirectory.mkdir();    DirectoryService中=新DefaultDirectoryService();
    directoryService.setWorkingDirectory(工作目录);    SchemaPartition schemaPartition = directoryService.getSchemaService()
        .getSchemaPartition();    LdifPartition ldifPartition =新LdifPartition();
    字符串workingDirectoryPath = directoryService.getWorkingDirectory()
        .getPath();
    ldifPartition.setWorkingDirectory(workingDirectoryPath +/模式);    文件schemaRepository =新的文件(工作目录,纲目);
    SchemaLdifExtractor提取=新DefaultSchemaLdifExtractor(
        工作目录);
    extractor.extractOrCopy(真);    schemaPartition.setWrappedPartition(ldifPartition);    SchemaLoader装载机=新LdifSchemaLoader(schemaRepository);
    SchemaManager schemaManager =新DefaultSchemaManager(装载机);
    directoryService.setSchemaManager(schemaManager);    schemaManager.loadAllEnabled();    schemaPartition.setSchemaManager(schemaManager);    清单<&Throwable的GT;误差= schemaManager.getErrors();    如果(!errors.isEmpty())
      抛出新的异常(架构加载失败:+错误);    JdbmPartition SYSTEMPARTITION =新JdbmPartition();
    systemPartition.setId(系统);
    systemPartition.setPartitionDir(新文件(DirectoryService中
        .getWorkingDirectory(),系统));
    systemPartition.setSuffix(ServerDNConstants.SYSTEM_DN);
    systemPartition.setSchemaManager(schemaManager);
    directoryService.setSystemPartition(SYSTEMPARTITION);    directoryService.setShutdownHookEnabled(假);
    directoryService.getChangeLog()的setEnabled(假)。    ldapServer =新LdapServer();
    ldapServer.setTransports(新TcpTransport(11389));
    ldapServer.setDirectoryService(DirectoryService中);    directoryService.startup();
    ldapServer.start();
  }  @下课以后
  公共静态无效stopApacheDs()抛出异常{
    ldapServer.stop();
    directoryService.shutdown();
    directoryService.getWorkingDirectory()删除()。
  }  @测试
  公共无效anonAuth()抛出NamingException的{
    DirContextSource.Builder建设者=新DirContextSource.Builder(
        LDAP://本地主机:11389);
    DirContextSource好的ContextSource = builder.build();    DirContext的环境= contextSource.getDirContext();
    assertNotNull(context.getNameInNamespace());
    context.close();
  }}

I'm trying to run an embedded ApacheDS in my application. After reading http://directory.apache.org/apacheds/1.5/41-embedding-apacheds-into-an-application.html I build this:

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

But I can't connect to the server after running it. What is the default port? Or am I missing something?

Here is the solution:

    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();
解决方案

I wasn't able to make it run neither with cringe's, Kevin's nor Jörg Pfünder's version. Received constantly NPEs from within my JUnit test. I have debugged that and compiled all of them to a working solution:

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

}

这篇关于运行Apache DS嵌在我的应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-08 07:20