Author: ruwan
Date: Tue Dec 11 21:28:17 2007
New Revision: 10991
Log:
Refactored and fixed the module
Added:
trunk/commons/caching/modules/core/src/main/java/org/wso2/caching/CacheConfiguration.java
trunk/commons/caching/modules/core/src/main/java/org/wso2/caching/util/
trunk/commons/caching/modules/core/src/main/java/org/wso2/caching/util/SOAPMessageHelper.java
Modified:
trunk/commons/caching/modules/core/src/main/java/org/wso2/caching/CacheManager.java
trunk/commons/caching/modules/core/src/main/java/org/wso2/caching/CachingConstants.java
trunk/commons/caching/modules/core/src/main/java/org/wso2/caching/policy/CachingPolicyProcessor.java
trunk/commons/caching/modules/mar/src/main/java/org/wso2/caching/CachingModule.java
trunk/commons/caching/modules/mar/src/main/java/org/wso2/caching/handlers/CachingInHandler.java
trunk/commons/caching/modules/mar/src/main/java/org/wso2/caching/handlers/CachingOutHandler.java
trunk/commons/caching/modules/mar/src/main/java/org/wso2/caching/receivers/CachedMessageReceiver.java
trunk/commons/caching/modules/mar/src/main/resources/META-INF/module.xml
trunk/commons/caching/pom.xml
Added:
trunk/commons/caching/modules/core/src/main/java/org/wso2/caching/CacheConfiguration.java
==============================================================================
--- (empty file)
+++
trunk/commons/caching/modules/core/src/main/java/org/wso2/caching/CacheConfiguration.java
Tue Dec 11 21:28:17 2007
@@ -0,0 +1,70 @@
+package org.wso2.caching;
+
+import org.wso2.caching.digest.DigestGenerator;
+
+/**
+ *
+ */
+public class CacheConfiguration {
+
+ /**
+ * This variable will hold the DigestGenerator implementation to be used
for
+ * the request/response identifier generation
+ */
+ private DigestGenerator digestGenerator =
CachingConstants.DEFAULT_XML_IDENTIFIER;
+
+ /**
+ * This holds the maximum message size that can be used in caching. i.e.
if a particular
+ * message exceeds this specified size, then that message will not be
cached. This is
+ * because hash generation can be slower in big messages and might take
much longer time
+ * than normal serve
+ */
+ private int maxMessageSize = 0;
+
+ /**
+ * This holds the maximum number of response messages that will be cached
in the
+ * CacheManager. If this is not specified by the policy then, it will be
taken as the
+ * default value specified in the CachingConstants.
+ */
+ private int maxCacheSize = CachingConstants.DEFAULT_CACHE_SIZE;
+
+ /**
+ * This will hold the cache expiration interval in milliseconds. From the
cache
+ * implementation point of view each and every cached response will be
cached
+ * for this time period enless there is a explicit value set by the
response
+ * from the server. (For example through http cache-control headers)
+ */
+ private long timeout = Long.MAX_VALUE;
+
+ public DigestGenerator getDigestGenerator() {
+ return digestGenerator;
+ }
+
+ public void setDigestGenerator(DigestGenerator digestGenerator) {
+ this.digestGenerator = digestGenerator;
+ }
+
+ public int getMaxMessageSize() {
+ return maxMessageSize;
+ }
+
+ public void setMaxMessageSize(int maxMessageSize) {
+ this.maxMessageSize = maxMessageSize;
+ }
+
+ public int getMaxCacheSize() {
+ return maxCacheSize;
+ }
+
+ public void setMaxCacheSize(int maxCacheSize) {
+ this.maxCacheSize = maxCacheSize;
+ }
+
+ public long getTimeout() {
+ return timeout;
+ }
+
+ public void setTimeout(long timeout) {
+ this.timeout = timeout;
+ }
+}
Modified:
trunk/commons/caching/modules/core/src/main/java/org/wso2/caching/CacheManager.java
==============================================================================
---
trunk/commons/caching/modules/core/src/main/java/org/wso2/caching/CacheManager.java
(original)
+++
trunk/commons/caching/modules/core/src/main/java/org/wso2/caching/CacheManager.java
Tue Dec 11 21:28:17 2007
@@ -24,11 +24,11 @@
/**
* This is the cache management class and this holds the list of CachedObjects
in the
- * cache, the DigestGenerator, and the cache timeout in milliseconds. An
instance of
- * this will be stored in the appropriate place to retireve when a request is
processed
- * (Note: CacheManager does not hold the actual cached responses in it, rather
it keeps
- * a list of references to the cached responses stored in a place which is
accessible
- * and replicateable through the whole cluster, mostly in one of the
AbstractContext)
+ * cache. An instance of this will be stored in the appropriate place to
retireve when
+ * a request is processed (Note: CacheManager does not hold the actual cached
responses
+ * in it, rather it keeps a list of references to the cached responses stored
in a
+ * place which is accessible and replicateable through the whole cluster,
mostly in one
+ * of the AbstractContext)
*/
public class CacheManager implements Serializable {
@@ -39,47 +39,10 @@
private List cache = null;
/**
- * This variable will hold the DigestGenerator implementation to be used
for
- * the request/response identifier generation
- */
- private DigestGenerator generator = null;
-
- /**
- * This will hold the cache expiration interval in milliseconds. From the
cache
- * implementation point of view each and every cached response will be
cached
- * for this time period enless there is a explicit value set by the
response
- * from the server. (For example through http cache-control headers)
- */
- private long timeout = Long.MAX_VALUE;
-
- /**
* This default constructor instantiates the CacheManager with defafult
parameters
*/
public CacheManager() {
- this(CachingConstants.DEFAULT_XML_IDENTIFIER);
- }
-
- /**
- * This constructor of the CacheManager will instatiate the cached
response reference
- * list and set the DigestGenerator as provided
- *
- * @param generator - DigestGenerator implementation to be used on caching
- */
- public CacheManager(DigestGenerator generator) {
- this.cache = new ArrayList();
- this.generator = generator;
- }
-
- /**
- * This constructor of the CacheManager will instantiate the cached
response reference
- * list and set the DigestGenerator and the expiration timeout as provided
- *
- * @param generator - DigestGenerator implementation to be used
- * @param timeout - long value specifying the expire time
- */
- public CacheManager(DigestGenerator generator, long timeout) {
- this(generator);
- this.timeout = timeout;
+ cache = new ArrayList();
}
/**
@@ -163,42 +126,6 @@
}
/**
- * This gets the DigestGenerator implemntation of the CacheManager
- *
- * @return DigestGenerator implemntation of the CacheManager
- */
- public DigestGenerator getGenerator() {
- return generator;
- }
-
- /**
- * This sets the DigestGenerator implementation of the CacheManager
- *
- * @param generator - DigestGenerator to be set to the CacheManager
- */
- public void setGenerator(DigestGenerator generator) {
- this.generator = generator;
- }
-
- /**
- * This gets the cache expiration timeout associated with this particular
CacheManager
- *
- * @return long variable specifying the expiration time interval in
milliseconds
- */
- public long getTimeout() {
- return timeout;
- }
-
- /**
- * This sets the cache expiration time interval associated to the
CacheManager
- *
- * @param timeout - long value spcifying the expire time interval in
milliseconds
- */
- public void setTimeout(long timeout) {
- this.timeout = timeout;
- }
-
- /**
* This will remove the cached responses from the provided AbstractContext
which are
* in the reference list inside the CacheManager
*
Modified:
trunk/commons/caching/modules/core/src/main/java/org/wso2/caching/CachingConstants.java
==============================================================================
---
trunk/commons/caching/modules/core/src/main/java/org/wso2/caching/CachingConstants.java
(original)
+++
trunk/commons/caching/modules/core/src/main/java/org/wso2/caching/CachingConstants.java
Tue Dec 11 21:28:17 2007
@@ -30,14 +30,17 @@
public static QName CACHED_OPERATION_QNAME = new QName("CachedOperation");
/** String key to store the the request hash in the message contetx */
- public static final String REQUEST_HASH_KEY = "requestHash";
+ public static final String REQUEST_HASH = "requestHash";
/** String key to store the cached response in the message context */
- public static final String CACHED_RESPONSE_KEY = "CachedResponse";
+ public static final String CACHED_OBJECT = "CachedObject";
/** String key to store the cache object */
public static final String CACHE_MANAGER = "cacheManager";
+ /** String key to store the cache object */
+ public static final String CACHE_CONFIGURATION = "cacheConfiguration";
+
/** Caching namespace string value */
public static final String CACHING_NS =
"http://www.wso2.org/ns/2007/06/commons/caching";
@@ -56,6 +59,14 @@
public static final QName CACHE_EXPIRATION_TIME_QNAME
= new QName(CACHING_NS, "ExpireTime", CACHING_NS_PREFIX);
+ /** Expire time QName for the cache in the caching policy */
+ public static final QName MAX_CACHE_SIZE_QNAME
+ = new QName(CACHING_NS, "MaxCacheSize", CACHING_NS_PREFIX);
+
+ /** Expire time QName for the cache in the caching policy */
+ public static final QName MAX_MESSAGE_SIZE_QNAME
+ = new QName(CACHING_NS, "MaxMessageSize", CACHING_NS_PREFIX);
+
/** Default DigestGenerator for the caching impl */
public static final DigestGenerator DEFAULT_XML_IDENTIFIER = new
DOMHASHGenerator();
Modified:
trunk/commons/caching/modules/core/src/main/java/org/wso2/caching/policy/CachingPolicyProcessor.java
==============================================================================
---
trunk/commons/caching/modules/core/src/main/java/org/wso2/caching/policy/CachingPolicyProcessor.java
(original)
+++
trunk/commons/caching/modules/core/src/main/java/org/wso2/caching/policy/CachingPolicyProcessor.java
Tue Dec 11 21:28:17 2007
@@ -16,20 +16,22 @@
package org.wso2.caching.policy;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.neethi.All;
+import org.apache.neethi.ExactlyOne;
import org.apache.neethi.Policy;
import org.apache.neethi.PolicyEngine;
-import org.apache.neethi.ExactlyOne;
import org.apache.neethi.builders.xml.XmlPrimtiveAssertion;
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-import org.wso2.caching.digest.DigestGenerator;
+import org.wso2.caching.CacheConfiguration;
import org.wso2.caching.CacheManager;
-import org.wso2.caching.CachingException;
import org.wso2.caching.CachingConstants;
+import org.wso2.caching.CachingException;
+import org.wso2.caching.digest.DigestGenerator;
import javax.xml.namespace.QName;
-import java.util.List;
import java.util.Iterator;
+import java.util.List;
public class CachingPolicyProcessor {
@@ -48,10 +50,10 @@
* - If an error occured in processing the policy
* or if there is a problem in loading the DigestGenerator
*/
- public static CacheManager processInitCachingPolicy(Policy policy) throws
CachingException {
+ public static CacheConfiguration processCachingPolicy(Policy policy)
throws CachingException {
// object to be initialized
- CacheManager cacheManager = null;
+ CacheConfiguration cacheConfig = new CacheConfiguration();
// if policy is provided use that to get the DigestGenerator
implementation
if (policy != null) {
@@ -78,14 +80,14 @@
Object aConfigDataAssertionObject =
configDataAssertionIterator.next();
// Validating the caching policy
- if (aConfigDataAssertionObject instanceof
ExactlyOne) {
+ if (aConfigDataAssertionObject instanceof All) {
long expireTime = -1;
DigestGenerator digestGenerator
- =
CachingConstants.DEFAULT_XML_IDENTIFIER;
+ = CachingConstants.DEFAULT_XML_IDENTIFIER;
- ExactlyOne exactlyOne = (ExactlyOne)
aConfigDataAssertionObject;
- List childAssertionsList =
exactlyOne.getPolicyComponents();
+ All all = (All) aConfigDataAssertionObject;
+ List childAssertionsList =
all.getPolicyComponents();
for (Iterator childAssertionSIterator =
childAssertionsList
.iterator();
childAssertionSIterator.hasNext();) {
@@ -94,6 +96,7 @@
XmlPrimtiveAssertion
cachingPrimtiveAssertion
= (XmlPrimtiveAssertion)
childAssertionObject;
+ //todo : break the processing
// Is Identifier specified?
if
(cachingPrimtiveAssertion.getName().equals(
CachingConstants.CACHING_XML_IDENTIFIER_QNAME)) {
@@ -105,6 +108,7 @@
// Loading the class using
Reflection
digestGenerator =
(DigestGenerator)
Class.forName(value).newInstance();
+
cacheConfig.setDigestGenerator(digestGenerator);
} catch (ClassNotFoundException e)
{
@@ -134,159 +138,28 @@
expireTime = Long.parseLong(
cachingPrimtiveAssertion.getValue().getText());
- }
- } else {
- // invalid caching policy
- handleException("Unexpected caching
policy, " +
- "Wrong policy assertion for
the caching module");
- }
- }
-
- cacheManager = new
CacheManager(digestGenerator, expireTime);
-
- } else {
- // caching policy ExactlyOne not found
- handleException("Unexpected caching " +
- "policy, \"wsp:ExactlyOne\" expected");
- }
- }
- } else {
- // caching assertion not found
- handleException("Couldn't find the Caching policy " +
- "assertion, Unable to process the caching
policy");
- }
- }
- }
- } else {
- // if the policy is not specified, use the DOMHASHGenerator as the
default identifier
- cacheManager = new CacheManager();
- }
-
- return cacheManager;
- }
-
- /**
- * This static method will be use to process the caching policy when the
engageNodify is called
- * and get the CacheManager oject initialized according to the policy
- *
- * @param policy - Policy object from the EffectivePolicy
- * @return CacheManager object initialized and returned
- * @throws org.wso2.caching.CachingException
- * - If an error occured in processing the policy
- * or if there is a problem in loading the DigestGenerator
- */
- public static CacheManager processEngageCachingPolicy(Policy policy)
throws CachingException {
-
- // object to be initialized
- CacheManager cacheManager = null;
-
- // if policy is provided use that to get the DigestGenerator
implementation
- if (policy != null) {
-
- List topLevelAssertionList = policy.getPolicyComponents();
- for (Iterator topLevelAssertionsIterator
- = topLevelAssertionList.iterator();
topLevelAssertionsIterator.hasNext();) {
-
- Object topLevelAssertionObject =
topLevelAssertionsIterator.next();
- // Validating the policy
- if (topLevelAssertionObject instanceof XmlPrimtiveAssertion) {
-
- XmlPrimtiveAssertion topLevelXmlPrimtiveAssertion
- = (XmlPrimtiveAssertion) topLevelAssertionObject;
- QName qName = topLevelXmlPrimtiveAssertion.getName();
- // validating the Caching assertion
- if
(qName.equals(CachingConstants.CACHING_ASSERTION_QNAME)) {
-
- Policy cachingPolicy
- =
PolicyEngine.getPolicy(topLevelXmlPrimtiveAssertion.getValue());
- List assertionList =
cachingPolicy.getPolicyComponents();
- for (Iterator configDataAssertionIterator =
assertionList.iterator();
- configDataAssertionIterator.hasNext();) {
-
- Object aConfigDataAssertionObject =
configDataAssertionIterator.next();
- // Validating the caching policy
- if (aConfigDataAssertionObject instanceof
ExactlyOne) {
-
- long expireTime = -1;
- DigestGenerator digestGenerator
- =
CachingConstants.DEFAULT_XML_IDENTIFIER;
-
- ExactlyOne exactlyOne = (ExactlyOne)
aConfigDataAssertionObject;
- List childAssertionsList =
exactlyOne.getPolicyComponents();
- for (Iterator childAssertionSIterator =
childAssertionsList
- .iterator();
childAssertionSIterator.hasNext();) {
-
- Object childAssertionObject =
childAssertionSIterator.next();
- if (childAssertionObject instanceof
XmlPrimtiveAssertion) {
-
- XmlPrimtiveAssertion
cachingPrimtiveAssertion
- = (XmlPrimtiveAssertion)
childAssertionObject;
- // Is Identifier specified?
- if
(cachingPrimtiveAssertion.getName().equals(
-
CachingConstants.CACHING_XML_IDENTIFIER_QNAME)) {
-
- String value
- =
cachingPrimtiveAssertion.getValue().getText();
- try {
-
- // Loading the class using
Reflection
- digestGenerator =
(DigestGenerator)
-
Class.forName(value).newInstance();
-
- } catch (ClassNotFoundException e)
{
-
- handleException("Unable to
find the " +
- "DigestGenerator
implementation \"" +
- value + "\"", e);
-
- } catch (IllegalAccessException e)
{
-
- handleException("Unable to
load the " +
- "DigestGenerator
implementation \"" +
- value + "\"", e);
-
- } catch (InstantiationException e)
{
-
- handleException("Unable to
instantiate the " +
- "DigestGenerator
implementation \"" +
- value + "\"", e);
-
- }
+ cacheConfig.setTimeout(expireTime);
}
- // Is expiration time specified?
- if
(cachingPrimtiveAssertion.getName().equals(
-
CachingConstants.CACHE_EXPIRATION_TIME_QNAME) &&
-
cachingPrimtiveAssertion.getValue() != null) {
-
- expireTime = Long.parseLong(
-
cachingPrimtiveAssertion.getValue().getText());
- }
+ // todo: max message size and the max
cache size
} else {
// invalid caching policy
handleException("Unexpected caching
policy, " +
"Wrong policy assertion for
the caching module");
}
}
-
- cacheManager = new
CacheManager(digestGenerator, expireTime);
-
} else {
// caching policy ExactlyOne not found
handleException("Unexpected caching " +
"policy, \"wsp:ExactlyOne\" expected");
}
}
- } else {
- // caching assertion not found
- handleException("Couldn't find the Caching policy " +
- "assertion, Unable to process the caching
policy");
}
}
}
}
- return cacheManager;
+ return cacheConfig;
}
/**
Added:
trunk/commons/caching/modules/core/src/main/java/org/wso2/caching/util/SOAPMessageHelper.java
==============================================================================
--- (empty file)
+++
trunk/commons/caching/modules/core/src/main/java/org/wso2/caching/util/SOAPMessageHelper.java
Tue Dec 11 21:28:17 2007
@@ -0,0 +1,47 @@
+package org.wso2.caching.util;
+
+import org.apache.axiom.soap.SOAPEnvelope;
+import org.apache.axiom.soap.SOAP11Constants;
+import org.apache.axiom.om.OMAbstractFactory;
+import org.apache.axiom.om.OMNode;
+
+import java.util.Iterator;
+
+/**
+ *
+ */
+public class SOAPMessageHelper {
+
+ /**
+ * This method will clone the provided SOAPEnvelope and returns the cloned
envelope
+ * as an exact copy of the provided envelope
+ *
+ * @param envelope - this will be cloned to get the new envelope
+ * @return cloned SOAPEnvelope from the provided one
+ */
+ public static SOAPEnvelope cloneSOAPEnvelope(SOAPEnvelope envelope) {
+ SOAPEnvelope newEnvelope;
+ if (SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI
+ .equals(envelope.getBody().getNamespace().getNamespaceURI())) {
+ newEnvelope =
OMAbstractFactory.getSOAP11Factory().getDefaultEnvelope();
+ } else {
+ newEnvelope =
OMAbstractFactory.getSOAP12Factory().getDefaultEnvelope();
+ }
+
+ if (envelope.getHeader() != null) {
+ Iterator itr = envelope.getHeader().cloneOMElement().getChildren();
+ while (itr.hasNext()) {
+ newEnvelope.getHeader().addChild((OMNode) itr.next());
+ }
+ }
+
+ if (envelope.getBody() != null) {
+ Iterator itr = envelope.getBody().cloneOMElement().getChildren();
+ while (itr.hasNext()) {
+ newEnvelope.getBody().addChild((OMNode) itr.next());
+ }
+ }
+
+ return newEnvelope;
+ }
+}
Modified:
trunk/commons/caching/modules/mar/src/main/java/org/wso2/caching/CachingModule.java
==============================================================================
---
trunk/commons/caching/modules/mar/src/main/java/org/wso2/caching/CachingModule.java
(original)
+++
trunk/commons/caching/modules/mar/src/main/java/org/wso2/caching/CachingModule.java
Tue Dec 11 21:28:17 2007
@@ -41,46 +41,50 @@
public String moduleName;
/**
- * This method will be executed in order to initialize the caching module
and this will set the
- * cache object to the configuration context with the XMLIdentifier
initialized
+ * This method will be executed in order to initialize the caching module
and this
+ * will set the CacheManager object to the ConfigurationContext and also
populates the
+ * CacheConfiguration according to the cache policy
*
- * @param configContext - ConfigurationContext fo the cache object to be
added
+ * @param configContext - ConfigurationContext for the cache objects to be
added
* @param module - AxisModule to be initialized
- * @throws AxisFault - If an error occured in the process of initializing
+ * @throws AxisFault if an error occured in the process of initializing
*/
public void init(ConfigurationContext configContext, AxisModule module)
throws AxisFault {
this.moduleName = module.getName();
- CacheManager cacheManager = null;
+ CacheConfiguration cacheConfig = null;
PolicyInclude policyInclude = module.getPolicyInclude();
if (policyInclude != null) {
Policy policy = policyInclude.getEffectivePolicy();
if (policy != null) {
-
try {
-
- cacheManager =
CachingPolicyProcessor.processInitCachingPolicy(policy);
-
+ cacheConfig =
CachingPolicyProcessor.processCachingPolicy(policy);
} catch (CachingException e) {
-
- handleException("Unable to initialize " +
- "the caching module, caching policy not found", e);
+ handleException("Unable to initialize the caching module :
" +
+ "Error in processing caching policy", e);
}
} else {
- cacheManager = new CacheManager();
+ if (log.isDebugEnabled()) {
+ log.debug("Using the default initializer for the
CacheConfiguration");
+ }
+ cacheConfig = new CacheConfiguration();
}
} else {
-
if (log.isDebugEnabled()) {
- log.debug("Policy for the caching module not " +
- "found. Using the default initializer for the
cacheManager");
+ log.debug("Using the default initializer for the
CacheConfiguration");
}
- cacheManager = new CacheManager();
+ cacheConfig = new CacheConfiguration();
}
-
configContext.getAxisConfiguration().addParameter(CachingConstants.CACHE_MANAGER,
cacheManager);
+ if (cacheConfig != null) {
+ configContext.getAxisConfiguration().addParameter(
+ CachingConstants.CACHE_CONFIGURATION, cacheConfig);
+ configContext.setProperty(CachingConstants.CACHE_MANAGER, new
CacheManager());
+ } else {
+ handleException("Unable to engage caching : Error in processing
policy");
+ }
}
/**
@@ -89,24 +93,35 @@
*/
public void engageNotify(AxisDescription axisDescription) throws AxisFault
{
- CacheManager cacheManager = null;
+ CacheConfiguration cacheConfig = null;
PolicyInclude policyInclude = axisDescription.getPolicyInclude();
if (policyInclude != null) {
-
- try {
-
- cacheManager = CachingPolicyProcessor.
-
processEngageCachingPolicy(policyInclude.getEffectivePolicy());
-
- } catch (CachingException e) {
-
- handleException("Unable to engage caching", e);
+ Policy policy = policyInclude.getEffectivePolicy();
+ if (policy != null) {
+ try {
+ cacheConfig =
CachingPolicyProcessor.processCachingPolicy(policy);
+ } catch (CachingException e) {
+ handleException("Unable to engage the caching module : " +
+ "Error in processing caching policy", e);
+ }
+ } else {
+ if (log.isDebugEnabled()) {
+ log.debug("Using the default initializer for the
CacheConfiguration");
+ }
+ cacheConfig = new CacheConfiguration();
+ }
+ } else {
+ if (log.isDebugEnabled()) {
+ log.debug("Using the default initializer for the
CacheConfiguration");
}
+ cacheConfig = new CacheConfiguration();
}
- if (cacheManager != null) {
- axisDescription.addParameter(CachingConstants.CACHE_MANAGER,
cacheManager);
+ if (cacheConfig != null) {
+ axisDescription.addParameter(CachingConstants.CACHE_CONFIGURATION,
cacheConfig);
+ } else {
+ handleException("Unable to engage caching : Error in processing
policy");
}
}
@@ -133,20 +148,30 @@
* @throws AxisFault
*/
public void shutdown(ConfigurationContext configurationContext) throws
AxisFault {
- // TODO
+ configurationContext.removeProperty(CachingConstants.CACHE_MANAGER);
}
/**
- * This is a private method to be called for handling exception and loggin
the debug information
+ * This is a private method to be called for handling exception and
+ * loggin the debug information
*
* @param message - String message of the exception
* @param cause - Throwable cause of the exception
+ * @throws AxisFault wrapping the exception encountered, with the passed
message
*/
- private void handleException(String message, Throwable cause) {
+ private void handleException(String message, Throwable cause) throws
AxisFault {
if (log.isDebugEnabled()) {
log.debug(message, cause);
}
- new AxisFault(message, cause);
+ throw new AxisFault(message, cause);
+ }
+
+ private void handleException(String message) throws AxisFault {
+
+ if (log.isDebugEnabled()) {
+ log.debug(message);
+ }
+ throw new AxisFault(message);
}
}
Modified:
trunk/commons/caching/modules/mar/src/main/java/org/wso2/caching/handlers/CachingInHandler.java
==============================================================================
---
trunk/commons/caching/modules/mar/src/main/java/org/wso2/caching/handlers/CachingInHandler.java
(original)
+++
trunk/commons/caching/modules/mar/src/main/java/org/wso2/caching/handlers/CachingInHandler.java
Tue Dec 11 21:28:17 2007
@@ -17,16 +17,15 @@
package org.wso2.caching.handlers;
import org.apache.axis2.AxisFault;
+import org.apache.axis2.context.ConfigurationContext;
import org.apache.axis2.context.MessageContext;
import org.apache.axis2.context.OperationContext;
import org.apache.axis2.description.AxisOperation;
-import org.apache.axis2.description.Parameter;
import org.apache.axis2.description.AxisService;
-import org.wso2.caching.CacheManager;
-import org.wso2.caching.CachingConstants;
-import org.wso2.caching.CachingException;
-import org.wso2.caching.transport.TransportProcessorFactory;
+import org.apache.axis2.description.Parameter;
+import org.wso2.caching.*;
import org.wso2.caching.digest.DigestGenerator;
+import org.wso2.caching.transport.TransportProcessorFactory;
public class CachingInHandler extends CachingHandler {
@@ -47,105 +46,138 @@
try {
if
(TransportProcessorFactory.getInTransportProcessor(msgContext).isCachingDisabled())
{
if (log.isDebugEnabled()) {
- log.debug("Caching is forcelly disabled by the transport");
+ log.debug("Caching is forcelly disabled by the transport :
SKIP the cache");
}
return InvocationResponse.CONTINUE;
}
} catch (CachingException e) {
if (log.isDebugEnabled()) {
- log.debug("Unable to check the transport headers for disable
caching");
+ log.debug("Unable to check the transport headers to infer
cache-control", e);
}
}
+
+ CacheConfiguration chCfg = null;
+ Parameter ccfgParam
+ =
msgContext.getAxisMessage().getParameter(CachingConstants.CACHE_CONFIGURATION);
+
+ if (ccfgParam != null && ccfgParam.getValue() instanceof
CacheConfiguration) {
+ chCfg = (CacheConfiguration) ccfgParam.getValue();
+ } else {
+ handleException("Unable to perform " +
+ "caching : Could not find the CacheConfiguration");
+ }
- AxisOperation axisOperation = msgContext.getAxisOperation();
-
- if (axisOperation != null) {
+ ConfigurationContext cfgCtx = msgContext.getConfigurationContext();
+ if (cfgCtx != null) {
- Parameter cacheParam =
axisOperation.getParameter(CachingConstants.CACHE_MANAGER);
- Object requestHash = null;
- CacheManager cacheManager = null;
-
- if (cacheParam != null && cacheParam.getValue() != null) {
-
- cacheManager = (CacheManager) cacheParam.getValue();
- try {
- requestHash =
TransportProcessorFactory.getInTransportProcessor(
- msgContext).readHashKey(msgContext);
- } catch (CachingException e) {
- log.debug("Unable to read the tarnsport " +
- "headers for caching information", e);
+ String requestHash = null;
+ try {
+ DigestGenerator digestGenerator = chCfg.getDigestGenerator();
+ if (digestGenerator != null) {
+ requestHash = digestGenerator.getDigest(msgContext);
+ } else {
+ handleException("Unable to retrieve the DigestGenerator
from the CacheManager");
}
+ } catch (CachingException ce) {
+ handleException("Unable to perform " +
+ "caching : Error in generating the request hash");
+ }
- if (requestHash == null) {
- DigestGenerator digestGenerator =
cacheManager.getGenerator();
- if (digestGenerator != null) {
+ CacheManager cacheManager;
+ Object cmObj =
cfgCtx.getPropertyNonReplicable(CachingConstants.CACHE_MANAGER);
+ if (cmObj != null && cmObj instanceof CacheManager) {
+ cacheManager = (CacheManager) cmObj;
+ } else {
-// requestHash =
cacheManager.getGenerator().getDigest(msgContext);
+ synchronized (cfgCtx) {
+ // check again after taking the lock to make sure no one
else did it before us
+ cmObj =
cfgCtx.getPropertyNonReplicable(CachingConstants.CACHE_MANAGER);
+ if (cmObj != null && cmObj instanceof CacheManager) {
+ cacheManager = (CacheManager) cmObj;
} else {
-
- handleException("Unable to get the DigestGenerator");
+ if (log.isDebugEnabled()) {
+ log.debug("Creating/recreating the CacheManager
object");
+ }
+ cacheManager = new CacheManager();
+ cfgCtx.setProperty(CachingConstants.CACHE_MANAGER,
cacheManager);
}
}
-
- } else {
- handleException("Unable to find the CacheManager object");
}
- OperationContext operationCntxt = msgContext.getOperationContext();
-
- if (operationCntxt != null && requestHash != null) {
+ OperationContext opCtx = msgContext.getOperationContext();
+ if (opCtx != null) {
if (msgContext.isServerSide()) {
+ opCtx.setProperty(CachingConstants.REQUEST_HASH,
requestHash);
- operationCntxt.setProperty(
- CachingConstants.REQUEST_HASH_KEY, requestHash);
-
- synchronized (this) {
-// if (cacheManager.getExpireTime() <
System.currentTimeMillis()) {
-// cacheManager.resetCache();
-// } else if (cacheManager.containsKey(requestHash)) {
+ if (cacheManager.containsKey(requestHash)
+ && cacheManager.getResponseForKey(requestHash, cfgCtx)
!= null) {
+ CachedObject cachedObj =
cacheManager.getResponseForKey(
+ requestHash, cfgCtx);
+ if (!cachedObj.isExpired() &&
cachedObj.getResponseEnvelope() != null) {
if (log.isDebugEnabled()) {
- log.debug("Response for the request found in
the cacheManager");
+ log.debug("Cache-hit for message ID : "
+ + msgContext.getMessageID());
}
+ opCtx.setProperty(CachingConstants.CACHED_OBJECT,
cachedObj);
- AxisService axisService =
msgContext.getAxisService();
- if (axisService != null) {
- AxisOperation cachedOperation = axisService
-
.getOperation(CachingConstants.CACHED_OPERATION_QNAME);
- if (cachedOperation != null) {
- cachedOperation.setControlOperation(true);
-
msgContext.setAxisOperation(cachedOperation);
- } else {
- handleException("Unable to find the cached
operation");
- }
+ } else {
+ // cache exists, but has expired...
+ cachedObj.expire();
+ if (log.isDebugEnabled()) {
+ log.debug("Existing cached " +
+ "response has expired. Reset cache
element");
}
-// operationCntxt.setProperty(CachingConstants.
-// CACHED_RESPONSE_KEY,
cacheManager.getResponseForKey(requestHash));
-// } else {
-
- if (log.isDebugEnabled()) {
- log.debug("There are no cached responses for
the request.");
+ opCtx.setProperty(CachingConstants.CACHED_OBJECT,
null);
+ cacheManager.addResponseWithKey(requestHash,
cachedObj, cfgCtx);
+ cfgCtx.setProperty(CachingConstants.CACHE_MANAGER,
cacheManager);
+ cfgCtx.flush();
+ return InvocationResponse.CONTINUE;
+ }
+
+ AxisService axisService = msgContext.getAxisService();
+ if (axisService != null) {
+ AxisOperation cachedOperation = axisService
+
.getOperation(CachingConstants.CACHED_OPERATION_QNAME);
+ if (cachedOperation != null) {
+ cachedOperation.setControlOperation(true);
+ msgContext.setAxisOperation(cachedOperation);
+ } else {
+ handleException("Unable to perform " +
+ "caching : Could not find the cached
operation");
}
+ }
- operationCntxt.setProperty(
- CachingConstants.CACHED_RESPONSE_KEY,
null);
-// }
+ } else {
+ if (log.isDebugEnabled()) {
+ log.debug("There are no cached responses for the
request.");
+ }
+ CachedObject cachedObj = new CachedObject();
+ cachedObj.setRequestHash(requestHash);
+ cachedObj.setTimeout(chCfg.getTimeout());
+ cacheManager.addResponseWithKey(requestHash,
cachedObj, cfgCtx);
+ opCtx.setProperty(CachingConstants.CACHED_OBJECT,
null);
+ return InvocationResponse.CONTINUE;
}
-
} else {
-// cacheManager.addResponseWithKey(requestHash,
msgContext.getEnvelope());
+ // todo: client side caching
}
} else {
- handleException("Error in caching : possibly " +
- "the OperationContext not found to process");
+ handleException("Unable to perform caching : OperationContext
not " +
+ "found to store cache details");
}
- } else {
- handleException("AxisOperation not found to process");
+ } else {
+ handleException("Unable to perform caching : ConfigurationContext
not " +
+ "found to process cache");
}
+ if (log.isDebugEnabled()) {
+ log.debug("CachingInHandler execution finished");
+ }
+
return InvocationResponse.CONTINUE;
}
}
Modified:
trunk/commons/caching/modules/mar/src/main/java/org/wso2/caching/handlers/CachingOutHandler.java
==============================================================================
---
trunk/commons/caching/modules/mar/src/main/java/org/wso2/caching/handlers/CachingOutHandler.java
(original)
+++
trunk/commons/caching/modules/mar/src/main/java/org/wso2/caching/handlers/CachingOutHandler.java
Tue Dec 11 21:28:17 2007
@@ -16,15 +16,18 @@
package org.wso2.caching.handlers;
+import org.apache.axiom.soap.SOAPEnvelope;
import org.apache.axis2.AxisFault;
+import org.apache.axis2.context.ConfigurationContext;
import org.apache.axis2.context.MessageContext;
import org.apache.axis2.context.OperationContext;
-import org.apache.axis2.description.AxisOperation;
import org.apache.axis2.description.Parameter;
-import org.wso2.caching.CacheManager;
-import org.wso2.caching.CachingConstants;
-import org.wso2.caching.CachingException;
+import org.wso2.caching.*;
import org.wso2.caching.transport.TransportProcessorFactory;
+import org.wso2.caching.util.SOAPMessageHelper;
+
+import javax.xml.stream.XMLStreamException;
+import java.io.ByteArrayOutputStream;
public class CachingOutHandler extends CachingHandler {
@@ -42,60 +45,88 @@
log.debug("Starting the execution of the CachingOutHandler");
}
- // todo: disable caching on the transport if specified (how?)
TrpProFac.getOutTrpPro.disable
+ try {
+ if (TransportProcessorFactory.getOutTransportProcessor(
+ msgContext).isCachingDisabled()) {
+
+ if (log.isDebugEnabled()) {
+ log.debug("Caching is forcelly disabled by the transport :
SKIP the cache");
+ }
+ return InvocationResponse.CONTINUE;
+ }
+ } catch (CachingException e) {
+ if (log.isDebugEnabled()) {
+ log.debug("Unable to check the transport headers to infer
cache-control", e);
+ }
+ }
- OperationContext operationCntx = msgContext.getOperationContext();
+ CacheConfiguration cachCfg = null;
+ Parameter chCfgParam = msgContext.getAxisMessage()
+ .getParameter(CachingConstants.CACHE_CONFIGURATION);
+ if (chCfgParam != null && chCfgParam.getValue() != null
+ && chCfgParam.getValue() instanceof CacheConfiguration) {
+ cachCfg = (CacheConfiguration) chCfgParam.getValue();
+ } else {
+ handleException("Unable to process caching : Could not find the
CacheConfiguration");
+ }
+ OperationContext operationCntx = msgContext.getOperationContext();
if (operationCntx != null) {
- Object cachedResponse =
operationCntx.getProperty(CachingConstants.CACHED_RESPONSE_KEY);
- Object requestHash =
operationCntx.getProperty(CachingConstants.REQUEST_HASH_KEY);
-
- AxisOperation axisOperation = msgContext.getAxisOperation();
- Parameter cache = null;
- if (axisOperation != null) {
- cache =
axisOperation.getParameter(CachingConstants.CACHE_MANAGER);
+ ConfigurationContext cfgCtx = msgContext.getConfigurationContext();
+ Object cacheMgrObj = null;
+ if (cfgCtx != null) {
+ cacheMgrObj =
cfgCtx.getProperty(CachingConstants.CACHE_MANAGER);
} else {
- handleException("AxisOperation not found for the processing");
+ handleException("Unable to store the response in to the cache
: " +
+ "ConfigurationContext cannot be found");
}
- if (cache != null && cache.getValue() != null) {
-
+ if (cacheMgrObj != null && cacheMgrObj instanceof CacheManager) {
+ CacheManager cacheMgr = (CacheManager) cacheMgrObj;
if (msgContext.isServerSide()) {
-
- if (cachedResponse == null) {
-// ((CacheManager) cache.getValue()).addResponseWithKey(
-// requestHash, msgContext.getEnvelope());
+
+ Object cachedRespObj = operationCntx.getProperty(
+ CachingConstants.CACHED_OBJECT);
+ String requestHash = (String) operationCntx.getProperty(
+ CachingConstants.REQUEST_HASH);
+
+ // process the server side out path cache
+ if (cachedRespObj == null) {
+ Object cachedObj =
cacheMgr.getResponseForKey(requestHash, cfgCtx);
+ if (cachedObj != null && cachedObj instanceof
CachedObject) {
+ CachedObject cachedObject = (CachedObject)
cachedObj;
+ SOAPEnvelope envelope
+ =
SOAPMessageHelper.cloneSOAPEnvelope(msgContext.getEnvelope());
+ ByteArrayOutputStream outStream = new
ByteArrayOutputStream();
+ try {
+ envelope.serialize(outStream);
+
cachedObject.setResponseEnvelope(outStream.toByteArray());
+ cachedObject.setResponseHash(
+
cachCfg.getDigestGenerator().getDigest(msgContext));
+ } catch (XMLStreamException e) {
+ handleException("Unable to store the cached
response : " +
+ "Error in serializing the response", e);
+ } catch (CachingException e) {
+ handleException("Unable to store the cached
response : " +
+ "Error in generating the response hash",
e);
+ }
+ cacheMgr.addResponseWithKey((String) requestHash,
cachedObject, cfgCtx);
+ cfgCtx.flush();
+ }
}
} else {
-
-// CacheManager cacheManagerObj = (CacheManager)
cache.getValue();
-// requestHash =
cacheManagerObj.getGenerator().getDigest(msgContext);
-//
-// if (cacheManagerObj.getExpireTime() <
System.currentTimeMillis()) {
-// cacheManagerObj.resetCache();
-// } else if (cacheManagerObj.containsKey(requestHash)) {
- // todo: clientside - how to stop sending the request
and use the existing response
-// return InvocationResponse.ABORT; // or may be SUSPEND
-// }
+ // todo: client side caching
}
} else {
- handleException("Unable to find the CacheManager object");
+ handleException("Unable to store the cached response : " +
+ "Unable to find the CacheManager object");
}
-
- try {
- TransportProcessorFactory.getOutTransportProcessor(
- msgContext).writeHashKey(msgContext,
requestHash.toString());
- } catch (CachingException e) {
- if (log.isDebugEnabled()) {
- log.debug("Unable to write the caching headers to the
transport", e);
- }
- }
-
} else {
- handleException("OperationContext not found for the processing");
+ handleException("Unable to store the cached response : " +
+ "OperationContext not found for the processing");
}
return InvocationResponse.CONTINUE;
Modified:
trunk/commons/caching/modules/mar/src/main/java/org/wso2/caching/receivers/CachedMessageReceiver.java
==============================================================================
---
trunk/commons/caching/modules/mar/src/main/java/org/wso2/caching/receivers/CachedMessageReceiver.java
(original)
+++
trunk/commons/caching/modules/mar/src/main/java/org/wso2/caching/receivers/CachedMessageReceiver.java
Tue Dec 11 21:28:17 2007
@@ -16,18 +16,26 @@
package org.wso2.caching.receivers;
-import org.apache.axiom.soap.SOAPEnvelope;
import org.apache.axis2.AxisFault;
import org.apache.axis2.context.MessageContext;
import org.apache.axis2.context.OperationContext;
import org.apache.axis2.engine.AxisEngine;
import org.apache.axis2.receivers.AbstractMessageReceiver;
+import org.apache.axis2.saaj.util.SAAJUtil;
import org.apache.axis2.util.MessageContextBuilder;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
+import org.wso2.caching.CachedObject;
import org.wso2.caching.CachingConstants;
import org.wso2.caching.CachingException;
+import javax.xml.soap.MessageFactory;
+import javax.xml.soap.MimeHeaders;
+import javax.xml.soap.SOAPException;
+import javax.xml.soap.SOAPMessage;
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+
public class CachedMessageReceiver extends AbstractMessageReceiver {
/**
@@ -62,33 +70,60 @@
if (operationContext != null) {
operationContext.addMessageContext(outMsgContext);
-
if (log.isDebugEnabled()) {
log.debug("Serving from the cache...");
}
-
- Object response =
operationContext.getProperty(CachingConstants.CACHED_RESPONSE_KEY);
-
- if (response != null) {
- outMsgContext.setEnvelope((SOAPEnvelope) response);
+
+ Object cachedObj =
operationContext.getProperty(CachingConstants.CACHED_OBJECT);
+ if (cachedObj != null && cachedObj instanceof CachedObject) {
+
+ try {
+
+ MessageFactory mf = MessageFactory.newInstance();
+ SOAPMessage smsg = mf.createMessage(new MimeHeaders(),
+ new ByteArrayInputStream(
+
((CachedObject)cachedObj).getResponseEnvelope()));
+ org.apache.axiom.soap.SOAPEnvelope omSOAPEnv =
+
SAAJUtil.toOMSOAPEnvelope(smsg.getSOAPPart().getDocumentElement());
+ outMsgContext.setEnvelope(omSOAPEnv);
+
+ } catch (SOAPException e) {
+ handleException("Unable to serve from the cache : " +
+ "Unable to get build the response from the byte
stream", e);
+ } catch (IOException e) {
+ handleException("Unable to serve from the cache : " +
+ "I/O Error in building the response envelope from
the byte stream");
+ }
+
AxisEngine.send(outMsgContext);
+
} else {
handleException("Unable to find the response in the
cache");
}
} else {
- handleException("OperationContext not found for processing");
+ handleException("Unable to serve from " +
+ "the cache : OperationContext not found for processing");
}
} else {
- handleException("Unable to get the out message context");
+ handleException("Unable to serve from " +
+ "the cache : Unable to get the out message context");
}
}
- private void handleException(String message) {
+ private void handleException(String message) throws AxisFault {
if (log.isDebugEnabled()) {
log.debug(message);
}
- new AxisFault(message, new CachingException(message));
+ throw new AxisFault(message, new CachingException(message));
+ }
+
+ private void handleException(String message, Throwable cause) throws
AxisFault {
+
+ if (log.isDebugEnabled()) {
+ log.debug(message, cause);
+ }
+ throw new AxisFault(message, new CachingException(message, cause));
}
}
Modified:
trunk/commons/caching/modules/mar/src/main/resources/META-INF/module.xml
==============================================================================
--- trunk/commons/caching/modules/mar/src/main/resources/META-INF/module.xml
(original)
+++ trunk/commons/caching/modules/mar/src/main/resources/META-INF/module.xml
Tue Dec 11 21:28:17 2007
@@ -41,13 +41,14 @@
xmlns:wsch="http://www.wso2.org/ns/2007/06/commons/caching"
xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
<wsch:CachingAssertion>
- <!--<wsp:Policy>-->
- <!--<wsch:ID>Default</wsch:ID>-->
- <wsp:ExactlyOne>
+ <wsp:Policy>
+ <wsp:All>
<wsch:XMLIdentifier>org.wso2.caching.digest.DOMHASHGenerator</wsch:XMLIdentifier>
<wsch:ExpireTime>60000</wsch:ExpireTime>
- </wsp:ExactlyOne>
- <!--</wsp:Policy>-->
+ <wsch:MaxCacheSize>1000</wsch:MaxCacheSize>
+ <wsch:MaxMessageSize>1000</wsch:MaxMessageSize>
+ </wsp:All>
+ </wsp:Policy>
</wsch:CachingAssertion>
</wsp:Policy>
</module>
Modified: trunk/commons/caching/pom.xml
==============================================================================
--- trunk/commons/caching/pom.xml (original)
+++ trunk/commons/caching/pom.xml Tue Dec 11 21:28:17 2007
@@ -17,6 +17,16 @@
<artifactId>axis2-kernel</artifactId>
<version>${axis2.version}</version>
</dependency>
+ <dependency>
+ <groupId>org.apache.axis2</groupId>
+ <artifactId>axis2-saaj</artifactId>
+ <version>${axis2.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.axis2</groupId>
+ <artifactId>axis2-clustering</artifactId>
+ <version>${axis2.version}</version>
+ </dependency>
<!-- AXIOM Dependencies -->
<dependency>
<groupId>org.apache.ws.commons.axiom</groupId>
_______________________________________________
Commons-dev mailing list
[email protected]
http://wso2.org/cgi-bin/mailman/listinfo/commons-dev