You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@synapse.apache.org by ru...@apache.org on 2007/10/11 13:13:16 UTC

svn commit: r583772 - in /webservices/synapse/trunk/java: ./ modules/core/ modules/core/src/main/java/org/apache/synapse/config/xml/ modules/core/src/main/java/org/apache/synapse/mediators/builtin/ modules/core/src/test/java/org/apache/synapse/mediator...

Author: ruwan
Date: Thu Oct 11 04:13:15 2007
New Revision: 583772

URL: http://svn.apache.org/viewvc?rev=583772&view=rev
Log:
Adding the CacheMediator

Added:
    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/pom.xml
    webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/MediatorFactoryFinder.java
    webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/MediatorSerializerFinder.java
    webservices/synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/mediators/eip/IterateMediatorTest.java
    webservices/synapse/trunk/java/pom.xml

Modified: webservices/synapse/trunk/java/modules/core/pom.xml
URL: http://svn.apache.org/viewvc/webservices/synapse/trunk/java/modules/core/pom.xml?rev=583772&r1=583771&r2=583772&view=diff
==============================================================================
--- webservices/synapse/trunk/java/modules/core/pom.xml (original)
+++ webservices/synapse/trunk/java/modules/core/pom.xml Thu Oct 11 04:13:15 2007
@@ -119,6 +119,18 @@
                 </executions>
             </plugin>
 
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-surefire-plugin</artifactId>
+                <version>2.3</version>
+                <configuration>
+                    <forkMode>pertest</forkMode>
+                    <!--<redirectTestOutputToFile>true</redirectTestOutputToFile>-->
+                    <!--<workingDirectory>../..</workingDirectory>-->
+                    <childDelegation>false</childDelegation>
+                </configuration>
+            </plugin>
+
         </plugins>
     </build>
     

Added: 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=583772&view=auto
==============================================================================
--- webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/CacheMediatorFactory.java (added)
+++ webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/CacheMediatorFactory.java Thu Oct 11 04:13:15 2007
@@ -0,0 +1,163 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+
+package org.apache.synapse.config.xml;
+
+import org.apache.synapse.Mediator;
+import org.apache.synapse.mediators.builtin.CacheMediator;
+import org.apache.axiom.om.OMElement;
+import org.apache.axiom.om.OMAttribute;
+import org.wso2.caching.CachingConstants;
+import org.wso2.caching.digest.DigestGenerator;
+
+import javax.xml.namespace.QName;
+import java.util.Iterator;
+
+/**
+ * Creates an instance of a Cache mediator using XML configuration specified
+ * 
+ * &lt;cache (id="string")? hashGenerator="class" scope="string" timeout="mili-seconds"&gt;
+ *  &lt;onCacheHit (sequence="key")?&gt;
+ *   (mediator)+
+ *  &lt;/onCacheHit&gt;
+ *  &lt;implementation type=(memory | disk) maxSize="int"/&gt;
+ * &lt;/cache&gt;
+ */
+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_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 long DEFAULT_TIMEOUT = 5000L;
+    private static final int DEFAULT_DISK_CACHE_SIZE = 200;
+
+    public Mediator createMediator(OMElement elem) {
+
+        if (!CACHE_Q.equals(elem.getQName())) {
+            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){
+            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())) {
+            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());
+            } else {
+                cache.setOnCacheHit(
+                    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 {
+                    handleException("unknown implementation type for the Cache mediator");
+                }
+            }
+        }
+
+
+        return cache;
+    }
+
+    private boolean isValidScope(String scope) {
+        if (CachingConstants.SCOPE_PER_HOST.equals(scope)) {
+            return true;
+        } else if (CachingConstants.SCOPE_PER_MEDIATOR.equals(scope)) {
+            return true;
+        } else if (CachingConstants.SCOPE_DISTRIBUTED.equals(scope)) {
+            handleException("Scope distributed is not supported yet by the Cache mediator");
+            return false;
+        } else {
+            handleException("Unknown scope " + scope + " for the Cache mediator");
+            return false;
+        }
+    }
+
+    public QName getTagQName() {
+        return CACHE_Q;
+    }
+}

