early bail out for long urls, and threaded off group url construction: JAL-402
authorjprocter <Jim Procter>
Fri, 10 Sep 2010 15:21:04 +0000 (15:21 +0000)
committerjprocter <Jim Procter>
Fri, 10 Sep 2010 15:21:04 +0000 (15:21 +0000)
src/jalview/gui/PopupMenu.java
src/jalview/util/GroupUrlLink.java
src/jalview/ws/EnfinEnvision2OneWay.java

index 6a457d9..a952d29 100755 (executable)
@@ -30,6 +30,7 @@ import jalview.datamodel.*;
 import jalview.io.*;
 import jalview.schemes.*;
 import jalview.util.GroupUrlLink;
+import jalview.util.GroupUrlLink.UrlStringTooLongException;
 import jalview.util.UrlLink;
 
 /**
@@ -654,8 +655,14 @@ public class PopupMenu extends JPopupMenu
       }
       // and try and make the groupURL!
 
-      Object[] urlset = urlLink.makeUrlStubs(ids, seqstr, "FromJalview"
-              + System.currentTimeMillis(), false);
+      Object[] urlset = null;
+      try
+      {
+        urlset = urlLink.makeUrlStubs(ids, seqstr, "FromJalview"
+                + System.currentTimeMillis(), false);
+      } catch (UrlStringTooLongException e)
+      {
+      }
       if (urlset != null)
       {
         int type = urlLink.getGroupURLType() & 3;
@@ -752,7 +759,12 @@ public class PopupMenu extends JPopupMenu
 
           public void run()
           {
-            showLink(urlgenerator.constructFrom(urlstub));
+            try
+            {
+              showLink(urlgenerator.constructFrom(urlstub));
+            } catch (UrlStringTooLongException e)
+            {
+            }
           }
 
         }).start();
index ef32e99..6daf488 100644 (file)
@@ -25,6 +25,21 @@ import java.util.Vector;
 
 public class GroupUrlLink
 {
+  public class UrlStringTooLongException extends Exception
+  {
+    public UrlStringTooLongException(int lng)
+    {
+      urlLength = lng;
+    }
+
+    public int urlLength;
+
+    public String toString()
+    {
+      return "Generated url is estimated to be too long (" + urlLength
+              + ")";
+    }
+  }
 
   /**
    * Helper class based on the UrlLink class which enables URLs to be
@@ -69,14 +84,17 @@ public class GroupUrlLink
       { "SEQUENCEIDS", "SEQUENCES", "DATASETID" };
     }
   }
+
   /**
    * test for GroupURLType bitfield (with default tokens)
    */
   public static final int SEQUENCEIDS = 1;
+
   /**
    * test for GroupURLType bitfield (with default tokens)
    */
   public static final int SEQUENCES = 2;
+
   /**
    * test for GroupURLType bitfield (with default tokens)
    */
@@ -347,9 +365,11 @@ public class GroupUrlLink
    * @return null or Object[] { int[] { number of seqs substituted},boolean[] {
    *         which seqs were substituted }, StringBuffer[] { substituted lists
    *         for each token }, String[] { url } }
+   * @throws UrlStringTooLongException
    */
   public Object[] makeUrls(String[] idstrings, String[] seqstrings,
           String dsstring, boolean onlyIfMatches)
+          throws UrlStringTooLongException
   {
     Hashtable rstrings = replacementArgs(idstrings, seqstrings, dsstring);
     return makeUrls(rstrings, onlyIfMatches);
@@ -380,6 +400,7 @@ public class GroupUrlLink
   }
 
   public Object[] makeUrls(Hashtable repstrings, boolean onlyIfMatches)
+          throws UrlStringTooLongException
   {
     return makeUrlsIf(true, repstrings, onlyIfMatches);
   }
@@ -391,9 +412,10 @@ public class GroupUrlLink
    * @param string
    * @param b
    * @return URL stub objects ready to pass to constructFrom
