package argon; import javax.xml.messaging.JAXMServlet; import javax.xml.messaging.OnewayListener; import javax.xml.soap.*; import java.util.*; /** * Asynchronous JAXM Service for processing a invoice and updating the invoice processing system. */ public class InvoicingServlet extends JAXMServlet implements OnewayListener { /** * Extracts the invoice from the SOAP message and updates the invoice processing system. */ public void onMessage(SOAPMessage message) { try { SOAPPart part = message.getSOAPPart(); SOAPEnvelope envelope = part.getEnvelope(); SOAPBody body = envelope.getBody(); // Extract information about the invoice from the SOAPMessage. Iterator itr = body.getChildElements(envelope.createName("invoice")); if (itr.hasNext()) { SOAPElement element = (SOAPElement)itr.next(); Invoice invoice = new Invoice(); // Extract Client Name. String client = extractData(element.getChildElements(envelope.createName("client"))); invoice.setClient(client); // Extract Company Name. String company = extractData(element.getChildElements(envelope.createName("company"))); invoice.setCompany(company); // Extract Hours. String hours = extractData(element.getChildElements(envelope.createName("hours"))); invoice.setHours(Double.parseDouble(hours)); // Process the invoice. InvoiceProcessingSystem.getInstance().process(invoice); } } catch (SOAPException se) { se.printStackTrace(); } } // Extracts the text value of a XML element. private String extractData(Iterator itr) { if (itr.hasNext()) { SOAPElement element = (SOAPElement)itr.next(); return element.getValue(); } else { return ""; } } }