From 7cd71b6b2385b5bdd7bd3d845a56468c1fa16af2 Mon Sep 17 00:00:00 2001
From: gbellato <gbellato@localhost>
Date: Wed, 20 Jun 2007 08:46:34 +0000
Subject: [PATCH] small improvement in the replication monitoring

---
 opends/src/server/org/opends/server/replication/server/ReplicationServer.java              |  119 ++++++++++++++++++-----
 opends/src/server/org/opends/server/replication/server/ReplicationServerListenThread.java  |   67 +++++++++++++
 opends/src/server/org/opends/server/replication/server/ReplicationServerConnectThread.java |   68 +++++++++++++
 opends/src/server/org/opends/server/replication/server/ServerHandler.java                  |    4 
 4 files changed, 230 insertions(+), 28 deletions(-)

diff --git a/opends/src/server/org/opends/server/replication/server/ReplicationServer.java b/opends/src/server/org/opends/server/replication/server/ReplicationServer.java
index 556e5da..edb50df 100644
--- a/opends/src/server/org/opends/server/replication/server/ReplicationServer.java
+++ b/opends/src/server/org/opends/server/replication/server/ReplicationServer.java
@@ -41,17 +41,22 @@
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.HashMap;
+import java.util.LinkedHashSet;
 import java.util.List;
 import java.util.Set;
 
 import org.opends.server.admin.server.ConfigurationChangeListener;
 import org.opends.server.admin.std.server.ReplicationServerCfg;
 import org.opends.server.api.ConfigurableComponent;
-import org.opends.server.api.DirectoryThread;
+import org.opends.server.api.MonitorProvider;
 import org.opends.server.config.ConfigAttribute;
 import org.opends.server.config.ConfigEntry;
 import org.opends.server.config.ConfigException;
+import org.opends.server.core.DirectoryServer;
 import org.opends.server.replication.protocol.SocketSession;
+import org.opends.server.types.Attribute;
+import org.opends.server.types.AttributeType;
+import org.opends.server.types.AttributeValue;
 import org.opends.server.types.ConfigChangeResult;
 import org.opends.server.types.DN;
 import org.opends.server.types.ErrorLogCategory;
@@ -70,7 +75,7 @@
  *
  * It is responsible for creating the replication server cache and managing it
  */
