[MOBY-guts] biomoby commit

Eddie Kawas kawas at dev.open-bio.org
Thu Dec 6 15:59:52 UTC 2007


kawas
Thu Dec  6 10:59:52 EST 2007
Update of /home/repository/moby/moby-live/Java/src/main/org/biomoby/client
In directory dev.open-bio.org:/tmp/cvs-serv24682/Java/src/main/org/biomoby/client

Modified Files:
	CentralImpl.java 
Log Message:
updated the default URI/URL
added 2 new methods:
   getDefaultURL/getDefaultURI
these methods query http://biomoby.org/mobycentral iff the system property 'moby.check.default' equals true, otherwise the hardcoded URL/URI is returned.
moby-live/Java/src/main/org/biomoby/client CentralImpl.java,1.49,1.50
===================================================================
RCS file: /home/repository/moby/moby-live/Java/src/main/org/biomoby/client/CentralImpl.java,v
retrieving revision 1.49
retrieving revision 1.50
diff -u -r1.49 -r1.50
--- /home/repository/moby/moby-live/Java/src/main/org/biomoby/client/CentralImpl.java	2007/05/31 13:42:51	1.49
+++ /home/repository/moby/moby-live/Java/src/main/org/biomoby/client/CentralImpl.java	2007/12/06 15:59:52	1.50
@@ -34,6 +34,13 @@
 import org.apache.axis.AxisFault;
 import org.apache.axis.client.Call;
 import org.apache.axis.client.Service;
+import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
+import org.apache.commons.httpclient.Header;
+import org.apache.commons.httpclient.HttpClient;
+import org.apache.commons.httpclient.HttpException;
+import org.apache.commons.httpclient.HttpStatus;
+import org.apache.commons.httpclient.methods.HeadMethod;
+import org.apache.commons.httpclient.params.HttpMethodParams;
 
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
@@ -100,10 +107,10 @@
     protected boolean debug = false;
 
     /** Default location (endpoint) of a Moby registry. */
-    public static final String DEFAULT_ENDPOINT = "http://mobycentral.icapture.ubc.ca/cgi-bin/MOBY05/mobycentral.pl";
+    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://mobycentral.icapture.ubc.ca/MOBY/Central";
+    public static final String DEFAULT_NAMESPACE = "http://moby.ucalgary.ca/moby/MOBY/Central";
 
    /**
     * Thread local that gives each thread its own
@@ -1822,7 +1829,108 @@
 		}
 	    };
     }
+    
+    // cache URL/URI so we only check once 
+    private static String CHECKED_URL = null;
+    private static String CHECKED_URI = null;
+    
+    /**
+     * 
+     * @return a String representing the Default mobycentral endpoint. If the
+     *         system property 'moby.check.default' exists and is set to true,
+     *         then the URL http://biomoby.org/mobycentral is queried and the
+     *         default central endpoint is returned, otherwise DEFAULT_ENDPOINT
+     *         is returned.
+     */
+    public static String getDefaultURL() {
+	boolean check = false;
+	try {
+	    check = Boolean.getBoolean("moby.check.default");
+	} catch (Exception e) {
+	    
+	}
+	
+	if (check) {
+	    // return the last checked url if we have done this before
+	    if (CHECKED_URL != null && CHECKED_URL.trim() != "") {
+		return CHECKED_URL; 
+	    }
+	    
+	    // create a HttpClient object
+	    HttpClient client = new HttpClient();
+	    // set up the Head method
+	    HeadMethod method = new HeadMethod("http://biomoby.org/mobycentral");
+	    // do not follow redirects or we will get a 411 error
+	    method.setFollowRedirects(false);
+	    // retry 3 times
+	    method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,new DefaultHttpMethodRetryHandler(3, false));
+	    // set the user agent ... should probably make this something more reasonable
+	    method.getParams().setParameter(HttpMethodParams.USER_AGENT,"jMoby/1.0");
+	    try {
+		// Execute the method.
+		int statusCode = client.executeMethod(method);
 
+		if (statusCode != HttpStatus.SC_MOVED_PERMANENTLY) {
+		    System.err.println("Method failed: "
+			    + method.getStatusLine());
+		} else {
+		    try {
+			String location = method.getResponseHeader("location").getValue();
+			CHECKED_URL = location;
+			try {
+			    CHECKED_URI = "http://" + (new URL(CHECKED_URL).getAuthority()) + "/MOBY/Central";
+			} catch (MalformedURLException murle ) {
+			    CHECKED_URI = DEFAULT_NAMESPACE;
+			}
+			return CHECKED_URL;
+		    } catch (NullPointerException npe) {
+			return DEFAULT_ENDPOINT;
+		    }
+		}
+	    } catch (HttpException e) {
+		System.err.println("Fatal protocol violation: "
+			+ e.getMessage());
+		e.printStackTrace();
+	    } catch (IOException e) {
+		System.err.println("Fatal transport error: " + e.getMessage());
+		e.printStackTrace();
+	    } finally {
+		// Release the connection.
+		method.releaseConnection();
+	    } 
+	    
+	} else {
+	    return DEFAULT_ENDPOINT;
+	}
+	return DEFAULT_ENDPOINT;
+    }
+    
+    /**
+     * 
+     * @return a String representing the default mobycentral uri. If the
+     *         system property 'moby.check.default' exists and is set to true,
+     *         then the URL http://biomoby.org/mobycentral is queried and the
+     *         default central namespace is returned, otherwise DEFAULT_NAMESPACE
+     *         is returned.
+     */
+    public static String getDefaultURI() {
+	boolean check = false;
+	try {
+	    check = Boolean.getBoolean("moby.check.default");
+	} catch (Exception e) {
+	    
+	}
+	if (check) {
+	    if (CHECKED_URI != null && CHECKED_URI.trim() != "") {
+		return CHECKED_URI; 
+	    }
+	    // need to check ... 
+	    getDefaultURL();
+	    return CHECKED_URI;
+	} else {
+	    return DEFAULT_NAMESPACE;
+	}
+    }
     /**************************************************************************
      * Convert non-suitable characters in a XML string into their
      * entity references. <p>




More information about the MOBY-guts mailing list