【问题标题】:embedded ApacheDS in my application, it fails on service.startup() -> DefaultSchemaService.getSchemaManager() returns null在我的应用程序中嵌入 ApacheDS,它在 service.startup() -> DefaultSchemaService.getSchemaManager() 上失败,返回 null
【发布时间】:2012-04-27 15:03:33
【问题描述】:

我正在尝试在我的应用程序中运行嵌入式 ApacheDS。阅读后:Running Apache DS embedded in my applicationhttp://directory.apache.org/apacheds/1.5/41-embedding-apacheds-into-an-application.html

使用最新的稳定版本 1.5.7, 这个简单的例子在执行“service.startup();”时失败了

Exception in thread "main" java.lang.NullPointerException
    at org.apache.directory.server.core.schema.DefaultSchemaService.initialize(DefaultSchemaService.java:380)
    at org.apache.directory.server.core.DefaultDirectoryService.initialize(DefaultDirectoryService.java:1425)
    at org.apache.directory.server.core.DefaultDirectoryService.startup(DefaultDirectoryService.java:907)
    at Test3.runServer(Test3.java:41)
    at Test3.main(Test3.java:24)

即 DefaultSchemaService.getSchemaManager() 返回 null。

源代码:

import java.util.Properties;

import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.Attributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;

import org.apache.directory.server.core.DefaultDirectoryService;
import org.apache.directory.server.core.partition.Partition;
import org.apache.directory.server.core.partition.impl.btree.jdbm.JdbmPartition;
import org.apache.directory.server.ldap.LdapServer;
import org.apache.directory.server.protocol.shared.transport.TcpTransport;
import org.apache.directory.shared.ldap.entry.ServerEntry;
import org.apache.directory.shared.ldap.name.DN;


public class Test3 {

    public static void main(String[] args) throws Exception {
        runServer();
        testClient();
    }

    static void runServer() throws Exception {
        DefaultDirectoryService service = new DefaultDirectoryService();
        service.getChangeLog().setEnabled(false);

        Partition partition = new JdbmPartition();
        partition.setId("apache");
        partition.setSuffix("dc=apache,dc=org");
        service.addPartition(partition);

        LdapServer ldapService = new LdapServer();
        ldapService.setTransports(new TcpTransport(1400));
        ldapService.setDirectoryService(service);

        service.startup();

        // Inject the apache root entry if it does not already exist
        try {
            service.getAdminSession().lookup(partition.getSuffixDn());
        } catch (Exception lnnfe) {
            DN dnApache = new DN("dc=Apache,dc=Org");
            ServerEntry entryApache = service.newEntry(dnApache);
            entryApache.add("objectClass", "top", "domain", "extensibleObject");
            entryApache.add("dc", "Apache");
            service.getAdminSession().add(entryApache);
        }

        DN dnApache = new DN("dc=Apache,dc=Org");
        ServerEntry entryApache = service.newEntry(dnApache);
        entryApache.add("objectClass", "top", "domain", "extensibleObject");
        entryApache.add("dc", "Apache");
        service.getAdminSession().add(entryApache);

        ldapService.start();
    }


    static void testClient() throws NamingException {
        Properties p = new Properties();
        p.setProperty(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
        p.setProperty(Context.PROVIDER_URL, "ldap://localhost:1400/");
        p.setProperty(Context.SECURITY_PRINCIPAL, "uid=admin,ou=system");
        p.setProperty(Context.SECURITY_CREDENTIALS, "secret");
        p.setProperty(Context.SECURITY_AUTHENTICATION, "simple");

        DirContext rootCtx = new InitialDirContext(p);
        DirContext ctx = (DirContext) rootCtx.lookup("dc=apache,dc=org");
        SearchControls sc = new SearchControls();
        sc.setSearchScope(SearchControls.SUBTREE_SCOPE);

        NamingEnumeration<SearchResult> searchResults = ctx.search("", "(objectclass=*)", sc);

        while (searchResults.hasMoreElements()) {
            SearchResult searchResult = searchResults.next();
            Attributes attributes = searchResult.getAttributes();
            System.out.println("searchResult.attributes: " + attributes) ;
        }
    }
}

ApacheDS 1.5.x 版本似乎不向后兼容。

在 ApacheDS 1.5.4 中调用“service.startup();”工作正常(但在其他地方失败)。

知道如何使这个例子工作吗?

【问题讨论】:

    标签: java ldap apacheds


    【解决方案1】:

    这个版本的 ApacheDS 需要明确定义工作目录:

    service.setWorkingDirectory(new File("data"));
    

    【讨论】:

      【解决方案2】:

      我最近遇到了类似的问题,经过长时间的谷歌搜索,终于找到了一些有用的东西

      使用 1.5.7 的嵌入式 Apache 目录示例

      http://svn.apache.org/repos/asf/directory/documentation/samples/trunk/embedded-sample/src/main/java/org/apache/directory/seserver/EmbeddedADSVer157.java

      也看看 pom.xml

      使用 1.5.7 的嵌入式 Apache 目录的 Pom.xml 示例

      http://svn.apache.org/repos/asf/directory/documentation/samples/trunk/embedded-sample/pom.xml

      希望是帮助!

      【讨论】:

      • 请从链接中写一些摘要,这样即使链接失效,您的答案仍然有用。
      • 感谢 goFirendiAsgard
      猜你喜欢
      • 1970-01-01
      • 2011-06-15
      • 1970-01-01
      • 2021-05-24
      • 1970-01-01
      • 1970-01-01
      • 2023-01-30
      • 2014-02-14
      • 1970-01-01
      相关资源
      最近更新 更多