From bdec01e02d0790e0e5cb9b635bd200bd2ed0312b Mon Sep 17 00:00:00 2001
From: lutoff <lutoff@localhost>
Date: Mon, 09 Oct 2006 12:54:14 +0000
Subject: [PATCH] Update the JMX connection handler to ensure that it invokes the post-connect plugins at the appropriate time. Update the JMX client connection to ensure that it invokes the post-disconnect plugins at the appropriate time.  Add unit-test to check that post-connect and post-disconnect plugins are called.

---
 opends/tests/unit-tests-testng/src/server/org/opends/server/protocols/jmx/postConnectedDisconnectTest.java |  100 +++++++++++++++++++++++++++++++++
 opends/src/server/org/opends/server/protocols/jmx/JmxClientConnection.java                                 |   56 +++++++++++++-----
 opends/src/server/org/opends/server/protocols/jmx/RmiAuthenticator.java                                    |   15 ++++
 3 files changed, 155 insertions(+), 16 deletions(-)

diff --git a/opends/src/server/org/opends/server/protocols/jmx/JmxClientConnection.java b/opends/src/server/org/opends/server/protocols/jmx/JmxClientConnection.java
index 4711435..b0052f1 100644
--- a/opends/src/server/org/opends/server/protocols/jmx/JmxClientConnection.java
+++ b/opends/src/server/org/opends/server/protocols/jmx/JmxClientConnection.java
@@ -88,6 +88,11 @@
    */
   private JmxConnectionHandler jmxConnectionHandler;
 
+  /**
+   * Indicate that the disconnect process is started.
+   */
+  private Boolean disconnectStarted = new Boolean(false);
+
 
   /**
    * Creates a new Jmx client connection that will be authenticated as
@@ -181,7 +186,7 @@
 
     //
     // Ok, we can perform the unbind: call finalize
-    finalize();
+    disconnect(DisconnectReason.CLIENT_DISCONNECT, false, null, -1);
   }
 
 
@@ -884,7 +889,40 @@
                       String.valueOf(sendNotification), String.valueOf(message),
                       String.valueOf(messageID));
 
-    // No implementation is required since there is nothing to disconnect.
+    // we are already performing a disconnect
+    if (disconnectStarted)
+    {
+      return;
+    }
+    disconnectStarted = true ;
+
+
+
+    // unbind the underlying connection
+    try
+    {
+      UnbindOperation unbindOp = new UnbindOperation((ClientConnection) this,
+          this.nextOperationID(), this.nextMessageID(), null);
+      unbindOp.run();
+    }
+    catch (Exception e)
+    {
+      // TODO print a message ?
+      assert debugException(CLASS_NAME, "disconnect", e);
+    }
+
+    // Call postDisconnectPlugins
+    try
+    {
+      PluginConfigManager pluginManager =
+           DirectoryServer.getPluginConfigManager();
+      pluginManager.invokePostDisconnectPlugins(this, disconnectReason,
+                                                messageID, message);
+    }
+    catch (Exception e)
+    {
+      assert debugException(CLASS_NAME, "disconnect", e);
+    }
   }
 
 
@@ -1067,19 +1105,7 @@
    */
   protected void finalize()
   {
-    try
-    {
-      ArrayList<Control> requestControls = new ArrayList<Control>(0);
-      UnbindOperation unbindOp = new UnbindOperation((ClientConnection) this,
-          this.nextOperationID(), this.nextMessageID(), requestControls);
-
-      unbindOp.run();
-    }
-    catch (Exception e)
-    {
-      // TODO print a message ?
-      assert debugException(CLASS_NAME, "bind", e);
-    }
+    disconnect(DisconnectReason.OTHER, false, null, -1);
   }
 }
 
diff --git a/opends/src/server/org/opends/server/protocols/jmx/RmiAuthenticator.java b/opends/src/server/org/opends/server/protocols/jmx/RmiAuthenticator.java
index 7cfab8a..16d02cf 100644
--- a/opends/src/server/org/opends/server/protocols/jmx/RmiAuthenticator.java
+++ b/opends/src/server/org/opends/server/protocols/jmx/RmiAuthenticator.java
@@ -31,7 +31,10 @@
 import javax.management.remote.JMXAuthenticator;
 import javax.security.auth.Subject;
 