-public class ReplicationServer
+public class ReplicationServer extends MonitorProvider
   implements Runnable, ConfigurableComponent,
              ConfigurationChangeListener<ReplicationServerCfg>
 {
@@ -81,8 +86,6 @@
   private Thread myListenThread;
   private Thread myConnectThread;
 
-  private boolean runListen = true;
-
   /* The list of replication servers configured by the administrator */
   private Collection<String> replicationServers;
 
@@ -115,8 +118,9 @@
   public ReplicationServer(ReplicationServerCfg configuration)
          throws ConfigException
   {
+    super("Replication Server" + configuration.getReplicationPort());
+
     shutdown = false;
-    runListen = true;
     replicationPort = configuration.getReplicationPort();
     replicationServerId = (short) configuration.getReplicationServerId();
     replicationServers = configuration.getReplicationServer();
@@ -148,6 +152,7 @@
     initialize(replicationServerId, replicationPort);
     configuration.addChangeListener(this);
     configDn = configuration.dn();
+    DirectoryServer.registerMonitorProvider(this);
   }
 
   /**
@@ -186,20 +191,6 @@
     return null;
   }
 
-  /**
-   * spawn the listen thread and the connect thread.
-   * Used a a workaround because there can be only one run method
-   */
-  public void run()
-  {
-    if (runListen)
-    {
-      runListen = false;
-      runListen();
-    }
-    else
-      runConnect();
-  }
 
   /**
    * The run method for the Listen thread.
@@ -208,7 +199,7 @@
    * and spawn further thread responsible for handling those connections
    */
 
-  private void runListen()
+  void runListen()
   {
     Socket newSocket = null;
     while (shutdown == false)
@@ -240,7 +231,7 @@
    * to all the other replication servers and if not attempts to
    * make the connection.
    */
-  private void runConnect()
+  void runConnect()
   {
     while (shutdown == false)
     {
@@ -368,9 +359,11 @@
       /*
        * create working threads
        */
-      myListenThread = new DirectoryThread(this, "Replication Server Listener");
+      myListenThread =
+        new ReplicationServerListenThread("Replication Server Listener", this);
       myListenThread.start();
-      myConnectThread = new DirectoryThread(this, "Replication Server Connect");
+      myConnectThread =
+        new ReplicationServerConnectThread("Replication Server Connect", this);
       myConnectThread.start();
 
     } catch (DatabaseException e)
@@ -407,7 +400,7 @@
    * @param baseDn The base Dn for which the ReplicationCache must be returned.
    * @return The ReplicationCache associated to the base DN given in parameter.
    */
-  public ReplicationCache getReplicationCache(DN baseDn)
+  ReplicationCache getReplicationCache(DN baseDn)
   {
     ReplicationCache replicationCache;
 
@@ -455,6 +448,7 @@
     }
 
     dbEnv.shutdown();
+    DirectoryServer.deregisterMonitorProvider(getMonitorInstanceName());
   }
 
 
@@ -468,7 +462,7 @@
    *         DN given in parameter.
    * @throws DatabaseException in case of underlying database problem.
    */
-  public DbHandler newDbHandler(short id, DN baseDn) throws DatabaseException
+  DbHandler newDbHandler(short id, DN baseDn) throws DatabaseException
   {
     return new DbHandler(id, baseDn, this, dbEnv);
   }
@@ -480,7 +474,7 @@
    * @return  The time after which changes must be deleted from the
    *          persistent storage (in milliseconds).
    */
-  public long getTrimage()
+  long getTrimage()
   {
     return trimAge * 1000;
   }
@@ -535,4 +529,77 @@
   {
     return true;
   }
+
+  /**
+   * {@inheritDoc}
+   */
+  @Override
+  public void initializeMonitorProvider(ConfigEntry configEntry)
+  {
+    // Nothing to do for now
+  }
+
+  /**
+   * {@inheritDoc}
+   */
+  @Override
+  public String getMonitorInstanceName()
+  {
+    return "Replication Server " + this.replicationPort + " "
+           + replicationServerId;
+  }
+
+  /**
+   * {@inheritDoc}
+   */
+  @Override
+  public long getUpdateInterval()
+  {
+    /* we don't wont to do polling on this monitor */
+    return 0;
+  }
+
+  /**
+   * {@inheritDoc}
+   */
+  @Override
+  public void updateMonitorData()
+  {
+    // As long as getUpdateInterval() returns 0, this will never get called
+
+  }
+
+  /**
+   * {@inheritDoc}
+   */
+  @Override
+  public ArrayList<Attribute> getMonitorData()
+  {
+    /*
+     * publish the server id and the port number.
+     */
+    ArrayList<Attribute> attributes = new ArrayList<Attribute>();
+    attributes.add(new Attribute("replication server id",
+        String.valueOf(serverId)));
+    attributes.add(new Attribute("replication server port",
+        String.valueOf(replicationPort)));
+
+    /*
+     * Add all the base DNs that are known by this replication server.
+     */
+    AttributeType baseType=
+      DirectoryServer.getAttributeType("base-dn", true);
+    LinkedHashSet<AttributeValue> baseValues =
+      new LinkedHashSet<AttributeValue>();
+    for (DN base : baseDNs.keySet())
+    {
+      baseValues.add(new AttributeValue(baseType, base. toString()));
+    }
+
+    Attribute bases = new Attribute(baseType, "base-dn", baseValues);
+    attributes.add(bases);
+
+    return attributes;
+  }
+
 }
diff --git a/opends/src/server/org/opends/server/replication/server/ReplicationServerConnectThread.java b/opends/src/server/org/opends/server/replication/server/ReplicationServerConnectThread.java
new file mode 100644
index 0000000..11e716a
--- /dev/null
+++ b/opends/src/server/org/opends/server/replication/server/ReplicationServerConnectThread.java
@@ -0,0 +1,68 @@
+/*
+ * CDDL HEADER START
+ *
+ * The contents of this file are subject to the terms of the
+ * Common Development and Distribution License, Version 1.0 only
+ * (the "License").  You may not use this file except in compliance
+ * with the License.
+ *
+ * You can obtain a copy of the license at
+ * trunk/opends/resource/legal-notices/OpenDS.LICENSE
+ * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
+ * See the License for the specific language governing permissions
+ * and limitations under the License.
+ *
+ * When distributing Covered Code, include this CDDL HEADER in each
+ * file and include the License file at
+ * trunk/opends/resource/legal-notices/OpenDS.LICENSE.  If applicable,
+ * add the following below this CDDL HEADER, with the fields enclosed
+ * by brackets "[]" replaced with your own identifying information:
+ *      Portions Copyright [yyyy] [name of copyright owner]
+ *
+ * CDDL HEADER END
+ *
+ *
+ *      Portions Copyright 2007 Sun Microsystems, Inc.
+ */
+package org.opends.server.replication.server;
+
+import org.opends.server.api.DirectoryThread;
+
+/**
+ * This Class is used to create a thread that is responsible for
+ * opening connection from this replication server to the other
+ * Replication Servers.
+ */
+public class ReplicationServerConnectThread extends DirectoryThread
+{
+  /**
+   * The Replication Server that created this thread.
+   */
+  private ReplicationServer server;
+
+  /**
+   * Creates a new instance of this directory thread with the
+   * specified name.
+   *
+   * @param  threadName  The human-readable name to use for this
+   *                     thread for debugging purposes.
+   * @param  server      The ReplicationServer that will be called to
+   *                     handle the connections.
+   */
+  public ReplicationServerConnectThread(
+      String threadName, ReplicationServer server)
+  {
+    super(threadName);
+    this.server = server;
+  }
+
+  /**
+   * {@inheritDoc}
+   */
+  @Override
+  public void run()
+  {
+    server.runConnect();
+  }
+
+}
diff --git a/opends/src/server/org/opends/server/replication/server/ReplicationServerListenThread.java b/opends/src/server/org/opends/server/replication/server/ReplicationServerListenThread.java
new file mode 100644
index 0000000..7b89e24
--- /dev/null
+++ b/opends/src/server/org/opends/server/replication/server/ReplicationServerListenThread.java
@@ -0,0 +1,67 @@
+/*
+ * CDDL HEADER START
+ *
+ * The contents of this file are subject to the terms of the
+ * Common Development and Distribution License, Version 1.0 only
+ * (the "License").  You may not use this file except in compliance
+ * with the License.
+ *
+ * You can obtain a copy of the license at
+ * trunk/opends/resource/legal-notices/OpenDS.LICENSE
+ * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
+ * See the License for the specific language governing permissions
+ * and limitations under the License.
+ *
+ * When distributing Covered Code, include this CDDL HEADER in each
+ * file and include the License file at
+ * trunk/opends/resource/legal-notices/OpenDS.LICENSE.  If applicable,
+ * add the following below this CDDL HEADER, with the fields enclosed
+ * by brackets "[]" replaced with your own identifying information:
+ *      Portions Copyright [yyyy] [name of copyright owner]
+ *
+ * CDDL HEADER END
+ *
+ *
+ *      Portions Copyright 2007 Sun Microsystems, Inc.
+ */
+package org.opends.server.replication.server;
+
+import org.opends.server.api.DirectoryThread;
+
+/**
+ * This Class is used to create a thread that is responsible for listening
+ * on the Replication Server thread and accept new incomng connections
+ * from other replication servers or from LDAP servers.
+ */
+public class ReplicationServerListenThread extends DirectoryThread
+{
+  /**
+   * The Replication Server that created this thread.
+   */
+  private ReplicationServer server;
+
+  /**
+   * Creates a new instance of this directory thread with the
+   * specified name.
+   *
+   * @param  threadName  The human-readable name to use for this
+   *                     thread for debugging purposes.
+   * @param  server      The ReplicationServer that will be called to
+   *                     handle the connections.
+   */
+  public ReplicationServerListenThread(
+      String threadName, ReplicationServer server)
+  {
+    super(threadName);
+    this.server = server;
+  }
+
+  /**
+   * {@inheritDoc}
+   */
+  @Override
+  public void run()
+  {
+    server.runListen();
+  }
+}
diff --git a/opends/src/server/org/opends/server/replication/server/ServerHandler.java b/opends/src/server/org/opends/server/replication/server/ServerHandler.java
index d308870..22447e2 100644
--- a/opends/src/server/org/opends/server/replication/server/ServerHandler.java
+++ b/opends/src/server/org/opends/server/replication/server/ServerHandler.java
@@ -1137,9 +1137,9 @@
                  " " + serverURL + " " + String.valueOf(serverId);
 
     if (serverIsLDAPserver)
-      return "LDAP Server " + str;
+      return "Remote LDAP Server " + str;
     else
-      return "Replication Server " + str;
+      return "Remote Replication Server " + str;
   }
 
   /**

--
Gitblit v1.10.0