package test; import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; import java.net.MalformedURLException; import java.net.URL; import java.rmi.RemoteException; import java.util.Hashtable; import java.util.Iterator; import java.util.Set; import javax.xml.namespace.QName; import javax.xml.rpc.ServiceException; import org.apache.axis.AxisFault; import org.apache.axis.client.Call; import org.apache.axis.client.Service; import org.w3c.dom.Element; public class PknotsrgCOrig { /** * sample webservice client using the request_orig / response_orig methods * * @param args * [0] length: integer for length of response */ public static void main(String[] args) { try { // server url final String server = "http://bibiwstest.techfak.uni-bielefeld.de"; // Production: http://bibiwsserv.techfak.uni-bielefeld.de // Test: http://bibitest.techfak.uni-bielefeld.de // wsdl file location final URL wsdl = new URL("http://bibiwstest.techfak.uni-bielefeld.de/pknotsRG/axis/pknotsRGPort?wsdl"); // Production: // http://bibiserv.techfak.uni-bielefeld.de/wsdl/pknotsRG.wsdl // Test: // http://bibiwstest.techfak.uni-bielefeld.de/pknotsRG/axis/pknotsRGPort?wsdl // parameter to hashtable Hashtable paramshash = parseParameter(args); if (!paramshash.containsKey("F")) { System.err .println("java pknotsrgCOrig -F [-strategy (m|f|l)] [-s] [-c ] [-n ] [-p ]\n"); System.exit(0); } // read sequence from file File infile = new File((String) paramshash.get("F")); BufferedReader reader = new BufferedReader(new FileReader(infile)); String line; StringBuffer sequence = new StringBuffer(); while ((line = reader.readLine()) != null) { sequence.append(line + "\n"); } reader.close(); // remove paramshash.remove("F"); // print System.out.println("param :: " + paramshash); // prepare the call (the same for all called methods) Service ser = new Service(wsdl, new QName(server + "/pknotsRG/axis/pknotsRGPort", "pknotsRGImplementationService")); Call call = (Call) ser.createCall(new QName("pknotsRGPort"), "request_orig"); // call and get id String id = (String) call.invoke(new Object[] { hashtable2array(paramshash), sequence.toString() }); // print id on STDOUT System.err.println("get id - '" + id + "'"); int statuscode = 601; while ((statuscode > 600) && (statuscode < 700)) { try { Thread.sleep(2500); call = (Call) ser.createCall(new QName("pknotsRGPort"), "response_orig"); // call and get result as DOM Tree(if finished) String result = (String) call.invoke(new Object[] { id }); // and print it to STDOUT System.out.println(result); // finished statuscode = 600; } catch (InterruptedException e) { System.err.println("process can't sleep!"); } catch (RemoteException e) { // on error WS will throw a soapfault as hobitstatuscode Element root = ((AxisFault) e) .lookupFaultDetail(new QName( "http://hobit.sourceforge.net/xsds/hobitStatuscode.xsd", "hobitStatuscode")); if (root == null) { System.err .println("ws remote error (no Hobitstatuscode): " + e.toString()); System.exit(1); } String description = root.getLastChild().getFirstChild() .getNodeValue(); statuscode = Integer.parseInt(root.getFirstChild() .getFirstChild().getNodeValue()); // print Statusinformation to STDERR System.err.println("(" + statuscode + " - " + description + ")"); } } // error handling with proper information for the user } catch (RemoteException e) { // on error WS will throw a soapfault as hobitstatuscode Element root = ((AxisFault) e).lookupFaultDetail(new QName( "http://hobit.sourceforge.net/xsds/hobitStatuscode.xsd", "hobitStatuscode")); if (root == null) { System.err.println("ws remote error (no Hobitstatuscode): " + e.toString()); System.exit(1); } String description = root.getLastChild().getFirstChild() .getNodeValue(); String code = root.getFirstChild().getFirstChild().getNodeValue(); System.out.println("Statuscode: " + code); System.out.println("Description: " + description); // Using this kind of Webservice there is only one one field for // giving back a // error message. When an axception occours, the client side of Axis // will throw // an RemoteException which includes the class name of the thrown // exception. // There is no way to get more information like the original // stacktrace !!! // System.err.println("ws remote error (" + e.toString() + ")"); System.exit(1); } catch (MalformedURLException e) { System.err.println("failed (" + e.toString() + ")"); System.exit(1); } catch (ServiceException e) { System.err.println("Service unavailable (" + e.toString() + ")"); System.exit(1); } catch (IOException e) { System.err.println("can't read sequence file " + args[0]); e.printStackTrace(); System.exit(1); } } /* * ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ following * function(s) used by class * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */ /** static method converting an InputStream to a string */ private static String get(InputStream in) throws IOException { StringBuffer buf_s = new StringBuffer(); Reader r = new InputStreamReader(new BufferedInputStream(in)); for (int c = r.read(); c != -1; c = r.read()) { buf_s.append((char) c); } in.close(); return buf_s.toString(); } /** static method parse Inputparameter into a Hashtable */ private static Hashtable parseParameter(String args[]) { Hashtable prop = new Hashtable(); prop.put("strategy", new String()); prop.put("c", new Double(0.0)); prop.put("s", new Boolean(true)); prop.put("k", new Integer(0)); prop.put("n", new Double(0.0)); prop.put("p", new Integer(0)); prop.put("F", new String()); Hashtable ret = new Hashtable(); String key = ""; for (int i = 0; i < args.length; ++i) { String current = args[i]; // found key if (current.startsWith("-")) { // remove -- key = current.replaceAll("-", ""); // check - maybe boolean if ((key != null) && (prop.get(key) != null)) { Class c = (prop.get(key)).getClass(); if ((c.getName()).equals("java.lang.Boolean")) { ret.put(key, new Boolean(true)); } } } else { // found value // check if current key exists in prop if (prop.get(key) != null) { // get Class of value Class c = (prop.get(key)).getClass(); if ((c.getName()).equals("java.lang.Integer")) { ret.put(key, new Integer(Integer.parseInt(current))); } else if ((c.getName()).equals("java.lang.String")) { ret.put(key, current); } else if ((c.getName()).equals("java.lang.Double")) { ret.put(key, new Double(Double.parseDouble(current))); } } key = null; } } return ret; } /** convert a Hashtable to an ObjectArray */ private static Object[] hashtable2array(Hashtable ht) { Set keys = ht.keySet(); Object ret[] = new Object[keys.size() * 2]; int counter = 0; for (Iterator i = keys.iterator(); i.hasNext();) { Object key = i.next(); ret[counter] = key; counter++; ret[counter] = ht.get(key); counter++; } return ret; } }