[wsas-java-dev] svn commit r9371 - in branches/wsas/java/2.1/commons/throttle/src/main: java/org/wso2/throttle java/org/wso2/throttle/impl/ipbase java/org/wso2/throttle/module java/org/wso2/throttle/module/handler resources/META-INF

svn at wso2.org svn at wso2.org
Thu Nov 1 01:59:28 PDT 2007


Author: indika
Date: Thu Nov  1 01:59:16 2007
New Revision: 9371

Modified:
   branches/wsas/java/2.1/commons/throttle/src/main/java/org/wso2/throttle/Caller.java
   branches/wsas/java/2.1/commons/throttle/src/main/java/org/wso2/throttle/Throttle.java
   branches/wsas/java/2.1/commons/throttle/src/main/java/org/wso2/throttle/ThrottleConstants.java
   branches/wsas/java/2.1/commons/throttle/src/main/java/org/wso2/throttle/ThrottlePolicyProcessor.java
   branches/wsas/java/2.1/commons/throttle/src/main/java/org/wso2/throttle/impl/ipbase/IPBaseAccessController.java
   branches/wsas/java/2.1/commons/throttle/src/main/java/org/wso2/throttle/module/ThrottleModule.java
   branches/wsas/java/2.1/commons/throttle/src/main/java/org/wso2/throttle/module/handler/ThrottleHandler.java
   branches/wsas/java/2.1/commons/throttle/src/main/resources/META-INF/module.xml
Log:
add concurrency throttling and for run in cluster evn ,now runtime state 
keep in configurationcontext -- code form throttle trunk 


Modified: branches/wsas/java/2.1/commons/throttle/src/main/java/org/wso2/throttle/Caller.java
==============================================================================
--- branches/wsas/java/2.1/commons/throttle/src/main/java/org/wso2/throttle/Caller.java	(original)
+++ branches/wsas/java/2.1/commons/throttle/src/main/java/org/wso2/throttle/Caller.java	Thu Nov  1 01:59:16 2007
@@ -38,9 +38,7 @@
     private long nextTimeWindow = 0;
     /** The count to keep track number of request   */
     private int count = 0;
