[MOBY-guts] biomoby commit

Paul Gordon gordonp at dev.open-bio.org
Wed Jul 16 20:33:32 UTC 2008


gordonp
Wed Jul 16 16:33:32 EDT 2008
Update of /home/repository/moby/moby-live/Java/src/main/org/biomoby/client
In directory dev.open-bio.org:/tmp/cvs-serv16156/src/main/org/biomoby/client

Modified Files:
	CentralImpl.java 
Log Message:
Added methods to get a CentralImpl from a Registry
moby-live/Java/src/main/org/biomoby/client CentralImpl.java,1.55,1.56
===================================================================
RCS file: /home/repository/moby/moby-live/Java/src/main/org/biomoby/client/CentralImpl.java,v
retrieving revision 1.55
retrieving revision 1.56
diff -u -r1.55 -r1.56
--- /home/repository/moby/moby-live/Java/src/main/org/biomoby/client/CentralImpl.java	2008/03/03 11:54:28	1.55
+++ /home/repository/moby/moby-live/Java/src/main/org/biomoby/client/CentralImpl.java	2008/07/16 20:33:32	1.56
@@ -7,6 +7,7 @@
 
 package org.biomoby.client;
 
+import org.biomoby.registry.meta.Registry;
 import org.biomoby.shared.Central;
 import org.biomoby.shared.MobyData;
 import org.biomoby.shared.MobyDataType;
@@ -48,8 +49,11 @@
 import java.io.File;
 import java.io.FileOutputStream;
 import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.LineNumberReader;
 import java.io.PrintStream;
 import java.io.InputStream;
+import java.lang.reflect.Constructor;
 import java.net.HttpURLConnection;
 import java.net.MalformedURLException;
 import java.net.URL;
@@ -62,6 +66,7 @@
 import java.util.TreeMap;
 import java.util.Comparator;
 import java.util.zip.GZIPInputStream;
+import java.util.logging.*;
 
 /**
  * A default implementation of the
@@ -109,12 +114,25 @@
     private String uri;
     protected boolean debug = false;
 
+    /** Common central used to if getDefaultCentral() is called */
+    protected static Map<String,Central> defaultCentrals = new HashMap<String,Central>();
+
     /** Default location (endpoint) of a Moby registry. */
     public static final String DEFAULT_ENDPOINT = "http://moby.ucalgary.ca/moby/MOBY-Central.pl";
 
     /** Default namespace used by the contacted Moby registry. */
     public static final String DEFAULT_NAMESPACE = "http://moby.ucalgary.ca/MOBY/Central";
 
+    /**
+     * The META-INF resource file that will be checked to determine what
+     * default class should be instantiated in order to create a Central Implementation     
+     * when getDefaultCentral() is called.
+     */
+    public static final String CENTRAL_IMPL_RESOURCE_NAME = "org.biomoby.shared.CentralDefaultImpl";
+    /** The class to use for getDefaultCentral if all else fails */
+    public static final String DEFAULT_CENTRAL_IMPL_CLASSNAME = "org.biomoby.client.CentralDigestCachedImpl";
+    private static Logger logger = Logger.getLogger("org.biomoby.client.CentralImpl");
+
    /**
     * Thread local that gives each thread its own
     * DocumentBuilderFactory (since it is not thread-safe). Code taken
@@ -1836,6 +1854,80 @@
     private static String CHECKED_URI = null;
     
     /**
+     * Using this method to get a Central object will ensure that other parts of the org.biomoby.shared 
+     * class hierarchy that implicitly check the registry will use the same cache.  Otherwise, methods
+     * such as MobyNamespace.getNamespace() must be passed a Central object parameter as well.
+     *
+     * @return a CentralImpl using the default Central URI, and currently a class implementing a caching mechanism
+     */
+    public static Central getDefaultCentral() throws MobyException{
+	return getDefaultCentral(null);
+    }
+
+    public static Central getDefaultCentral(Registry reg) throws MobyException{
+	if(reg == null && defaultCentrals.containsKey("")){
+	    return defaultCentrals.get("");
+	}
+	else if(reg != null && defaultCentrals.containsKey(reg.getEndpoint())){
+	    return defaultCentrals.get(reg.getEndpoint());
+	}
+
+	String className = DEFAULT_CENTRAL_IMPL_CLASSNAME;
+	ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
+	URL resURL = classLoader.getResource("META-INF/"+CENTRAL_IMPL_RESOURCE_NAME);
+	if(resURL != null){
+	    try{
+		LineNumberReader reader = new LineNumberReader(new InputStreamReader(resURL.openStream()));
+		for(String line = reader.readLine(); line != null; line = reader.readLine()){
+		    if(!line.trim().startsWith("#")){
+			className = line.trim();
+			break;
+		    }
+		}
+	    } catch(Exception e){
+		logger.log(Level.WARNING,
+			   "Error reading " + resURL,
+			   e);
+	    }
+	}
+	try{
+	    Class clazz = Class.forName(className);
+	    if(reg == null){  // should use default nullary c-tor
+		defaultCentrals.put("", (Central) clazz.newInstance());
+	    }
+	    else{  // should have (String endpoint, String namespace) c-tor
+		for(Constructor ctor: clazz.getDeclaredConstructors()){
+		    Class[] params = ctor.getParameterTypes();
+		    if(params.length == 2 && params[0].getName().equals("java.lang.String") &&
+		       params[1].getName().equals("java.lang.String") ){
+			defaultCentrals.put(reg.getEndpoint(),
+					    (Central) ctor.newInstance(reg.getEndpoint(), reg.getNamespace()));
+			break;
+		    }
+		}
+		if(!defaultCentrals.containsKey(reg.getEndpoint())){
+		    logger.log(Level.WARNING,
+			       "Could not find required (String endpoint, String namespace)" +
+			       "constructor for class " + className);
+		}
+	    }
+	} catch(Exception e){
+	    logger.log(Level.WARNING,
+		       "Could not load class " + className,
+		       e);
+	    if(reg == null){
+		defaultCentrals.put("", new CentralImpl());  //fallback to this class, no caching, etc.
+	    }
+	    else{
+		defaultCentrals.put(reg.getEndpoint(), 
+				    new CentralImpl(reg.getEndpoint(), reg.getNamespace()));
+	    }
+	}
+
+	return defaultCentrals.get(reg == null ? "" : reg.getEndpoint());
+    }
+
+    /**
      * 
      * @return a String representing the Default mobycentral endpoint. If the
      *         system property 'moby.check.default' exists and is set to true,




More information about the MOBY-guts mailing list