From 74a8448e45dbeba4803e92851f8a3023e038509b Mon Sep 17 00:00:00 2001
From: jvergara <jvergara@localhost>
Date: Tue, 24 Apr 2007 15:41:08 +0000
Subject: [PATCH] Fix for issue 1448 (closing the status panel causes I/O Error in opends)

---
 opends/src/statuspanel/org/opends/statuspanel/ui/LoginDialog.java        |   13 +++++-
 opends/src/statuspanel/org/opends/statuspanel/StatusPanelController.java |    1 
 opends/src/statuspanel/org/opends/statuspanel/ConfigFromLDAP.java        |   17 ++++++++
 opends/src/statuspanel/org/opends/statuspanel/ServerStatusPooler.java    |   33 +++++++++++++---
 4 files changed, 55 insertions(+), 9 deletions(-)

diff --git a/opends/src/statuspanel/org/opends/statuspanel/ConfigFromLDAP.java b/opends/src/statuspanel/org/opends/statuspanel/ConfigFromLDAP.java
index 5366004..b7f5e1e 100644
--- a/opends/src/statuspanel/org/opends/statuspanel/ConfigFromLDAP.java
+++ b/opends/src/statuspanel/org/opends/statuspanel/ConfigFromLDAP.java
@@ -125,6 +125,23 @@
   }
 
   /**
+   * Method to be called to close properly the connection.
+   */
+  public void closeConnection()
+  {
+    if (ctx != null)
+    {
+      try
+      {
+        ctx.close();
+      }
+      catch (Throwable t)
+      {
+      }
+    }
+  }
+
+  /**
    * Reads the configuration and monitoring information of the server using
    * LDAP.
    * When calling this method the thread is blocked until all the configuration
diff --git a/opends/src/statuspanel/org/opends/statuspanel/ServerStatusPooler.java b/opends/src/statuspanel/org/opends/statuspanel/ServerStatusPooler.java
index 993b10e..a56274c 100644
--- a/opends/src/statuspanel/org/opends/statuspanel/ServerStatusPooler.java
+++ b/opends/src/statuspanel/org/opends/statuspanel/ServerStatusPooler.java
@@ -49,7 +49,7 @@
   private String pwd;
   private ServerStatusDescriptor lastDescriptor;
   private boolean stopPooling;
-  private Thread t;
+  private Thread poolingThread;
   private HashSet<ServerStatusChangeListener> listeners =
     new HashSet<ServerStatusChangeListener>();
   private boolean starting;
@@ -83,7 +83,7 @@
   public void startPooling()
   {
     stopPooling = false;
-    t = new Thread(new Runnable()
+    poolingThread = new Thread(new Runnable()
     {
       public void run()
       {
@@ -133,7 +133,25 @@
         }
       }
     });
-    t.start();
+    poolingThread.start();
+  }
+
+  /**
+   * Stop pooling the server status.  This method does not block the thread
+   * that called it.
+   *
+   */
+  public void stopPooling()
+  {
+    stopPooling = true;
+    try
+    {
+      onLineConf.closeConnection();
+      poolingThread.interrupt();
+    }
+    catch (Throwable t)
+    {
+    }
   }
 
   /**
@@ -218,7 +236,8 @@
   {
     this.dn = dn;
     this.pwd = pwd;
-    if ((ldapUrl != null) && (t != null) && t.isAlive() && !stopPooling)
+    if ((ldapUrl != null) && (poolingThread != null) &&
+        poolingThread.isAlive() && !stopPooling)
     {
       /* If we are pooling, stop the pooling update the connection information
        * and restart the pooling.  Set the stopPooling boolean to true to
@@ -227,17 +246,17 @@
        *
        */
       stopPooling = true;
-      t.interrupt();
+      poolingThread.interrupt();
       try
       {
-        t.join(5000);
+        poolingThread.join(5000);
       }
       catch (Throwable t)
       {
         /* This should not happen: this thread should not be interrupted. */
         t.printStackTrace();
       }
-      t = null;
+      poolingThread = null;
       onLineConf.setConnectionInfo(ldapUrl, dn, pwd);
       startPooling();
     }
diff --git a/opends/src/statuspanel/org/opends/statuspanel/StatusPanelController.java b/opends/src/statuspanel/org/opends/statuspanel/StatusPanelController.java
index 6506664..f248539 100644
--- a/opends/src/statuspanel/org/opends/statuspanel/StatusPanelController.java
+++ b/opends/src/statuspanel/org/opends/statuspanel/StatusPanelController.java
@@ -178,6 +178,7 @@
    */
   public void quitClicked()
   {
+    serverStatusPooler.stopPooling();
     System.exit(0);
   }
 
diff --git a/opends/src/statuspanel/org/opends/statuspanel/ui/LoginDialog.java b/opends/src/statuspanel/org/opends/statuspanel/ui/LoginDialog.java
index bcf8bc4..f60a851 100644
--- a/opends/src/statuspanel/org/opends/statuspanel/ui/LoginDialog.java
+++ b/opends/src/statuspanel/org/opends/statuspanel/ui/LoginDialog.java
@@ -327,10 +327,9 @@
       public Object processBackgroundTask() throws NamingException
       {
         Boolean isServerRunning = Boolean.TRUE;
+        InitialLdapContext ctx = null;
         try
         {
-          InitialLdapContext ctx = null;
-
           String ldapUrl = getLDAPURL();
 
           if (ldapUrl != null)
@@ -369,6 +368,16 @@
         {
           throw new IllegalStateException("Unexpected throwable.", t);
         }
+        if (ctx != null)
+        {
+          try
+          {
+            ctx.close();
+          }
+          catch (Throwable t)
+          {
+          }
+        }
         return isServerRunning;
       }
 

--
Gitblit v1.10.0