Java 在我的应用程序中嵌入ApacheDS,它在服务时失败。startup()->;DefaultSchemaService.getSchemaManager()返回null

Java 在我的应用程序中嵌入ApacheDS,它在服务时失败。startup()->;DefaultSchemaService.getSchemaManager()返回null,java,ldap,apacheds,Java,Ldap,Apacheds,我正在尝试在我的应用程序中运行嵌入式ApacheDS。阅读后: 及 使用最新的稳定版本1.5.7, 此简单示例在执行“service.startup();”时失败 也就是说,DefaultSchemaService.getSchemaManager()返回null 源代码: import java.util.Properties; import javax.naming.Context; import javax.naming.NamingEnumeration; import javax.n

我正在尝试在我的应用程序中运行嵌入式ApacheDS。阅读后: 及

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

也就是说,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) ;
        }
    }
}
import java.util.Properties;
导入javax.naming.Context;
导入javax.naming.NamingEnumeration;
导入javax.naming.NamingException;
导入javax.naming.directory.Attributes;
导入javax.naming.directory.DirContext;
导入javax.naming.directory.InitialDirContext;
导入javax.naming.directory.SearchControls;
导入javax.naming.directory.SearchResult;
导入org.apache.directory.server.core.DefaultDirectoryService;
导入org.apache.directory.server.core.partition.partition;
导入org.apache.directory.server.core.partition.impl.btree.jdbm.JdbmPartition;
导入org.apache.directory.server.ldap.LdapServer;
导入org.apache.directory.server.protocol.shared.transport.tcpttransport;
导入org.apache.directory.shared.ldap.entry.ServerEntry;
导入org.apache.directory.shared.ldap.name.DN;
公共类Test3{
公共静态void main(字符串[]args)引发异常{
runServer();
testClient();
}
静态void runServer()引发异常{
DefaultDirectoryService=新的DefaultDirectoryService();
service.getChangeLog().setEnabled(false);
Partition Partition=新的JdbmPartition();
setId(“apache”);
setSuffix(“dc=apache,dc=org”);
addPartition(分区);
LdapServer ldapService=新的LdapServer();
ldapService.setTransports(新的tcpttransport(1400));
ldapService.setDirectoryService(服务);
service.startup();
//如果apache根条目不存在,则注入它
试一试{
service.getAdminSession().lookup(partition.getSuffixDn());
}捕获(异常lnnfe){
DN dnApache=new DN(“dc=Apache,dc=Org”);
ServerEntry entryApache=service.newEntry(dnApache);
add(“objectClass”、“top”、“domain”、“extensibleObject”);
add(“dc”、“Apache”);
service.getAdminSession().add(entryApache);
}
DN dnApache=new DN(“dc=Apache,dc=Org”);
ServerEntry entryApache=service.newEntry(dnApache);
add(“objectClass”、“top”、“domain”、“extensibleObject”);
add(“dc”、“Apache”);
service.getAdminSession().add(entryApache);
ldapService.start();
}
静态void testClient()引发NamingException{
属性p=新属性();
p、 setProperty(Context.INITIAL\u Context\u工厂,“com.sun.jndi.ldap.LdapCtxFactory”);
p、 setProperty(Context.PROVIDER\u URL,“ldap://localhost:1400/");
p、 setProperty(Context.SECURITY_主体,“uid=admin,ou=system”);
p、 setProperty(Context.SECURITY_凭证,“secret”);
p、 setProperty(Context.SECURITY_身份验证,“简单”);
DirContext rootCtx=新的初始DirContext(p);
DirContext ctx=(DirContext)rootCtx.lookup(“dc=apache,dc=org”);
SearchControls sc=新的SearchControls();
sc.setSearchScope(SearchControls.SUBTREE_范围);
NamingEnumeration searchResults=ctx.search(“,”(objectclass=*)”,sc);
while(searchResults.hasMoreElements()){
SearchResult SearchResult=searchResults.next();
Attributes=searchResult.getAttributes();
System.out.println(“searchResult.attributes:+attributes”);
}
}
}
ApacheDS版本1.5.x似乎不向后兼容

在Apacheds1.5.4中,调用“service.startup();”工作正常(但在其他地方失败)


你知道如何让这个示例工作吗?

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

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

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

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

还可以查看pom.xml

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


希望就是帮助

请写一些链接的摘要,这样即使你的链接死了,你的答案仍然有用。谢谢goFirendiAsgard
service.setWorkingDirectory(new File("data"));