synapse-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From asan...@apache.org
Subject svn commit: r584077 - in /webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse: config/xml/CacheMediatorFactory.java config/xml/CacheMediatorSerializer.java mediators/builtin/CacheMediator.java
Date Fri, 12 Oct 2007 06:50:52 GMT
Author: asankha
Date: Thu Oct 11 23:50:51 2007
New Revision: 584077

URL: http://svn.apache.org/viewvc?rev=584077&view=rev
Log:
review and fix cache mediator - ruwan pls verify

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=584077&r1=584076&r2=584077&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
Thu Oct 11 23:50:51 2007
@@ -112,7 +112,7 @@
             if (sequenceAttr != null && sequenceAttr.getAttributeValue() != null)
{
                 cache.setOnCacheHitRef(sequenceAttr.getAttributeValue());
             } else {
-                cache.setOnCacheHit(
+                cache.setOnCacheHitSequence(
                     new SequenceMediatorFactory().createAnonymousSequence(onCacheHitElem));
             }
         }

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=584077&r1=584076&r2=584077&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
Thu Oct 11 23:50:51 2007
@@ -67,10 +67,10 @@
             onCacheHit.addAttribute(
                 fac.createOMAttribute("sequence", nullNS, mediator.getOnCacheHitRef()));
             cache.addChild(onCacheHit);
-        } else if (mediator.getOnCacheHit() != null) {
+        } else if (mediator.getOnCacheHitSequence() != null) {
             OMElement onCacheHit = fac.createOMElement("onCacheHit", synNS);
             new SequenceMediatorSerializer().serializeChildren(
-                onCacheHit, mediator.getOnCacheHit().getList());
+                onCacheHit, mediator.getOnCacheHitSequence().getList());
             cache.addChild(onCacheHit);
         }
 

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=584077&r1=584076&r2=584077&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
Thu Oct 11 23:50:51 2007
@@ -20,6 +20,7 @@
 package org.apache.synapse.mediators.builtin;
 
 import org.apache.axis2.AxisFault;
+import org.apache.axis2.engine.AxisConfiguration;
 import org.apache.axis2.description.Parameter;
 import org.apache.synapse.MessageContext;
 import org.apache.synapse.core.axis2.Axis2MessageContext;
@@ -45,9 +46,10 @@
     // factory will asign a default value to enable disk based caching
     private int diskCacheSize = 0;
     private long timeout = 0L;
-    private SequenceMediator onCacheHit = null;
+    private SequenceMediator onCacheHitSequence = null;
     private String onCacheHitRef = null;
-    private static final String CACHE_OBJ_PREFIX = "chache_obj_";
+    private String cacheObjKey = CachingConstants.CACHE_OBJECT; // default per-host
+    private static final String CACHE_OBJ_PREFIX = "synapse.cache_obj_";
 
     public boolean mediate(MessageContext synCtx) {
 
@@ -63,201 +65,232 @@
             }
         }
 
