JAL-3210 Improvements to eclipse detection. New src tree and SwingJS updated from...
[jalview.git] / src / jalview / ws / seqfetcher / ASequenceFetcher.java
index 2a27cce..e205e76 100644 (file)
@@ -36,6 +36,7 @@ import java.util.HashSet;
 import java.util.Hashtable;
 import java.util.List;
 import java.util.Map;
+import java.util.Map.Entry;
 import java.util.Stack;
 import java.util.Vector;
 
@@ -45,7 +46,7 @@ public class ASequenceFetcher
   /*
    * set of databases we can retrieve entries from
    */
-  protected Hashtable<String, Map<String, DbSourceProxy>> fetchableDbs;
+  protected Hashtable<String, Map<String, DbSourceProxyRoot>> fetchableDbs;
 
   /*
    * comparator to sort by tier (0/1/2) and name
@@ -57,8 +58,6 @@ public class ASequenceFetcher
    */
   protected ASequenceFetcher()
   {
-    super();
-
     /*
      * comparator to sort proxies by tier and name
      */
@@ -305,7 +304,7 @@ public class ASequenceFetcher
   public List<DbSourceProxy> getSourceProxy(String db)
   {
     db = DBRefUtils.getCanonicalName(db);
-    Map<String, DbSourceProxy> dblist = fetchableDbs.get(db);
+    Map<String, DbSourceProxyRoot> dblist = fetchableDbs.get(db);
     if (dblist == null)
     {
       return new ArrayList<>();
@@ -314,27 +313,75 @@ public class ASequenceFetcher
     /*
      * sort so that primary sources precede secondary
      */
-    List<DbSourceProxy> dbs = new ArrayList<>(dblist.values());
+    List<DbSourceProxy> dbs = new ArrayList<>();
+    for (Entry<String, DbSourceProxyRoot> entry : dblist.entrySet())
+    {
+      DbSourceProxyRoot proxy = entry.getValue();
+      if (proxy instanceof DbRoot)
+      {
+        proxy = setProxy((DbRoot) proxy, dblist);
+      }
+      dbs.add((DbSourceProxy) proxy);
+    }
     Collections.sort(dbs, proxyComparator);
     return dbs;
   }
 
+  class DbRoot implements DbSourceProxyRoot
+  {
+
+    private String sourceName;
+
+    private String className;
+
+    DbRoot(String sourceName, String className)
+    {
+      this.sourceName = sourceName;
+      this.className = className;
+    }
+
+    @Override
+    public String getDbSource()
+    {
+      return sourceName;
+    }
+
+    /**
+     * lazy class creation
+     * 
+     * @return the actual proxy object
+     */
+    public DbSourceProxy getProxy()
+    {
+      try
+      {
+        System.err.println("ASeqFetch " + className);
+        return (DbSourceProxy) Class.forName(className).newInstance();
+      } catch (Exception e)
+      {
+        // Serious problems if this happens.
+        throw new Error(MessageManager.getString(
+                "error.dbrefsource_implementation_exception"), e);
+      }
+    }
+
+  }
+
   /**
    * constructs an instance of the proxy and registers it as a valid dbrefsource
    * 
-   * @param dbSourceProxy
+   * @param dbSourceProxyClass
    *          reference for class implementing
    *          jalview.ws.seqfetcher.DbSourceProxy
    */
   protected void addDBRefSourceImpl(
-          Class<? extends DbSourceProxy> dbSourceProxy)
+          Class<? extends DbSourceProxy> dbSourceProxyClass)
           throws IllegalArgumentException
   {
     DbSourceProxy proxy = null;
     try
     {
-      DbSourceProxy proxyObj = dbSourceProxy.getConstructor().newInstance();
-      proxy = proxyObj;
+      proxy = dbSourceProxyClass.getConstructor().newInstance();
     } catch (IllegalArgumentException e)
     {
       throw e;
@@ -347,13 +394,18 @@ public class ASequenceFetcher
     addDbRefSourceImpl(proxy);
   }
 
+  public void addDBRefSourceImpl(String sourceName, String className)
+  {
+    addDbRefSourceImpl(new DbRoot(sourceName, className));
+  }
+
   /**
    * add the properly initialised DbSourceProxy object 'proxy' to the list of
    * sequence fetchers
    * 
    * @param proxy
    */
-  protected void addDbRefSourceImpl(DbSourceProxy proxy)
+  void addDbRefSourceImpl(DbSourceProxyRoot proxy)
   {
     if (proxy != null)
     {
@@ -361,25 +413,31 @@ public class ASequenceFetcher
       {
         fetchableDbs = new Hashtable<>();
       }
-      Map<String, DbSourceProxy> slist = fetchableDbs
-              .get(proxy.getDbSource());
+      String key = proxy.getDbSource();
+      Map<String, DbSourceProxyRoot> slist = fetchableDbs.get(key);
       if (slist == null)
       {
-        fetchableDbs.put(proxy.getDbSource(),
-                slist = new Hashtable<>());
+        fetchableDbs.put(key, slist = new Hashtable<>());
+      }
+      if (proxy instanceof DbRoot)
+      {
+        slist.put("", proxy);
+      }
+      else
+      {
+        slist.put(((DbSourceProxy) proxy).getDbName(), proxy);
       }
-      slist.put(proxy.getDbName(), proxy);
     }
   }
 
   /**
    * select sources which are implemented by instances of the given class
    * 
-   * @param class
+   * @param class1
    *          that implements DbSourceProxy
    * @return null or vector of source names for fetchers
    */
-  public String[] getDbInstances(Class class1)
+  public String[] getDbInstances(Class<?> class1)
   {
     if (!DbSourceProxy.class.isAssignableFrom(class1))
     {
@@ -392,20 +450,25 @@ public class ASequenceFetcher
     {
       return null;
     }
-    String[] sources = null;
     Vector<String> src = new Vector<>();
-    Enumeration<String> dbs = fetchableDbs.keys();
-    while (dbs.hasMoreElements())
+    for (String dbSource : fetchableDbs.keySet())
     {
-      String dbn = dbs.nextElement();
-      for (DbSourceProxy dbp : fetchableDbs.get(dbn).values())
+      Map<String, DbSourceProxyRoot> dblist = fetchableDbs.get(dbSource);
+      for (Entry<String, DbSourceProxyRoot> entry : dblist.entrySet())
       {
-        if (class1.isAssignableFrom(dbp.getClass()))
+        DbSourceProxyRoot proxy = entry.getValue();
+        if (proxy instanceof DbRoot)
         {
-          src.addElement(dbn);
+          proxy = setProxy((DbRoot) proxy, dblist);
+        }
+        Class<?> c = proxy.getClass();
+        if (class1 == c || class1.isAssignableFrom(c))
+        {
+          src.addElement(dbSource);
         }
       }
     }
+    String[] sources = null;
     if (src.size() > 0)
     {
       src.copyInto(sources = new String[src.size()]);
@@ -413,10 +476,24 @@ public class ASequenceFetcher
     return sources;
   }
 
-  public DbSourceProxy[] getDbSourceProxyInstances(Class class1)
+  private DbSourceProxyRoot setProxy(DbRoot root,
+          Map<String, DbSourceProxyRoot> dblist)
+  {
+    DbSourceProxy proxy = root.getProxy();
+    // Time to create the actual proxy
+    dblist.remove("");
+    dblist.put(proxy.getDbName(), proxy);
+    return proxy;
+  }
+
+  public DbSourceProxy[] getDbSourceProxyInstances(Class<?> class1)
   {
+    if (fetchableDbs == null)
+    {
+      return null;
+    }
     List<DbSourceProxy> prlist = new ArrayList<>();
-    for (String fetchable : getSupportedDb())
+    for (String fetchable : fetchableDbs.keySet())
     {
       for (DbSourceProxy pr : getSourceProxy(fetchable))
       {