-    /** The Object for used to lock in synchronizing  */
-    final Object lock = new Object();
-    /** The Id of caller */
+     /** The Id of caller */
     private Object ID;
 
     public Caller(Object ID) {
@@ -65,15 +63,14 @@
      */
     public void initAccess(CallerConfiguration configurationIPBased, ThrottleContext throttleContext, long currentTime) throws ThrottleException {
         if (!(configurationIPBased.getMaximumRequestPerUnitTime() == 0)) {
-            synchronized (lock) {
-                this.firstAccessTime = currentTime;
-                if (this.nextTimeWindow != 0) {
-                    throttleContext.removeCaller(new Long(nextTimeWindow));
-                }
-                this.nextTimeWindow = currentTime + configurationIPBased.getUnitTimeInMiliSecond();
+            this.firstAccessTime = currentTime;
+            if (this.nextTimeWindow != 0) {
+                throttleContext.removeCaller(new Long(nextTimeWindow));
             }
-            throttleContext.addCaller(this);
+            this.nextTimeWindow = currentTime + configurationIPBased.getUnitTimeInMiliSecond();
         }
+        throttleContext.addCaller(this);
+
     }
 
     /**
@@ -88,48 +85,47 @@
     public boolean canAccessIfUnitTimeNotOver(CallerConfiguration configurationIPBased, ThrottleContext throttleContext, long currentTime) throws ThrottleException {
         boolean canAcess = false;
         if (!(configurationIPBased.getMaximumRequestPerUnitTime() == 0)) {
-            synchronized (lock) {
-                if (count <= configurationIPBased.getMaximumRequestPerUnitTime() - 1) {
-                    canAcess = true;
-                    count++;
-                    // can complete access
+            if (count <= configurationIPBased.getMaximumRequestPerUnitTime() - 1) {
+                canAcess = true;
+                count++;
+                // can complete access
+
+            } else {
+                //else , if caller has not already prohibit
+                if (this.nextAccessTime == 0) {
+                    //and if there is no prohibit time  period in configurationIPBased
+                    if (configurationIPBased.getProhibitTimePeriod() == 0) {
+                        //prohibit access untill unit time period is over
+                        this.nextAccessTime = this.firstAccessTime +
+                            configurationIPBased.getUnitTimeInMiliSecond();
+                    } else {
+                        //if there is a prohibit time period in configuartion ,then
+                        //set it as prohibit period
+                        this.nextAccessTime = currentTime +
+                            configurationIPBased.getProhibitTimePeriod();
+                    }
+                    log.debug("Maximum Number of requests are reached :IP-" + ID);
 
                 } else {
-                    //else , if caller has not already prohibit
-                    if (this.nextAccessTime == 0) {
-                        //and if there is no prohibit time  period in configurationIPBased
-                        if (configurationIPBased.getProhibitTimePeriod() == 0) {
-                            //prohibit access untill unit time period is over
-                            this.nextAccessTime = this.firstAccessTime +
-                                    configurationIPBased.getUnitTimeInMiliSecond();
-                        } else {
-                            //if there is a prohibit time period in configuartion ,then
-                            //set it as prohibit period
-                            this.nextAccessTime = currentTime +
-                                    configurationIPBased.getProhibitTimePeriod();
+                    // else , if the caller has already prohabit and prohabit
+                    // time period has already overed
+                    if (this.nextAccessTime
+                        <= currentTime) {
+                        this.nextAccessTime = 0;
+                        canAcess = true;
+                        count = 1;
+                        this.firstAccessTime = currentTime;
+                        if (this.nextTimeWindow != 0) {
+                            throttleContext.removeCaller(new Long(nextTimeWindow));
                         }
-                        log.debug("Maximum Number of requests are reached :IP-" + ID);
-
+                        this.nextTimeWindow = currentTime + configurationIPBased.getUnitTimeInMiliSecond();
                     } else {
-                        // else , if the caller has already prohabit and prohabit
-                        // time period has already overed
-                        if (this.nextAccessTime
-                                <= currentTime) {
-                            this.nextAccessTime = 0;
-                            canAcess = true;
-                            count = 1;
-                            this.firstAccessTime = currentTime;
-                            if (this.nextTimeWindow != 0) {
-                                throttleContext.removeCaller(new Long(nextTimeWindow));
-                            }
-                            this.nextTimeWindow = currentTime + configurationIPBased.getUnitTimeInMiliSecond();
-                        } else {
-                            log.debug("Prohibit period is not yet over :IP- " + ID);
+                        log.debug("Prohibit period is not yet over :IP- " + ID);
 
-                        }
                     }
                 }
             }
+
         }
         return canAcess;
     }
@@ -148,36 +144,35 @@
         // if number of access for a unit time is less than MAX and
         // if the unit time period (session time) has just overed
         if (!(configurationIPBased.getMaximumRequestPerUnitTime() == 0)) {
-            synchronized (lock) {
-                if (this.count <= configurationIPBased.getMaximumRequestPerUnitTime() - 1) {
+            if (this.count <= configurationIPBased.getMaximumRequestPerUnitTime() - 1) {
+                if (this.nextTimeWindow != 0) {
+                    throttleContext.removeCaller(new Long(nextTimeWindow));
+                }
+                canAcess = true; // this is bounus access
+                //next time callers can access as a new one
+            } else {
+                // if number of access for a unit time has just been greater than MAX
+                // now same as a new session
+                // OR
+                //  if caller in prohabit session  and prohabit period has just overed
+                if ((this.nextAccessTime == 0) ||
+                    (this.nextAccessTime <= currentTime)) {
+                    this.nextAccessTime = 0;
+                    canAcess = true;
+                    count = 1;// can access the system   and this is same as fristAccess
+                    this.firstAccessTime = currentTime;
                     if (this.nextTimeWindow != 0) {
                         throttleContext.removeCaller(new Long(nextTimeWindow));
                     }
-                    canAcess = true; // this is bounus access
-                    //next time callers can access as a new one
-                } else {
-                    // if number of access for a unit time has just been greater than MAX
-                    // now same as a new session
-                    // OR
-                    //  if caller in prohabit session  and prohabit period has just overed
-                    if ((this.nextAccessTime == 0) ||
-                            (this.nextAccessTime <= currentTime)) {
-                        this.nextAccessTime = 0;
-                        canAcess = true;
-                        count = 1;// can access the system   and this is same as fristAccess
-                        this.firstAccessTime = currentTime;
-                        if (this.nextTimeWindow != 0) {
-                            throttleContext.removeCaller(new Long(nextTimeWindow));
-                        }
-                        this.nextTimeWindow = currentTime + configurationIPBased.getUnitTimeInMiliSecond();
+                    this.nextTimeWindow = currentTime + configurationIPBased.getUnitTimeInMiliSecond();
 
-                    } else {
-                        // if  caller in prohabit session  and prohabit period has not  overed
-                        log.debug("Even unit time has overed , CallerIP in prohibit state :IP -" + ID);
+                } else {
+                    // if  caller in prohabit session  and prohabit period has not  overed
+                    log.debug("Even unit time has overed , CallerIP in prohibit state :IP -" + ID);
 
-                    }
                 }
             }
+
         }
         return canAcess;
 
@@ -197,24 +192,22 @@
         if (ipBasedconfiguration == null) {
             return false;
         }
-        if (ipBasedconfiguration.getMaximumRequestPerUnitTime() < 0 || ipBasedconfiguration.getUnitTimeInMiliSecond() <= 0 || ipBasedconfiguration.getProhibitTimePeriod() < 0)
-        {
+        if (ipBasedconfiguration.getMaximumRequestPerUnitTime() < 0 || ipBasedconfiguration.getUnitTimeInMiliSecond() <= 0 || ipBasedconfiguration.getProhibitTimePeriod() < 0) {
             throw new ThrottleException("Invalid Throttle Configuration");
         }
 
         if (!(ipBasedconfiguration.getMaximumRequestPerUnitTime() == 0)) {
             // if caller access first time in his new session
-            synchronized (lock) {
-                if (this.firstAccessTime == 0) {
-                    initAccess(ipBasedconfiguration, throttleContext, currentTime);
-                }
-                // if unit time period (session time) is not over
-                if (nextTimeWindow > currentTime) {
-                    canAcess = canAccessIfUnitTimeNotOver(ipBasedconfiguration, throttleContext, currentTime);
-                } else {
-                    canAcess = canAccessIfUnitTimeOver(ipBasedconfiguration, throttleContext, currentTime);
-                }
+            if (this.firstAccessTime == 0) {
+                initAccess(ipBasedconfiguration, throttleContext, currentTime);
             }
+            // if unit time period (session time) is not over
+            if (nextTimeWindow > currentTime) {
+                canAcess = canAccessIfUnitTimeNotOver(ipBasedconfiguration, throttleContext, currentTime);
+            } else {
+                canAcess = canAccessIfUnitTimeOver(ipBasedconfiguration, throttleContext, currentTime);
+            }
+
         }
         return canAcess;
 

Modified: branches/wsas/java/2.1/commons/throttle/src/main/java/org/wso2/throttle/Throttle.java
==============================================================================
--- branches/wsas/java/2.1/commons/throttle/src/main/java/org/wso2/throttle/Throttle.java	(original)
+++ branches/wsas/java/2.1/commons/throttle/src/main/java/org/wso2/throttle/Throttle.java	Thu Nov  1 01:59:16 2007
@@ -31,7 +31,8 @@
     private HashMap throttleContexts;
     /** Holder for ThrottleConfigurations     */
     private HashMap throttleConfigurations;
-
+    /** ConcurrentAccessController insatnce- this is common to all remote callers*/
+    private ConcurrentAccessController controller;
     /**
      * Default Constructor
      */
@@ -80,4 +81,12 @@
     public ThrottleConfiguration getThrottleConfiguration(String key) {
         return (ThrottleConfiguration) throttleConfigurations.get(key);
     }
+
+    public void setConcurrentAccessController(ConcurrentAccessController controller) {
+        this.controller = controller;
+    }
+
+    public ConcurrentAccessController getConcurrentAccessController() {
+        return controller;
+    }
 }

Modified: branches/wsas/java/2.1/commons/throttle/src/main/java/org/wso2/throttle/ThrottleConstants.java
==============================================================================
--- branches/wsas/java/2.1/commons/throttle/src/main/java/org/wso2/throttle/ThrottleConstants.java	(original)
+++ branches/wsas/java/2.1/commons/throttle/src/main/java/org/wso2/throttle/ThrottleConstants.java	Thu Nov  1 01:59:16 2007
@@ -19,7 +19,7 @@
 
 import javax.xml.namespace.QName;
 
-public final class ThrottleConstants {    
+public final class ThrottleConstants {
 
     public static final int IP_BASE = 0;
 
@@ -31,6 +31,8 @@
 
     public static final long DEFAULT_THROTTLE_CLEAN_PERIOD = 5 * 1000 * 60;
 
+    public static final String THROTTLES_MAP = "thottles_map";
+
     public static final String IP_BASED_THROTTLE_KEY = "Key_of_ip_based_Throttle";
 
     public static final String GLOBAL_IP_BASED_THROTTLE_KEY = "Key_of_global_ip_based_Throttle";
@@ -47,6 +49,8 @@
 
     public static final String ID_PARAMETER_NAME = "ID";
 
+    public static final String MAXIMUM_CONCURRENT_ACCESS_PARAMETER_NAME = "MaximumConcurrentAccess";
+
     public static final String THROTTLE_NS = "http://www.wso2.org/products/wso2commons/throttle";
 
     public static final String THROTTLE_NS_PREFIX = "throttle";

Modified: branches/wsas/java/2.1/commons/throttle/src/main/java/org/wso2/throttle/ThrottlePolicyProcessor.java
==============================================================================
--- branches/wsas/java/2.1/commons/throttle/src/main/java/org/wso2/throttle/ThrottlePolicyProcessor.java	(original)
+++ branches/wsas/java/2.1/commons/throttle/src/main/java/org/wso2/throttle/ThrottlePolicyProcessor.java	Thu Nov  1 01:59:16 2007
@@ -79,8 +79,8 @@
 
                     for (Iterator assertionsIterator = assertionList.iterator(); assertionsIterator.hasNext();)
                     {
-                        Object allObject = assertionsIterator.next();
-                        if (allObject instanceof All) {
+                        Object topLevelPolicy = assertionsIterator.next();
+                        if (topLevelPolicy instanceof All) {
 
                             // boolean isOtherConfiguration = false;
                             //  // To track default configuration for all ips
@@ -89,7 +89,7 @@
                             boolean isIPRangeFound = false;
                             boolean isExactlyOneFound = false;
                             ExactlyOne controleParmeter = null;
-                            List configDataAssertionList = ((All) allObject).getAssertions();
+                            List configDataAssertionList = ((All) topLevelPolicy).getAssertions();
                             if (configDataAssertionList != null) {
                                 for (Iterator configDataAssertionIterator =
                                     configDataAssertionList.iterator(); configDataAssertionIterator.hasNext();)
@@ -241,15 +241,36 @@
                                 }
 
                             } else {
-                               if(log.isDebugEnabled()){
-                                   log.debug("Couldn't find a configuration for a throttle configuration for an one caller  ");
-                               }
+                                if (log.isDebugEnabled()) {
+                                    log.debug("Couldn't find a configuration for a throttle configuration for an one caller  ");
+                                }
                             }
                             if (isIPRangeFound && isExactlyOneFound) {// If the Throttle Configuration is valid
                                 throttleConfiguration.addCallerConfiguration(configuration);
                             } else {
                                 handleException("ID and one of Valid Control policy component are Mandatory in Throttle Policy");
                             }
+                        } else if (topLevelPolicy instanceof XmlPrimtiveAssertion) {
+
+                            XmlPrimtiveAssertion maxConAccess = (XmlPrimtiveAssertion) topLevelPolicy;
+                            OMElement element = maxConAccess.getValue();
+                            // Name of the policy assertion
+                            String name = element.getLocalName();
+                            //Value of the policy assertion
+                            String value = element.getText();
+
+                            //if Value and Name name are null,then it is a invalid policy configuration
+                            if (name == null || value == null) {
+                                handleException("Either Value or Name of the policy cannot be null");
+                            } else
+                            if (name.equals(ThrottleConstants.MAXIMUM_CONCURRENT_ACCESS_PARAMETER_NAME)) {
+                                int intvalue = Integer.parseInt(value.trim());
+                                if (intvalue > 0) {
+                                    throttle.setConcurrentAccessController(new ConcurrentAccessController(intvalue));
+                                }
+                            } else {
+                                handleException("Invalied Throttle Policy configuration");
+                            }
                         }
                     }
                 }
@@ -265,7 +286,7 @@
      * @throws ThrottleException
      */
     private static void handleException(String messeage) throws ThrottleException {
-        String msg = "Error was ocuured during throttle policy processing  "+messeage;
+        String msg = "Error was ocuured during throttle policy processing  " + messeage;
         log.error(msg);
         throw new ThrottleException(msg);
     }

Modified: branches/wsas/java/2.1/commons/throttle/src/main/java/org/wso2/throttle/impl/ipbase/IPBaseAccessController.java
==============================================================================
--- branches/wsas/java/2.1/commons/throttle/src/main/java/org/wso2/throttle/impl/ipbase/IPBaseAccessController.java	(original)
+++ branches/wsas/java/2.1/commons/throttle/src/main/java/org/wso2/throttle/impl/ipbase/IPBaseAccessController.java	Thu Nov  1 01:59:16 2007
@@ -36,41 +36,54 @@
     private static Log log = LogFactory.getLog(IPBaseAccessController.class.getName());
     private static final String ACCESS_DENIED =
         "You cannot access this service since you have exceeded the allocated quota.";
+    /**
+     * The Object for used to lock in synchronizing
+     */
+    final Object lock = new Object();
 
     /**
-     * To check wheather caller can access
+     * To check wheather caller can access not not
      *
      * @param throttleContext - current states of throttle - RunTime Data
-     * @param callerID
-     * @return boolean
+     * @param callerID - Identifer for remote caller
+     * @return boolean - true if current remote user can continue access
      * @throws ThrottleException
      */
     public boolean canAccess(ThrottleContext throttleContext,
                              Object callerID) throws ThrottleException {
 
+        boolean debugOn = log.isDebugEnabled();  //is debug enable
+
         ThrottleConfiguration throttleConfigurationBean = throttleContext.getThrottleConfiguration();
+
+        if (throttleConfigurationBean == null) {
+            if (debugOn) {
+                log.debug("Thorttle Configuration couldn't find - Throttling will not occur");
+            }
+            return true;
+        }
         //Meta-data about caller
         String remoteIP = null;
         if (callerID instanceof String) {
             remoteIP = (String) callerID;
         } else {
-            if (log.isDebugEnabled()) {
+            if (debugOn) {
                 log.debug("Caller ID should instance of String for IPBASE Throttle");
             }
         }
         if (remoteIP == null) {
-            if (log.isWarnEnabled()) {
-                log.warn("Caller IP not found!");
+            if (debugOn) {
+                log.debug("Caller IP not found!");
             }
             return false;
         }
         CallerConfiguration configuration =
             throttleContext.getThrottleConfiguration().getCallerConfiguration(remoteIP);
         if (configuration == null) {
-            if (log.isWarnEnabled()) {
-                log.warn("Throttle configuration not be found for IP address " + remoteIP + " so will not occur throttling");
+            if (debugOn) {
+                log.debug("Caller configuration couldn't find for IP address " + remoteIP);
             }
-            return true;
+            return false;
         }
         if (configuration.getAccessState() == ThrottleConstants.ACCESS_DENIED) {
             log.info(ACCESS_DENIED);
@@ -78,30 +91,37 @@
         } else if (configuration.getAccessState() == ThrottleConstants.ACCESS_ALLOWED) {
             return true;
         } else if (configuration.getAccessState() == ThrottleConstants.ACCESS_CONTROLLED) {
-            Caller caller = throttleContext.getCaller(remoteIP);
-            if (caller == null) {
-                //if caller has not already registered ,then create new caller description and
-                //set it in throttle
-                Object correctedEPR =
-                    throttleConfigurationBean.getConfigurationKeyOfCaller(remoteIP);
-                if (correctedEPR != null) {
-                    caller = CallerFactory.createCaller(ThrottleConstants.IP_BASE, correctedEPR);
+            synchronized (lock) {
+                Caller caller = throttleContext.getCaller(remoteIP);
+                if (caller == null) {
+                    //if caller has not already registered ,then create new caller description and
+                    //set it in throttle
+                    Object correctedEPR =
+                        throttleConfigurationBean.getConfigurationKeyOfCaller(remoteIP);
+                    if (correctedEPR != null) {
+                        caller = CallerFactory.createCaller(ThrottleConstants.IP_BASE, correctedEPR);
+                    }
                 }
-            }
-            if (caller != null) {
-                long currentTime = System.currentTimeMillis();
-                if (!caller.canAccess(throttleContext, currentTime)) {
-                    //if current caller cannot access , then perform cleaning
-                    log.info(ACCESS_DENIED);
-                    throttleContext.processCleanList(currentTime);
-                    return false;
+                if (caller != null) {
+                    long currentTime = System.currentTimeMillis();
+
+                    if (!caller.canAccess(throttleContext, currentTime)) {
+                        //if current caller cannot access , then perform cleaning
+                        log.info(ACCESS_DENIED);
+                        throttleContext.processCleanList(currentTime);
+                        return false;
+                    } else {
+                        if (debugOn) {
+                            log.debug("Access  from IP address " + remoteIP + "is successful.");
+                        }
+                        return true;
+                    }
                 } else {
-                    log.debug("Access  from IP address " + remoteIP + "is successful.");
-                    return true;
+                    if (debugOn) {
+                        log.debug("Caller IP not found! " + remoteIP);
+                    }
+                    return false;
                 }
-            } else {
-                log.warn("Caller IP not found! " + remoteIP);
-                return false;
             }
         }
         return true;

Modified: branches/wsas/java/2.1/commons/throttle/src/main/java/org/wso2/throttle/module/ThrottleModule.java
==============================================================================
--- branches/wsas/java/2.1/commons/throttle/src/main/java/org/wso2/throttle/module/ThrottleModule.java	(original)
+++ branches/wsas/java/2.1/commons/throttle/src/main/java/org/wso2/throttle/module/ThrottleModule.java	Thu Nov  1 01:59:16 2007
@@ -33,6 +33,8 @@
 import org.wso2.throttle.ThrottlePolicyProcessor;
 
 import java.io.InputStream;
+import java.util.HashMap;
+import java.util.Map;
 
 public class ThrottleModule implements Module {
 
@@ -45,10 +47,13 @@
 
     private Policy defaultPolicy = null;
     private Throttle defaultThrottle = null;
+    private ConfigurationContext configctx;
+
     /**
      * initialize the module
      */
     public void init(ConfigurationContext configContext, AxisModule module) throws AxisFault {
+        this.configctx = configContext;
         initDefaultPolicy();
         initDefaultThrottle();
         Throttle throttle;
@@ -68,14 +73,16 @@
                     String id = policy.getId();
                     policyInclude.removePolicyElement(id);
                     defaultPolicy.setId(id);
-                    policyInclude.addPolicyElement(PolicyInclude.AXIS_MODULE_POLICY,defaultPolicy);
+                    policyInclude.addPolicyElement(PolicyInclude.AXIS_MODULE_POLICY, defaultPolicy);
                     throttle = defaultThrottle;
                 }
                 if (throttle != null) {
-                    Parameter globalParameter = new Parameter();
-                    globalParameter.setName(ThrottleConstants.GLOBAL_IP_BASED_THROTTLE_KEY);
-                    globalParameter.setValue(throttle);
-                    configContext.getAxisConfiguration().addParameter(globalParameter);
+                    Map throttles = (Map) configctx.getProperty(ThrottleConstants.THROTTLES_MAP);
+                    if (throttles == null) {
+                        throttles = new HashMap();
+                        configctx.setProperty(ThrottleConstants.THROTTLES_MAP, throttles);
+                    }
+                    throttles.put(ThrottleConstants.GLOBAL_IP_BASED_THROTTLE_KEY, throttle);
                 }
             }
         }
@@ -113,10 +120,12 @@
                     throttle = defaultThrottle;
                 }
                 if (throttle != null) {
-                    Parameter service_throttle_Parameter = new Parameter();
-                    service_throttle_Parameter.setName(currentService.getName());
-                    service_throttle_Parameter.setValue(throttle);
-                    axisDescription.getAxisConfiguration().addParameter(service_throttle_Parameter);
+                    Map throttles = (Map) configctx.getProperty(ThrottleConstants.THROTTLES_MAP);
+                    if (throttles == null) {
+                        throttles = new HashMap();
+                        configctx.setProperty(ThrottleConstants.THROTTLES_MAP, throttles);
+                    }
+                    throttles.put(currentService.getName(), throttle);
                 }
             }
         } else if (axisDescription instanceof AxisOperation) {
@@ -149,12 +158,13 @@
                 }
 
                 if (throttle != null) {
-                    Parameter operation_throttle_Parameter = new Parameter();
-                    operation_throttle_Parameter.setName(currentServiceName + currentOperation.getName());
-                    operation_throttle_Parameter.setValue(throttle);
-                    axisDescription.getAxisConfiguration().addParameter(operation_throttle_Parameter);
+                    Map throttles = (Map) configctx.getProperty(ThrottleConstants.THROTTLES_MAP);
+                    if (throttles == null) {
+                        throttles = new HashMap();
+                        configctx.setProperty(ThrottleConstants.THROTTLES_MAP, throttles);
+                    }
+                    throttles.put(currentServiceName + currentOperation.getName(), throttle);
                 }
-
             }
         }
     }
@@ -184,7 +194,6 @@
         InputStream inputStream = this.getClass().getResourceAsStream("/resources/policy/default_module_policy.xml");
         if (inputStream != null) {
             try {
-
                 defaultThrottle = ThrottlePolicyProcessor.processPolicy(defaultPolicy);
             } catch (ThrottleException e) {
                 String msg = "Error during processing default throttle policy + system will not works" + e.getMessage();

Modified: branches/wsas/java/2.1/commons/throttle/src/main/java/org/wso2/throttle/module/handler/ThrottleHandler.java
==============================================================================
--- branches/wsas/java/2.1/commons/throttle/src/main/java/org/wso2/throttle/module/handler/ThrottleHandler.java	(original)
+++ branches/wsas/java/2.1/commons/throttle/src/main/java/org/wso2/throttle/module/handler/ThrottleHandler.java	Thu Nov  1 01:59:16 2007
@@ -24,13 +24,14 @@
 import org.apache.axis2.context.MessageContext;
 import org.apache.axis2.description.AxisOperation;
 import org.apache.axis2.description.AxisService;
-import org.apache.axis2.description.Parameter;
 import org.apache.axis2.handlers.AbstractHandler;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.wso2.throttle.*;
 import org.wso2.throttle.factory.AccessControllerFactory;
 
+import java.util.Map;
+
 
 public abstract class ThrottleHandler extends AbstractHandler {
 
@@ -58,35 +59,47 @@
         Throttle throttle = null;
         ConfigurationContext configContext = messageContext.getConfigurationContext();
         //the Parameter which hold throttle ipbase object
-        Parameter throttleObjectParameter;
-        // to get parameter from configuration context
+        // to get thottles map from the configuration context
+
+        Map throttles = (Map) configContext.getProperty(ThrottleConstants.THROTTLES_MAP);
+        if (throttles == null) {
+            if (log.isDebugEnabled()) {
+                log.debug("Couldn't find thottles object map .. thottlling will not be occurred ");
+            }
+            return null;
+        }
         switch (throttleType) {
             case ThrottleConstants.GLOBAL_THROTTLE: {
-                throttleObjectParameter =
-                    configContext.getAxisConfiguration().
-                        getParameter(ThrottleConstants.GLOBAL_IP_BASED_THROTTLE_KEY);
+                throttle =
+                    (Throttle) throttles.get(ThrottleConstants.GLOBAL_IP_BASED_THROTTLE_KEY);
                 break;
             }
             case ThrottleConstants.OPERATION_BASED_THROTTLE: {
                 AxisOperation axisOperation = messageContext.getAxisOperation();
                 if (axisOperation != null) {
                     String currentServiceName = ((AxisService) axisOperation.getParent()).getName();
-                    throttleObjectParameter =
-                        configContext.getAxisConfiguration().
-                            getParameter(currentServiceName +
+                    throttle =
+                        (Throttle) throttles.
+                            get(currentServiceName +
                                 axisOperation.getName().getLocalPart());
                 } else {
-                    throw new AxisFault("Axis operation cannot be null");
+                    if (log.isDebugEnabled()) {
+                        log.debug("Couldn't find axis operation ");
+                    }
+                    return null;
                 }
                 break;
             }
             case ThrottleConstants.SERVICE_BASED_THROTTLE: {
                 AxisService axisService = messageContext.getAxisService();
                 if (axisService != null) {
-                    throttleObjectParameter =
-                        configContext.getAxisConfiguration().getParameter(axisService.getName());
+                    throttle =
+                        (Throttle) throttles.get(axisService.getName());
                 } else {
-                    throw new AxisFault("Axis service cannot be null");
+                    if (log.isDebugEnabled()) {
+                        log.debug("Couldn't find axis service ");
+                    }
+                    return null;
                 }
                 break;
             }
@@ -94,15 +107,6 @@
                 throw new ThrottleException("Unsupported Throttle type");
             }
         }
-        //if there is a throttlebean
-        if (throttleObjectParameter != null) {
-            Object throttleObject = throttleObjectParameter.getValue();
-            if (throttleObject instanceof Throttle) {
-                throttle = (Throttle) throttleObject;
-            } else {
-                throw new ThrottleException("Incompatible object for IPBaseThrottleConfiguration");
-            }
-        }
         return throttle;
     }
 
@@ -116,21 +120,63 @@
     public void process(Throttle throttle,
                         MessageContext messageContext) throws ThrottleException, AxisFault {
 
-        Object remoteIP = messageContext.getProperty(MessageContext.REMOTE_ADDR);
-        if (remoteIP == null) {
-            throw new AxisFault("Could not find the IP address of the caller - Currently only support caller-IP base access control");
+        boolean canAccess = doConcurrentThrottling(throttle, messageContext);
+        if (canAccess) { // if the access is success then
+
+            if (log.isDebugEnabled()) {
+                log.debug("Access success from concurrent throttlling");
+            }
+
+            if (messageContext.getFLOW() == MessageContext.IN_FLOW) {
+                // do the normal throttling
+                Object remoteIP = messageContext.getProperty(MessageContext.REMOTE_ADDR);
+                if (remoteIP == null) {
+                    throw new AxisFault("Could not find the IP address of the caller - Currently only support caller-IP base access control");
+                } else {
+                    ThrottleContext throttleContext =
+                        throttle.getThrottleContext(ThrottleConstants.IP_BASED_THROTTLE_KEY);
+                    if (throttleContext == null) {
+                        throw new AxisFault("Could not find the Throttle Context for IPBased Thottling");
+                    }
+                    AccessController accessController = AccessControllerFactory.createAccessControler(ThrottleConstants.IP_BASE);
+                    if (!accessController.canAccess(throttleContext, remoteIP)) {
+                        throw new AxisFault("You cannot access this service since you have" +
+                            " exceeded the allocated quota.");
+                    }
+                }
+            }
+
         } else {
-            ThrottleContext throttleContext =
-                throttle.getThrottleContext(ThrottleConstants.IP_BASED_THROTTLE_KEY);
-            if (throttleContext == null) {
-                throw new AxisFault("Could not find the Throttle Context for IPBased Thottling");
-            }
-            AccessController accessController = AccessControllerFactory.createAccessControler(ThrottleConstants.IP_BASE);
-            if (!accessController.canAccess(throttleContext, remoteIP)) {
-                throw new AxisFault("You cannot access this service since you have" +
-                    " exceeded the allocated quota.");
+            if (log.isDebugEnabled()) {
+                log.debug("Access deny from concurrent throttlling");
+            }
+        }
+    }
+
+    private boolean doConcurrentThrottling(Throttle throttle, MessageContext messageContext) {
+
+        ConcurrentAccessController concurrentAccessController = throttle.getConcurrentAccessController();
+        boolean canAccess = true;
+
+        if (concurrentAccessController != null) {
+            if (messageContext.getFLOW() == MessageContext.IN_FLOW) {
+                if (log.isDebugEnabled()) {
+                    log.debug("Incoming message process through the ConcurrentThrottlling");
+                }
+                canAccess = concurrentAccessController.getAndDecrement() > 0;
+                if (log.isDebugEnabled()) {
+                    if (!canAccess) {
+                        log.debug("Access has currently been denied since allowed maximum concurrent access has exceeded");
+                    }
+                }
+            } else if (messageContext.getFLOW() == MessageContext.OUT_FLOW) {
+                if (log.isDebugEnabled()) {
+                    log.debug("Outcoming message process through the ConcurrentThrottlling");
+                }
+                concurrentAccessController.incrementAndGet();
             }
         }
+        return canAccess;
     }
 
     public InvocationResponse invoke(MessageContext msgContext) throws AxisFault {
@@ -140,7 +186,9 @@
             if (throttle != null) {
                 process(throttle, msgContext);
             } else {
-                log.warn("Cannot find throttle configuration.");
+                if (log.isDebugEnabled()) {
+                    log.debug("Cannot find throttle configuration. Thottlling will not be occurred");
+                }
             }
         }
         catch (ThrottleException e) {

Modified: branches/wsas/java/2.1/commons/throttle/src/main/resources/META-INF/module.xml
==============================================================================
--- branches/wsas/java/2.1/commons/throttle/src/main/resources/META-INF/module.xml	(original)
+++ branches/wsas/java/2.1/commons/throttle/src/main/resources/META-INF/module.xml	Thu Nov  1 01:59:16 2007
@@ -17,15 +17,30 @@
             <order phase="OperationInPhase"/>
         </handler>
     </InFlow>
+    <OutFlow>
+        <handler name="OperationLevelThrottleHandler"
+                 class="org.wso2.throttle.module.handler.OperationLevelThrottleHandler">
+            <order phase="MessageOut"/>
+        </handler>
+        <handler name="ServiceLevelThrottleHandler"
+                 class="org.wso2.throttle.module.handler.ServiceLevelThrottleHandler">
+            <order phase="MessageOut"/>
+        </handler>
+        <handler name="GlobalThrottleHandler"
+                 class="org.wso2.throttle.module.handler.GlobalThrottleHandler">
+            <order phase="MessageOut"/>
+        </handler>
+    </OutFlow>
+
     <wsp:Policy xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy"
-                xmlns:throttle="http://www.wso2.org/products/wso2commons/throttle">
-        <throttle:ThrottleAssertion>
-            <wsp:All>
-                <throttle:ID throttle:type="IP">Other</throttle:ID>
-                <wsp:ExactlyOne>                    
-                    <throttle:IsAllow>true</throttle:IsAllow>
-                </wsp:ExactlyOne>
-            </wsp:All>
-        </throttle:ThrottleAssertion>
+                    xmlns:throttle="http://www.wso2.org/products/wso2commons/throttle">
+            <throttle:ThrottleAssertion>
+                <wsp:All>
+                    <throttle:ID throttle:type="IP">Other</throttle:ID>
+                    <wsp:ExactlyOne>
+                        <throttle:IsAllow>true</throttle:IsAllow>
+                    </wsp:ExactlyOne>
+                </wsp:All>
+            </throttle:ThrottleAssertion>
     </wsp:Policy>
 </module>




More information about the Wsas-java-dev mailing list