-        if (synCtx.getConfiguration().getAxisConfiguration() == null) {
-            handleException("Unable to mediate the message in the cache "
-                + ": AxisConfiguration not found", synCtx);
+        AxisConfiguration axisCfg = synCtx.getConfiguration().getAxisConfiguration();
+        if (axisCfg == null) {
+            handleException("Unable to perform caching, "
+                + " AxisConfiguration cannot be found", synCtx);
+            return false; // never executes.. but keeps IDE happy
         }
 
-        try {
+        if (traceOrDebugOn) {
+            traceOrDebug(traceOn, "Looking up cache at scope : " +
+                scope + " with ID : " + cacheObjKey);
+        }
 
-            String cacheObjKey = CachingConstants.CACHE_OBJECT;
-            if (CachingConstants.SCOPE_PER_HOST.equals(scope)) {
-                if (traceOrDebugOn) {
-                    traceOrDebug(traceOn, "Looking up the global cache object : scope = "
+ scope);
-                }
-                cacheObjKey = CachingConstants.CACHE_OBJECT;
-            } else if (CachingConstants.SCOPE_PER_MEDIATOR.equals(scope)) {
-                if (traceOrDebugOn) {
-                    traceOrDebug(traceOn, "Looking up the mediator " +
-                        "specific cache object : scope = " + scope);
+        // look up cache
+        Parameter param = axisCfg.getParameter(cacheObjKey);
+        Cache cache = null;
+        if (param != null && param.getValue() instanceof Cache) {
+            cache = (Cache) param.getValue();
+
+        } else {
+            synchronized (axisCfg) {
+                // check again after taking the lock to make sure no one else did it before
us
+                param = axisCfg.getParameter(cacheObjKey);
+                if (param != null && param.getValue() instanceof Cache) {
+                    cache = (Cache) param.getValue();
+
+                } else {
+                    if (traceOrDebugOn) {
+                        traceOrDebug(traceOn, "Creating/recreating the cache object");
+                    }
+                    cache = new Cache();
+                    try {
+                        axisCfg.addParameter(cacheObjKey, cache);
+                    } catch (AxisFault af) {
+                        auditWarn("Unable to create a cache with ID : " + cacheObjKey, synCtx);
+                    }
                 }
-                cacheObjKey = CACHE_OBJ_PREFIX + id;
-            } else {
-                handleException("Scope for the cache mediator "
-                    + scope + " is not supported yet", synCtx);
             }
+        }
 
-            Parameter param =
-                synCtx.getConfiguration().getAxisConfiguration().getParameter(cacheObjKey);
-            Cache cache;
-            if (param != null && param.getValue() instanceof Cache) {
-                cache = (Cache) param.getValue();
-            } else {
+        boolean result = true;
+        if (synCtx.isResponse()) {
+            processResponseMessage(traceOrDebugOn, traceOn, synCtx, cache);
+
+        } else {
+            result = processRequestMessage(synCtx, traceOrDebugOn, traceOn, cache);
+        }
+
+        if (traceOrDebugOn) {
+            traceOrDebug(traceOn, "End : Cache mediator");
+        }
+        return result;
+    }
+
+    /**
+     * Process a response message through this cache mediator. This finds the Cache used,
and
+     * updates it for the corresponding request hash
+     * @param traceOrDebugOn is trace or debug logging on?
+     * @param traceOn is tracing on?
+     * @param synCtx the current message (response)
+     * @param cache the cache
+     */
+    private void processResponseMessage(boolean traceOrDebugOn, boolean traceOn,
+        MessageContext synCtx, Cache cache) {
+
+        Object requestHash = synCtx.getProperty(CachingConstants.REQUEST_HASH_KEY);
+
+        if (requestHash != null) {
+            if (traceOrDebugOn) {
+                traceOrDebug(traceOn, "Storing the response message into the cache at scope
: "
+                    + scope + " with ID : " + cacheObjKey + " for request hash : " + requestHash);
+            }
+
+            Object obj = cache.getResponseForKey(requestHash);
+
+            if (obj != null && obj instanceof CachedObject) {
+
+                CachedObject cachedObj = (CachedObject) obj;
                 if (traceOrDebugOn) {
-                    traceOrDebug(traceOn, "Creating/Recreating the cache object");
+                    traceOrDebug(traceOn, "Storing the response for the message with ID :
"
+                        + synCtx.getMessageID() + " with request hash ID : " +
+                        cachedObj.getRequestHash() + " in the cache : " + cacheObjKey);
                 }
-                cache = new Cache();
-                synCtx.getConfiguration().getAxisConfiguration().addParameter(cacheObjKey,
cache);
+
+                cachedObj.setResponseEnvelope(
+                    MessageHelper.cloneSOAPEnvelope(synCtx.getEnvelope()));
+
+                // todo: there seems to be a problem with the digest generation of the response
+                // this is not required for the moment
+                // cachedObj.setResponseHash(digestGenerator.getDigest(
+                //     ((Axis2MessageContext) synCtx).getAxis2MessageContext()));
+
+                cachedObj.setExpireTime(
+                    System.currentTimeMillis() + cachedObj.getTimeout());
+
+            } else {
+                auditWarn("A response message without a valid mapping to the " +
+                    "request hash found. Unable to store the response in cache", synCtx);
             }
 
-            if (synCtx.isResponse()) {
+        } else {
+            auditWarn("A response message without a mapping to the " +
+                "request hash found. Unable to store the response in cache", synCtx);
+        }
+    }
+
+    /**
+     * Processes a request message through the cache mediator. Generates the request hash
and
+     * looks up for a hit, if found; then the specified named or anonymous sequence is executed
+     * or marks this message as a response and sends back directly to client.
+     * @param synCtx incoming request message
+     * @param traceOrDebugOn is tracing or debug logging on?
+     * @param traceOn is tracing on?
+     * @param cache the cache
+     * @return should this mediator terminate further processing?
+     */
+    private boolean processRequestMessage(MessageContext synCtx, boolean traceOrDebugOn,
boolean traceOn, Cache cache) {
+
+        Object requestHash = digestGenerator
+            .getDigest(((Axis2MessageContext) synCtx).getAxis2MessageContext());
+        synCtx.setProperty(CachingConstants.REQUEST_HASH_KEY, requestHash);
+
+        if (traceOrDebugOn) {
+            traceOrDebug(traceOn, "Generated request hash : " + requestHash);
+        }
+
+        if (cache.containsKey(requestHash) &&
+            cache.getResponseForKey(requestHash) instanceof CachedObject) {
+
+            // get the response from the cache and attach to the context and change the
+            // direction of the message
+            CachedObject cachedObj = (CachedObject) cache.getResponseForKey(requestHash);
+
+            if (!cachedObj.isExpired() && cachedObj.getResponseEnvelope() != null)
{
 
                 if (traceOrDebugOn) {
-                    traceOrDebug(traceOn, "Starting the response message store in the cache");
+                    traceOrDebug(traceOn,
+                        "Cache-hit for message ID : " + synCtx.getMessageID());
                 }
 
-                Object obj;
-                if (synCtx.getProperty(CachingConstants.REQUEST_HASH_KEY) != null) {
-                    obj = cache.getResponseForKey(
-                        synCtx.getProperty(CachingConstants.REQUEST_HASH_KEY));
-                } else {
+                // mark as a response and replace envelope from cache
+                synCtx.setResponse(true);
+                try {
+                    synCtx.setEnvelope(cachedObj.getResponseEnvelope());
+                } catch (AxisFault axisFault) {
+                    handleException(
+                        "Error setting response envelope from cache : " + cacheObjKey, synCtx);
+                }
+
+                // take specified action on cache hit
+                if (onCacheHitSequence != null) {
+                    // if there is an onCacheHit use that for the mediation
                     if (traceOrDebugOn) {
-                        traceOrDebug(traceOn, "Response message with no mapping to the "
+
-                            "request hash found : Unable to store the response for caching");
-                        traceOrDebug(traceOn, "End : Clone mediator");
+                        traceOrDebug(traceOn, "Delegating message to the onCachingHit " +
+                            "Anonymous sequence");
                     }
-                    return true;
-                }
-                
-                if (obj != null && obj instanceof CachedObject) {
-                    
+                    onCacheHitSequence.mediate(synCtx);
+
+                } else if (onCacheHitRef != null) {
+
                     if (traceOrDebugOn) {
-                        traceOrDebug(traceOn, "Storing the response for the message "
-                            + synCtx.getMessageID() + " in the cache");
+                        traceOrDebug(traceOn, "Delegating message to the onCachingHit " +
+                            "sequence : " + onCacheHitRef);
                     }
-                    
-                    CachedObject cachedObj = (CachedObject) obj;
-                    cachedObj.setResponseEnvelope(
-                        MessageHelper.cloneSOAPEnvelope(synCtx.getEnvelope()));
-                    // todo: there seems to be a problem with the digest generation of the
response
-                    // this is not required for the moment
-                    // cachedObj.setResponseHash(digestGenerator.getDigest(
-                    //     ((Axis2MessageContext) synCtx).getAxis2MessageContext()));
-                    cachedObj.setExpireTime(
-                        System.currentTimeMillis() + cachedObj.getTimeout());
+                    synCtx.getSequence(onCacheHitRef).mediate(synCtx);
+
                 } else {
+
                     if (traceOrDebugOn) {
-                        traceOrDebug(traceOn, "Response message with a mapping in " +
-                            "the cache for the request no found : " +
-                            "Unable to store the response for caching");
-                        traceOrDebug(traceOn, "End : Clone mediator");
+                        traceOrDebug(traceOn, "Request message " + synCtx.getMessageID()
+
+                            " has served from the cache : " + cacheObjKey);
                     }
-                    return true;
+                    // send the response back if there is not onCacheHit is specified
+                    synCtx.setTo(null);
+                    Axis2Sender.sendBack(synCtx);
                 }
-            } else {
+                // stop any following mediators from executing
+                return false;
 
+            } else {
+                // cache exists, but has expired...
+                cachedObj.clearCache();
                 if (traceOrDebugOn) {
-                    traceOrDebug(traceOn, "Starting the request message cache lookup");
+                    traceOrDebug(traceOn, "Existing cached response has expired. Reset cache
element");
                 }
+            }
 
-                Object requestHash = digestGenerator
-                    .getDigest(((Axis2MessageContext) synCtx).getAxis2MessageContext());
-                synCtx.setProperty(CachingConstants.REQUEST_HASH_KEY, requestHash);
-
-                if (cache.containsKey(requestHash) &&
-                    cache.getResponseForKey(requestHash) instanceof CachedObject) {
-                    
-                    // get the response from the cache and attach to the context and change
the
-                    // direction of the message
-                    CachedObject cachedObj
-                        = (CachedObject) cache.getResponseForKey(requestHash);
-                    
-                    if (!cachedObj.isExpired() && cachedObj.getResponseEnvelope()
!= null) {
-
-                        if (traceOrDebugOn) {
-                            traceOrDebug(traceOn,
-                                "Cache-hit occures for the message : " + synCtx.getMessageID());
-                        }
-                        synCtx.setResponse(true);
-                        synCtx.setEnvelope(cachedObj.getResponseEnvelope());
-                        if (onCacheHit != null) {
-                            // if there is an onCacheHit use that for the mediation
-                            if (traceOrDebugOn) {
-                                traceOrDebug(traceOn, "Mediating the message using the "
+
-                                    "onCachingHit Anonymous sequence");
-                            }
-                            onCacheHit.mediate(synCtx);
-                        } else if (onCacheHitRef != null) {
-                            
-                            if (traceOrDebugOn) {
-                                traceOrDebug(traceOn, "Mediating the message using the "
+
-                                    "onCachingHit sequence : " + onCacheHitRef);
-                            }
-                            synCtx.getSequence(onCacheHitRef).mediate(synCtx);
-
-                        } else {
-                            
-                            if (traceOrDebugOn) {
-                                traceOrDebug(traceOn, "Request message "
-                                    + synCtx.getMessageID() + " has surved from the cache");
-                            }
-                            // send the response back if there is not onCacheHit is specified
-                            synCtx.setTo(null);
-                            Axis2Sender.sendBack(synCtx);
-                        }
-                    } else {
-
-                        cachedObj.clearCache();
-
-                        if (traceOrDebugOn) {
-                            traceOrDebug(
-                                traceOn, "Cached response has expired and hence cleared");
-                            traceOrDebug(traceOn, "End : Clone mediator");
-                        }
-                        return true;
-                    }
+        } else {
 
+            // if not found in cache, check if we can cache this request
+            if (cache.getCache().size() == inMemoryCacheSize) {
+                cache.removeExpiredResponses();
+                if (cache.getCache().size() == inMemoryCacheSize) {
                     if (traceOrDebugOn) {
-                        traceOrDebug(traceOn, "End : Clone mediator");
+                        traceOrDebug(traceOn, "In-memory cache is full. Unable to cache");
                     }
-                    return false;
-                    
                 } else {
-                    
-                    if (cache.getCache().size() == inMemoryCacheSize) {
-                        cache.removeExpiredResponses();
-                        if (cache.getCache().size() == inMemoryCacheSize) {
-                            if (log.isDebugEnabled()) {
-                                log.debug("In-Memory cache size exceeded and there are no
" +
-                                    "expired caches unable to store the cache");
-                            }
-
-                            // finalize tracing and debugging
-                            if (traceOrDebugOn) {
-                                traceOrDebug(traceOn, "End : Clone mediator");
-                            }
-                            
-                            return true;
-                        }
-                    }
-                    
-                    CachedObject cachedObj = new CachedObject();
-                    cachedObj.setRequestEnvelope(
-                        MessageHelper.cloneSOAPEnvelope(synCtx.getEnvelope()));
-                    cachedObj.setRequestHash(requestHash);
-                    cachedObj.setTimeout(timeout);
-                    cache.addResponseWithKey(requestHash, cachedObj);
+                    storeRequestToCache(synCtx, requestHash, cache);
                 }
+            } else {
+                storeRequestToCache(synCtx, requestHash, cache);
             }
-
-        } catch (AxisFault fault) {
-            handleException("Error occured in the caching mediator processing", fault, synCtx);
-        }
-
-        // finalize tracing and debugging
-        if (traceOrDebugOn) {
-            traceOrDebug(traceOn, "End : Clone mediator");
         }
-
         return true;
     }
 
+    /**
+     * Store request message to the cache
+     * @param synCtx the request message
+     * @param requestHash the request hash that has already been computed
+     * @param cache the cache
+     */
+    private void storeRequestToCache(MessageContext synCtx, Object requestHash, Cache cache)
{
+        CachedObject cachedObj = new CachedObject();
+        cachedObj.setRequestEnvelope(MessageHelper.cloneSOAPEnvelope(synCtx.getEnvelope()));
+        cachedObj.setRequestHash(requestHash);
+        cachedObj.setTimeout(timeout);
+        cache.addResponseWithKey(requestHash, cachedObj);
+    }
+
     public String getId() {
         return id;
     }
 
     public void setId(String id) {
         this.id = id;
+        this.cacheObjKey = CACHE_OBJ_PREFIX + id;
     }
 
     public String getScope() {
@@ -300,12 +333,12 @@
         this.timeout = timeout;
     }
 
-    public SequenceMediator getOnCacheHit() {
-        return onCacheHit;
+    public SequenceMediator getOnCacheHitSequence() {
+        return onCacheHitSequence;
     }
 
-    public void setOnCacheHit(SequenceMediator onCacheHit) {
-        this.onCacheHit = onCacheHit;
+    public void setOnCacheHitSequence(SequenceMediator onCacheHitSequence) {
+        this.onCacheHitSequence = onCacheHitSequence;
     }
 
     public String getOnCacheHitRef() {



---------------------------------------------------------------------
To unsubscribe, e-mail: synapse-dev-unsubscribe@ws.apache.org
For additional commands, e-mail: synapse-dev-help@ws.apache.org


Mime
View raw message