+import org.opends.server.api.plugin.PostConnectPluginResult;
 import org.opends.server.core.BindOperation;
+import org.opends.server.core.DirectoryServer;
+import org.opends.server.core.PluginConfigManager;
 import org.opends.server.messages.CoreMessages;
 import org.opends.server.protocols.asn1.ASN1OctetString;
 import org.opends.server.protocols.ldap.LDAPException;
@@ -195,7 +198,17 @@
 
     //
     // If we've gotten here, then the authentication was
-    // successful.
+    // successful. We'll take the connection so
+    // invoke the post-connect plugins.
+    PluginConfigManager pluginManager = DirectoryServer
+        .getPluginConfigManager();
+    PostConnectPluginResult pluginResult = pluginManager
+        .invokePostConnectPlugins(jmxClientConnection);
+    if (pluginResult.connectionTerminated())
+    {
+      SecurityException se = new SecurityException(pluginResult.toString());
+      throw se;
+    }
 
     // initialize a subject
     Subject s = new Subject();
diff --git a/opends/tests/unit-tests-testng/src/server/org/opends/server/protocols/jmx/postConnectedDisconnectTest.java b/opends/tests/unit-tests-testng/src/server/org/opends/server/protocols/jmx/postConnectedDisconnectTest.java
new file mode 100644
index 0000000..860dd60
--- /dev/null
+++ b/opends/tests/unit-tests-testng/src/server/org/opends/server/protocols/jmx/postConnectedDisconnectTest.java
@@ -0,0 +1,100 @@
+/*
+ * 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 2006 Sun Microsystems, Inc.
+ */
+package org.opends.server.protocols.jmx;
+
+import java.util.HashMap;
+
+import org.opends.server.TestCaseUtils;
+import org.opends.server.plugins.InvocationCounterPlugin;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.Test;
+import static org.testng.Assert.*;
+
+/**
+ * This class check is the pos-connected and post-disconnected plugin are
+ * called (see issue #728).
+ */
+public class postConnectedDisconnectTest extends JmxTestCase
+{
+
+  /**
+   * Set up the environment for performing the tests in this suite.
+   *
+   * @throws Exception
+   *           If the environment could not be set up.
+   */
+  @BeforeClass
+  public void setUp() throws Exception
+  {
+    // Make sure that the server is up and running.
+    TestCaseUtils.startServer();
+    synchronized (this)
+    {
+        this.wait(500);
+    }
+  }
+
+  /**
+   * Perform a simple connect.
+   * @throws Exception If something wrong occurs.
+   */
+  @Test(enabled = true)
+  public void checkPostconnectDisconnectPlugin() throws Exception
+  {
+    // Before the test, how many time postconnect and postdisconnect
+    // have been called.
+    int postConnectBefore = InvocationCounterPlugin.getPostConnectCount();
+    int postDisconnectBefore = InvocationCounterPlugin.getPostDisconnectCount();
+
+    // Create a new client connection
+    HashMap<String, Object> env = new HashMap<String, Object>();
+    String[] credentials = new String[] { "cn=directory manager" , "password"};
+    env.put("jmx.remote.credentials", credentials);
+    env.put("jmx.remote.x.client.connection.check.period",0);
+    OpendsJmxConnector opendsConnector = new OpendsJmxConnector("localhost",
+        (int) TestCaseUtils.getServerJmxPort(), env);
+    opendsConnector.connect();
+    assertNotNull(opendsConnector);
+
+    // Check that number of postconnect has been incremented.
+    Thread.sleep(3000);
+    int postConnectAfter = InvocationCounterPlugin.getPostConnectCount();
+    int postDisconnectAfter = InvocationCounterPlugin.getPostDisconnectCount();
+    assertEquals(postConnectBefore +1, postConnectAfter);
+    assertEquals(postDisconnectBefore, postDisconnectAfter);
+
+    // Close the client connection
+    opendsConnector.close();
+    Thread.sleep(3000);
+
+    // Check that number of postdisconnect has been incremented.
+    postConnectAfter = InvocationCounterPlugin.getPostConnectCount();
+    postDisconnectAfter = InvocationCounterPlugin.getPostDisconnectCount();
+    assertEquals(postConnectBefore +1 , postConnectAfter);
+    assertEquals(postDisconnectBefore +1 , postDisconnectAfter);
+  }
+}

--
Gitblit v1.10.0