Skip to content

Instantly share code, notes, and snippets.

@lindenb
Created May 17, 2010 19:55
Show Gist options
  • Save lindenb/404157 to your computer and use it in GitHub Desktop.
Save lindenb/404157 to your computer and use it in GitHub Desktop.
/**
* test if the web services listed in http://www.biocatalogue.org/ are
* valid for wsimport
* Author : Pierre Lindenbaum PhD
* http://plindenbaum.blogspot.com
*/
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathFactory;
//see http://code.google.com/p/lindenb/source/browse/trunk/src/java/org/lindenb/xml/NamespaceContextImpl.java
import org.lindenb.xml.NamespaceContextImpl;
import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
public class StateOfBiocatalogue
{
private File generated;
private DocumentBuilder builder;
private XPathExpression servicesExpr;
private XPathExpression titleExpr;
private XPathExpression wsdlExpr;
StateOfBiocatalogue() throws Exception
{
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
factory.setCoalescing(true);
factory.setExpandEntityReferences(true);
factory.setValidating(false);
factory.setIgnoringComments(true);
factory.setNamespaceAware(true);
factory.setIgnoringElementContentWhitespace(true);
this.builder=factory.newDocumentBuilder();
XPathFactory xpathFactory=XPathFactory.newInstance();
XPath xpath=xpathFactory.newXPath();
NamespaceContextImpl nsContext=new NamespaceContextImpl();
nsContext.setPrefixURI("rest", "http://www.biocatalogue.org/2009/xml/rest");
nsContext.setPrefixURI("xlink", "http://www.w3.org/1999/xlink");
nsContext.setPrefixURI("dc", "http://purl.org/dc/elements/1.1/");
xpath.setNamespaceContext(nsContext);
this.servicesExpr= xpath.compile("/rest:services/rest:results/rest:service/@xlink:href");
this.titleExpr= xpath.compile("/rest:service/dc:title");
this.wsdlExpr= xpath.compile("/rest:service/rest:variants/rest:soapService/rest:wsdlLocation");
}
private int cleanup(File generated)
{
int count=0;
if(!generated.exists()) return 0;
if(generated.isDirectory())
{
for(File f2: generated.listFiles())
{
count+=cleanup(f2);
}
generated.delete();
}
else if(generated.isFile())
{
if(generated.getName().endsWith(".class")) ++count;
generated.delete();
}
return count;
}
private Document parseXML(String url) throws Exception
{
System.err.println(url);
int n_try=0;
Document dom= null;
while(n_try<10)
{
try
{
dom=this.builder.parse(url);
break;
}
catch(Exception err)
{
Thread.sleep(30000);//wait 30 secs
System.err.println("Trying again.... "+url);
n_try++;
continue;
}
}
if(dom==null) throw new IOException("Cannot read "+url);
return dom;
}
private void lookup(PrintWriter out,String url) throws Exception
{
File generated=new File(System.getProperty("java.io.tmpdir","/tmp/"),"generated");
String javaHome= System.getProperty("java.home");
Document dom=null;
try
{
dom=parseXML(url+".xml");
}
catch(Exception err)
{
out.print(url);
out.print("\t");
out.print("[error]");
out.print("\tNO:CONNECTION_FAILED");
out.println();
return;
}
NodeList wsdls=(NodeList)this.wsdlExpr.evaluate(dom, XPathConstants.NODESET);
if(wsdls.getLength()==0)
{
out.print(url);
out.print("\t");
out.print(this.titleExpr.evaluate(dom,XPathConstants.STRING));
out.print("\tNO-WSDL");
out.println();
return;
}
for(int i=0;i< wsdls.getLength();++i)
{
out.print(url);
out.print("\t");
out.print(this.titleExpr.evaluate(dom,XPathConstants.STRING));
out.print("\tYES");
out.print("\t");
String wsdlURI=wsdls.item(i).getTextContent();
out.print(wsdlURI);
generated.mkdir();
String cmdarray[]={javaHome+"/bin/wsimport",
//"-httpproxy:host:port",
"-d",generated.toString(),wsdlURI
};
StringBuilder message=new StringBuilder();
Process proc=Runtime.getRuntime().exec(cmdarray);
System.err.println("run : "+Arrays.toString(cmdarray));
InputStream in=proc.getInputStream();
int c;
while(in!=null && (c=in.read())!=-1)
{
message.append((char)c);
}
int rez=proc.waitFor();
if(in!=null) in.close();
int nClass=cleanup(generated);
if(rez==0 && nClass==0) rez=-1;
out.print("\t"+(rez==0?"SUCCESS":"FAILURE"));
if(rez!=0)
{
out.print("\t");
for(String s:message.toString().replace("\r", "\n").replace('\t', ' ').split("[\n]"))
{
s=s.trim();
if(!s.startsWith("[ERROR]")) continue;
out.print(s);
break;
}
}
else
{
out.print("\t"+(nClass)+" classes");
}
out.println();
}
out.flush();
}
private void run(PrintWriter out) throws Exception
{
int service_page=0;
while(true)
{
service_page++;
Document dom=parseXML("http://www.biocatalogue.org/services.xml?page="+service_page);
NodeList serviceList=(NodeList)this.servicesExpr.evaluate(dom, XPathConstants.NODESET);
for(int i=0;i< serviceList.getLength();++i)
{
lookup(out,Attr.class.cast(serviceList.item(i)).getValue());
}
if(serviceList.getLength()==0) break;
}
System.err.println("\nDone\n");
}
public static void main(String[] args)
{
try {
PrintWriter out=new PrintWriter(new File("biocatalog.xls"));
new StateOfBiocatalogue().run(out);
out.flush();
out.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment