directory-api mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Kiran Ayyagari <kayyag...@apache.org>
Subject Re: How to add partition permanently to config in ApacheDS 2.0?
Date Sat, 16 Aug 2014 11:15:13 GMT
On Fri, Aug 15, 2014 at 7:30 PM, Sergio Montoro <sergiom@knowgate.com>
wrote:

> Hi,
> I have been trying to create programmatically a new partition on Apache DS
> 2.0.
> The partition seems to be successfully with the code below.
> But when I shutdown and startup the service again it is no longer there.
> I think that I need to add the new partition to config somehow but I do
> not know how.
> Can anybody provide advice on this?
> How can I create a new partition an add it permanently to a service from
> Java code?
>
> in embedded mode the partition should always be initialized through code.
The partition files will be
created only if the partition doesn't exist, otherwise the existing
partition data will be loaded.

You cannot see the partition without initializing even if the data files
exist.

>
>
> import java.io.File;
> import java.util.List;
> import java.util.ArrayList;
> import java.util.Properties;
>
> import org.apache.directory.api.ldap.model.name.Dn;
> import org.apache.directory.api.ldap.model.entry.Entry;
> import org.apache.directory.api.ldap.model.entry.Attribute;
> import org.apache.directory.api.ldap.model.entry.DefaultEntry;
> import org.apache.directory.api.ldap.model.entry.Modification;
> import org.apache.directory.api.ldap.model.entry.DefaultModification;
> import org.apache.directory.api.ldap.model.entry.ModificationOperation;
> import org.apache.directory.api.ldap.model.schema.SchemaManager;
> import org.apache.directory.api.ldap.model.schema.registries.SchemaLoader;
> import org.apache.directory.api.ldap.model.exception.LdapException;
> import org.apache.directory.api.ldap.schemaloader.LdifSchemaLoader;
> import org.apache.directory.api.ldap.schemamanager.impl.
> DefaultSchemaManager;
> import org.apache.directory.api.util.exception.Exceptions;
> import org.apache.directory.server.i18n.I18n;
> import org.apache.directory.server.core.api.DnFactory;
> import org.apache.directory.server.core.DefaultDirectoryService;
> import org.apache.directory.server.core.api.CacheService;
> import org.apache.directory.server.core.api.InstanceLayout;
> import org.apache.directory.server.core.api.DirectoryService;
> import org.apache.directory.server.core.api.schema.SchemaPartition;
> import org.apache.directory.server.core.api.partition.Partition;
> import org.apache.directory.server.core.api.interceptor.context.
> AddOperationContext;
> import org.apache.directory.server.core.api.interceptor.context.
> ModifyOperationContext;
> import org.apache.directory.server.core.shared.DefaultDnFactory;
> import org.apache.directory.server.core.factory.JdbmPartitionFactory;
> import org.apache.directory.server.core.partition.ldif.LdifPartition;
> import org.apache.directory.server.core.partition.ldif.
> SingleFileLdifPartition;
> import org.apache.directory.server.config.LdifConfigExtractor;
> import org.apache.directory.server.constants.ServerDNConstants;
> import org.apache.directory.server.constants.SystemSchemaConstants;
>
> import org.apache.directory.server.ldap.LdapServer;
> import org.apache.directory.server.protocol.shared.transport.TcpTransport;
>
> import com.knowgate.directory.LDAPApache;
>
> public class ApacheDSPartitionerTest {
>
>     private static void printUsage() {
>         System.out.println("");
>         System.out.println("Create Apache Directory partition");
>         System.out.println("Usage:\n");
>         System.out.println("ApacheDSPartitionerTest working_directory
> partition_name");
>         System.out.println("Example:\n");
>         System.out.println("ApacheDSPartitionerTest
> /var/lib/apacheds-2.0.0-M17 testPartition");
>     }
>
>     public static DirectoryService createDirectoryService(File workDir)
> throws Exception {
>             DirectoryService serv = new DefaultDirectoryService();
>             serv.setAllowAnonymousAccess( true );
>             serv.setInstanceLayout(new InstanceLayout(workDir));
>
>             CacheService cache = new CacheService();
>             cache.initialize( serv.getInstanceLayout() );
>
>             // Init schema and system LdifPartitions
>             File schDir = new File( serv.getInstanceLayout().getPartitionsDirectory(),
> "schema" );
>              SchemaLoader schLdr = new LdifSchemaLoader( schDir );
>             SchemaManager schMan = new DefaultSchemaManager( schLdr );
>             schMan.loadAllEnabled();
>
>             List<Throwable> errors = schMan.getErrors();
>
>             if ( errors.size() != 0 )
>                     throw new Exception( I18n.err( I18n.ERR_317,
> Exceptions.printErrors( errors ) ) );
>
>             serv.setSchemaManager(schMan);
>             serv.setDnFactory(new DefaultDnFactory(serv.getSchemaManager(),
> cache.getCache("dnCache")));
>
>             LdifPartition schLdifPart = new LdifPartition(
> serv.getSchemaManager(), serv.getDnFactory() );
>             schLdifPart.setPartitionPath( schDir.toURI() );
>             SchemaPartition schPart = new SchemaPartition(serv.
> getSchemaManager());
>             schPart.setWrappedPartition( schLdifPart );
>             serv.setSchemaPartition( schPart );
>
>             LdifPartition sysPart = new LdifPartition(
> serv.getSchemaManager(), serv.getDnFactory() );
>             sysPart.setId( SystemSchemaConstants.SCHEMA_NAME );
>             sysPart.setPartitionPath( new File(serv.getInstanceLayout().getPartitionsDirectory(),
> SystemSchemaConstants.SCHEMA_NAME).toURI() );
>             sysPart.setSuffixDn( serv.getDnFactory().create(ServerDNConstants.SYSTEM_DN)
> );
>             serv.setSystemPartition( sysPart );
>
>             return serv;
>     }
>
>     public static void createContextEntry(DirectoryService serv,
> Partition part, String name, String suffix) throws LdapException {
>         if ( !serv.getAdminSession().exists( part.getSuffixDn() ) ) {
>             System.out.println("Creating context entry "+suffix);
>             Dn contextDn = new Dn( suffix );
>             Entry contextEntry = serv.newEntry( contextDn );
>             contextEntry.add( "objectClass", "top", "organizationalUnit" );
>             contextEntry.add( "ou", name );
>             serv.getAdminSession().add( contextEntry );
>         }
>     }
>
>     public static void createUsersEntry(DirectoryService serv, String
> suffix) throws LdapException {
>             System.out.println("Creating users entry dc=users,"+suffix);
>             Dn usersDn = new Dn( "dc=users,"+suffix );
>             Entry usersEntry = serv.newEntry( usersDn );
>             usersEntry.add( "objectClass", "domain" );
>             usersEntry.add( "dc", "users" );
>             serv.getAdminSession().add( usersEntry );
>     }
>
>     public static Partition createJdbmPartition(DirectoryService serv,
> File workDir, String name, String suffix) throws Exception {
>         JdbmPartitionFactory fact = new JdbmPartitionFactory();
>         Partition part = fact.createPartition(serv.getSchemaManager(),
> serv.getDnFactory(), name, suffix, 1000,
>                                               new
> File(workDir.getAbsolutePath()+File.separator+"partitions"+
> File.separator+name));
>         fact.addIndex( part, "objectClass", 100 );
>         fact.addIndex( part, "ou", 100 );
>         part.initialize();
>         serv.addPartition(part);
>
>         return part;
>     }
>
>     public static void main(String[] argv) throws Exception {
>
>         if (argv.length!=2) {
>
>             printUsage();
>
>         } else {
>
>             File workDir = new File(argv[0]);
>             if (!workDir.exists()) {
>                     System.out.println("Working directory " + argv[0] + "
> does not exist");
>                     return;
>             }
>
>             DirectoryService serv = createDirectoryService(workDir);
>
>             boolean alreadyExists = false;
>             for (Partition part : serv.getPartitions()) {
>                 System.out.println("Loaded partition "+part.getId());
>               alreadyExists = alreadyExists ||
> part.getId().equals(argv[1]);
>             }
>
>             if (alreadyExists) {
>
>                 System.out.println("Partition "+argv[1]+" already exists");
>
>             } else {
>                 final String partitionName = argv[1];
>                 final String suffix = "ou=" + partitionName;
>
>                 if (!serv.isStarted()) serv.startup();
>
>                 Partition part = createJdbmPartition(serv, workDir,
> partitionName, suffix);
>
>                 createContextEntry(serv, part, partitionName, suffix);
>                 createUsersEntry(serv, suffix);
>
>                 for (Partition part2 : serv.getPartitions()) {
>                     System.out.println("Got partition "+part2.getId());
>                 }
>
>                 Entry result = serv.getAdminSession().lookup( new Dn(
> suffix ) );
>                 if (result==null)
>                     System.out.println("Could not create context entry
> "+suffix);
>                 else
>                     System.out.println("Created context entry "+suffix);
>                 result = serv.getAdminSession().lookup( new Dn(
> "dc=users,"+suffix ) );
>                 if (result==null)
>                     System.out.println("Could not create users domain
> dc=user,"+suffix);
>                 else
>                     System.out.println("Created users domain
> dc=users,"+suffix);
>
>                 LdapServer ldapSrv = new LdapServer();
>                 ldapSrv.setDirectoryService( serv );
>
>                 // Set LDAP port to 10389
>                 int port = 10389;
>                 TcpTransport ldapTransport = new TcpTransport( port );
>                 ldapSrv.setTransports( ldapTransport );
>
>                 ldapSrv.start();
>
>                 Properties props = new Properties();
>                 props.put( "ldapconnect", "ldap://localhost:" +
> String.valueOf(port) + "/" + suffix );
>                 LDAPApache ldap = new LDAPApache();
>                 ldap.connectAndBind(props);
>                 if (ldap.exists("objectClass=*"))
>                   System.out.println("Partition "+suffix+" is successfully
> populated");
>                 else
>                   System.out.println("ERROR: Partition "+suffix+" is
> empty");
>                 if (ldap.exists("dc=users"))
>                   System.out.println("Entry dc=users is successfully
> created");
>               else
>                   System.out.println("ERROR: Entry dc=users does not
> exist");
>                 ldap.disconnect();
>
>                 ldapSrv.stop();
>                 serv.sync();
>                 serv.shutdown();
>
>                 serv = createDirectoryService(workDir);
>                 serv.startup();
>
>                 ldapSrv = new LdapServer();
>                 ldapSrv.setDirectoryService( serv );
>                 ldapSrv.setTransports( ldapTransport );
>
>                 for (Partition part2 : serv.getPartitions()) {
>                     System.out.println("Reloaded partition
> "+part2.getId());
>                 }
>
>                 ldapSrv.start();
>
>                 ldap = new LDAPApache();
>                 ldap.connectAndBind(props);
>                 if (ldap.exists("objectClass=*"))
>                   System.out.println("Partition "+suffix+" was
> successfully repopulated");
>                 else
>                   System.out.println("ERROR: Reloaded partition "+suffix+"
> is empty");
>                 if (ldap.exists("dc=users"))
>                   System.out.println("Entry dc=users was successfully
> reloaded");
>               else
>                   System.out.println("ERROR: Could not reload entry
> dc=users");
>                 ldap.disconnect();
>
>                 ldapSrv.stop();
>                 serv.shutdown();
>
>             }
>
>         } // fi
>     } // main
> }
>



-- 
Kiran Ayyagari
http://keydap.com

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message