throw new ReplicationCliException(
getMessageForException(ne, ConnectionUtils.getHostPort(ctx)),
ERROR_READING_CONFIGURATION, ne);
}
ADSContext adsCtx = new ADSContext(ctx);
TopologyCache cache = null;
// Only try to update remote server if the user provided a Global
// Administrator to authenticate.
boolean tryToUpdateRemote = uData.getAdminUid() != null;
try
{
if (adsCtx.hasAdminData() && tryToUpdateRemote)
{
cache = new TopologyCache(adsCtx, getTrustManager(),
getConnectTimeout());
cache.setPreferredConnections(
PreferredConnection.getPreferredConnections(ctx));
cache.getFilter().setSearchMonitoringInformation(false);
if (!uData.disableAll())
{
for (String dn : uData.getBaseDNs())
{
cache.getFilter().addBaseDNToSearch(dn);
}
}
cache.reloadTopology();
}
}
catch (ADSContextException adce)
{
throw new ReplicationCliException(
ERR_REPLICATION_READING_ADS.get(adce.getMessage()),
ERROR_READING_ADS, adce);
}
catch (TopologyCacheException tce)
{
throw new ReplicationCliException(
ERR_REPLICATION_READING_ADS.get(tce.getMessage()),
ERROR_READING_TOPOLOGY_CACHE, tce);
}
if (!argParser.isInteractive())
{
// Inform the user of the potential errors that we found.
LinkedHashSet<Message> messages = new LinkedHashSet<Message>();
if (cache != null)
{
messages.addAll(cache.getErrorMessages());
}
if (!messages.isEmpty())
{
println(
ERR_REPLICATION_READING_REGISTERED_SERVERS_WARNING.get(
Utils.getMessageFromCollection(messages,
Constants.LINE_SEPARATOR).toString()));
}
}
boolean disableReplicationServer = false;
if (server.isReplicationServer() &&
(uData.disableReplicationServer() || uData.disableAll()))
{
disableReplicationServer = true;
}
if ((cache != null) && disableReplicationServer)
{
String replicationServer = server.getReplicationServerHostPort();
// Figure out if this is the last replication server for a given
// topology (containing a different replica) or there will be only
// another replication server left (single point of failure).
Set<SuffixDescriptor> lastRepServer =
new TreeSet<SuffixDescriptor>(new SuffixComparator());
Set<SuffixDescriptor> beforeLastRepServer =
new TreeSet<SuffixDescriptor>(new SuffixComparator());
for (SuffixDescriptor suffix : cache.getSuffixes())
{
if (Utils.areDnsEqual(suffix.getDN(),
ADSContext.getAdministrationSuffixDN()) ||
Utils.areDnsEqual(suffix.getDN(), Constants.SCHEMA_DN))
{
// Do not display these suffixes.
continue;
}
Set<String> repServers = suffix.getReplicationServers();
if (repServers.size() <= 2)
{
boolean found = false;
for (String repServer : repServers)
{
if (repServer.equalsIgnoreCase(replicationServer))
{
found = true;
break;
}
}
if (found)
{
if (repServers.size() == 2)
{
beforeLastRepServer.add(suffix);
}
else
{
lastRepServer.add(suffix);
}
}
}
}
// Inform the user
if (beforeLastRepServer.size() > 0)
{
LinkedHashSet<String> baseDNs = new LinkedHashSet<String>();
for (SuffixDescriptor suffix : beforeLastRepServer)
{
if (!Utils.areDnsEqual(suffix.getDN(),
ADSContext.getAdministrationSuffixDN()) &&
!Utils.areDnsEqual(suffix.getDN(), Constants.SCHEMA_DN))
{
// Do not display these suffixes.
baseDNs.add(suffix.getDN());
}
}
if (!baseDNs.isEmpty())
{
String arg =
Utils.getStringFromCollection(baseDNs, Constants.LINE_SEPARATOR);
if (!isInteractive())
{
println(INFO_DISABLE_REPLICATION_ONE_POINT_OF_FAILURE.get(arg));
}
else
{
try
{
if (!askConfirmation(
INFO_DISABLE_REPLICATION_ONE_POINT_OF_FAILURE_PROMPT.get(arg),
false, LOG))
{
throw new ReplicationCliException(
ERR_REPLICATION_USER_CANCELLED.get(),
ReplicationCliReturnCode.USER_CANCELLED, null);
}
}
catch (CLIException ce)
{
println(ce.getMessageObject());
throw new ReplicationCliException(
ERR_REPLICATION_USER_CANCELLED.get(),
ReplicationCliReturnCode.USER_CANCELLED, null);
}
}
}
}
if (lastRepServer.size() > 0)
{
// Check that there are other replicas and that this message, really
// makes sense to be displayed.
LinkedHashSet<String> suffixArg = new LinkedHashSet<String>();
for (SuffixDescriptor suffix : lastRepServer)
{
boolean baseDNSpecified = false;
for (String baseDN : uData.getBaseDNs())
{
if (Utils.areDnsEqual(baseDN,
ADSContext.getAdministrationSuffixDN()) ||
Utils.areDnsEqual(baseDN, Constants.SCHEMA_DN))
{
// Do not display these suffixes.
continue;
}
if (Utils.areDnsEqual(baseDN, suffix.getDN()))
{
baseDNSpecified = true;
break;
}
}
if (!baseDNSpecified)
{
Set<ServerDescriptor> servers =
new TreeSet<ServerDescriptor>(new ServerComparator());
for (ReplicaDescriptor replica : suffix.getReplicas())
{
servers.add(replica.getServer());
}
suffixArg.add(getSuffixDisplay(suffix.getDN(), servers));
}
else
{
// Check that there are other replicas.
if (suffix.getReplicas().size() > 1)
{
// If there is just one replica, it is the one in this server.
Set<ServerDescriptor> servers =
new TreeSet<ServerDescriptor>(new ServerComparator());
for (ReplicaDescriptor replica : suffix.getReplicas())
{
if (!replica.getServer().isSameServer(server))
{
servers.add(replica.getServer());
}
}
if (!servers.isEmpty())
{
suffixArg.add(getSuffixDisplay(suffix.getDN(), servers));
}
}
}
}
if (!suffixArg.isEmpty())
{
String arg =
Utils.getStringFromCollection(suffixArg, Constants.LINE_SEPARATOR);
if (!isInteractive())
{
println(INFO_DISABLE_REPLICATION_DISABLE_IN_REMOTE.get(arg));
}
else
{
try
{
if (!askConfirmation(
INFO_DISABLE_REPLICATION_DISABLE_IN_REMOTE_PROMPT.get(arg),
false, LOG))
{
throw new ReplicationCliException(
ERR_REPLICATION_USER_CANCELLED.get(),
ReplicationCliReturnCode.USER_CANCELLED, null);
}
}
catch (CLIException ce)
{
println(ce.getMessageObject());
throw new ReplicationCliException(
ERR_REPLICATION_USER_CANCELLED.get(),
ReplicationCliReturnCode.USER_CANCELLED, null);
}
}
}
}
}
/**
* Try to figure out if we must explicitly disable replication on
* cn=admin data and cn=schema.
*/
boolean forceDisableSchema = false;
boolean forceDisableADS = false;
boolean schemaReplicated = false;
boolean adsReplicated = false;
boolean disableAllBaseDns = disableAllBaseDns(ctx, uData);
Collection<ReplicaDescriptor> replicas = getReplicas(ctx);
for (ReplicaDescriptor rep : replicas)
{
String dn = rep.getSuffix().getDN();
if (rep.isReplicated())
{
if (Utils.areDnsEqual(ADSContext.getAdministrationSuffixDN(), dn))
{
adsReplicated = true;
}
else if (Utils.areDnsEqual(Constants.SCHEMA_DN, dn))
{
schemaReplicated = true;
}
}
}
if (disableAllBaseDns &&
(disableReplicationServer || !server.isReplicationServer()))
{
// Unregister the server from the ADS if no other server has dependencies
// with it (no replicated base DNs and no replication server).
server.updateAdsPropertiesWithServerProperties();
try
{
adsCtx.unregisterServer(server.getAdsProperties());
try
{
// To be sure that the change gets propagated
Thread.sleep(2000);
}
catch (Throwable t)
{
}
}
catch (ADSContextException adce)
{
LOG.log(Level.SEVERE, "Error unregistering server: "+
server.getAdsProperties(), adce);
if (adce.getError() != ADSContextException.ErrorType.NOT_YET_REGISTERED)
{
throw new ReplicationCliException(
ERR_REPLICATION_UPDATING_ADS.get(adce.getMessageObject()),
ERROR_READING_ADS, adce);
}
}
}
Set<String> suffixesToDisable = new HashSet<String>();
if (uData.disableAll())
{
for (ReplicaDescriptor replica : server.getReplicas())
{
if (replica.isReplicated())
{
suffixesToDisable.add(replica.getSuffix().getDN());
}
}
}
else
{
suffixesToDisable.addAll(uData.getBaseDNs());
if (disableAllBaseDns &&
(disableReplicationServer || !server.isReplicationServer()))
{
forceDisableSchema = schemaReplicated;
forceDisableADS = adsReplicated;
}
for (String dn : uData.getBaseDNs())
{
if (Utils.areDnsEqual(ADSContext.getAdministrationSuffixDN(), dn))
{
// The user already asked this to be explicitly disabled
forceDisableADS = false;
}
else if (Utils.areDnsEqual(Constants.SCHEMA_DN, dn))
{
// The user already asked this to be explicitly disabled
forceDisableSchema = false;
}
}
if (forceDisableSchema)
{
suffixesToDisable.add(Constants.SCHEMA_DN);
}
if (forceDisableADS)
{
suffixesToDisable.add(ADSContext.getAdministrationSuffixDN());
}
}
String replicationServerHostPort = null;
if (server.isReplicationServer())
{
replicationServerHostPort = server.getReplicationServerHostPort();
}
boolean replicationServerDisabled = false;
for (String baseDN : suffixesToDisable)
{
try
{
deleteReplicationDomain(ctx, baseDN);
}
catch (OpenDsException ode)
{
Message msg = getMessageForDisableException(ode,
ConnectionUtils.getHostPort(ctx), baseDN);
throw new ReplicationCliException(msg,
ERROR_DISABLING_REPLICATION_ON_BASEDN, ode);
}
}
if ((replicationServerHostPort != null) && (cache != null))
{
Set<ServerDescriptor> serversToUpdate =
new LinkedHashSet<ServerDescriptor>();
Set<String> baseDNsToUpdate = new HashSet<String>(suffixesToDisable);
for (String baseDN : baseDNsToUpdate)
{
SuffixDescriptor suffix = getSuffix(baseDN, cache, server);
if (suffix != null)
{
for (ReplicaDescriptor replica : suffix.getReplicas())
{
serversToUpdate.add(replica.getServer());
}
}
}
if (disableReplicationServer)
{
// Find references in all servers.
Set<SuffixDescriptor> suffixes = cache.getSuffixes();
for (SuffixDescriptor suffix : suffixes)
{
boolean found = false;
for (String repServer : suffix.getReplicationServers())
{
found = repServer.equalsIgnoreCase(replicationServerHostPort);
if (found)
{
break;
}
}
if (found)
{
baseDNsToUpdate.add(suffix.getDN());
for (ReplicaDescriptor replica : suffix.getReplicas())
{
serversToUpdate.add(replica.getServer());
}
}
}
}
String bindDn = ConnectionUtils.getBindDN(ctx);
String pwd = ConnectionUtils.getBindPassword(ctx);
for (ServerDescriptor s : serversToUpdate)
{
removeReferencesInServer(s, replicationServerHostPort, bindDn, pwd,
baseDNsToUpdate, disableReplicationServer,
PreferredConnection.getPreferredConnections(ctx));
}
if (disableReplicationServer)
{
// Disable replication server
disableReplicationServer(ctx);
replicationServerDisabled = true;
// Wait to be sure that changes are taken into account and reset the
// contents of the ADS.
try
{
Thread.sleep(5000);
}
catch (Throwable t)
{
}
}
}
if (disableReplicationServer && !replicationServerDisabled)
{
// This can happen if we could not retrieve the TopologyCache
disableReplicationServer(ctx);
replicationServerDisabled = true;
}
if (uData.disableAll())
{
try
{
// Delete all contents from ADSContext.
printProgress(formatter.getFormattedWithPoints(
INFO_REPLICATION_REMOVE_ADS_CONTENTS.get()));
adsCtx.removeAdminData();
String adminBackendName = null;
for (ReplicaDescriptor replica : server.getReplicas())
{
if (Utils.areDnsEqual(ADSContext.getAdministrationSuffixDN(),
replica.getSuffix().getDN()))
{
adminBackendName = replica.getBackendName();
break;
}
}
adsCtx.createAdminData(adminBackendName);
printProgress(formatter.getFormattedDone());
printlnProgress();
}
catch (ADSContextException adce)
{
LOG.log(Level.SEVERE, "Error removing contents of cn=admin data: "+
adce, adce);
throw new ReplicationCliException(
ERR_REPLICATION_UPDATING_ADS.get(adce.getMessageObject()),
ERROR_UPDATING_ADS, adce);
}
try
{
// Delete all contents from truststore.
printProgress(formatter.getFormattedWithPoints(
INFO_REPLICATION_REMOVE_TRUSTSTORE_CONTENTS.get()));
ServerDescriptor.cleanAdsTrustStore(adsCtx.getDirContext());
printProgress(formatter.getFormattedDone());
printlnProgress();
}
catch (Throwable t)
{
LOG.log(Level.SEVERE, "Error removing contents of truststore: "+t, t);
throw new ReplicationCliException(
ERR_REPLICATION_UPDATING_ADS.get(t.toString()),
ERROR_UPDATING_ADS, t);
}
}
else if (disableAllBaseDns &&
(disableReplicationServer || !server.isReplicationServer()))
{
// Unregister the servers from the ADS of the local server.
try
{
Set<Map<ADSContext.ServerProperty, Object>> registry =
adsCtx.readServerRegistry();
for (Map<ADSContext.ServerProperty, Object> s : registry)
{
adsCtx.unregisterServer(s);
}
try
{
// To be sure that the change gets propagated
Thread.sleep(2000);