Use custom Java code to build the cache id from input SOAP message
contentIf you use custom Java code to build the cache id, create an
ID generator Java class that implements the IdGenerator interface defined
in the com.ibm.websphere.cache.webservices.IdGenerator package and add a reference
to the class you create in the cachespec.xml file by using the idgenerator tag.
You
can also implement the com.ibm.websphere.cache.webservices.MetaDataGenerator
package to assign cache metadata such as timeout, priority, and dependency
ids to cache entries using the metadatagenerator tag.
Implement
the com.ibm.websphere.cache.webservices.InvalidationGenerator interface and
use the invalidationgenerator tag in the cachespec.xml file to generate
cache ids and to invalidate entries in the cache. The id generated by the
invalidation generator can be a cache id or a dependency id.
For example,
if you develop an ID generator class named SampleIdGeneratorImpl, a metadata
generator class named SampleMetaDataGeneratorImpl, and an invalidation generator
class named SampleInvalidationGeneratorImpl, your cachespec.xml file might
contain the following:
<cache-entry>
<class>JAXRPCClient</class>
<name>http://TradeSample.com:9080/service/getquote</name>
<cache-id>
<idgenerator>com.mycompany.SampleIdGeneratorImpl</idgenerator>
<metadatagenerator>
com.mycompany.SampleMetaDataAndInvalidationGeneratorImpl
</metadatagenerator>
<timeout>60</timeout>
</cache-id>
<invalidation>http://TradeSample.com:9080/service/GetQuote
<invalidationgenerator>
com.mycompany.SampleMetaDataAndInvalidationGeneratorImpl
</invalidationgenerator>
</invalidation>
</cache-entry>
The SampleIdGeneratorImpl class is a custom Java
class that implements the
com.websphere.cache.webservices.IdGenerator interface.
The SampleIdGeneratorImpl class contains the getID method:
String getId(javax.xml.rpc.handler.soap.SOAPMessageContext messageContext)
The following is an example of the SampleIdGeneratorImpl.java class.
public class SampleIdGeneratorImpl implements IdGenerator {
//The SampleIdGenerator class builds cache keys using SOAP header entries
public String getId(javax.xml.rpc.handler.soap.SOAPMessageContext
messageContext) {
....
// retrieve SOAP header entries from SOAPMessage
SOAPHeader sh = soapEnvelope.getHeader();
if (sh != null) {
Iterator it = sh.examineHeaderElements("com.mycompany.actor");
while (it.hasNext()) {
SOAPHeaderElement element =
(SOAPHeaderElement)it.next();
Name name = element.getElementName();
String headerEntryName = name.getLocalName();
if (headerEntryName.equals("getQuote")){
String sNamespace = element.getNamespaceURI("");
if (sNamespace != null && !sNamespace.equals("")) {
headerEntryName = sNamespace + ":" + headerEntryName;
String quotes = element.getValue();
}
...
...
// create a method "parseAndSort" to parse and sort quotes
// By parsing and sorting quotes, you avoid duplicate cache
// entries.
// quotes e.g. IBM,CSCO,MSFT,INTC
// to return a cache key "urn:stock:getQuote=CSCO,IBM,INTC,MSFT"
String sortQuotes = parseAndSort(quotes);
cacheKey = headerEntryName + "=" + sortQuotes;
}
}
return cacheKey;
}
}
The cache id for this sample is generated as http://TradeSample.com:9080/service/getquote:urn:stock:symbol=CSCO,IBM,INTC,MSFT.
The SampleMetaDataAndInvalidationGeneratorImpl class is a custom Java
class that implements the com.websphere.cache.webservices.MetaDataGenerator interface
and the com.websphere.cache.webservices.InvalidationGenerator interface.
The SampleMetaDataAndInvalidationGeneratorImpl class contains the setMetaData
method and the getInvalidationIds method. You can also set up two smaller
classes instead of this one large class. For example, create one class for
the metadata generator and a different class for the invalidation generator.
The following are method prototypes for the setMetaData method and the getInvalidationIds
method:
void setMetaData (javax.xml.rpc.handler.soap.SOAPMessageContext messageContext,
com.ibm.websphere.cache.webservices.JAXRPCEntryInfo entryInfo)
String[] getInvalidationIds (javax.xml.rpc.handler.soap.SOAPMessageContext messageContext)
An
example of the SampleMetaDataAndInvalidationGeneratorImpl.java class follows:
public class SampleMetaDataAndInvalidationGeneratorImpl implements
MetaDataGenerator, InvalidationGenerator {
//assigns time limit, and priority metadata
public void setMetadata(javax.xml.rpc.handler.soap.SOAPMessageContext messageContext,
com.ibm.websphere.cache.webservices.JAXRPCEntryInfo entryInfo) {
....
// retrieve SOAP header entries from SOAPMessage
SOAPHeader sh = soapEnvelope.getHeader();
if (sh != null) {
Iterator it = sh.examineHeaderElements("com.mycompany.actor");
while (it.hasNext()) {
SOAPHeaderElement element =
(SOAPHeaderElement)it.next();
Name name = element.getElementName();
String headerEntryName = name.getLocalName();
if (headerEntryName.equals(“metadata”)) {
// retrieve each metadata element and set metadata
entryInfo.setTimeLimit(timeLimit);
entryInfo.setPriority(priority);
}
}
}
//builds invalidation ids using SOAP header.
public String[] getInvalidationIds(javax.xml.rpc.handler.soap.SOAPMessageContext
messageContext) { ....
// retrieve SOAP header entries from SOAPMessage
String[] invalidationIds = new String[1];
SOAPHeader sh = soapEnvelope.getHeader();
if (sh != null) {
Iterator it = sh.examineHeaderElements("com.mycompany.actor");
while (it.hasNext()) {
SOAPHeaderElement element =
(SOAPHeaderElement)it.next();
Name name = element.getElementName();
String headerEntryName = name.getLocalName();
if (headerEntryName.equals("invalidation")) {
String sNamespace = element.getNamespaceURI("");
if (sNamespace != null && !sNamespace.equals("")) {
headerEntryName = sNamespace + ":symbol";
String quotes = element.getValue();
}
...
...
// create a method "parseAndSort" to parse and sort quotes
// By parsing and sorting quotes, you avoid duplicate cache
// entries.
// quotes e.g. SUNW,NT
// to return a cache key "urn:stock:symbol=NT,SUNW"
String sortQuotes = parseAndSort(quotes);
invalidationIds[0] = headerEntryName + "=" sortQuotes;
}
}
return invalidationIds;
}
}
The invalidation id for this sample is generated as:
http://TradeSample.com:9080/service/getquote:urn:stock:symbol=NT,SUNW
An example of the SOAP request generated by the client when using custom
Java code follows:
POST /wsgwsoap1/soaprpcrouter HTTP/1.1
SOAPAction: ""
Context-type: text/xml, charset=utf-8
User-Agent: Java/1.4.1
Host: localhost
Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
Connection: keep-alive
Content-Length:645
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Header>
<getQuote soapenv:actor="com.mycompany.actor"
xmlns="urn:stock">IBM,CSCO,MSFT,INTC</getQuote>
<metaData soapenv:actor="com.mycompany.actor" xmlns="urn:stock">
<priority>10</priority>
<timeLimit>30000</timeLimit>
</metaData>
<invalidation soapenv:actor="com.mycompany.actor"
xmlns="urn:stock">SUNW, NT</invalidation>
</soapenv:Header>
<soapenv:Body
soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding">
<getQuote xmlns="urn:ibmwsgw#GetQuoteSample">
<symbol xsi:type="xsd:string">IBM,CSCO,MSFT,INTC</symbol>
</getQuote>
</soapenv:Body>
</soapenv:Envelope>