From 931bbbfa71839678ca6ba075f8b261bcf62e26bc Mon Sep 17 00:00:00 2001
From: jvergara <jvergara@localhost>
Date: Wed, 07 Nov 2007 14:39:27 +0000
Subject: [PATCH] Update the code of the installer and Replication CLI main to be able to handle the case where an instance was uninstalled (but the registry not properly cleaned up) and we want to register it again.  Instead of failing with an 'already registered' error, the code will try to update the registration information in the ADS.

---
 opends/src/guitools/org/opends/guitools/replicationcli/ReplicationCliMain.java |   46 ++++++++++++++++++++++++++++++++++++++++------
 1 files changed, 40 insertions(+), 6 deletions(-)

diff --git a/opends/src/guitools/org/opends/guitools/replicationcli/ReplicationCliMain.java b/opends/src/guitools/org/opends/guitools/replicationcli/ReplicationCliMain.java
index b320a45..68bf385 100644
--- a/opends/src/guitools/org/opends/guitools/replicationcli/ReplicationCliMain.java
+++ b/opends/src/guitools/org/opends/guitools/replicationcli/ReplicationCliMain.java
@@ -3792,11 +3792,11 @@
           if (registry2.size() == 0)
           {
             server2.updateAdsPropertiesWithServerProperties();
-            adsCtx1.registerServer(server2.getAdsProperties());
+            registerServer(adsCtx1, server2.getAdsProperties());
           }
           else
           {
-            adsCtx1.registerServer(registry2.iterator().next());
+            registerServer(adsCtx1, registry2.iterator().next());
           }
 
           ctxSource = ctx1;
@@ -3813,11 +3813,11 @@
           if (registry1.size() == 0)
           {
             server1.updateAdsPropertiesWithServerProperties();
-            adsCtx2.registerServer(server1.getAdsProperties());
+            registerServer(adsCtx2, server1.getAdsProperties());
           }
           else
           {
-            adsCtx2.registerServer(registry1.iterator().next());
+            registerServer(adsCtx2, registry1.iterator().next());
           }
 
           ctxSource = ctx2;
@@ -3849,7 +3849,7 @@
           adsCtx2.createAdministrator(getAdministratorProperties(uData));
         }
         server1.updateAdsPropertiesWithServerProperties();
-        adsCtx2.registerServer(server1.getAdsProperties());
+        registerServer(adsCtx2, server1.getAdsProperties());
 
         ctxSource = ctx2;
         ctxDestination = ctx1;
@@ -3863,7 +3863,7 @@
           adsCtx1.createAdministrator(getAdministratorProperties(uData));
         }
         server2.updateAdsPropertiesWithServerProperties();
-        adsCtx1.registerServer(server2.getAdsProperties());
+        registerServer(adsCtx1, server2.getAdsProperties());
 
         ctxSource = ctx1;
         ctxDestination = ctx2;
@@ -6106,4 +6106,38 @@
     }
     return mustInitializeSchema;
   }
+
+  /**
+   * This method registers a server in a given ADSContext.  If the server was
+   * already registered it unregisters it and registers again (some properties
+   * might have changed).
+   * @param adsContext the ADS Context to be used.
+   * @param server the server to be registered.
+   * @throws ADSContextException if an error occurs during the registration or
+   * unregistration of the server.
+   */
+  private void registerServer(ADSContext adsContext,
+      Map<ADSContext.ServerProperty, Object> serverProperties)
+  throws ADSContextException
+  {
+    try
+    {
+      adsContext.registerServer(serverProperties);
+    }
+    catch (ADSContextException ade)
+    {
+      if (ade.getError() ==
+        ADSContextException.ErrorType.ALREADY_REGISTERED)
+      {
+        LOG.log(Level.WARNING, "The server was already registered: "+
+            serverProperties);
+        adsContext.unregisterServer(serverProperties);
+        adsContext.registerServer(serverProperties);
+      }
+      else
+      {
+        throw ade;
+      }
+    }
+  }
 }

--
Gitblit v1.10.0