/* Rajarshi Guha * 14/12/2004 * * Updated 03/03/2006 - Added calls to the MW and MF WS's * Updated 01/03/2006 - Synced with latest CDK. Added usage message * * Based on code written by Marty Hall */ import org.apache.axis.client.Call; import org.apache.axis.client.Service; import org.apache.axis.encoding.XMLType; import org.apache.axis.AxisFault; import javax.xml.namespace.QName; import javax.xml.rpc.ParameterMode; import java.util.Vector; import java.util.Iterator; public class CDKwsClient { private Call call; private QName qn; private boolean requestError; private String requestErrorMessage; // Initialize the Axis setup public CDKwsClient(String host) { String endpoint = "http://localhost:8080/axis/services/CDKws"; if (host != null) { endpoint = host; } try { Service service = new Service(); call = (Call) service.createCall(); call.setTargetEndpointAddress( new java.net.URL(endpoint) ); } catch (Throwable t) { handleError("CDKwsClient (constructor) had an Exception.",t); } } // Return true in an error was found public boolean error() { return requestError; } // Return the error message public String getErrorText() { String msg; if (requestError) { msg = requestErrorMessage; } else { msg = ""; } return msg; } public String getFPString(String[] args) { try { call.removeAllParameters(); call.setOperationName( "getFingerprintString" ); call.addParameter( "s", XMLType.XSD_STRING, ParameterMode.IN); call.setReturnType( XMLType.XSD_STRING ); String ret = (String)call.invoke( new Object[] { args[0] }); System.out.println("CDKwsClient got result : "); System.out.println(ret); return ret; } catch (Throwable t) { handleError("CDKwsClient (getFPString) had an Exception.",t); return null; } } public Vector getFPVector(String[] args) { try { call.removeAllParameters(); call.setOperationName( "getFingerprintVector" ); call.addParameter( "s", XMLType.XSD_STRING, ParameterMode.IN); call.setReturnType( new QName("Vector"), Vector.class ); Vector ret = (Vector)call.invoke( new Object[] { args[0] }); System.out.println("CDKwsClient got result : "); Iterator it = ret.iterator(); while (it.hasNext()) { System.out.println( it.next() ); } return ret; } catch (Throwable t) { handleError("CDKwsClient (getFPVector) had an Exception.",t); return null; } } public void getWeightAndFormula(String[] args) { System.out.println("\nMolecular weight & formula"); try { call.removeAllParameters(); call.setOperationName( "getMolecularWeight" ); call.addParameter( "s", XMLType.XSD_STRING, ParameterMode.IN); call.setReturnType( XMLType.XSD_FLOAT ); Float ret = (Float)call.invoke( new Object[] { args[0] }); System.out.println("CDKwsClient: MW = "+ret); call.removeAllParameters(); call.setOperationName( "getMolecularFormula" ); call.addParameter( "s", XMLType.XSD_STRING, ParameterMode.IN); call.setReturnType( XMLType.XSD_STRING ); String ret2 = (String)call.invoke( new Object[] { args[0] }); System.out.println("CDKwsClient: MF = "+ret2); call.removeAllParameters(); call.setOperationName( "getHTMLMolecularFormula" ); call.addParameter( "s", XMLType.XSD_STRING, ParameterMode.IN); call.setReturnType( XMLType.XSD_STRING ); String ret3 = (String)call.invoke( new Object[] { args[0] }); System.out.println("CDKwsClient: MF (HTML'ized) = "+ret3); } catch (Throwable t) { handleError("CDKwsClient (getFPVector) had an Exception.",t); } } // Set error flags, print stack trace public void handleError(String text, Throwable t) { requestError = true; requestErrorMessage = t.toString(); System.out.println(text); System.out.println(t); t.printStackTrace(); return; } // Test program public static void main(String [] args) { if (args.length < 1) { System.out.println("Usage: java CDKwsClient SMILES [host]"); System.out.println("\nIf host is not specified then http://localhost:8080/axis/services/CDKws is used\n"); System.exit(0); } String host = null; if (args.length == 3) { host = args[2]; } CDKwsClient sr = new CDKwsClient(host); System.out.println("\nAccessing the java.lang.String version"); sr.getFPString(args); System.out.println("\nAccessing the java.util.Vector version"); sr.getFPVector(args); sr.getWeightAndFormula(args); } }