Listing 1
try
{
pcf = ProviderConnectionFactory.newInstance();
pc = pcf.createConnection();
}
catch (SOAPException e)
{
throw new ServletException(e);
}

Listing 2
String[] supportedProfiles = metaData.getSupportedProfiles();
String profile = null;
for(int i=0; i < supportedProfiles.length; i++)
{
if(supportedProfiles[i].equals("soaprp"))
{
profile = supportedProfiles[i];
break;





Listing 3: 
Method of ControllerServlet to Invoke A JAXM Service using a JAXM Provider.

private void submitBill(HttpServletRequest request) throws ServletException


try 
{
// Create a message factory.
if (mf == null)
{
ProviderMetaData metaData = null;
try
{
metaData = pc.getMetaData();
}
catch (SOAPException e)

throw new ServletException(e);
}

// Find the SOAP-RP profie and create a MessageFactory that supports
// SOAP-RP messages.
String[] supportedProfiles = metaData.getSupportedProfiles();
String profile = null;
for(int i=0; i < supportedProfiles.length; i++)
{
if(supportedProfiles[i].equals("soaprp"))
{
profile = supportedProfiles[i];
break;


mf = pc.createMessageFactory(profile);
}

// Create a SOAP-RP message from the message factory.
SOAPRPMessageImpl msg = (SOAPRPMessageImpl)mf.createMessage();
// Set the end point.
msg.setTo(new Endpoint(submitBillURL));

SOAPPart sp = msg.getSOAPPart();
SOAPEnvelope envelope = sp.getEnvelope();
SOAPBody bdy = envelope.getBody();

// Add a root element "bill" to the soap message.
SOAPBodyElement billElement =
bdy.addBodyElement(envelope.createName("bill"));

// Add the name of the company to the message
String company = request.getParameter("company");
billElement.addChildElement(envelope.createName
("company")).addTextNode(company);

// Add the name of the company to the message
String customer = request.getParameter("customer");
billElement.addChildElement(envelope.createName
("customer")).addTextNode(customer);

// Add the name of the company to the message
String hours = request.getParameter("hours");
billElement.addChildElement(envelope.createName
("hours")).addTextNode(hours);

// Send the message
msg.writeTo(System.out);
pc.send(msg);

}
catch (Exception se)
{
se.printStackTrace();
}




Listing 4: Bill information as a SOAP message.

<?xml version="1.0" encoding="UTF-8"?>
<soap-env:Envelope xmlns:soap-env=
"http://schemas.xmlsoap.org/soap/envelope/">
<soap-env:Header>
<m:path xmlns:m="http://schemas.xmlsoap.org/rp">
<m:to>http://xyz.acme.com/processbill</m:to>
<m:id>2ea82d39-b48e-47f2-b85d-d94ad2cbce03</m:id><m:fwd/><m:rev/>
</m:path>
</soap-env:Header>
<soap-env:Body>
<bill>
<company>Cysive</company>
<customer>Tom Jones</customer>
<hours>2</hours>
</bill>
</soap-env:Body>
</soap-env:Envelope>

Listing 5: Synchronous JAXM Service.

package acme;

import javax.xml.messaging.JAXMServlet;
import javax.xml.messaging.OnewayListener;
import javax.xml.soap.*;
import java.util.*;

/**
* Asynchronous JAXM Service.
*/
public class BillProcessingServlet
extends JAXMServlet
implements OnewayListener
{

/**
* Extracts billing information from the SOAP message and updates
* the billing system.
* @param message SOAP message containing billing information.
*/
public void onMessage(SOAPMessage message)
{
try
{

SOAPPart part = message.getSOAPPart(); 
SOAPEnvelope envelope = part.getEnvelope();
SOAPBody body = envelope.getBody();

// Extract information about the bill from the SOAPMessage.
Iterator itr = body.getChildElements(envelope.createName("bill"));
if (itr.hasNext())
{
SOAPElement element = (SOAPElement)itr.next();
Bill bill = new Bill();

// Extract Customer Name.
String customerName = extractData(element.getChildElements
(envelope.createName("customer")));
bill.setCustomerName(customerName);

// Extract Company Name.
String company = extractData(element.getChildElements
(envelope.createName("company")));
bill.setCompany(company);

// Extract Hours.
String hours = extractData(element.getChildElements
(envelope.createName("hours")));
bill.setHours(Double.parseDouble(hours)); 

// Process the Bill.
BillingSystem.getInstance().process(bill); 


}
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 "";
}
}

}

Listing 6: 
Code Snippet from Controller Servlet to invoke a Synchronous JAXM Client.

private void findBills(HttpServletRequest request,
HttpServletResponse response
)
throws IOException, ServletException
{
String company = request.getParameter("company");
if (company == null || company.trim().equals(""))
{
request.setAttribute("error","Company Name cannot be null or empty");
sendResponse("/search.jsp", request, response);
}
else
{
try
{
// Create a SOAPMessage
SOAPMessage message = MessageFactory.newInstance().createMessage();
SOAPPart part = message.getSOAPPart();
SOAPEnvelope envelope = part.getEnvelope();
SOAPBody body = envelope.getBody();

SOAPBodyElement bodyElement =
body.addBodyElement(envelope.createName("findBills"));
bodyElement.addChildElement(envelope.createName
("company")).addTextNode(company);

// Create a SOAP Connection
SOAPConnection connection =
SOAPConnectionFactory.newInstance().createConnection();
java.net.URL endpoint = new java.net.URL
("http://localhost:8080/messaging/findBills");

// Send the message
message.writeTo(System.out);
SOAPMessage responseMessage = connection.call(message, endpoint);
responseMessage.writeTo(response.getOutputStream());
}
catch (Exception se)
{
throw new ServletException(se);
}

}

Listing 7: 
Synchronous JAXM Service which provides billing information.

package acme;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.xml.messaging.*;
import javax.xml.soap.*;
import java.util.*;

/**
* Synchronous JAXM Service.
*/
public class BillingInformationServlet
extends JAXMServlet
implements ReqRespListener
{
MessageFactory factory = null;

/** 
* Returns a SOAPMessage containing a list of bills for a given
* company.
* @param message Incoming SOAPMessage containing the company name.
* @return A SOAPMessage containing a list of bills.
*/
public SOAPMessage onMessage(SOAPMessage message)
{
try
{
// Extract the name of the company.
String companyName = extractCompanyName(message);

// Search the billing system for a collection of bills for a given company.
Collection bills = BillingSystem.getInstance().getBills(companyName);

// Build and return a SOAPMessage from the collection of bills.
return buildMessage(bills);

}
catch (SOAPException ex)
{
// Catch the exception and return a SOAPMessage with a SOAPFault.
ex.printStackTrace();
return buildFault(ex);

}

/**
* Initializes the servlet.
* @param config Instance of ServletConfig.
* @throws javax.servlet.ServletException thrown if there is an error in intialization.
*/
public void init(ServletConfig config) throws ServletException
{
super.init(config);
try
{
factory = MessageFactory.newInstance();
}
catch (SOAPException se)
{
throw new ServletException(se);
}
}

/**
* Extracts the name of the company
*/ 
private String extractCompanyName(SOAPMessage message)
throws SOAPException
{
SOAPPart part = message.getSOAPPart();
SOAPEnvelope envelope = part.getEnvelope();
SOAPBody body = envelope.getBody(); 

// Extract the company Name
java.util.Iterator itr = body.getChildElements
(envelope.createName("findBills"));
if (itr.hasNext())
{
SOAPElement element = (SOAPElement)itr.next();
Iterator children = element.getChildElements
(envelope.createName("company")); 
return extractData(children); 

}
return null;
}

/**
* Returns a text value of a certain element.
*/
private String extractData(Iterator itr)
{
if (itr.hasNext())
{
SOAPElement element = (SOAPElement)itr.next();
return element.getValue();
}
else
{
return "";
}
}

// Builds a SOAPMessage from a collection of bills.
private SOAPMessage buildMessage(Collection collection)
throws SOAPException
{
SOAPMessage message = factory.createMessage();
SOAPPart sp = message.getSOAPPart();
SOAPEnvelope envelope = sp.getEnvelope();
SOAPBody bdy = envelope.getBody();

// Add the root element "bills" to the SOAPBody.
SOAPBodyElement billsElement = bdy.addBodyElement
(envelope.createName("bills"));

Iterator itr = collection.iterator();
while (itr.hasNext())
{
Bill bill = (Bill)itr.next();

// Add information about the bill to the root element "bills". 
SOAPElement billElement = billsElement.addChildElement
(envelope.createName("bill")); 
billElement.addChildElement(envelope.createName
("company")).addTextNode(bill.getCompany()); 
billElement.addChildElement(envelope.createName
("customer")).addTextNode(bill.getCustomerName());

String hours = String.valueOf(bill.getHours());
billElement.addChildElement(envelope.createName
("hours")).addTextNode(hours);

String rate = String.valueOf(bill.getHourlyRate());
billElement.addChildElement(envelope.createName
("rate")).addTextNode(rate);

String total = String.valueOf(bill.getAmount());
billElement.addChildElement(envelope.createName
("total")).addTextNode(total);
}

return message;
}

// Returns SOAPMessage containing a SOAPFault.
private SOAPMessage buildFault(Exception ex)
{
try
{
SOAPMessage message = factory.createMessage();
SOAPPart sp = message.getSOAPPart();
SOAPEnvelope envelope = sp.getEnvelope();
SOAPBody bdy = envelope.getBody();
SOAPFault fault = bdy.addFault();
fault.setFaultString(ex.getMessage());
return message;
}
catch (SOAPException se)
{
se.printStackTrace();
return null;

}
}


Listing 8: 

JAXM request for searching for bills.

<?xml version="1.0" encoding="UTF-8"?>
<soap-env:Envelope xmlns:soap-env=
"http://schemas.xmlsoap.org/soap/envelope/">
<soap-env:Header/>
<soap-env:Body>
<findBills>
<company>Cysive</company>
</findBills>
</soap-env:Body>
</soap-env:Envelope>


JAXM Response for the search request.

<?xml version="1.0" encoding="UTF-8" ?> 
<soap-env:Envelope xmlns:soap-env=
"http://schemas.xmlsoap.org/soap/envelope/">
<soap-env:Header /> 
<soap-env:Body>
<bills>
<bill>
<company>Cysive</company> 
<customer>Tom Jones</customer> 
<hours>2.0</hours> 
<rate>50.0</rate> 
<total>100.0</total> 
</bill>
</bills>
</soap-env:Body>
</soap-env:Envelope>