parallelised service endpoint discovery and ensured that old threads are destroyed...
authorjprocter <Jim Procter>
Fri, 29 Oct 2010 16:10:06 +0000 (16:10 +0000)
committerjprocter <Jim Procter>
Fri, 29 Oct 2010 16:10:06 +0000 (16:10 +0000)
src/jalview/gui/Desktop.java
src/jalview/gui/JalviewChangeSupport.java [new file with mode: 0644]
src/jalview/ws/jws2/JabaWsServerQuery.java [new file with mode: 0644]
src/jalview/ws/jws2/Jws2Discoverer.java

index ec9974c..6228afb 100755 (executable)
@@ -25,7 +25,6 @@ import java.awt.*;
 import java.awt.datatransfer.*;
 import java.awt.dnd.*;
 import java.awt.event.*;
-import java.beans.PropertyChangeEvent;
 import java.beans.PropertyChangeListener;
 import java.io.BufferedInputStream;
 import java.io.BufferedOutputStream;
@@ -53,77 +52,11 @@ public class Desktop extends jalview.jbgui.GDesktop implements
         DropTargetListener, ClipboardOwner, IProgressIndicator
 {
 
-  private class JalviewChangeSupport implements PropertyChangeListener
-  {
-    public void propertyChange(PropertyChangeEvent evt)
-    {
-      // Handle change events - most are simply routed to other sources
-      changeSupport.firePropertyChange(evt);
-    }
-
-    /**
-     * change listeners are notified of changes to resources so they can update
-     * their state. E.g. - the 'services' property notifies when the available
-     * set of web service endpoints have changed.
-     */
-    private java.beans.PropertyChangeSupport changeSupport = new java.beans.PropertyChangeSupport(
-            this);
-
-    /**
-     * @param propertyName
-     * @param listener
-     * @see java.beans.PropertyChangeSupport#addPropertyChangeListener(java.lang.String,
-     *      java.beans.PropertyChangeListener)
-     */
-    public void addJalviewPropertyChangeListener(String propertyName,
-            PropertyChangeListener listener)
-    {
-      changeSupport.addPropertyChangeListener(propertyName, listener);
-    }
-
-    /**
-     * @param listener
-     * @see java.beans.PropertyChangeSupport#addPropertyChangeListener(java.beans.PropertyChangeListener)
-     */
-    public void addJalviewPropertyChangeListener(
-            PropertyChangeListener listener)
-    {
-      changeSupport.addPropertyChangeListener(listener);
-    }
-
-    /*
-     * @param propertyName
-     * 
-     * @param oldValue
-     * 
-     * @param newValue
-     * 
-     * @see
-     * java.beans.PropertyChangeSupport#firePropertyChange(java.lang.String,
-     * java.lang.Object, java.lang.Object) public void firePropertyChange(String
-     * propertyName, Object oldValue, Object newValue) {
-     * changeSupport.firePropertyChange(propertyName, oldValue, newValue); }
-     */
-
-    /**
-     * @param propertyName
-     * @param listener
-     * @see java.beans.PropertyChangeSupport#removePropertyChangeListener(java.lang.String,
-     *      java.beans.PropertyChangeListener)
-     */
-    public void removeJalviewPropertyChangeListener(String propertyName,
-            PropertyChangeListener listener)
-    {
-      changeSupport.removePropertyChangeListener(propertyName, listener);
-    }
-
-  }
-
   private JalviewChangeSupport changeSupport = new JalviewChangeSupport();
 
   /**
    * @param listener
-   * @see jalview.gui.Desktop.JalviewChangeSupport#addJalviewPropertyChangeListener(java.beans.PropertyChangeListener)
+   * @see jalview.gui.JalviewChangeSupport#addJalviewPropertyChangeListener(java.beans.PropertyChangeListener)
    */
   public void addJalviewPropertyChangeListener(
           PropertyChangeListener listener)
@@ -134,7 +67,7 @@ public class Desktop extends jalview.jbgui.GDesktop implements
   /**
    * @param propertyName
    * @param listener
-   * @see jalview.gui.Desktop.JalviewChangeSupport#addJalviewPropertyChangeListener(java.lang.String,
+   * @see jalview.gui.JalviewChangeSupport#addJalviewPropertyChangeListener(java.lang.String,
    *      java.beans.PropertyChangeListener)
    */
   public void addJalviewPropertyChangeListener(String propertyName,
@@ -146,7 +79,7 @@ public class Desktop extends jalview.jbgui.GDesktop implements
   /**
    * @param propertyName
    * @param listener
-   * @see jalview.gui.Desktop.JalviewChangeSupport#removeJalviewPropertyChangeListener(java.lang.String,
+   * @see jalview.gui.JalviewChangeSupport#removeJalviewPropertyChangeListener(java.lang.String,
    *      java.beans.PropertyChangeListener)
    */
   public void removeJalviewPropertyChangeListener(String propertyName,
@@ -2199,10 +2132,11 @@ public class Desktop extends jalview.jbgui.GDesktop implements
     }
     if (Cache.getDefault("SHOW_JWS2_SERVICES", true))
     {
-      jalview.ws.jws2.Jws2Discoverer.getDiscoverer()
-              .addPropertyChangeListener(changeSupport);
-      (t2 = new Thread(jalview.ws.jws2.Jws2Discoverer.getDiscoverer()))
-              .start();
+      if (jalview.ws.jws2.Jws2Discoverer.getDiscoverer().isRunning()) {
+        jalview.ws.jws2.Jws2Discoverer.getDiscoverer().setAborted(true);
+      }
+      t2=jalview.ws.jws2.Jws2Discoverer.getDiscoverer().startDiscoverer(changeSupport);
+      
     }
     if (blocking)
     {
diff --git a/src/jalview/gui/JalviewChangeSupport.java b/src/jalview/gui/JalviewChangeSupport.java
new file mode 100644 (file)
index 0000000..371beac
--- /dev/null
@@ -0,0 +1,70 @@
+package jalview.gui;
+
+import java.beans.PropertyChangeEvent;
+import java.beans.PropertyChangeListener;
+
+public class JalviewChangeSupport implements PropertyChangeListener
+{
+  public void propertyChange(PropertyChangeEvent evt)
+  {
+    // Handle change events - most are simply routed to other sources
+    changeSupport.firePropertyChange(evt);
+  }
+
+  /**
+   * change listeners are notified of changes to resources so they can update
+   * their state. E.g. - the 'services' property notifies when the available
+   * set of web service endpoints have changed.
+   */
+  private java.beans.PropertyChangeSupport changeSupport = new java.beans.PropertyChangeSupport(
+          this);
+
+  /**
+   * @param propertyName
+   * @param listener
+   * @see java.beans.PropertyChangeSupport#addPropertyChangeListener(java.lang.String,
+   *      java.beans.PropertyChangeListener)
+   */
+  public void addJalviewPropertyChangeListener(String propertyName,
+          PropertyChangeListener listener)
+  {
+    changeSupport.addPropertyChangeListener(propertyName, listener);
+  }
+
+  /**
+   * @param listener
+   * @see java.beans.PropertyChangeSupport#addPropertyChangeListener(java.beans.PropertyChangeListener)
+   */
+  public void addJalviewPropertyChangeListener(
+          PropertyChangeListener listener)
+  {
+    changeSupport.addPropertyChangeListener(listener);
+  }
+
+  /*
+   * @param propertyName
+   * 
+   * @param oldValue
+   * 
+   * @param newValue
+   * 
+   * @see
+   * java.beans.PropertyChangeSupport#firePropertyChange(java.lang.String,
+   * java.lang.Object, java.lang.Object) public void firePropertyChange(String
+   * propertyName, Object oldValue, Object newValue) {
+   * changeSupport.firePropertyChange(propertyName, oldValue, newValue); }
+   */
+
+  /**
+   * @param propertyName
+   * @param listener
+   * @see java.beans.PropertyChangeSupport#removePropertyChangeListener(java.lang.String,
+   *      java.beans.PropertyChangeListener)
+   */
+  public void removeJalviewPropertyChangeListener(String propertyName,
+          PropertyChangeListener listener)
+  {
+    changeSupport.removePropertyChangeListener(propertyName, listener);
+  }
+
+}
\ No newline at end of file
diff --git a/src/jalview/ws/jws2/JabaWsServerQuery.java b/src/jalview/ws/jws2/JabaWsServerQuery.java
new file mode 100644 (file)
index 0000000..5ca5065
--- /dev/null
@@ -0,0 +1,101 @@
+/**
+ * 
+ */
+package jalview.ws.jws2;
+
+import jalview.bin.Cache;
+
+import compbio.data.msa.MsaWS;
+import compbio.ws.client.Jws2Client;
+import compbio.ws.client.Services;
+
+/**
+ * @author JimP
+ *
+ */
+public class JabaWsServerQuery implements Runnable
+{
+
+  Jws2Discoverer jws2Discoverer=null;
+  String jwsservers=null;
+  boolean quit=false,
+  running=false;
+  /**
+   * @return the running
+   */
+  public  boolean isRunning()
+  {
+    return running;
+  }
+
+  /**
+   * @param quit the quit to set
+   */
+  public void setQuit(boolean quit)
+  {
+    this.quit = quit;
+  }
+
+  public JabaWsServerQuery(Jws2Discoverer jws2Discoverer, String jwsservers)
+  {
+    this.jws2Discoverer = jws2Discoverer;
+    this.jwsservers=jwsservers;
+  }
+
+  /* (non-Javadoc)
+   * @see java.lang.Runnable#run()
+   */
+  @Override
+  public void run()
+  {
+    running=true;
+        try
+    {
+      if (Jws2Client.validURL(jwsservers))
+      {
+        // look for services
+        for (Services srv : Services.values())
+        {
+          if (quit)
+          {
+            running=false;
+            return;
+          }
+          MsaWS service = null;
+          try
+          {
+            service = Jws2Client.connect(jwsservers, srv);
+          } catch (Exception e)
+          {
+            System.err.println("Jws2 Discoverer: Problem on "
+                    + jwsservers + " with service " + srv + ":\n"
+                    + e.getMessage());
+            if (!(e instanceof javax.xml.ws.WebServiceException))
+            {
+              e.printStackTrace();
+            }
+          }
+          ;
+          if (service != null)
+          {
+            jws2Discoverer.addService(jwsservers, srv, service);
+          }
+        }
+
+      }
+      else
+      {
+        Cache.log.info("Ignoring invalid Jws2 service url " + jwsservers);
+      }
+    } catch (Exception e)
+    {
+      e.printStackTrace();
+      Cache.log.warn("Exception when discovering Jws2 services.", e);
+    } catch (Error e)
+    {
+      Cache.log.error("Exception when discovering Jws2 services.", e);
+    }
+    running=false;
+  }
+
+}
index e56a5e5..8cc39d7 100644 (file)
@@ -25,8 +25,10 @@ import java.beans.PropertyChangeListener;
 import java.io.Closeable;
 import java.net.ConnectException;
 import java.net.URL;
+import java.util.ArrayList;
 import java.util.HashSet;
 import java.util.Hashtable;
+import java.util.List;
 import java.util.StringTokenizer;
 import java.util.Vector;
 
@@ -41,6 +43,7 @@ import jalview.bin.Cache;
 import jalview.datamodel.AlignmentView;
 import jalview.gui.AlignFrame;
 import jalview.gui.Desktop;
+import jalview.gui.JalviewChangeSupport;
 import jalview.ws.WSMenuEntryProviderI;
 import jalview.ws.params.ParamDatastoreI;
 import compbio.data.msa.MsaWS;
@@ -87,7 +90,16 @@ public class Jws2Discoverer implements Runnable, WSMenuEntryProviderI
     changeSupport.removePropertyChangeListener(listener);
   }
 
-  boolean running = false;
+  boolean running = false, aborted = false;
+
+  /**
+   * @param aborted
+   *          the aborted to set
+   */
+  public void setAborted(boolean aborted)
+  {
+    this.aborted = aborted;
+  }
 
   Thread oldthread = null;
 
@@ -95,7 +107,23 @@ public class Jws2Discoverer implements Runnable, WSMenuEntryProviderI
   {
     if (running && oldthread != null && oldthread.isAlive())
     {
-      return;
+      if (!aborted)
+      {
+        return;
+      }
+      while (running)
+      {
+        try
+        {
+          Cache.log
+                  .debug("Waiting around for old discovery thread to finish.");
+          // wait around until old discoverer dies
+          Thread.sleep(100);
+        } catch (Exception e)
+        {
+        }
+      }
+      Cache.log.debug("Old discovery thread has finished.");
     }
     running = true;
     oldthread = Thread.currentThread();
@@ -115,53 +143,43 @@ public class Jws2Discoverer implements Runnable, WSMenuEntryProviderI
     {
       services.removeAllElements();
     }
-    for (String jwsservers : getServiceUrls())
+    List<JabaWsServerQuery> qrys = new ArrayList<JabaWsServerQuery>();
+    for (final String jwsservers : getServiceUrls())
+    {
+      JabaWsServerQuery squery = new JabaWsServerQuery(this, jwsservers);
+      qrys.add(squery);
+      new Thread(squery).start();
+    }
+    boolean finished = false;
+    do
     {
       try
       {
-        if (Jws2Client.validURL(jwsservers))
-        {
-          // look for services
-          for (Services srv : Services.values())
-          {
-            MsaWS service = null;
-            try
-            {
-              service = Jws2Client.connect(jwsservers, srv);
-            } catch (Exception e)
-            {
-              System.err.println("Jws2 Discoverer: Problem on "
-                      + jwsservers + " with service " + srv + ":\n"
-                      + e.getMessage());
-              if (!(e instanceof javax.xml.ws.WebServiceException))
-              {
-                e.printStackTrace();
-              }
-            }
-            ;
-            if (service != null)
-            {
-              addService(jwsservers, srv, service);
-            }
-          }
-
-        }
-        else
-        {
-          Cache.log.info("Ignoring invalid Jws2 service url " + jwsservers);
-        }
+        Thread.sleep(100);
       } catch (Exception e)
       {
-        e.printStackTrace();
-        Cache.log.warn("Exception when discovering Jws2 services.", e);
-      } catch (Error e)
+      }
+      ;
+      for (JabaWsServerQuery squery : qrys)
+      {
+        finished |= !squery.isRunning();
+      }
+      if (aborted)
       {
-        Cache.log.error("Exception when discovering Jws2 services.", e);
+        Cache.log.debug("Aborting " + qrys.size()
+                + " JABAWS discovery threads.");
+        for (JabaWsServerQuery squery : qrys)
+        {
+          squery.setQuit(true);
+        }
       }
-    }
+    } while (!aborted && !finished);
     oldthread = null;
     running = false;
-    changeSupport.firePropertyChange("services", new Vector(), services);
+    if (!aborted)
+    {
+      changeSupport.firePropertyChange("services", new Vector(), services);
+    }
   }
 
   /**
@@ -171,7 +189,8 @@ public class Jws2Discoverer implements Runnable, WSMenuEntryProviderI
    * @param srv
    * @param service2
    */
