[MOBY-guts] biomoby commit

Martin Senger senger at pub.open-bio.org
Mon Nov 21 17:17:28 UTC 2005


senger
Mon Nov 21 12:17:27 EST 2005
Update of /home/repository/moby/moby-live/Java/src/main/org/biomoby/client
In directory pub.open-bio.org:/tmp/cvs-serv9749/src/main/org/biomoby/client

Modified Files:
	CentralImpl.java 
Log Message:


moby-live/Java/src/main/org/biomoby/client CentralImpl.java,1.36,1.37
===================================================================
RCS file: /home/repository/moby/moby-live/Java/src/main/org/biomoby/client/CentralImpl.java,v
retrieving revision 1.36
retrieving revision 1.37
diff -u -r1.36 -r1.37
--- /home/repository/moby/moby-live/Java/src/main/org/biomoby/client/CentralImpl.java	2005/11/20 12:30:51	1.36
+++ /home/repository/moby/moby-live/Java/src/main/org/biomoby/client/CentralImpl.java	2005/11/21 17:17:27	1.37
@@ -24,6 +24,8 @@
 
 import org.tulsoft.tools.soap.axis.AxisUtils;
 
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.DocumentBuilder;
 import org.w3c.dom.Document;
 import org.w3c.dom.Element;
 import org.w3c.dom.Node;
@@ -94,7 +96,6 @@
 
     private URL endpoint;
     private String uri;
-    private javax.xml.parsers.DocumentBuilder docBuilder;
     protected boolean debug = false;
 
     /** Default location (endpoint) of a Moby registry. */
@@ -103,6 +104,20 @@
     /** Default namespace used by the contacted Moby registry. */
     public static final String DEFAULT_NAMESPACE = "http://mobycentral.icapture.ubc.ca/MOBY/Central";
 
+   /**
+    * Thread local that gives each thread its own
+    * DocumentBuilderFactory (since it is not thread-safe). Code taken
+    * from Apache's JaxpUtils.
+    */
+   public static ThreadLocal DOCUMENT_BUILDER_FACTORIES = new ThreadLocal() {
+	   protected synchronized Object initialValue() {
+	       DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
+	       dbf.setNamespaceAware (true);
+	       return dbf;
+	   }
+       };
+
+
     /*************************************************************************
      * Default constructor. It connects to a default Moby registry
      * (as defined in {@link #DEFAULT_ENDPOINT}) using a default namespace
@@ -147,21 +162,27 @@
 	}
 	this.uri = namespace;
 
-	// This method should work on almost all platforms to get an XML parser instance
-        try {
-            javax.xml.parsers.DocumentBuilderFactory dbf = 
-		javax.xml.parsers.DocumentBuilderFactory.newInstance();
-    	    dbf.setNamespaceAware(true);	
-	    docBuilder = dbf.newDocumentBuilder();
-        } catch (Exception e) {
-            throw new MobyException ("Could not configure an XML parser: " + e, e);
-        }
-
 	cache = new Hashtable();
 	useCache = true;
     }
 
     /*************************************************************************
+     * Loads a DOM Document from an InputStream. Uses thread-safe
+     * mechanism.
+     *************************************************************************/
+    public static Document loadDocument (InputStream input)
+	throws MobyException {
+	try {
+	    DocumentBuilderFactory dbf
+		= (DocumentBuilderFactory)DOCUMENT_BUILDER_FACTORIES.get();
+	    DocumentBuilder db = dbf.newDocumentBuilder();
+	    return (db.parse (input));
+	} catch (Exception e) {
+	    throw new MobyException ("Problem with reading XML input: " + e.toString(), e);
+	}
+    }
+
+    /*************************************************************************
      * Call 'method' with 'parameters' and return its result.
      *************************************************************************/
     protected Object doCall (String method, Object[] parameters)
@@ -237,12 +258,7 @@
 	String id = "", success = "0", message = "", rdf = "";
 
 	// parse returned XML