+   * @throws UrlStringTooLongException
    */
   public Object[] makeUrlStubs(String[] ids, String[] seqstr,
-          String string, boolean b)
+          String string, boolean b) throws UrlStringTooLongException
   {
     Hashtable rstrings = replacementArgs(ids, seqstr, string);
     Object[] stubs = makeUrlsIf(false, rstrings, b);
@@ -413,8 +435,10 @@ public class GroupUrlLink
    * 
    * @param stubs
    * @return URL string.
+   * @throws UrlStringTooLongException
    */
   public String constructFrom(Object[] stubs)
+          throws UrlStringTooLongException
   {
     Object[] results = makeUrlsIf(true, (Hashtable) stubs[2],
             ((boolean[]) stubs[3])[0]);
@@ -432,9 +456,11 @@ public class GroupUrlLink
    *         seqs }, boolean[] { which matched }, (if createFullUrl also has
    *         StringBuffer[] { segment generated from inputs that is used in URL
    *         }, String[] { url })}
+   * @throws UrlStringTooLongException
    */
   protected Object[] makeUrlsIf(boolean createFullUrl,
           Hashtable repstrings, boolean onlyIfMatches)
+          throws UrlStringTooLongException
   {
     int pass = 0;
 
@@ -528,6 +554,11 @@ public class GroupUrlLink
             rematchat = rg.matchedTo();
             thismatched[sq] |= true;
             urllength += rg.charsMatched(); // count length
+            if ((urllength + 32) > Platform.getMaxCommandLineLength())
+            {
+              throw new UrlStringTooLongException(urllength);
+            }
+
             if (!createFullUrl)
             {
               continue; // don't bother making the URL replacement text.
@@ -654,8 +685,7 @@ public class GroupUrlLink
     // platform
     if ((urllength + 32) > Platform.getMaxCommandLineLength())
     {
-      System.err.println("URL estimated to be too long " + urllength);
-      return null;
+      throw new UrlStringTooLongException(urllength);
     }
     if (!createFullUrl)
     {
@@ -824,13 +854,27 @@ public class GroupUrlLink
 
         System.out
                 .println("<insert input id and sequence strings here> Without onlyIfMatches:");
-        Object[] urls = ul.makeUrls(seqsandids[0], seqsandids[1],
-                "mydataset", false);
-        testUrls(ul, seqsandids, urls);
+        Object[] urls;
+        try
+        {
+          urls = ul.makeUrls(seqsandids[0], seqsandids[1], "mydataset",
+                  false);
+          testUrls(ul, seqsandids, urls);
+        } catch (UrlStringTooLongException ex)
+        {
+          System.out.println("too long exception " + ex);
+        }
         System.out
                 .println("<insert input id and sequence strings here> With onlyIfMatches set:");
-        urls = ul.makeUrls(seqsandids[0], seqsandids[1], "mydataset", true);
-        testUrls(ul, seqsandids, urls);
+        try
+        {
+          urls = ul.makeUrls(seqsandids[0], seqsandids[1], "mydataset",
+                  true);
+          testUrls(ul, seqsandids, urls);
+        } catch (UrlStringTooLongException ex)
+        {
+          System.out.println("too long exception " + ex);
+        }
       }
       else
       {
index ae2792c..b36956c 100644 (file)
@@ -25,6 +25,7 @@ import jalview.gui.AlignFrame;
 import jalview.gui.Desktop;
 import jalview.gui.JvSwingUtils;
 import jalview.util.GroupUrlLink;
+import jalview.util.GroupUrlLink.UrlStringTooLongException;
 
 import java.awt.Component;
 import java.awt.Cursor;
@@ -318,7 +319,12 @@ public class EnfinEnvision2OneWay extends DefaultHandler implements
 
           public void run()
           {
-            showLink(urlgenerator.constructFrom(urlstub));
+            try {
+              showLink(urlgenerator.constructFrom(urlstub));
+            } catch (UrlStringTooLongException ex)
+            {
+              Cache.log.warn("Not showing link: URL is too long!", ex);
+            }
           }
 
         }).start();
@@ -462,6 +468,10 @@ public class EnfinEnvision2OneWay extends DefaultHandler implements
     }
     // now create group links for all distinct ID/sequence sets.
     Hashtable<String, JMenu[]> gurlMenus = new Hashtable<String, JMenu[]>();