Added: 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=583772&view=auto
==============================================================================
--- webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/CacheMediatorSerializer.java (added)
+++ webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/CacheMediatorSerializer.java Thu Oct 11 04:13:15 2007
@@ -0,0 +1,99 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+
+package org.apache.synapse.config.xml;
+
+import org.apache.axiom.om.OMElement;
+import org.apache.synapse.Mediator;
+import org.apache.synapse.mediators.builtin.CacheMediator;
+
+/**
+ * Serializes the Cache mediator to the XML configuration specified
+ *
+ * &lt;cache (id="string")? hashGenerator="class" scope="string" timeout="mili-seconds"&gt;
+ *  &lt;onCacheHit (sequence="key")?&gt;
+ *   (mediator)+
+ *  &lt;/onCacheHit&gt;
+ *  &lt;implementation type=(memory | disk) maxSize="int"/&gt;
+ * &lt;/cache&gt;
+ */
+public class CacheMediatorSerializer extends AbstractMediatorSerializer {
+
+    public OMElement serializeMediator(OMElement parent, Mediator m) {
+
+        if (!(m instanceof CacheMediator)) {
+            handleException("Unsupported mediator passed in for serialization : " + m.getType());
+        }
+        CacheMediator mediator = (CacheMediator) m;
+        OMElement cache = fac.createOMElement("cache", synNS);
+        saveTracingState(cache, mediator);
+
+        if (mediator.getId() != null) {
+            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.getOnCacheHit() != null) {
+            OMElement onCacheHit = fac.createOMElement("onCacheHit", synNS);
+            new SequenceMediatorSerializer().serializeChildren(
+                onCacheHit, mediator.getOnCacheHit().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);
+        }
+
+        return cache;
+    }
+
+    public String getMediatorClassName() {
+        return CacheMediator.class.getName();
+    }
+}

Modified: webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/MediatorFactoryFinder.java
URL: http://svn.apache.org/viewvc/webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/MediatorFactoryFinder.java?rev=583772&r1=583771&r2=583772&view=diff
==============================================================================
--- webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/MediatorFactoryFinder.java (original)
+++ webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/MediatorFactoryFinder.java Thu Oct 11 04:13:15 2007
@@ -66,7 +66,8 @@
         CloneMediatorFactory.class,
         IterateMediatorFactory.class,
         DBReportMediatorFactory.class,
-        DBLookupMediatorFactory.class
+        DBLookupMediatorFactory.class,
+        CacheMediatorFactory.class
     };
 
     private static MediatorFactoryFinder instance = null;

Modified: webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/MediatorSerializerFinder.java
URL: http://svn.apache.org/viewvc/webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/MediatorSerializerFinder.java?rev=583772&r1=583771&r2=583772&view=diff
==============================================================================
--- webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/MediatorSerializerFinder.java (original)
+++ webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/MediatorSerializerFinder.java Thu Oct 11 04:13:15 2007
@@ -54,7 +54,8 @@
         CloneMediatorSerializer.class,
         IterateMediatorSerializer.class,
         DBLookupMediatorSerializer.class,
-        DBReportMediatorSerializer.class
+        DBReportMediatorSerializer.class,
+        CacheMediatorSerializer.class
     };
 
     private static MediatorSerializerFinder instance = null;

Added: 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=583772&view=auto
==============================================================================
--- webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/builtin/CacheMediator.java (added)
+++ webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/builtin/CacheMediator.java Thu Oct 11 04:13:15 2007
@@ -0,0 +1,318 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+
+package org.apache.synapse.mediators.builtin;
+
+import org.apache.axis2.AxisFault;
+import org.apache.axis2.description.Parameter;
+import org.apache.synapse.MessageContext;
+import org.apache.synapse.core.axis2.Axis2MessageContext;
+import org.apache.synapse.core.axis2.Axis2Sender;
+import org.apache.synapse.mediators.AbstractMediator;
+import org.apache.synapse.mediators.base.SequenceMediator;
+import org.apache.synapse.util.MessageHelper;
+import org.wso2.caching.Cache;
+import org.wso2.caching.CachedObject;
+import org.wso2.caching.CachingConstants;
+import org.wso2.caching.digest.DigestGenerator;
+
+/**
+ *
+ */
+public class CacheMediator extends AbstractMediator {
+
+    private String id = null;
+    private String scope = CachingConstants.SCOPE_PER_HOST;
+    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
+    // factory will asign a default value to enable disk based caching
+    private int diskCacheSize = 0;
+    private long timeout = 0L;
+    private SequenceMediator onCacheHit = null;
+    private String onCacheHitRef = null;
+    private static final String CACHE_OBJ_PREFIX = "chache_obj_";
+
+    public boolean mediate(MessageContext synCtx) {
+
+        // tracing and debuggin related mediation initiation
+        boolean traceOn = isTraceOn(synCtx);
+        boolean traceOrDebugOn = isTraceOrDebugOn(traceOn);
+
+        if (traceOrDebugOn) {
+            traceOrDebug(traceOn, "Start : Cache mediator");
+
+            if (traceOn && trace.isTraceEnabled()) {
+                trace.trace("Message : " + synCtx);
+            }
+        }
+
+        if (synCtx.getConfiguration().getAxisConfiguration() == null) {
+            handleException("Unable to mediate the message in the cache "
+                + ": AxisConfiguration not found", synCtx);
+        }
+
+        try {
+
+            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);
+                }
+                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 {
+                if (traceOrDebugOn) {
+                    traceOrDebug(traceOn, "Creating/Recreating the cache object");
+                }
+                cache = new Cache();
+                synCtx.getConfiguration().getAxisConfiguration().addParameter(cacheObjKey, cache);
+            }
+
+            if (synCtx.isResponse()) {
+
+                if (traceOrDebugOn) {
+                    traceOrDebug(traceOn, "Starting the response message store in the cache");
+                }
+
+                Object obj;
+                if (synCtx.getProperty(CachingConstants.REQUEST_HASH_KEY) != null) {
+                    obj = cache.getResponseForKey(
+                        synCtx.getProperty(CachingConstants.REQUEST_HASH_KEY));
+                } else {
+                    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");
+                    }
+                    return true;
+                }
+                
+                if (obj != null && obj instanceof CachedObject) {
+                    
+                    if (traceOrDebugOn) {
+                        traceOrDebug(traceOn, "Storing the response for the message "
+                            + synCtx.getMessageID() + " in the cache");
+                    }
+                    
+                    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());
+                } 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");
+                    }
+                    return true;
+                }
+            } else {
+
+                if (traceOrDebugOn) {
+                    traceOrDebug(traceOn, "Starting the request message cache lookup");
+                }
+
+                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;
+                    }
+
+                    if (traceOrDebugOn) {
+                        traceOrDebug(traceOn, "End : Clone mediator");
+                    }
+                    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);
+                }
+            }
+
+        } 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;
+    }
+
+    public String getId() {
+        return id;
+    }
+
+    public void setId(String id) {
+        this.id = id;
+    }
+
+    public String getScope() {
+        return scope;
+    }
+
+    public void setScope(String scope) {
+        this.scope = scope;
+    }
+
+    public DigestGenerator getDigestGenerator() {
+        return digestGenerator;
+    }
+
+    public void setDigestGenerator(DigestGenerator digestGenerator) {
+        this.digestGenerator = digestGenerator;
+    }
+
+    public int getInMemoryCacheSize() {
+        return inMemoryCacheSize;
+    }
+
+    public void setInMemoryCacheSize(int inMemoryCacheSize) {
+        this.inMemoryCacheSize = inMemoryCacheSize;
+    }
+
+    public int getDiskCacheSize() {
+        return diskCacheSize;
+    }
+
+    public void setDiskCacheSize(int diskCacheSize) {
+        this.diskCacheSize = diskCacheSize;
+    }
+
+    public long getTimeout() {
+        return timeout;
+    }
+
+    public void setTimeout(long timeout) {
+        this.timeout = timeout;
+    }
+
+    public SequenceMediator getOnCacheHit() {
+        return onCacheHit;
+    }
+
+    public void setOnCacheHit(SequenceMediator onCacheHit) {
+        this.onCacheHit = onCacheHit;
+    }
+
+    public String getOnCacheHitRef() {
+        return onCacheHitRef;
+    }
+
+    public void setOnCacheHitRef(String onCacheHitRef) {
+        this.onCacheHitRef = onCacheHitRef;
+    }
+}

