From 61484f9c89a866cc5c9a22499653a1c17b938ffb Mon Sep 17 00:00:00 2001 From: gmungoc Date: Thu, 3 Oct 2019 14:36:10 +0100 Subject: [PATCH] JAL-3253 one global singleton instance of EnsemblInfo suffices --- src/jalview/ext/ensembl/EnsemblInfo.java | 109 +++++++++++++++--------------- src/jalview/ext/ensembl/EnsemblMap.java | 2 +- src/jalview/gui/CrossRefAction.java | 2 +- 3 files changed, 58 insertions(+), 55 deletions(-) diff --git a/src/jalview/ext/ensembl/EnsemblInfo.java b/src/jalview/ext/ensembl/EnsemblInfo.java index a59a64d..71730de 100644 --- a/src/jalview/ext/ensembl/EnsemblInfo.java +++ b/src/jalview/ext/ensembl/EnsemblInfo.java @@ -1,7 +1,5 @@ package jalview.ext.ensembl; -import jalview.bin.ApplicationSingletonProvider; -import jalview.bin.ApplicationSingletonProvider.ApplicationSingletonI; import jalview.datamodel.AlignmentI; import jalview.datamodel.DBRefSource; @@ -16,64 +14,27 @@ import java.util.Set; import org.json.simple.parser.ParseException; -public class EnsemblInfo extends EnsemblRestClient implements ApplicationSingletonI +public class EnsemblInfo extends EnsemblRestClient { /** - * 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 + * *
    * { 
-   *  { "ENSEMBLFUNGI", "http://rest.ensemblgenomes.org"},
-   *    "ENSEMBLBACTERIA", "http://rest.ensemblgenomes.org"},
-   *    "ENSEMBLPROTISTS", "http://rest.ensemblgenomes.org"},
-   *    "ENSEMBLMETAZOA", "http://rest.ensemblgenomes.org"},
-   *    "ENSEMBLPLANTS",  "http://rest.ensemblgenomes.org"},
-   *    "ENSEMBL", "http://rest.ensembl.org" }
+   *  "ENSEMBLFUNGI", "http://rest.ensemblgenomes.org"},
+   *  "ENSEMBLBACTERIA", "http://rest.ensemblgenomes.org"},
+   *  "ENSEMBLPROTISTS", "http://rest.ensemblgenomes.org"},
+   *  "ENSEMBLMETAZOA", "http://rest.ensemblgenomes.org"},
+   *  "ENSEMBLPLANTS",  "http://rest.ensemblgenomes.org"},
+   *  "ENSEMBL", "http://rest.ensembl.org"
    *  }
    * 
+ * * The values for EnsemblGenomes are retrieved by a REST call, that for * Ensembl is added programmatically for convenience of lookup */ - private Map divisions = new HashMap<>(); + private static Map divisions; @Override public String getDbName() @@ -107,9 +68,46 @@ public class EnsemblInfo extends EnsemblRestClient implements ApplicationSinglet * @param division * @return */ - public static String getDomain(String division) + public String getDomain(String division) { - return getInstance().divisions.get(division.toUpperCase()); + 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); + 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 + } } /** @@ -130,8 +128,13 @@ public class EnsemblInfo extends EnsemblRestClient implements ApplicationSinglet * * @return */ - public static Set getDivisions() + public Set getDivisions() { - return getInstance().divisions.keySet(); + if (divisions == null) + { + fetchDivisions(); + } + + return divisions.keySet(); } } diff --git a/src/jalview/ext/ensembl/EnsemblMap.java b/src/jalview/ext/ensembl/EnsemblMap.java index 9789819..ae36749 100644 --- a/src/jalview/ext/ensembl/EnsemblMap.java +++ b/src/jalview/ext/ensembl/EnsemblMap.java @@ -221,7 +221,7 @@ public class EnsemblMap extends EnsemblRestClient URL url = null; try { - String domain = EnsemblInfo.getDomain(division); + String domain = new EnsemblInfo().getDomain(division); if (domain != null) { url = getIdMapUrl(domain, accession, start, end, cdsOrCdna); diff --git a/src/jalview/gui/CrossRefAction.java b/src/jalview/gui/CrossRefAction.java index f607686..d37cb29 100644 --- a/src/jalview/gui/CrossRefAction.java +++ b/src/jalview/gui/CrossRefAction.java @@ -276,7 +276,7 @@ public class CrossRefAction implements Runnable return; } - Set ensemblDivisions = EnsemblInfo.getDivisions(); + Set ensemblDivisions = new EnsemblInfo().getDivisions(); /* * first look for direct dbrefs from sequence to Ensembl -- 1.7.10.2