LOG.debug( "Starting DS {}...", dsBuilder.name() );
Class<?> factory = dsBuilder.factory();
DirectoryServiceFactory dsf = ( DirectoryServiceFactory ) factory
.newInstance();
DirectoryService service = dsf.getDirectoryService();
service.setAccessControlEnabled( dsBuilder.enableAccessControl() );
service.setAllowAnonymousAccess( dsBuilder.allowAnonAccess() );
service.getChangeLog().setEnabled( dsBuilder.enableChangeLog() );
List<Interceptor> interceptorList = service.getInterceptors();
for ( Class<?> interceptorClass : dsBuilder.additionalInterceptors() )
{
interceptorList.add( ( Interceptor ) interceptorClass.newInstance() );
}
if ( dsBuilder.authenticators().length != 0 )
{
AuthenticationInterceptor authenticationInterceptor = null;
for ( Interceptor interceptor : interceptorList )
{
if ( interceptor instanceof AuthenticationInterceptor )
{
authenticationInterceptor = ( AuthenticationInterceptor ) interceptor;
break;
}
}
if ( authenticationInterceptor == null )
{
throw new IllegalStateException(
"authentication interceptor not found" );
}
Set<Authenticator> authenticators = new HashSet<Authenticator>();
for ( CreateAuthenticator createAuthenticator : dsBuilder
.authenticators() )
{
Authenticator auth = createAuthenticator.type().newInstance();
if ( auth instanceof DelegatingAuthenticator )
{
DelegatingAuthenticator dauth = ( DelegatingAuthenticator ) auth;
dauth.setDelegateHost( createAuthenticator.delegateHost() );
dauth.setDelegatePort( createAuthenticator.delegatePort() );
}
authenticators.add( auth );
}
}
service.setInterceptors( interceptorList );
dsf.init( dsBuilder.name() );
// Process the Partition, if any.
for ( CreatePartition createPartition : dsBuilder.partitions() )
{
Partition partition;
// Determine the partition type
if ( createPartition.type() == Partition.class )
{
// The annotation does not specify a specific partition type.
// We use the partition factory to create partition and index
// instances.
PartitionFactory partitionFactory = dsf.getPartitionFactory();
partition = partitionFactory.createPartition(
service.getSchemaManager(),
createPartition.name(),
createPartition.suffix(),
createPartition.cacheSize(),
new File( service.getInstanceLayout().getPartitionsDirectory(), createPartition.name() ) );
CreateIndex[] indexes = createPartition.indexes();
for ( CreateIndex createIndex : indexes )
{
partitionFactory.addIndex( partition,
createIndex.attribute(), createIndex.cacheSize() );
}
}
else
{
// The annotation contains a specific partition type, we use
// that type.
Class<?> partypes[] = new Class[]{SchemaManager.class};
Constructor<?> constructor = createPartition.type().getConstructor(partypes);
partition = (Partition)constructor.newInstance( new Object[]{service.getSchemaManager()} );
partition.setId( createPartition.name() );
partition.setSuffixDn( new Dn( service.getSchemaManager(), createPartition.suffix() ) );
if ( partition instanceof AbstractBTreePartition<?> )
{
AbstractBTreePartition<?> btreePartition = ( AbstractBTreePartition<?> ) partition;
btreePartition.setCacheSize( createPartition.cacheSize() );
btreePartition.setPartitionPath( new File( service
.getInstanceLayout().getPartitionsDirectory(),
createPartition.name() ).toURI() );
// Process the indexes if any
CreateIndex[] indexes = createPartition.indexes();
for ( CreateIndex createIndex : indexes )
{
// The annotation does not specify a specific index
// type.
// We use the generic index implementation.
JdbmIndex index = new JdbmIndex();
index.setAttributeId( createIndex.attribute() );
btreePartition.addIndexedAttributes( index );
}
}
}
partition.setSchemaManager( service.getSchemaManager() );
// Inject the partition into the DirectoryService
service.addPartition( partition );
// Last, process the context entry
ContextEntry contextEntry = createPartition.contextEntry();
if ( contextEntry != null )