blob: f6e5b9a1f0958a35f243b1d9e8e77d907a6dae07 [file] [log] [blame]
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
*/
package org.apache.directory.mitosis.service;
import org.apache.commons.io.FileUtils;
import org.apache.directory.mitosis.common.Replica;
import org.apache.directory.mitosis.common.ReplicaId;
import org.apache.directory.mitosis.configuration.ReplicationConfiguration;
import org.apache.directory.server.constants.ServerDNConstants;
import org.apache.directory.server.core.DefaultDirectoryService;
import org.apache.directory.server.core.DirectoryService;
import org.apache.directory.server.core.interceptor.Interceptor;
import org.apache.directory.server.core.jndi.CoreContextFactory;
import org.apache.mina.util.AvailablePortFinder;
import org.junit.After;
import org.junit.Before;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.naming.Context;
import javax.naming.ldap.InitialLdapContext;
import javax.naming.ldap.LdapContext;
import java.io.File;
import java.net.InetSocketAddress;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Random;
/**
* An abstract base class for replication tests.
*
* @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
* @version $Rev$, $Date$
*/
public abstract class AbstractReplicationServiceTestCase
{
private static final Logger LOG = LoggerFactory.getLogger( AbstractReplicationServiceTestCase.class );
protected Map<String, LdapContext> contexts = new HashMap<String, LdapContext>();
protected Map<String, DirectoryService> services = new HashMap<String, DirectoryService>();
protected Map<String, ReplicationInterceptor> replicationServices = new HashMap<String, ReplicationInterceptor>();
@Before public void setUp() throws Exception
{
createReplicas( new String[] { "A", "B", "C" } );
}
@After public void tearDown() throws Exception
{
destroyAllReplicas();
}
protected void createReplicas( String[] names ) throws Exception
{
int lastAvailablePort = 1024;
Replica[] replicas = new Replica[ names.length ];
for( int i = 0; i < names.length; i++ )
{
int replicationPort = AvailablePortFinder.getNextAvailable( lastAvailablePort );
lastAvailablePort = replicationPort + 1;
replicas[ i ] = new Replica( new ReplicaId( names[ i ] ),
new InetSocketAddress( "127.0.0.1", replicationPort ) );
}
Random random = new Random();
String homeDirectory = System.getProperty( "java.io.tmpdir" )
+ File.separator + "mitosis-"
+ Long.toHexString( random.nextLong() );
for ( Replica replica : replicas )
{
String replicaId = replica.getId().getId();
DirectoryService service = new DefaultDirectoryService();
service.setInstanceId( replicaId );
File workDir = new File( homeDirectory + File.separator + service.getInstanceId() );
service.setShutdownHookEnabled( false );
service.setWorkingDirectory( workDir );
List<Interceptor> interceptors = service.getInterceptors();
ReplicationConfiguration replicationCfg = new ReplicationConfiguration();
replicationCfg.setReplicaId( replica.getId() );
// Disable automatic replication to prevent unexpected behavior
replicationCfg.setReplicationInterval( 0 );
replicationCfg.setServerPort( replica.getAddress().getPort() );
for ( Replica replica1 : replicas )
{
if ( replica1 != replica )
{
replicationCfg.addPeerReplica( replica1 );
}
}
ReplicationInterceptor replicationInterceptor = new ReplicationInterceptor();
replicationInterceptor.setConfiguration( replicationCfg );
interceptors.add( replicationInterceptor );
service.setInterceptors( interceptors );
if ( workDir.exists() )
{
FileUtils.deleteDirectory( workDir );
}
service.startup();
Hashtable<String,Object> env = new Hashtable<String,Object>();
env.put( DirectoryService.JNDI_KEY, service );
env.put( Context.SECURITY_PRINCIPAL, ServerDNConstants.ADMIN_SYSTEM_DN );
env.put( Context.SECURITY_CREDENTIALS, "secret" );
env.put( Context.SECURITY_AUTHENTICATION, "simple" );
env.put( Context.PROVIDER_URL, "" );
env.put( Context.INITIAL_CONTEXT_FACTORY, CoreContextFactory.class.getName() );
// Initialize the server instance.
LdapContext context = new InitialLdapContext( env, null );
contexts.put( replicaId, context );
services.put( replicaId, service );
replicationServices.put( replicaId, replicationInterceptor );
}
// Ensure all replicas have had a chance to connect to each other since the last one started.
for ( ReplicationInterceptor replicationInterceptor : replicationServices.values() )
{
replicationInterceptor.interruptConnectors();
}
Thread.sleep( 5000 );
}
protected LdapContext getReplicaContext( String name ) throws Exception
{
LdapContext context = contexts.get( name );
if( context == null )
{
throw new IllegalArgumentException( "No such replica: " + name );
}
return ( LdapContext ) context.lookup( "" );
}
@SuppressWarnings("unchecked")
protected void destroyAllReplicas() throws Exception
{
for( Iterator<DirectoryService> i = services.values().iterator(); i.hasNext(); )
{
DirectoryService replica = i.next();
File workDir = replica.getWorkingDirectory();
try
{
replica.shutdown();
}
catch( Exception e )
{
LOG.error( "Encountered error while shutting down replica {}", replica.getInstanceId(), e );
throw e;
}
try
{
FileUtils.deleteDirectory( workDir );
}
catch( Exception e )
{
e.printStackTrace();
}
workDir.getParentFile().delete();
i.remove();
}
}
}