Modified: webservices/synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/mediators/eip/IterateMediatorTest.java
URL: http://svn.apache.org/viewvc/webservices/synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/mediators/eip/IterateMediatorTest.java?rev=583772&r1=583771&r2=583772&view=diff
==============================================================================
--- webservices/synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/mediators/eip/IterateMediatorTest.java (original)
+++ webservices/synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/mediators/eip/IterateMediatorTest.java Thu Oct 11 04:13:15 2007
@@ -51,7 +51,7 @@
             "expression=\"//original/itr\" xmlns=\"http://ws.apache.org/ns/synapse\">" +
             "<target soapAction=\"urn:iterate\" sequence=\"seqRef\"/></iterate>"));
         iterate.mediate(testCtx);
-        Thread.sleep(1000);
+        Thread.sleep(2000);
         MessageContext mediatedCtx = helperMediator.getMediatedContext(0);
         assertEquals(mediatedCtx.getSoapAction(), "urn:iterate");
         OMElement formerBody = mediatedCtx.getEnvelope().getBody().getFirstElement();
@@ -69,7 +69,7 @@
             "xmlns=\"http://ws.apache.org/ns/synapse\"><target soapAction=\"urn:iterate\" " +
             "sequence=\"seqRef\"/></iterate>"));
         iterate.mediate(testCtx);
-        Thread.sleep(1000);
+        Thread.sleep(2000);
         MessageContext mediatedCtx = helperMediator.getMediatedContext(0);
         assertEquals(mediatedCtx.getSoapAction(), "urn:iterate");
         OMElement formerBody = mediatedCtx.getEnvelope().getBody().getFirstElement();

Modified: webservices/synapse/trunk/java/pom.xml
URL: http://svn.apache.org/viewvc/webservices/synapse/trunk/java/pom.xml?rev=583772&r1=583771&r2=583772&view=diff
==============================================================================
--- webservices/synapse/trunk/java/pom.xml (original)
+++ webservices/synapse/trunk/java/pom.xml Thu Oct 11 04:13:15 2007
@@ -679,6 +679,22 @@
                 </exclusion>
             </exclusions>
         </dependency>
+        <dependency>
+            <groupId>org.wso2.caching</groupId>
+            <artifactId>wso2caching-core</artifactId>
+            <version>${wso2caching.version}</version>
+            <type>jar</type>
+            <exclusions>
+                <exclusion>
+                    <groupId>org.apache.axis2</groupId>
+                    <artifactId>axis2</artifactId>
+                </exclusion>
+                <exclusion>
+                    <groupId>org.apache.neethi</groupId>
+                    <artifactId>neethi</artifactId>
+                </exclusion>
+            </exclusions>
+        </dependency>
 
         <!-- dependencies for Rampart -->
         <dependency>
@@ -997,6 +1013,7 @@
 
         <!-- dependencies of Synapse extensions module -->
         <wso2commons.version>1.2</wso2commons.version>
+        <wso2caching.version>SNAPSHOT</wso2caching.version>
         <spring.version>1.2.6</spring.version>
         <xbean.version>2.2.0</xbean.version>
         <bsf.version>3.0-beta1</bsf.version>



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