X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;ds=sidebyside;f=src%2Fjalview%2Fext%2Fensembl%2FEnsemblInfo.java;h=a59a64df35bb7f0790943dc01eb7d296a44316d2;hb=b690e55cf50b5d253ec5545ee21cd93fc381f8c1;hp=fa24f1e08b15c56f38f21e038a37537a1f629243;hpb=f59dd9efbb3dfc313ab0b0507832e21cd0076fe1;p=jalview.git diff --git a/src/jalview/ext/ensembl/EnsemblInfo.java b/src/jalview/ext/ensembl/EnsemblInfo.java index fa24f1e..a59a64d 100644 --- a/src/jalview/ext/ensembl/EnsemblInfo.java +++ b/src/jalview/ext/ensembl/EnsemblInfo.java @@ -1,9 +1,10 @@ package jalview.ext.ensembl; +import jalview.bin.ApplicationSingletonProvider; +import jalview.bin.ApplicationSingletonProvider.ApplicationSingletonI; import jalview.datamodel.AlignmentI; import jalview.datamodel.DBRefSource; -import java.io.BufferedReader; import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; @@ -13,13 +14,50 @@ import java.util.List; import java.util.Map; import java.util.Set; -import org.json.simple.JSONArray; -import org.json.simple.parser.JSONParser; import org.json.simple.parser.ParseException; -public class EnsemblInfo extends EnsemblRestClient +public class EnsemblInfo extends EnsemblRestClient implements ApplicationSingletonI { + /** + * On first request only, populate the lookup map by fetching the list of + * divisions known to EnsemblGenomes + * + */ + private static EnsemblInfo getInstance() + { + return (EnsemblInfo) ApplicationSingletonProvider.getInstance(EnsemblInfo.class); + } + + private EnsemblInfo() + { + // use getInstance() + + /* + * for convenience, pre-fill ensembl.org as the domain for "ENSEMBL" + */ + divisions.put(DBRefSource.ENSEMBL.toUpperCase(), ensemblDomain); + try + { + @SuppressWarnings("unchecked") + Iterator rvals = (Iterator) getJSON( + getDivisionsUrl(ensemblGenomesDomain), null, -1, + MODE_ITERATOR, null); + if (rvals == null) + { + return; + } + while (rvals.hasNext()) + { + String division = rvals.next().toString(); + divisions.put(division.toUpperCase(), ensemblGenomesDomain); + } + } catch (IOException | ParseException | NumberFormatException e) + { + // ignore + } + } + /* * cached results of REST /info/divisions service, currently *
@@ -35,7 +73,7 @@ public class EnsemblInfo extends EnsemblRestClient
    * The values for EnsemblGenomes are retrieved by a REST call, that for
    * Ensembl is added programmatically for convenience of lookup
    */
-  private static Map divisions;
+  private Map divisions = new HashMap<>();
 
   @Override
   public String getDbName()
@@ -69,79 +107,9 @@ public class EnsemblInfo extends EnsemblRestClient
    * @param division
    * @return
    */
-  public String getDomain(String division)
+  public static String getDomain(String division)
   {
-    if (divisions == null)
-    {
-      fetchDivisions();
-    }
-    return divisions.get(division.toUpperCase());
-  }
-
-  /**
-   * On first request only, populate the lookup map by fetching the list of
-   * divisions known to EnsemblGenomes.
-   */
-  void fetchDivisions()
-  {
-    divisions = new HashMap<>();
-
-    /*
-     * for convenience, pre-fill ensembl.org as the domain for "ENSEMBL"
-     */
-    divisions.put(DBRefSource.ENSEMBL.toUpperCase(), ensemblDomain);
-
-    BufferedReader br = null;
-    try
-    {
-      URL url = getDivisionsUrl(ensemblGenomesDomain);
-      if (url != null)
-      {
-        br = getHttpResponse(url, null);
-      }
-      parseResponse(br, ensemblGenomesDomain);
-    } catch (IOException e)
-    {
-      // ignore
-    } finally
-    {
-      if (br != null)
-      {
-        try
-        {
-          br.close();
-        } catch (IOException e)
-        {
-          // ignore
-        }
-      }
-    }
-  }
-
-  /**
-   * Parses the JSON response to /info/divisions, and add each to the lookup map
-   * 
-   * @param br
-   * @param domain
-   */
-  void parseResponse(BufferedReader br, String domain)
-  {
-    JSONParser jp = new JSONParser();
-
-    try
-    {
-      JSONArray parsed = (JSONArray) jp.parse(br);
-
-      Iterator rvals = parsed.iterator();
-      while (rvals.hasNext())
-      {
-        String division = rvals.next().toString();
-        divisions.put(division.toUpperCase(), domain);
-      }
-    } catch (IOException | ParseException | NumberFormatException e)
-    {
-      // ignore
-    }
+    return getInstance().divisions.get(division.toUpperCase());
   }
 
   /**
@@ -162,12 +130,8 @@ public class EnsemblInfo extends EnsemblRestClient
    * 
    * @return
    */
-  public Set getDivisions() {
-    if (divisions == null)
-    {
-      fetchDivisions();
-    }
-
-    return divisions.keySet();
+  public static Set getDivisions()
+  {
+    return getInstance().divisions.keySet();
   }
 }