Apache Directory以编程方式添加分区

时间:2014-02-11 21:08:38

标签: apache ldap apacheds

我正在尝试以编程方式创建分区。我试过在ApacheDS网站(https://directory.apache.org/apacheds/basic-ug/1.4.3-adding-partition.html#adding-a-partition-programmatically)上关注这个例子,但这个例子绝对不正确。

这是我的代码:

LdapConnection connection = new LdapNetworkConnection(host, port);     
connection.bind(admin, password);

connection.loadSchema();
SchemaManager schemaManager = connection.getSchemaManager();
Dn suffixDn = new Dn(schemaManager, "dc=newParition,dc=com");

JdbmPartition newPartition = new JdbmPartition(schemaManager);
newPartition.setId("newParition");
newPartition.setSuffixDn(suffixDn);
newPartition.setCacheSize(1000);
newPartition.setPartitionPath(new URI("file:///var/lib/apacheds-2.0.0-M15/default/partitions/newParition"));

newPartition.addIndex(new JdbmIndex("objectClass", false));
newPartition.addIndex(new JdbmIndex("dc", false));

Entry contextEntry = new DefaultEntry(schemaManager, suffixDn);
contextEntry.put("objectClass", "domain", "top");
contextEntry.put("dc", "newParition");

newPartition.initialize();
newPartition.add(new AddOperationContext(null, contextEntry)); 

当我尝试将contextEntry添加到分区时,我看到以下错误:

org.apache.directory.api.ldap.model.exception.LdapSchemaViolationException: ERR_219 Entry dc=newParition,dc=com contains no entryCsn attribute: Entry …

它看起来甚至看起来没有将分区添加到我的服务器中(当我重新启动我的apacheds服务器时,我没有在Root DSE下看到任何新的namingContexts)。我想我在这里错过了一些步骤,但不确定它们是什么。

1 个答案:

答案 0 :(得分:-1)

来自Apache DS dev邮件列表的建议:

“//始终使用CoreSession的API添加条目”。检查http://apaste.info/KHX以获取有关如何添加分区的几乎完整示例。缺少的类EmbeddedServer如下:

 private static final class EmbeddedServer {
    private DirectoryService directoryService;
    private LdapServer ldapService;

    public EmbeddedServer(final String host, final int port) throws Exception {
        init(host, port);
    }

    private void init(final String host, final int port) throws Exception {

        DefaultDirectoryServiceFactory factory = new DefaultDirectoryServiceFactory();
        factory.init("Test");
        this.directoryService = factory.getDirectoryService();
        this.directoryService.getChangeLog().setEnabled(false);
        this.directoryService.setShutdownHookEnabled(true);
        this.directoryService.setInstanceLayout(new InstanceLayout("/tmp/ldapServer"));

        this.ldapService = new LdapServer();
        this.ldapService.setTransports(new TcpTransport(host, port));
        this.ldapService.setDirectoryService(this.directoryService);
    }

    public void start() throws Exception {

        this.directoryService.startup();
        this.ldapService.start();
    }

    public void stop() throws Exception {

        this.ldapService.stop();
        this.directoryService.shutdown();
    }
}