-  private void addService(String jwsservers, Services srv, MsaWS service2)
+  synchronized void addService(String jwsservers, Services srv,
+          MsaWS service2)
   {
     if (services == null)
     {
@@ -467,17 +486,18 @@ public class Jws2Discoverer implements Runnable, WSMenuEntryProviderI
 
   public static void main(String[] args)
   {
-    Thread runner = new Thread(getDiscoverer());
-    getDiscoverer().addPropertyChangeListener(new PropertyChangeListener()
+    Thread runner = getDiscoverer().startDiscoverer(new PropertyChangeListener()
     {
 
       public void propertyChange(PropertyChangeEvent evt)
       {
-        System.out.println("Changesupport: There are now "
+        if (getDiscoverer().services!=null)
+          {System.out.println("Changesupport: There are now "
                 + getDiscoverer().services.size() + " services");
+          
+          }
       }
     });
-    runner.start();
     while (runner.isAlive())
     {
       try
@@ -617,4 +637,21 @@ public class Jws2Discoverer implements Runnable, WSMenuEntryProviderI
     return true;
   }
 
+  /**
+   * Start a fresh discovery thread and notify the given object when we're finished. Any known existing threads will be killed before this one is started. 
+   * @param changeSupport2
+   * @return new thread
+   */
+  public Thread startDiscoverer(PropertyChangeListener changeSupport2)
+  {
+    if (isRunning())
+    {
+      setAborted(true);
+    }
+    addPropertyChangeListener(changeSupport2);
+    Thread thr = new Thread(this);
+    thr.start();
+    return thr;
+  }
+
 }