Author: ruwan
Date: Wed Nov 14 04:28:39 2007
New Revision: 594856
URL: http://svn.apache.org/viewvc?rev=594856&view=rev
Log:
Minor fixes to the caching mediator
Modified:
webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/CacheMediatorFactory.java
webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/CacheMediatorSerializer.java
webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/builtin/CacheMediator.java
Modified:
webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/CacheMediatorFactory.java
URL:
http://svn.apache.org/viewvc/webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/CacheMediatorFactory.java?rev=594856&r1=594855&r2=594856&view=diff
==============================================================================
---
webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/CacheMediatorFactory.java
(original)
+++
webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/CacheMediatorFactory.java
Wed Nov 14 04:28:39 2007
@@ -31,8 +31,9 @@
/**
* Creates an instance of a Cache mediator using XML configuration specified
- *
- * <cache (id="string")? hashGenerator="class" scope="string"
timeout="mili-seconds">
+ *
+ * <cache (id="string")? scope="string" collector=(true | false)
+ * hashGenerator="class" timeout="mili-seconds">
* <onCacheHit (sequence="key")?>
* (mediator)+
* </onCacheHit>
@@ -42,17 +43,18 @@
public class CacheMediatorFactory extends AbstractMediatorFactory {
private static final QName CACHE_Q = new
QName(XMLConfigConstants.SYNAPSE_NAMESPACE, "cache");
- private static final QName ATT_ID = new QName("id");
+ private static final QName ATT_ID = new QName("id");
+ private static final QName ATT_COLLECTOR = new QName("collector");
private static final QName ATT_HASH_GENERATOR = new QName("hashGenerator");
- private static final QName ATT_TIMEOUT = new QName("timeout");
- private static final QName ATT_SCOPE = new QName("scope");
- private static final QName ATT_SEQUENCE = new QName("sequence");
- private static final QName ATT_TYPE = new QName("type");
- private static final QName ATT_SIZE = new QName("maxSize");
- private static final QName ON_CACHE_HIT_Q
- = new QName(XMLConfigConstants.SYNAPSE_NAMESPACE, "onCacheHit");
- private static final QName IMPLEMENTATION_Q
- = new QName(XMLConfigConstants.SYNAPSE_NAMESPACE, "implementation");
+ private static final QName ATT_TIMEOUT = new QName("timeout");
+ private static final QName ATT_SCOPE = new QName("scope");
+ private static final QName ATT_SEQUENCE = new QName("sequence");
+ private static final QName ATT_TYPE = new QName("type");
+ private static final QName ATT_SIZE = new QName("maxSize");
+ private static final QName ON_CACHE_HIT_Q =
+ new QName(XMLConfigConstants.SYNAPSE_NAMESPACE, "onCacheHit");
+ private static final QName IMPLEMENTATION_Q =
+ new QName(XMLConfigConstants.SYNAPSE_NAMESPACE, "implementation");
private static final long DEFAULT_TIMEOUT = 5000L;
private static final int DEFAULT_DISK_CACHE_SIZE = 200;
@@ -62,83 +64,92 @@
handleException("Unable to create the cache mediator. " +
"Unexpected element as the cache mediator configuration");
}
-
+
CacheMediator cache = new CacheMediator();
OMAttribute idAttr = elem.getAttribute(ATT_ID);
- if (idAttr != null && idAttr.getAttributeValue() != null){
+ if (idAttr != null && idAttr.getAttributeValue() != null) {
cache.setId(idAttr.getAttributeValue());
}
- OMAttribute hashGeneratorAttr = elem.getAttribute(ATT_HASH_GENERATOR);
- if (hashGeneratorAttr != null && hashGeneratorAttr.getAttributeValue()
!= null) {
- try {
- Class generator =
Class.forName(hashGeneratorAttr.getAttributeValue());
- Object o = generator.newInstance();
- if (o instanceof DigestGenerator) {
- cache.setDigestGenerator((DigestGenerator) o);
- } else {
- handleException("Specified class for the hashGenerator is
not a " +
- "DigestGenerator. It *must* implement " +
- "org.wso2.caching.digest.DigestGenerator interface");
- }
- } catch (ClassNotFoundException e) {
- handleException("Unable to load the hash generator class", e);
- } catch (IllegalAccessException e) {
- handleException("Unable to access the hash generator class",
e);
- } catch (InstantiationException e) {
- handleException("Unable to instantiate the hash generator
class", e);
- }
- }
-
- OMAttribute timeoutAttr = elem.getAttribute(ATT_TIMEOUT);
- if (timeoutAttr != null && timeoutAttr.getAttributeValue() != null) {
- cache.setTimeout(Long.parseLong(timeoutAttr.getAttributeValue()));
- } else {
- cache.setTimeout(DEFAULT_TIMEOUT);
- }
-
OMAttribute scopeAttr = elem.getAttribute(ATT_SCOPE);
- if (scopeAttr != null && scopeAttr.getAttributeValue() != null
- && isValidScope(scopeAttr.getAttributeValue())) {
+ if (scopeAttr != null && scopeAttr.getAttributeValue() != null &&
+ isValidScope(scopeAttr.getAttributeValue())) {
cache.setScope(scopeAttr.getAttributeValue());
} else {
cache.setScope(CachingConstants.SCOPE_PER_HOST);
}
- OMElement onCacheHitElem = elem.getFirstChildWithName(ON_CACHE_HIT_Q);
- if (onCacheHitElem != null) {
- OMAttribute sequenceAttr =
onCacheHitElem.getAttribute(ATT_SEQUENCE);
- if (sequenceAttr != null && sequenceAttr.getAttributeValue() !=
null) {
- cache.setOnCacheHitRef(sequenceAttr.getAttributeValue());
+ OMAttribute collectorAttr = elem.getAttribute(ATT_COLLECTOR);
+ if (collectorAttr != null && collectorAttr.getAttributeValue() != null
&&
+ "true".equals(collectorAttr.getAttributeValue())) {
+
+ cache.setCollector(true);
+ } else {
+
+ cache.setCollector(false);
+
+ OMAttribute hashGeneratorAttr =
elem.getAttribute(ATT_HASH_GENERATOR);
+ if (hashGeneratorAttr != null &&
hashGeneratorAttr.getAttributeValue() != null) {
+ try {
+ Class generator =
Class.forName(hashGeneratorAttr.getAttributeValue());
+ Object o = generator.newInstance();
+ if (o instanceof DigestGenerator) {
+ cache.setDigestGenerator((DigestGenerator) o);
+ } else {
+ handleException("Specified class for the hashGenerator
is not a " +
+ "DigestGenerator. It *must* implement " +
+ "org.wso2.caching.digest.DigestGenerator
interface");
+ }
+ } catch (ClassNotFoundException e) {
+ handleException("Unable to load the hash generator class",
e);
+ } catch (IllegalAccessException e) {
+ handleException("Unable to access the hash generator
class", e);
+ } catch (InstantiationException e) {
+ handleException("Unable to instantiate the hash generator
class", e);
+ }
+ }
+
+ OMAttribute timeoutAttr = elem.getAttribute(ATT_TIMEOUT);
+ if (timeoutAttr != null && timeoutAttr.getAttributeValue() !=
null) {
+
cache.setTimeout(Long.parseLong(timeoutAttr.getAttributeValue()));
} else {
- cache.setOnCacheHitSequence(
- new
SequenceMediatorFactory().createAnonymousSequence(onCacheHitElem));
+ cache.setTimeout(DEFAULT_TIMEOUT);
}
- }
- for (Iterator itr = elem.getChildrenWithName(IMPLEMENTATION_Q);
itr.hasNext();) {
- OMElement implElem = (OMElement) itr.next();
- OMAttribute typeAttr = implElem.getAttribute(ATT_TYPE);
- OMAttribute sizeAttr = implElem.getAttribute(ATT_SIZE);
- if (typeAttr != null && typeAttr.getAttributeValue() != null) {
- String type = typeAttr.getAttributeValue();
- if (CachingConstants.TYPE_MEMORY.equals(type) && sizeAttr !=
null
- && sizeAttr.getAttributeValue() != null) {
-
cache.setInMemoryCacheSize(Integer.parseInt(sizeAttr.getAttributeValue()));
- } else if (CachingConstants.TYPE_DISK.equals(type)) {
- log.warn("Disk based and hirearchycal caching is not
implemented yet");
- if (sizeAttr != null && sizeAttr.getAttributeValue() !=
null) {
-
cache.setDiskCacheSize(Integer.parseInt(sizeAttr.getAttributeValue()));
+ OMElement onCacheHitElem =
elem.getFirstChildWithName(ON_CACHE_HIT_Q);
+ if (onCacheHitElem != null) {
+ OMAttribute sequenceAttr =
onCacheHitElem.getAttribute(ATT_SEQUENCE);
+ if (sequenceAttr != null && sequenceAttr.getAttributeValue()
!= null) {
+ cache.setOnCacheHitRef(sequenceAttr.getAttributeValue());
+ } else {
+ cache.setOnCacheHitSequence(
+ new
SequenceMediatorFactory().createAnonymousSequence(onCacheHitElem));
+ }
+ }
+
+ for (Iterator itr = elem.getChildrenWithName(IMPLEMENTATION_Q);
itr.hasNext();) {
+ OMElement implElem = (OMElement) itr.next();
+ OMAttribute typeAttr = implElem.getAttribute(ATT_TYPE);
+ OMAttribute sizeAttr = implElem.getAttribute(ATT_SIZE);
+ if (typeAttr != null && typeAttr.getAttributeValue() != null) {
+ String type = typeAttr.getAttributeValue();
+ if (CachingConstants.TYPE_MEMORY.equals(type) && sizeAttr
!= null &&
+ sizeAttr.getAttributeValue() != null) {
+
cache.setInMemoryCacheSize(Integer.parseInt(sizeAttr.getAttributeValue()));
+ } else if (CachingConstants.TYPE_DISK.equals(type)) {
+ log.warn("Disk based and hirearchycal caching is not
implemented yet");
+ if (sizeAttr != null && sizeAttr.getAttributeValue()
!= null) {
+
cache.setDiskCacheSize(Integer.parseInt(sizeAttr.getAttributeValue()));
+ } else {
+ cache.setDiskCacheSize(DEFAULT_DISK_CACHE_SIZE);
+ }
} else {
- cache.setDiskCacheSize(DEFAULT_DISK_CACHE_SIZE);
+ handleException("unknown implementation type for the
Cache mediator");
}
- } else {
- handleException("unknown implementation type for the Cache
mediator");
}
}
}
-
return cache;
}
Modified:
webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/CacheMediatorSerializer.java
URL:
http://svn.apache.org/viewvc/webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/CacheMediatorSerializer.java?rev=594856&r1=594855&r2=594856&view=diff
==============================================================================
---
webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/CacheMediatorSerializer.java
(original)
+++
webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/CacheMediatorSerializer.java
Wed Nov 14 04:28:39 2007
@@ -25,8 +25,9 @@
/**
* Serializes the Cache mediator to the XML configuration specified
- *
- * <cache (id="string")? hashGenerator="class" scope="string"
timeout="mili-seconds">
+ * <p/>
+ * <cache (id="string")? scope="string" collector=(true | false)
+ * hashGenerator="class" timeout="mili-seconds">
* <onCacheHit (sequence="key")?>
* (mediator)+
* </onCacheHit>
@@ -48,46 +49,53 @@
cache.addAttribute(fac.createOMAttribute("id", nullNS,
mediator.getId()));
}
- if (mediator.getDigestGenerator() != null) {
- cache.addAttribute(fac.createOMAttribute("hashGenerator", nullNS,
- mediator.getDigestGenerator().getClass().getName()));
- }
-
if (mediator.getScope() != null) {
cache.addAttribute(fac.createOMAttribute("scope", nullNS,
mediator.getScope()));
}
- if (mediator.getTimeout() != 0) {
- cache.addAttribute(
- fac.createOMAttribute("timeout", nullNS,
Long.toString(mediator.getTimeout())));
- }
-
- if (mediator.getOnCacheHitRef() != null) {
- OMElement onCacheHit = fac.createOMElement("onCacheHit", synNS);
- onCacheHit.addAttribute(
- fac.createOMAttribute("sequence", nullNS,
mediator.getOnCacheHitRef()));
- cache.addChild(onCacheHit);
- } else if (mediator.getOnCacheHitSequence() != null) {
- OMElement onCacheHit = fac.createOMElement("onCacheHit", synNS);
- new SequenceMediatorSerializer().serializeChildren(
- onCacheHit, mediator.getOnCacheHitSequence().getList());
- cache.addChild(onCacheHit);
- }
-
- if (mediator.getInMemoryCacheSize() != 0) {
- OMElement implElem = fac.createOMElement("implementation", synNS);
- implElem.addAttribute(fac.createOMAttribute("type", nullNS,
"memory"));
- implElem.addAttribute(fac.createOMAttribute(
- "maxSize", nullNS,
Integer.toString(mediator.getInMemoryCacheSize())));
- cache.addChild(implElem);
- }
-
- if (mediator.getDiskCacheSize() != 0) {
- OMElement implElem = fac.createOMElement("implementation", synNS);
- implElem.addAttribute(fac.createOMAttribute("type", nullNS,
"disk"));
- implElem.addAttribute(fac.createOMAttribute(
- "maxSize", nullNS,
Integer.toString(mediator.getDiskCacheSize())));
- cache.addChild(implElem);
+ if (mediator.isCollector()) {
+ cache.addAttribute(fac.createOMAttribute("collector", nullNS,
"true"));
+ } else {
+
+ cache.addAttribute(fac.createOMAttribute("collector", nullNS,
"true"));
+
+ if (mediator.getDigestGenerator() != null) {
+ cache.addAttribute(fac.createOMAttribute("hashGenerator",
nullNS,
+ mediator.getDigestGenerator().getClass().getName()));
+ }
+
+ if (mediator.getTimeout() != 0) {
+ cache.addAttribute(
+ fac.createOMAttribute("timeout", nullNS,
Long.toString(mediator.getTimeout())));
+ }
+
+ if (mediator.getOnCacheHitRef() != null) {
+ OMElement onCacheHit = fac.createOMElement("onCacheHit",
synNS);
+ onCacheHit.addAttribute(
+ fac.createOMAttribute("sequence", nullNS,
mediator.getOnCacheHitRef()));
+ cache.addChild(onCacheHit);
+ } else if (mediator.getOnCacheHitSequence() != null) {
+ OMElement onCacheHit = fac.createOMElement("onCacheHit",
synNS);
+ new SequenceMediatorSerializer()
+ .serializeChildren(onCacheHit,
mediator.getOnCacheHitSequence().getList());
+ cache.addChild(onCacheHit);
+ }
+
+ if (mediator.getInMemoryCacheSize() != 0) {
+ OMElement implElem = fac.createOMElement("implementation",
synNS);
+ implElem.addAttribute(fac.createOMAttribute("type", nullNS,
"memory"));
+ implElem.addAttribute(fac.createOMAttribute("maxSize", nullNS,
+ Integer.toString(mediator.getInMemoryCacheSize())));
+ cache.addChild(implElem);
+ }
+
+ if (mediator.getDiskCacheSize() != 0) {
+ OMElement implElem = fac.createOMElement("implementation",
synNS);
+ implElem.addAttribute(fac.createOMAttribute("type", nullNS,
"disk"));
+ implElem.addAttribute(fac.createOMAttribute("maxSize", nullNS,
+ Integer.toString(mediator.getDiskCacheSize())));
+ cache.addChild(implElem);
+ }
}
if (parent != null) {
Modified:
webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/builtin/CacheMediator.java
URL:
http://svn.apache.org/viewvc/webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/builtin/CacheMediator.java?rev=594856&r1=594855&r2=594856&view=diff
==============================================================================
---
webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/builtin/CacheMediator.java
(original)
+++
webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/builtin/CacheMediator.java
Wed Nov 14 04:28:39 2007
@@ -51,6 +51,7 @@
private String id = null;
private String scope = CachingConstants.SCOPE_PER_HOST;
+ private boolean collector = false;
private DigestGenerator digestGenerator =
CachingConstants.DEFAULT_XML_IDENTIFIER;
private int inMemoryCacheSize = CachingConstants.DEFAULT_CACHE_SIZE;
// if this is 0 then no disk cache, and if there is no size specified in
the config
@@ -114,7 +115,7 @@
boolean result = true;
if (synCtx.isResponse()) {
- processResponseMessage(traceOrDebugOn, traceOn, synCtx, cache);
+ processResponseMessage(synCtx, traceOrDebugOn, traceOn, cache);
} else {
result = processRequestMessage(synCtx, traceOrDebugOn, traceOn,
cache);
@@ -143,8 +144,12 @@
* @param synCtx the current message (response)
* @param cache the cache
*/
- private void processResponseMessage(boolean traceOrDebugOn, boolean
traceOn,
- MessageContext synCtx, Cache cache) {
+ private void processResponseMessage(MessageContext synCtx, boolean
traceOrDebugOn,
+ boolean traceOn, Cache cache) {
+
+ if (!collector) {
+ handleException("Response messages cannot be handled in a non
collector cache", synCtx);
+ }
String requestHash = (String)
synCtx.getProperty(CachingConstants.REQUEST_HASH_KEY);
@@ -177,7 +182,7 @@
// in the future there can be a situation where user sends the
request with the
// response hash (if client side caching is on) in which case
we can compare that
// response hash with the given response hash and respond with
not-modified http header
- cachedObj.setResponseHash(digestGenerator.getDigest(
+ cachedObj.setResponseHash(cache.getGenerator().getDigest(
((Axis2MessageContext) synCtx).getAxis2MessageContext()));
cachedObj.setExpireTime(System.currentTimeMillis() +
cachedObj.getTimeout());
@@ -207,6 +212,10 @@
private boolean processRequestMessage(MessageContext synCtx, boolean
traceOrDebugOn,
boolean traceOn, Cache cache) {
+ if (collector) {
+ handleException("Request messages cannot be handled in a collector
cache", synCtx);
+ }
+
String requestHash = digestGenerator
.getDigest(((Axis2MessageContext)
synCtx).getAxis2MessageContext());
synCtx.setProperty(CachingConstants.REQUEST_HASH_KEY, requestHash);
@@ -344,6 +353,14 @@
public void setScope(String scope) {
this.scope = scope;
+ }
+
+ public boolean isCollector() {
+ return collector;
+ }
+
+ public void setCollector(boolean collector) {
+ this.collector = collector;
}
public DigestGenerator getDigestGenerator() {
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]