+    /**
+     * last number of sequences where URL generation failed
+     */
+    int[] nsqtype = new int[] { 0,0,0,0,0,0,0,0,0,0};
     for (int i = 0; i < groupURLLinks.size(); i++)
     {
       String link = groupURLLinks.elementAt(i).toString();
@@ -512,6 +522,10 @@ public class EnfinEnvision2OneWay extends DefaultHandler implements
           String[] allids = ((String[]) idset[1]);
           seqstr = new String[numinput];
           ids = new String[numinput];
+          if (nsqtype[urlLink.getGroupURLType()]>0 && numinput>=nsqtype[urlLink.getGroupURLType()])
+          {
+            continue;
+          }
           for (int sq = 0, idcount = 0; sq < seqs.length; sq++)
           {
             if (allids[sq] != null)
@@ -520,15 +534,28 @@ public class EnfinEnvision2OneWay extends DefaultHandler implements
               seqstr[idcount++] = idandseqs[1][sq];
             }
           }
-          createAndAddLinks(wflinkMenus, false, urlLink, ltarget, null,
+          try {createAndAddLinks(wflinkMenus, false, urlLink, ltarget, null,
                   descr, ids, seqstr);
+          } catch (UrlStringTooLongException ex)
+          {
+            nsqtype[urlLink.getGroupURLType()] = numinput;
+          }
         }
       }
       // also do names only.
       seqstr = idandseqs[1];
       ids = idandseqs[0];
-      createAndAddLinks(wflinkMenus, true, urlLink, "Any", null, descr,
+      if (nsqtype[urlLink.getGroupURLType()]>0 && idandseqs[0].length>=nsqtype[urlLink.getGroupURLType()])
+      {
+        continue;
+      }
+      
+      try {createAndAddLinks(wflinkMenus, true, urlLink, "Any", null, descr,
               ids, seqstr);
+      }catch (UrlStringTooLongException ex)
+      {
+        nsqtype[urlLink.getGroupURLType()] = idandseqs[0].length;
+      }
     }
     boolean anyadded = false; // indicates if there are any group links to give
     // to user
@@ -556,10 +583,11 @@ public class EnfinEnvision2OneWay extends DefaultHandler implements
 
   private boolean createAndAddLinks(JMenu[] linkMenus, boolean usingNames,
           GroupUrlLink urlLink, String label, String ltarget, String descr,
-          String[] ids, String[] seqstr)
+          String[] ids, String[] seqstr) throws UrlStringTooLongException
   {
-    Object[] urlset = urlLink.makeUrlStubs(ids, seqstr, "FromJalview"
+    Object[] urlset= urlLink.makeUrlStubs(ids, seqstr, "FromJalview"
             + System.currentTimeMillis(), false);
+
     if (urlset != null)
     {
       int type = urlLink.getGroupURLType() & 3;
@@ -582,7 +610,6 @@ public class EnfinEnvision2OneWay extends DefaultHandler implements
     }
     return false;
   }
-
   // / end of stuff copied from popupmenu
   public void attachWSMenuEntry(final JMenu wsmenu,
           final AlignFrame alignFrame)
@@ -611,6 +638,8 @@ public class EnfinEnvision2OneWay extends DefaultHandler implements
       {
         if (refresh)
         {
+          new Thread(new Runnable() {
+            public void run() {
           try
           {
             buildGroupLinkMenu(enfinServiceMenu, alignFrame);
@@ -621,6 +650,7 @@ public class EnfinEnvision2OneWay extends DefaultHandler implements
                     ex);
             enfinServiceMenu.setEnabled(false);
           }
+            }}).start();
           refresh = false;
         }
       }