-	Document document = null;
-	try {
-	    document=docBuilder.parse(new ByteArrayInputStream(xml.getBytes()));
-	} catch (Exception e) {
-	    throw new MobyException(e.toString(), e);
-	}
+	Document document = loadDocument (new ByteArrayInputStream (xml.getBytes()));
 	Element root = document.getDocumentElement();
 
 	NodeList children = root.getChildNodes();
@@ -465,15 +481,8 @@
      *************************************************************************/
     public MobyService[] extractServices (String xml)
 	throws MobyException {
-//    	if (xmk == null)
-//			throw new MobyException ()
-	Document document = null;
-	try {
-	    document = docBuilder.parse(new ByteArrayInputStream(xml.getBytes()));
-	} catch (Exception e) {
-	    throw new MobyException(e.toString(), e);
-	}
 
+	Document document = loadDocument (new ByteArrayInputStream (xml.getBytes()));
 	NodeList list = document.getElementsByTagName ("Service");
 	MobyService[] results = new MobyService [list.getLength()];
 	for (int i = 0; i < list.getLength(); i++) {
@@ -671,16 +680,9 @@
 
 	String result = (String)doCall ("retrieveServiceNames",
 					new Object[] {});
-
 	// parse returned XML
 	Map results = new TreeMap (getStringComparator());
-	Document document = null;
-	try {
-	    document = docBuilder.parse(new ByteArrayInputStream(result.getBytes()));
-	} catch (Exception e) {
-	    throw new MobyException(e.toString(), e);
-	}
-
+	Document document = loadDocument (new ByteArrayInputStream (result.getBytes()));
 	NodeList list = document.getElementsByTagName ("serviceName");
 	for (int i = 0; i < list.getLength(); i++) {
 	    Element elem = (Element)list.item (i);
@@ -720,13 +722,7 @@
 
 	// parse returned XML
 	Map results = new TreeMap (getStringComparator());
-	Document document = null;
-	try {
-	    document = docBuilder.parse (new ByteArrayInputStream (result.getBytes()));
-	} catch (Exception e) {
-	    throw new MobyException (e.toString(), e);
-	}
-
+	Document document = loadDocument (new ByteArrayInputStream (result.getBytes()));
 	NodeList list = document.getElementsByTagName ("serviceName");
 	for (int i = 0; i < list.getLength(); i++) {
 	    Element elem = (Element)list.item (i);
@@ -771,13 +767,7 @@
 					new Object[] {});
 
 	// parse returned XML
-	Document document = null;
-	try {
-	    document = docBuilder.parse(new ByteArrayInputStream(result.getBytes()));
-	} catch (Exception e) {
-	    throw new MobyException(e.toString(), e);
-	}
-
+	Document document = loadDocument (new ByteArrayInputStream (result.getBytes()));
 	NodeList list = document.getElementsByTagName ("serviceProvider");
 	String[] results = new String [list.getLength()];
 	for (int i = 0; i < list.getLength(); i++)
@@ -830,12 +820,7 @@
 	throws MobyException {
 
 	// parse returned XML
-	Document document = null;
-	try {
-	    document = docBuilder.parse(new ByteArrayInputStream(result.getBytes()));}
-	catch(Exception e){throw new MobyException(e.toString(), e);
-		}
-
+	Document document = loadDocument (new ByteArrayInputStream (result.getBytes()));
 	NodeList list = document.getElementsByTagName ("serviceType");
 	if (list == null || list.getLength() == 0)
 	    return new MobyServiceType[] {};
@@ -886,13 +871,7 @@
 	throws MobyException {
 
 	// parse returned XML
-	Document document = null;
-	try {
-	    document = docBuilder.parse(new ByteArrayInputStream(result.getBytes()));
-	} catch (Exception e) {
-	    throw new MobyException(e.toString(), e);
-	}
-
+	Document document = loadDocument (new ByteArrayInputStream (result.getBytes()));
 	NodeList list = document.getDocumentElement().getElementsByTagName ("Namespace");
 	if (list == null || list.getLength() == 0) {
 	    return new MobyNamespace[] {};
@@ -958,13 +937,7 @@
 
 	// parse returned XML
 	Map results = new TreeMap (getStringComparator());
-	Document document = null;
-	try {
-	    document = docBuilder.parse(new ByteArrayInputStream(result.getBytes()));
-	} catch (Exception e) {
-	    throw new MobyException(e.toString(), e);
-	}
-
+	Document document = loadDocument (new ByteArrayInputStream (result.getBytes()));
 	NodeList list = document.getElementsByTagName ("Object");
 	for (int i = 0; i < list.getLength(); i++) {
 	    Element elem = (Element)list.item (i);
@@ -1025,13 +998,7 @@
 	throws MobyException, NoSuccessException {
 
 	// parse returned XML
-	Document document = null;
-	try {
-	    document = docBuilder.parse(new ByteArrayInputStream (xmlSource.getBytes()));
-	} catch (Exception e) {
-	    throw new MobyException(e.toString(), e);
-	}
-
+	Document document = loadDocument (new ByteArrayInputStream (xmlSource.getBytes()));
 	NodeList list = document.getElementsByTagName ("retrieveObjectDefinition");
 	if (list == null || list.getLength() == 0)
 	    throw new NoSuccessException ("Data Type name was not found.",
@@ -1135,10 +1102,7 @@
 			    });
 
 	// parse returned XML
-	Document document = null;
-	try{document=docBuilder.parse(new ByteArrayInputStream(result.getBytes()));}
-	catch(Exception e){throw new MobyException(e.toString(), e);}
-
+	Document document = loadDocument (new ByteArrayInputStream (result.getBytes()));
 	Element service = document.getDocumentElement();
 	Node wsdl = service.getFirstChild();
 	if (wsdl == null)
@@ -1555,10 +1519,7 @@
 
 	// parse returned XML
 	Vector v = new Vector();
-	Document document = null;
-	try{document=docBuilder.parse(new ByteArrayInputStream(result.getBytes()));}
-	catch(Exception e){throw new MobyException(e.toString(), e);}
-
+	Document document = loadDocument (new ByteArrayInputStream (result.getBytes()));
 	NodeList list = document.getElementsByTagName ("Relationship");
 	for (int i = 0; i < list.getLength(); i++) {
 	    Element elem = (Element)list.item (i);
@@ -1611,10 +1572,7 @@
 
 	// parse returned XML
 	Map results = new HashMap();
-	Document document = null;
-	try{document=docBuilder.parse(new ByteArrayInputStream(result.getBytes()));}
-	catch(Exception e){throw new MobyException(e.toString(), e);}
-
+	Document document = loadDocument (new ByteArrayInputStream (result.getBytes()));
 	NodeList list = document.getElementsByTagName ("Relationship");
 
 	for (int i = 0; i < list.getLength(); i++) {
@@ -1668,10 +1626,7 @@
 
 	// parse returned XML
 	Vector v = new Vector();
-	Document document = null;
-	try{document=docBuilder.parse(new ByteArrayInputStream(result.getBytes()));}
-	catch(Exception e){throw new MobyException(e.toString(), e);}
-
+	Document document = loadDocument (new ByteArrayInputStream (result.getBytes()));
 	NodeList list = document.getElementsByTagName ("Relationship");
 
 	// it should always be just one element in this list
@@ -1730,13 +1685,7 @@
 
 	// parse returned XML
 	Vector v = new Vector();
-	Document document = null;
-	try {
-	    document = docBuilder.parse (new ByteArrayInputStream (result.getBytes()));
-	} catch (Exception e) {
-	    throw new MobyException(e.toString(), e);
-	}
-
+	Document document = loadDocument (new ByteArrayInputStream (result.getBytes()));
 	NodeList list = document.getElementsByTagName ("Resource");
 	for (int i = 0; i < list.getLength(); i++) {
 	    Element elem = (Element)list.item (i);




More information about the MOBY-guts mailing list