[wsas-java-dev] svn commit r9736 - in
branches/wsas/java/2.1/commons/throttle: . modules
modules/core modules/core/src modules/core/src/main
modules/core/src/main/java modules/core/src/main/java/org
modules/core/src/main/java/org/wso2
modules/core/src/main/java/org/wso2/throttle
modules/core/src/main/java/org/wso2/throttle/factory
modules/core/src/main/java/org/wso2/throttle/impl
modules/core/src/main/java/org/wso2/throttle/impl/domainbase
modules/core/src/main/java/org/wso2/throttle/impl/ipbase
modules/mar modules/mar/src modules/mar/src/main
modules/mar/src/main/java modules/mar/src/main/java/org
modules/mar/src/main/java/org/wso2
modules/mar/src/main/java/org/wso2/throttle
modules/mar/src/main/java/org/wso2/throttle/module
modules/mar/src/main/java/org/wso2/throttle/module/handler
modules/mar/src/main/resources
modules/mar/src/main/resources/META-INF
modules/mar/src/main/resources/resources
modules/mar/src/main/resources/resources/policy src
svn at wso2.org
svn at wso2.org
Tue Nov 13 05:16:33 PST 2007
Author: indika
Date: Tue Nov 13 05:16:10 2007
New Revision: 9736
Added:
branches/wsas/java/2.1/commons/throttle/modules/
branches/wsas/java/2.1/commons/throttle/modules/core/
branches/wsas/java/2.1/commons/throttle/modules/core/pom.xml
branches/wsas/java/2.1/commons/throttle/modules/core/src/
branches/wsas/java/2.1/commons/throttle/modules/core/src/main/
branches/wsas/java/2.1/commons/throttle/modules/core/src/main/java/
branches/wsas/java/2.1/commons/throttle/modules/core/src/main/java/org/
branches/wsas/java/2.1/commons/throttle/modules/core/src/main/java/org/wso2/
branches/wsas/java/2.1/commons/throttle/modules/core/src/main/java/org/wso2/throttle/
branches/wsas/java/2.1/commons/throttle/modules/core/src/main/java/org/wso2/throttle/AccessController.java
branches/wsas/java/2.1/commons/throttle/modules/core/src/main/java/org/wso2/throttle/Caller.java
branches/wsas/java/2.1/commons/throttle/modules/core/src/main/java/org/wso2/throttle/CallerConfiguration.java
branches/wsas/java/2.1/commons/throttle/modules/core/src/main/java/org/wso2/throttle/ConcurrentAccessController.java
branches/wsas/java/2.1/commons/throttle/modules/core/src/main/java/org/wso2/throttle/Throttle.java
branches/wsas/java/2.1/commons/throttle/modules/core/src/main/java/org/wso2/throttle/ThrottleConfiguration.java
branches/wsas/java/2.1/commons/throttle/modules/core/src/main/java/org/wso2/throttle/ThrottleConstants.java
branches/wsas/java/2.1/commons/throttle/modules/core/src/main/java/org/wso2/throttle/ThrottleContext.java
branches/wsas/java/2.1/commons/throttle/modules/core/src/main/java/org/wso2/throttle/ThrottleException.java
branches/wsas/java/2.1/commons/throttle/modules/core/src/main/java/org/wso2/throttle/ThrottlePolicyProcessor.java
branches/wsas/java/2.1/commons/throttle/modules/core/src/main/java/org/wso2/throttle/factory/
branches/wsas/java/2.1/commons/throttle/modules/core/src/main/java/org/wso2/throttle/factory/CallerConfigurationFactory.java
branches/wsas/java/2.1/commons/throttle/modules/core/src/main/java/org/wso2/throttle/factory/CallerFactory.java
branches/wsas/java/2.1/commons/throttle/modules/core/src/main/java/org/wso2/throttle/factory/ThrottleConfigurationFactory.java
branches/wsas/java/2.1/commons/throttle/modules/core/src/main/java/org/wso2/throttle/factory/ThrottleContextFactory.java
branches/wsas/java/2.1/commons/throttle/modules/core/src/main/java/org/wso2/throttle/impl/
branches/wsas/java/2.1/commons/throttle/modules/core/src/main/java/org/wso2/throttle/impl/domainbase/
branches/wsas/java/2.1/commons/throttle/modules/core/src/main/java/org/wso2/throttle/impl/domainbase/DomainBaseCaller.java
branches/wsas/java/2.1/commons/throttle/modules/core/src/main/java/org/wso2/throttle/impl/domainbase/DomainBaseCallerConfiguration.java
branches/wsas/java/2.1/commons/throttle/modules/core/src/main/java/org/wso2/throttle/impl/domainbase/DomainBaseThrottleConfiguration.java
branches/wsas/java/2.1/commons/throttle/modules/core/src/main/java/org/wso2/throttle/impl/domainbase/DomainBaseThrottleContext.java
branches/wsas/java/2.1/commons/throttle/modules/core/src/main/java/org/wso2/throttle/impl/ipbase/
branches/wsas/java/2.1/commons/throttle/modules/core/src/main/java/org/wso2/throttle/impl/ipbase/IPBaseCaller.java
branches/wsas/java/2.1/commons/throttle/modules/core/src/main/java/org/wso2/throttle/impl/ipbase/IPBaseCallerConfiguration.java
branches/wsas/java/2.1/commons/throttle/modules/core/src/main/java/org/wso2/throttle/impl/ipbase/IPBaseThrottleConfiguration.java
branches/wsas/java/2.1/commons/throttle/modules/core/src/main/java/org/wso2/throttle/impl/ipbase/IPBaseThrottleContext.java
branches/wsas/java/2.1/commons/throttle/modules/mar/
branches/wsas/java/2.1/commons/throttle/modules/mar/pom.xml
branches/wsas/java/2.1/commons/throttle/modules/mar/src/
branches/wsas/java/2.1/commons/throttle/modules/mar/src/main/
branches/wsas/java/2.1/commons/throttle/modules/mar/src/main/java/
branches/wsas/java/2.1/commons/throttle/modules/mar/src/main/java/org/
branches/wsas/java/2.1/commons/throttle/modules/mar/src/main/java/org/wso2/
branches/wsas/java/2.1/commons/throttle/modules/mar/src/main/java/org/wso2/throttle/
branches/wsas/java/2.1/commons/throttle/modules/mar/src/main/java/org/wso2/throttle/module/
branches/wsas/java/2.1/commons/throttle/modules/mar/src/main/java/org/wso2/throttle/module/ThrottleModule.java
branches/wsas/java/2.1/commons/throttle/modules/mar/src/main/java/org/wso2/throttle/module/handler/
branches/wsas/java/2.1/commons/throttle/modules/mar/src/main/java/org/wso2/throttle/module/handler/GlobalThrottleHandler.java
branches/wsas/java/2.1/commons/throttle/modules/mar/src/main/java/org/wso2/throttle/module/handler/OperationLevelThrottleHandler.java
branches/wsas/java/2.1/commons/throttle/modules/mar/src/main/java/org/wso2/throttle/module/handler/ServiceLevelThrottleHandler.java
branches/wsas/java/2.1/commons/throttle/modules/mar/src/main/java/org/wso2/throttle/module/handler/ThrottleHandler.java
branches/wsas/java/2.1/commons/throttle/modules/mar/src/main/resources/
branches/wsas/java/2.1/commons/throttle/modules/mar/src/main/resources/META-INF/
branches/wsas/java/2.1/commons/throttle/modules/mar/src/main/resources/META-INF/module.xml
branches/wsas/java/2.1/commons/throttle/modules/mar/src/main/resources/resources/
branches/wsas/java/2.1/commons/throttle/modules/mar/src/main/resources/resources/policy/
branches/wsas/java/2.1/commons/throttle/modules/mar/src/main/resources/resources/policy/default_module_policy.xml
Removed:
branches/wsas/java/2.1/commons/throttle/src/
Modified:
branches/wsas/java/2.1/commons/throttle/pom.xml
Log:
change the structure of the project
add domain based throttling
Added: branches/wsas/java/2.1/commons/throttle/modules/core/pom.xml
==============================================================================
--- (empty file)
+++ branches/wsas/java/2.1/commons/throttle/modules/core/pom.xml Tue Nov 13 05:16:10 2007
@@ -0,0 +1,36 @@
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+
+
+ <parent>
+ <groupId>org.wso2.wso2throttle</groupId>
+ <artifactId>wso2throttle</artifactId>
+ <version>2.1-SNAPSHOT</version>
+ </parent>
+
+ <modelVersion>4.0.0</modelVersion>
+ <artifactId>wso2throttle-core</artifactId>
+ <packaging>jar</packaging>
+ <version>2.1-SNAPSHOT</version>
+ <name>WSO2 Throttling module - core</name>
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-compiler-plugin</artifactId>
+ <version>2.0</version>
+ <configuration>
+ <source>1.4</source>
+ <target>1.4</target>
+ </configuration>
+ </plugin>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-surefire-plugin</artifactId>
+ <version>2.2</version>
+ </plugin>
+ </plugins>
+ </build>
+
+</project>
Added: branches/wsas/java/2.1/commons/throttle/modules/core/src/main/java/org/wso2/throttle/AccessController.java
==============================================================================
--- (empty file)
+++ branches/wsas/java/2.1/commons/throttle/modules/core/src/main/java/org/wso2/throttle/AccessController.java Tue Nov 13 05:16:10 2007
@@ -0,0 +1,132 @@
+/*
+* Copyright 2005,2006 WSO2, Inc. http://wso2.com
+*
+* Licensed 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.wso2.throttle;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.wso2.throttle.factory.CallerFactory;
+
+import java.io.Serializable;
+
+/**
+ *
+ *
+ */
+
+public class AccessController implements Serializable {
+
+ /**
+ * Log for this class
+ */
+ private static Log log = LogFactory.getLog(AccessController.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
+ */
+ private final transient Object lock = new Object();
+ private static final long serialVersionUID = -2628459494650665329L;
+
+ /**
+ * To check wheather caller can access not not
+ *
+ * @param throttleContext - current states of throttle - RunTime Data
+ * @param callerID - Identifer for remote caller
+ * @param callerType - the type of the caller
+ * @return boolean - true if current remote user can continue access
+ * @throws ThrottleException
+ */
+ public boolean canAccess(ThrottleContext throttleContext,
+ Object callerID, int callerType) throws ThrottleException {
+
+ boolean debugOn = log.isDebugEnabled(); //is debug enable
+ String type = ThrottleConstants.IP_BASE == callerType ? "IP address" : "Domain";
+ 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 (debugOn) {
+ log.debug("Caller ID should instance of String for Throttle");
+ }
+ }
+ if (remoteIP == null) {
+ if (debugOn) {
+ log.debug("Caller " + type + " not found!");
+ }
+ return false;
+ }
+ CallerConfiguration configuration =
+ throttleContext.getThrottleConfiguration().getCallerConfiguration(remoteIP);
+ if (configuration == null) {
+ if (debugOn) {
+ log.debug("Caller configuration couldn't find for " + type + " " + remoteIP);
+ }
+ return false;
+ }
+ if (configuration.getAccessState() == ThrottleConstants.ACCESS_DENIED) {
+ log.info(ACCESS_DENIED);
+ return false;
+ } else if (configuration.getAccessState() == ThrottleConstants.ACCESS_ALLOWED) {
+ return true;
+ } else if (configuration.getAccessState() == ThrottleConstants.ACCESS_CONTROLLED) {
+ 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(callerType, 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;
+ } else {
+ if (debugOn) {
+ log.debug("Access from " + type + " " + remoteIP + "is successful.");
+ }
+ return true;
+ }
+ } else {
+ if (debugOn) {
+ log.debug("Caller " + type + " not found! " + remoteIP);
+ }
+ return false;
+ }
+ }
+ }
+ return true;
+ }
+}
\ No newline at end of file
Added: branches/wsas/java/2.1/commons/throttle/modules/core/src/main/java/org/wso2/throttle/Caller.java
==============================================================================
--- (empty file)
+++ branches/wsas/java/2.1/commons/throttle/modules/core/src/main/java/org/wso2/throttle/Caller.java Tue Nov 13 05:16:10 2007
@@ -0,0 +1,245 @@
+/*
+* Copyright 2005,2006 WSO2, Inc. http://wso2.com
+*
+* Licensed 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.wso2.throttle;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+import java.io.Serializable;
+
+/**
+ *
+ *
+ */
+
+public abstract class Caller implements Serializable {
+
+ /**
+ * Log for this class
+ */
+ private static Log log = LogFactory.getLog(Caller.class.getName());
+ /**
+ * next access time
+ */
+ private long nextAccessTime = 0;
+ /**
+ * frist access time
+ */
+ private long firstAccessTime = 0;
+ /**
+ * The nextTimeWindow - begining of next unit time period- end of current unit time period
+ */
+ private long nextTimeWindow = 0;
+ /**
+ * The count to keep track number of request
+ */
+ private int count = 0;
+ /**
+ * The Id of caller
+ */
+ private String ID;
+ private static final long serialVersionUID = 1652165180220263492L;
+
+ public Caller(String ID) {
+ this.ID = ID;
+ }
+
+ /**
+ * @return return Id of caller
+ */
+ public String getID() {
+ return ID;
+ }
+
+ /**
+ * Init access
+ *
+ * @param configurationIPBased -The Configuration for this caller
+ * @param throttleContext -The Throttle that caller having pass
+ * @param currentTime -The system current time
+ * @throws ThrottleException
+ */
+ public void initAccess(CallerConfiguration configurationIPBased, ThrottleContext throttleContext, long currentTime) throws ThrottleException {
+ if (!(configurationIPBased.getMaximumRequestPerUnitTime() == 0)) {
+ this.firstAccessTime = currentTime;
+ if (this.nextTimeWindow != 0) {
+ throttleContext.removeCaller(new Long(nextTimeWindow));
+ }
+ this.nextTimeWindow = currentTime + configurationIPBased.getUnitTimeInMiliSecond();
+ }
+ throttleContext.addCaller(this);
+
+ }
+
+ /**
+ * To verify access if unit time has already not overred
+ *
+ * @param configurationIPBased -The Configuration for this caller
+ * @param throttleContext -The Throttle that caller having pass
+ * @param currentTime -The system current time
+ * @return boolean -The boolean value which say access will allolw or not
+ * @throws ThrottleException
+ */
+ public boolean canAccessIfUnitTimeNotOver(CallerConfiguration configurationIPBased, ThrottleContext throttleContext, long currentTime) throws ThrottleException {
+ boolean canAcess = false;
+ if (!(configurationIPBased.getMaximumRequestPerUnitTime() == 0)) {
+ 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 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);
+
+ }
+ }
+ }
+
+ }
+ return canAcess;
+ }
+
+ /**
+ * To verify access if unit time has already overred
+ *
+ * @param configurationIPBased -The Configuration for this caller
+ * @param throttleContext -The Throttle that caller having pass
+ * @param currentTime -The system current time
+ * @return boolean -The boolean value which say access will allolw or not
+ * @throws ThrottleException
+ */
+ public boolean canAccessIfUnitTimeOver(CallerConfiguration configurationIPBased, ThrottleContext throttleContext, long currentTime) throws ThrottleException {
+ boolean canAcess = false;
+ // 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)) {
+ if (this.count <= configurationIPBased.getMaximumRequestPerUnitTime() - 1) {
+ if (this.nextTimeWindow != 0) {
+ throttleContext.removeCaller(new Long(nextTimeWindow));
+ }
+ canAcess = true; // this is bounus access
+ count = 1;
+ //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();
+
+ } 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;
+
+ }
+
+ /**
+ * Check whether that caller can access or not
+ *
+ * @param throttleContext -The Configuration for this caller
+ * @return boolean -The boolean value which say access will allolw or not
+ * @throws ThrottleException
+ */
+ public boolean canAccess(ThrottleContext throttleContext, long currentTime) throws ThrottleException {
+ boolean canAcess = false;
+ ThrottleConfiguration throttleConfiguration = throttleContext.getThrottleConfiguration();
+ CallerConfiguration ipBasedconfiguration = throttleConfiguration.getCallerConfiguration(getID());
+ if (ipBasedconfiguration == null) {
+ return false;
+ }
+ 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
+ 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;
+
+ }
+
+ /**
+ * To get next time window
+ *
+ * @return long value of next time window
+ */
+ public long getNextTimeWindow() {
+ return nextTimeWindow;
+ }
+
+ /**
+ * get type of throttle that this caller belong
+ */
+ public abstract int getType();
+
+}
Added: branches/wsas/java/2.1/commons/throttle/modules/core/src/main/java/org/wso2/throttle/CallerConfiguration.java
==============================================================================
--- (empty file)
+++ branches/wsas/java/2.1/commons/throttle/modules/core/src/main/java/org/wso2/throttle/CallerConfiguration.java Tue Nov 13 05:16:10 2007
@@ -0,0 +1,170 @@
+/*
+* Copyright 2005,2006 WSO2, Inc. http://wso2.com
+*
+* Licensed 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.wso2.throttle;
+
+import java.io.Serializable;
+
+/**
+ *
+ *
+ */
+
+public abstract class CallerConfiguration implements Serializable {
+
+ /**
+ * The time window
+ */
+ private long unitTime;
+ /**
+ * The maximum number of Request that should have allowed for caller
+ */
+ private int maximumRequest;
+ /**
+ * The Time Period which access of caller should have denied if Maximum Number of Request had reached
+ */
+ private long prohibitTimePeriod;
+ /**
+ * int value that indicate that access is fully denied or allowed or controled for this IP
+ */
+ private int accessState = ThrottleConstants.ACCESS_CONTROLLED;
+ private static final long serialVersionUID = -3387080157870675918L;
+
+ /**
+ * The Default Constructor
+ */
+ protected CallerConfiguration() {
+ super();
+
+ }
+
+ /**
+ * The Constructor with all configuration data
+ *
+ * @param unitTime - long value which represents Unit Time Window
+ * @param maximumRequest - int value which represents Maximum Request
+ * @param prohibitTimePeriod - long value which represents Prohibit Time after Max request came
+ * @param ID - String value which represents ID
+ */
+ protected CallerConfiguration(long unitTime, int maximumRequest, long prohibitTimePeriod, String ID) {
+ this();
+ this.unitTime = unitTime;
+ this.maximumRequest = maximumRequest;
+ this.prohibitTimePeriod = prohibitTimePeriod;
+ setID(ID);
+ }
+
+ /**
+ * To get access state
+ *
+ * @return int value indicate access state
+ */
+ public int getAccessState() {
+ return accessState;
+ }
+
+ /**
+ * To get Maximum Request
+ *
+ * @return int value of Maximum Request Count
+ */
+ public int getMaximumRequestPerUnitTime() {
+ return maximumRequest;
+ }
+
+ /**
+ * To get UnitTime
+ *
+ * @return long value of Unit Time
+ */
+ public long getUnitTimeInMiliSecond() {
+ return unitTime;
+
+ }
+
+ /**
+ * To get prohibit time period
+ *
+ * @return long value of prohibit time period
+ */
+ public long getProhibitTimePeriod() {
+ return prohibitTimePeriod;
+ }
+
+
+ /**
+ * To set Maximum Request
+ *
+ * @param maximumRequest -int value
+ */
+ public void setMaximumRequestPerUnitTime(int maximumRequest) {
+ this.maximumRequest = maximumRequest;
+
+ }
+
+ /**
+ * To set Unit Time
+ *
+ * @param unitTime - long value
+ */
+ public void setUnitTimeInMiliSecond(long unitTime) {
+ this.unitTime = unitTime;
+ }
+
+ /**
+ * To set Prohibit Time Period
+ *
+ * @param prohibitTimePeriod -long value
+ */
+ public void setProhibitTimePeriod(long prohibitTimePeriod) {
+ this.prohibitTimePeriod = prohibitTimePeriod;
+ }
+
+ /**
+ * To set access state
+ *
+ * @param accessState
+ */
+ public void setAccessState(int accessState) {
+ this.accessState = accessState;
+ }
+
+ /**
+ * To get ID
+ *
+ * @return Object value of ID
+ */
+ public abstract String getID();
+
+
+ /**
+ * To set ID
+ *
+ * @param ID
+ */
+ public abstract void setID(String ID);
+
+ /**
+ * To get the type of the throttle
+ *
+ * @return the type of the throttle
+ */
+ public abstract int getType();
+
+
+}
Added: branches/wsas/java/2.1/commons/throttle/modules/core/src/main/java/org/wso2/throttle/ConcurrentAccessController.java
==============================================================================
--- (empty file)
+++ branches/wsas/java/2.1/commons/throttle/modules/core/src/main/java/org/wso2/throttle/ConcurrentAccessController.java Tue Nov 13 05:16:10 2007
@@ -0,0 +1,65 @@
+/*
+* 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.wso2.throttle;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+import java.io.Serializable;
+import java.util.concurrent.atomic.AtomicInteger;
+
+/**
+ *
+ */
+
+public class ConcurrentAccessController implements Serializable {
+
+ private static Log log = LogFactory.getLog(ConcurrentAccessController.class.getName());
+
+ private final int limit;
+ private final AtomicInteger counter;
+ private static final long serialVersionUID = -6857325377726757251L;
+
+ public ConcurrentAccessController(int limit) {
+ this.limit = limit;
+ counter = new AtomicInteger(limit);
+ }
+
+ public int getAndDecrement() {
+ int ret = counter.getAndDecrement();
+ if (ret <= 0) {
+ counter.incrementAndGet();
+ return 0;
+ } else {
+ return ret;
+ }
+ }
+
+ public int incrementAndGet() {
+ int ret = counter.incrementAndGet();
+ if (ret < 0) {
+ return 0;
+ }
+ return ret;
+ }
+
+ public int getLimit() {
+ return limit;
+ }
+}
Added: branches/wsas/java/2.1/commons/throttle/modules/core/src/main/java/org/wso2/throttle/Throttle.java
==============================================================================
--- (empty file)
+++ branches/wsas/java/2.1/commons/throttle/modules/core/src/main/java/org/wso2/throttle/Throttle.java Tue Nov 13 05:16:10 2007
@@ -0,0 +1,104 @@
+/*
+* Copyright 2005,2006 WSO2, Inc. http://wso2.com
+*
+* Licensed 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.wso2.throttle;
+
+import java.io.Serializable;
+import java.util.HashMap;
+
+/**
+ * The representation for Throttle-hold both runtime data and static data
+ * There is a one to one releationship between configuration and context
+ */
+
+public class Throttle implements Serializable {
+
+// private static final long serialVersionUID = 4517849288538613640L;
+
+ /**
+ * Holder for ThrottleContext
+ */
+ private HashMap throttleContexts;
+ /**
+ * Holder for ThrottleConfigurations
+ */
+ private HashMap throttleConfigurations;
+ /**
+ * ConcurrentAccessController insatnce- this is common to all remote callers
+ */
+ private ConcurrentAccessController controller;
+
+ private static final long serialVersionUID = 1937854014424827523L;
+
+ /**
+ * Default Constructor
+ */
+ public Throttle() {
+ throttleContexts = new HashMap();
+ throttleConfigurations = new HashMap();
+ }
+
+ /**
+ * To add a configuration
+ *
+ * @param key
+ * @param throttleConfiguration
+ */
+ public void addThrottleConfiguration(String key, ThrottleConfiguration throttleConfiguration) {
+ this.throttleConfigurations.put(key, throttleConfiguration);
+ }
+
+ /**
+ * To add a context
+ *
+ * @param key
+ * @param throttleContext
+ */
+ public void addThrottleContext(String key, ThrottleContext throttleContext) {
+ this.throttleContexts.put(key, throttleContext);
+ }
+
+ /**
+ * To get a ThrotleContext
+ *
+ * @param contextID
+ * @return ThrottleContext
+ */
+
+ public ThrottleContext getThrottleContext(String contextID) {
+ return (ThrottleContext) throttleContexts.get(contextID);
+ }
+
+ /**
+ * To get a ThrottleConfiguration
+ *
+ * @param key
+ * @return ThrottleConfiguration
+ */
+ public ThrottleConfiguration getThrottleConfiguration(String key) {
+ return (ThrottleConfiguration) throttleConfigurations.get(key);
+ }
+
+ public void setConcurrentAccessController(ConcurrentAccessController controller) {
+ this.controller = controller;
+ }
+
+ public ConcurrentAccessController getConcurrentAccessController() {
+ return controller;
+ }
+}
Added: branches/wsas/java/2.1/commons/throttle/modules/core/src/main/java/org/wso2/throttle/ThrottleConfiguration.java
==============================================================================
--- (empty file)
+++ branches/wsas/java/2.1/commons/throttle/modules/core/src/main/java/org/wso2/throttle/ThrottleConfiguration.java Tue Nov 13 05:16:10 2007
@@ -0,0 +1,59 @@
+/*
+* Copyright 2005,2006 WSO2, Inc. http://wso2.com
+*
+* Licensed 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.wso2.throttle;
+
+/**
+ *
+ *
+ */
+
+public interface ThrottleConfiguration {
+
+ /**
+ * To add a CallerConfiguration
+ *
+ * @param callerConfiguration
+ */
+ public void addCallerConfiguration(CallerConfiguration callerConfiguration);
+
+ /**
+ * To get a CallerConfiguration
+ *
+ * @param ID
+ * @return CallerConfiguration
+ */
+ public CallerConfiguration getCallerConfiguration(Object ID);
+
+ /**
+ * To get a access key for caller (In the case of group ID)
+ *
+ * @param callerID
+ * @return Object
+ */
+
+ public Object getConfigurationKeyOfCaller(Object callerID);
+
+ /**
+ * To get the type of the throttle
+ *
+ * @return the type of the throttle
+ */
+ public int getType();
+
+}
Added: branches/wsas/java/2.1/commons/throttle/modules/core/src/main/java/org/wso2/throttle/ThrottleConstants.java
==============================================================================
--- (empty file)
+++ branches/wsas/java/2.1/commons/throttle/modules/core/src/main/java/org/wso2/throttle/ThrottleConstants.java Tue Nov 13 05:16:10 2007
@@ -0,0 +1,82 @@
+/*
+* Copyright 2005,2006 WSO2, Inc. http://www.wso2.org
+*
+* Licensed 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.wso2.throttle;
+
+import javax.xml.namespace.QName;
+
+public final class ThrottleConstants {
+
+ public static final int IP_BASE = 0;
+
+ public static final int DOMAIN_BASE = 1;
+
+ public static final int GLOBAL_THROTTLE = 0;
+
+ public static final int SERVICE_BASED_THROTTLE = 1;
+
+ public static final int OPERATION_BASED_THROTTLE = 2;
+
+ 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 DOMAIN_BASED_THROTTLE_KEY = "Key_of_domain_based_Throttle";
+
+ public static final String GLOBAL_THROTTLE_KEY = "Key_of_global_Throttle";
+
+ public static final String DEFAULT_THROTTLE_CONFIGURATION_KEY = "Default_throttle_configuartion_Key";
+
+ public static final String DEFAULT_THROTTLE_CONTXET_ID = "ThrottleContextID";
+
+ public static final String UNIT_TIME_PARAMETER_NAME = "UnitTime";
+
+ public static final String MAXIMUM_COUNT_PARAMETER_NAME = "MaximumCount";
+
+ public static final String PROHIBIT_TIME_PERIOD_PARAMETER_NAME = "ProhibitTimePeriod";
+
+ 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";
+
+ public static final QName THROTTLE_ASSERTION_QNAME = new QName(THROTTLE_NS, "ThrottleAssertion", THROTTLE_NS_PREFIX);
+
+ public static final QName THROTTLE_TYPE_ATTRIBUTE_QNAME = new QName(THROTTLE_NS, "type", THROTTLE_NS_PREFIX);
+
+ public static final String LOCALNAME_THROTTLECONFIGURATIONS = "ThrottleConfigurations";
+
+ public static final String LOCALNAME_THROTTLECONFIGURATION = "IPBaseThrottleConfiguration";
+
+ public static final String KEY_OF_DEFAULT_CONFIGURATION_FOR_OTHER = "Other";
+
+ public static final String ISALLOW_PARAMETER_NAME = "IsAllow";
+
+ public static final int ACCESS_CONTROLLED = 0;
+
+ public static final int ACCESS_DENIED = 1;
+
+ public static final int ACCESS_ALLOWED = 2;
+
+ private ThrottleConstants() {
+ }
+}
Added: branches/wsas/java/2.1/commons/throttle/modules/core/src/main/java/org/wso2/throttle/ThrottleContext.java
==============================================================================
--- (empty file)
+++ branches/wsas/java/2.1/commons/throttle/modules/core/src/main/java/org/wso2/throttle/ThrottleContext.java Tue Nov 13 05:16:10 2007
@@ -0,0 +1,75 @@
+/*
+* Copyright 2005,2006 WSO2, Inc. http://wso2.com
+*
+* Licensed 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.wso2.throttle;
+
+public interface ThrottleContext {
+
+ /**
+ * To set the ThrottleConfiguration- The static data corresspounding to this context
+ *
+ * @param throttleConfiguration
+ */
+ public void setThrottleConfiguration(ThrottleConfiguration throttleConfiguration);
+
+ /**
+ * To get the ThrottleConfiguration
+ *
+ * @return ThrottleConfiguration
+ */
+ public ThrottleConfiguration getThrottleConfiguration();
+
+ /**
+ * To add a Caller
+ *
+ * @param caller
+ */
+ public void addCaller(Caller caller);
+
+ /**
+ * To get a Caller
+ *
+ * @param ID
+ * @return Caller
+ */
+ public Caller getCaller(Object ID);
+
+ /**
+ * To remove a Caller
+ *
+ * @param ID
+ */
+ public void removeCaller(Object ID);
+
+ /**
+ * To clean up callers(RunTime Data) according to current system time
+ *
+ * @param currentTime
+ * @throws ThrottleException
+ */
+ public void processCleanList(long currentTime) throws ThrottleException;
+
+ /**
+ * To get the type of the throttle
+ *
+ * @return the type of the throttle
+ */
+ public int getType();
+
+
+}
Added: branches/wsas/java/2.1/commons/throttle/modules/core/src/main/java/org/wso2/throttle/ThrottleException.java
==============================================================================
--- (empty file)
+++ branches/wsas/java/2.1/commons/throttle/modules/core/src/main/java/org/wso2/throttle/ThrottleException.java Tue Nov 13 05:16:10 2007
@@ -0,0 +1,35 @@
+/*
+* Copyright 2005,2006 WSO2, Inc. http://www.wso2.org
+*
+* Licensed 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.wso2.throttle;
+
+/**
+ * Exception for throttle
+ */
+public class ThrottleException extends Exception {
+
+ private static final long serialVersionUID = -1433310548520145461L;
+
+ public ThrottleException() {
+ super();
+ }
+
+ public ThrottleException(String message) {
+ super(message);
+ }
+}
Added: branches/wsas/java/2.1/commons/throttle/modules/core/src/main/java/org/wso2/throttle/ThrottlePolicyProcessor.java
==============================================================================
--- (empty file)
+++ branches/wsas/java/2.1/commons/throttle/modules/core/src/main/java/org/wso2/throttle/ThrottlePolicyProcessor.java Tue Nov 13 05:16:10 2007
@@ -0,0 +1,303 @@
+/*
+* Copyright 2005,2006 WSO2, Inc. http://www.wso2.org
+*
+* Licensed 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.wso2.throttle;
+
+import org.apache.axiom.om.OMElement;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.neethi.All;
+import org.apache.neethi.ExactlyOne;
+import org.apache.neethi.Policy;
+import org.apache.neethi.PolicyEngine;
+import org.apache.neethi.builders.xml.XmlPrimtiveAssertion;
+import org.wso2.throttle.factory.CallerConfigurationFactory;
+import org.wso2.throttle.factory.ThrottleConfigurationFactory;
+import org.wso2.throttle.factory.ThrottleContextFactory;
+
+import javax.xml.namespace.QName;
+import java.util.Iterator;
+import java.util.List;
+
+
+/**
+ * The class for processing policy that specify throttle configuration
+ */
+
+public class ThrottlePolicyProcessor {
+
+ /**
+ * Log for this class
+ */
+ private static Log log = LogFactory.getLog(ThrottlePolicyProcessor.class.getName());
+
+
+ /**
+ * process policy and return IPBaseThrottleConfiguration object
+ *
+ * @param policy - policy for throttle
+ * @return IPBaseThrottleConfiguration - Bean object which holds Mata-Data about throttle
+ * @throws ThrottleException
+ */
+
+ public static Throttle processPolicy(Policy policy) throws ThrottleException {
+
+ Throttle throttle = null;
+ ThrottleConfiguration throttleConfiguration = null;
+ if (policy == null) {
+ //if policy is not available ,then return null for ThrottleConfiguration
+ return null; // no pilicy is available in the module description
+ }
+ List topLevelAssertionList = policy.getPolicyComponents();
+ for (Iterator topLevelAssertionsIterator = topLevelAssertionList.iterator();
+ topLevelAssertionsIterator.hasNext();) {
+ Object topLevelAssertionObject = topLevelAssertionsIterator.next();
+ if (topLevelAssertionObject instanceof XmlPrimtiveAssertion) {
+ XmlPrimtiveAssertion topLevelXmlPrimtiveAssertion = (XmlPrimtiveAssertion)
+ topLevelAssertionObject;
+ QName qName = topLevelXmlPrimtiveAssertion.getName();
+ if (qName.equals(ThrottleConstants.THROTTLE_ASSERTION_QNAME)) {
+ throttle = new Throttle();
+ Policy throtlePolicy = PolicyEngine.
+ getPolicy(topLevelXmlPrimtiveAssertion.getValue());
+ List assertionList = throtlePolicy.getPolicyComponents();
+
+ for (Iterator assertionsIterator = assertionList.iterator(); assertionsIterator.hasNext();)
+ {
+ Object topLevelPolicy = assertionsIterator.next();
+ if (topLevelPolicy instanceof All) {
+
+ // boolean isOtherConfiguration = false;
+ // // To track default configuration for all ips
+ CallerConfiguration configuration = null; // To create a
+ //configurationbean object
+ boolean isIPRangeFound = false;
+ boolean isExactlyOneFound = false;
+ ExactlyOne controleParmeter = null;
+ List configDataAssertionList = ((All) topLevelPolicy).getAssertions();
+ if (configDataAssertionList != null) {
+ for (Iterator configDataAssertionIterator =
+ configDataAssertionList.iterator(); configDataAssertionIterator.hasNext();)
+ {
+ Object aConfigDataAssertionObject = configDataAssertionIterator.next();
+ if (aConfigDataAssertionObject instanceof XmlPrimtiveAssertion) {
+ XmlPrimtiveAssertion idPrimitiveAssertion = (XmlPrimtiveAssertion) aConfigDataAssertionObject;
+ OMElement element = idPrimitiveAssertion.getValue();
+
+ String throttleType = element.getAttributeValue(ThrottleConstants.THROTTLE_TYPE_ATTRIBUTE_QNAME);
+ if (throttleType == null) {
+ handleException("Type of Throtle in the policy cannot be null");
+ }
+ if (throttleType.equals("IP")) {
+ throttleConfiguration = throttle.getThrottleConfiguration(ThrottleConstants.IP_BASED_THROTTLE_KEY);
+ if (throttleConfiguration == null) {
+ throttleConfiguration = ThrottleConfigurationFactory.createThrottleConfiguration(ThrottleConstants.IP_BASE);
+ throttle.addThrottleContext(ThrottleConstants.IP_BASED_THROTTLE_KEY,
+ ThrottleContextFactory.createThrottleContext(ThrottleConstants.IP_BASE, throttleConfiguration));
+ throttle.addThrottleConfiguration(ThrottleConstants.IP_BASED_THROTTLE_KEY, throttleConfiguration);
+ }
+ configuration = CallerConfigurationFactory.createCallerConfiguration(ThrottleConstants.IP_BASE);
+ } else if (throttleType.equals("DOMAIN")) {
+ throttleConfiguration = throttle.getThrottleConfiguration(ThrottleConstants.DOMAIN_BASED_THROTTLE_KEY);
+ if (throttleConfiguration == null) {
+ throttleConfiguration = ThrottleConfigurationFactory.createThrottleConfiguration(ThrottleConstants.DOMAIN_BASE);
+ throttle.addThrottleContext(ThrottleConstants.DOMAIN_BASED_THROTTLE_KEY,
+ ThrottleContextFactory.createThrottleContext(ThrottleConstants.DOMAIN_BASE, throttleConfiguration));
+ throttle.addThrottleConfiguration(ThrottleConstants.DOMAIN_BASED_THROTTLE_KEY, throttleConfiguration);
+ }
+ configuration = CallerConfigurationFactory.createCallerConfiguration(ThrottleConstants.DOMAIN_BASE);
+ } else {
+ handleException("Unsupported throttle type : " + throttleType);
+ }
+ if (configuration != null) {
+
+ // 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.ID_PARAMETER_NAME)) {
+
+ if (!value.equals("")) {
+
+ isIPRangeFound = true;
+ configuration.setID(value);
+ } else {
+ handleException("Value of ID cannot find - invalid configuration");
+ }
+ } else {
+ handleException("Undefined pocilcy property for throttle - Expect ID ");
+ }
+ }
+
+ } else if (aConfigDataAssertionObject instanceof ExactlyOne) {
+ controleParmeter = (ExactlyOne) aConfigDataAssertionObject;
+ }
+ }
+ }
+ if (configuration != null) {
+ if (controleParmeter != null) {
+ List childAssertionsList = controleParmeter.getPolicyComponents();
+ boolean haveSelectOneFromExactlyOne = false;
+ for (Iterator childAssertionSIterator = childAssertionsList.iterator(); childAssertionSIterator.hasNext() && !haveSelectOneFromExactlyOne;)
+ {
+ Object childAssertionObject = childAssertionSIterator.next();
+ if (childAssertionObject instanceof All) {
+ haveSelectOneFromExactlyOne = true;
+ boolean isFoundMaxCount = false;
+ boolean isFoundUnitTime = false;
+ All childAll = (All) childAssertionObject;
+ List aConfigDataAssertionList = childAll.getPolicyComponents();
+ for (Iterator aConfigDataAssertionsIterator = aConfigDataAssertionList.iterator(); aConfigDataAssertionsIterator.hasNext();)
+ {
+ Object aOneOfConfigDataAssertions = aConfigDataAssertionsIterator.next();
+ if (aOneOfConfigDataAssertions instanceof XmlPrimtiveAssertion) {
+ XmlPrimtiveAssertion aOneOfConfigDataPrimitiveAssertion = (XmlPrimtiveAssertion) aOneOfConfigDataAssertions;
+ OMElement element = aOneOfConfigDataPrimitiveAssertion.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");
+ }
+ if (!value.equals("")) {
+
+ if (name.equals(ThrottleConstants.MAXIMUM_COUNT_PARAMETER_NAME)) {
+ isFoundMaxCount = true;
+ configuration.setMaximumRequestPerUnitTime(Integer.parseInt(value.trim()));
+
+ } else
+ if (name.equals(ThrottleConstants.UNIT_TIME_PARAMETER_NAME)) {
+ //TODO need to verify that value is milisecond
+ long timeInMiliSec = Long.parseLong(value.trim());
+ if (timeInMiliSec == 0) {
+ handleException("Unit Time cannot find - invalid throttle policy configuration");
+ }
+ isFoundUnitTime = true;
+ configuration.setUnitTimeInMiliSecond(timeInMiliSec);
+
+ } else
+ if (name.equals(ThrottleConstants.PROHIBIT_TIME_PERIOD_PARAMETER_NAME)) {
+ configuration.setProhibitTimePeriod(Long.parseLong(value.trim()));
+
+ } else {
+ handleException("Undefined Policy property for Throttle Policy");
+ }
+ } else {
+ if (!name.equals(ThrottleConstants.PROHIBIT_TIME_PERIOD_PARAMETER_NAME)) {
+ handleException("The policy which have not defined as optional should have value ");
+ }
+ }
+ }
+ }
+ if (isFoundUnitTime && isFoundMaxCount) {
+ isExactlyOneFound = true;
+ } else {
+ handleException("Maximum Count and UnitTime are Mandatory in Throttle Policy ");
+
+ }
+ } else
+ if (childAssertionObject instanceof XmlPrimtiveAssertion) {
+ haveSelectOneFromExactlyOne = true;
+ XmlPrimtiveAssertion isAllowXmlPrimtiveAssertion = (XmlPrimtiveAssertion) childAssertionObject;
+ OMElement element = isAllowXmlPrimtiveAssertion.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.ISALLOW_PARAMETER_NAME)) {
+ if (value.equals(Boolean.toString(true))) {
+ isExactlyOneFound = true;
+ configuration.setAccessState(ThrottleConstants.ACCESS_ALLOWED);
+ } else if (value.equals(Boolean.toString(false))) {
+ isExactlyOneFound = true;
+ configuration.setAccessState(ThrottleConstants.ACCESS_DENIED);
+ } else {
+ handleException("Value for isAllow policy component is invalied");
+ }
+ } else {
+ handleException("Invalied Throttle Policy configuration");
+ }
+ }
+ }
+ }
+
+ } else {
+ 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");
+ }
+ }
+ }
+ }
+ }
+ }
+ return throttle;
+ }
+
+ /**
+ * Helper method to handle exception
+ *
+ * @param messeage
+ * @throws ThrottleException
+ */
+ private static void handleException(String messeage) throws ThrottleException {
+ String msg = "Error was ocuured during throttle policy processing " + messeage;
+ log.error(msg);
+ throw new ThrottleException(msg);
+ }
+
+}
Added: branches/wsas/java/2.1/commons/throttle/modules/core/src/main/java/org/wso2/throttle/factory/CallerConfigurationFactory.java
==============================================================================
--- (empty file)
+++ branches/wsas/java/2.1/commons/throttle/modules/core/src/main/java/org/wso2/throttle/factory/CallerConfigurationFactory.java Tue Nov 13 05:16:10 2007
@@ -0,0 +1,47 @@
+/*
+* Copyright 2004,2005 The Apache Software Foundation.
+*
+* Licensed 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.wso2.throttle.factory;
+
+import org.wso2.throttle.CallerConfiguration;
+import org.wso2.throttle.ThrottleConstants;
+import org.wso2.throttle.ThrottleException;
+import org.wso2.throttle.impl.domainbase.DomainBaseCallerConfiguration;
+import org.wso2.throttle.impl.ipbase.IPBaseCallerConfiguration;
+
+/**
+ *
+ *
+ */
+
+public class CallerConfigurationFactory {
+
+ /**
+ * To create a CallerConfiguration instance
+ *
+ * @param throttletype - the type of the thorttle
+ * @return CallerConfiguration
+ * @throws ThrottleException
+ */
+ public static CallerConfiguration createCallerConfiguration(int throttletype) throws ThrottleException {
+ if (ThrottleConstants.IP_BASE == throttletype) {
+ return new IPBaseCallerConfiguration();
+ } else if (ThrottleConstants.DOMAIN_BASE == throttletype) {
+ return new DomainBaseCallerConfiguration();
+ } else {
+ throw new ThrottleException("unknown throttle type");
+ }
+ }
+}
Added: branches/wsas/java/2.1/commons/throttle/modules/core/src/main/java/org/wso2/throttle/factory/CallerFactory.java
==============================================================================
--- (empty file)
+++ branches/wsas/java/2.1/commons/throttle/modules/core/src/main/java/org/wso2/throttle/factory/CallerFactory.java Tue Nov 13 05:16:10 2007
@@ -0,0 +1,48 @@
+/*
+* Copyright 2004,2005 The Apache Software Foundation.
+*
+* Licensed 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.wso2.throttle.factory;
+
+import org.wso2.throttle.Caller;
+import org.wso2.throttle.ThrottleConstants;
+import org.wso2.throttle.ThrottleException;
+import org.wso2.throttle.impl.domainbase.DomainBaseCaller;
+import org.wso2.throttle.impl.ipbase.IPBaseCaller;
+
+/**
+ *
+ *
+ */
+
+public class CallerFactory {
+
+ /**
+ * To create a Caller
+ *
+ * @param throttletype - the type of the throttle
+ * @param id - the id of the caller
+ * @return caller
+ * @throws ThrottleException
+ */
+ public static Caller createCaller(int throttletype, Object id) throws ThrottleException {
+ if (ThrottleConstants.IP_BASE == throttletype) {
+ return new IPBaseCaller((String) id);
+ } else if (ThrottleConstants.DOMAIN_BASE == throttletype) {
+ return new DomainBaseCaller((String) id);
+ } else {
+ throw new ThrottleException("unknown throttle type");
+ }
+ }
+}
Added: branches/wsas/java/2.1/commons/throttle/modules/core/src/main/java/org/wso2/throttle/factory/ThrottleConfigurationFactory.java
==============================================================================
--- (empty file)
+++ branches/wsas/java/2.1/commons/throttle/modules/core/src/main/java/org/wso2/throttle/factory/ThrottleConfigurationFactory.java Tue Nov 13 05:16:10 2007
@@ -0,0 +1,48 @@
+/*
+* Copyright 2004,2005 The Apache Software Foundation.
+*
+* Licensed 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.wso2.throttle.factory;
+
+import org.wso2.throttle.ThrottleConfiguration;
+import org.wso2.throttle.ThrottleConstants;
+import org.wso2.throttle.ThrottleException;
+import org.wso2.throttle.impl.domainbase.DomainBaseThrottleConfiguration;
+import org.wso2.throttle.impl.ipbase.IPBaseThrottleConfiguration;
+
+/**
+ *
+ *
+ */
+
+public class ThrottleConfigurationFactory {
+
+ /**
+ * To create a ThrottleConfiguration
+ *
+ * @param throttletype - the type of the throttle
+ * @return ThrottleConfiguration
+ * @throws ThrottleException
+ */
+ public static ThrottleConfiguration createThrottleConfiguration(int throttletype) throws ThrottleException {
+ if (ThrottleConstants.IP_BASE == throttletype) {
+ return new IPBaseThrottleConfiguration();
+ } else if (ThrottleConstants.DOMAIN_BASE == throttletype) {
+ return new DomainBaseThrottleConfiguration();
+ } else {
+ throw new ThrottleException("unknown throttle type");
+ }
+ }
+
+}
Added: branches/wsas/java/2.1/commons/throttle/modules/core/src/main/java/org/wso2/throttle/factory/ThrottleContextFactory.java
==============================================================================
--- (empty file)
+++ branches/wsas/java/2.1/commons/throttle/modules/core/src/main/java/org/wso2/throttle/factory/ThrottleContextFactory.java Tue Nov 13 05:16:10 2007
@@ -0,0 +1,49 @@
+/*
+* Copyright 2004,2005 The Apache Software Foundation.
+*
+* Licensed 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.wso2.throttle.factory;
+
+import org.wso2.throttle.ThrottleConfiguration;
+import org.wso2.throttle.ThrottleConstants;
+import org.wso2.throttle.ThrottleContext;
+import org.wso2.throttle.ThrottleException;
+import org.wso2.throttle.impl.domainbase.DomainBaseThrottleContext;
+import org.wso2.throttle.impl.ipbase.IPBaseThrottleContext;
+
+/**
+ *
+ *
+ */
+
+public class ThrottleContextFactory {
+
+ /**
+ * To create a ThrottleContext
+ *
+ * @param throttletype - the type of the throttle
+ * @param configuration - the throttle configuration
+ * @return ThrottleContext
+ * @throws ThrottleException
+ */
+ public static ThrottleContext createThrottleContext(int throttletype, ThrottleConfiguration configuration) throws ThrottleException {
+ if (ThrottleConstants.IP_BASE == throttletype) {
+ return new IPBaseThrottleContext(configuration);
+ } else if (ThrottleConstants.DOMAIN_BASE == throttletype) {
+ return new DomainBaseThrottleContext(configuration);
+ } else {
+ throw new ThrottleException("unknown throttle type");
+ }
+ }
+}
Added: branches/wsas/java/2.1/commons/throttle/modules/core/src/main/java/org/wso2/throttle/impl/domainbase/DomainBaseCaller.java
==============================================================================
--- (empty file)
+++ branches/wsas/java/2.1/commons/throttle/modules/core/src/main/java/org/wso2/throttle/impl/domainbase/DomainBaseCaller.java Tue Nov 13 05:16:10 2007
@@ -0,0 +1,41 @@
+/*
+* 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.wso2.throttle.impl.domainbase;
+
+import org.wso2.throttle.Caller;
+import org.wso2.throttle.ThrottleConstants;
+
+import java.io.Serializable;
+
+/**
+ *
+ */
+
+public class DomainBaseCaller extends Caller implements Serializable {
+
+ private static final long serialVersionUID = 5681795068518758365L;
+
+ public DomainBaseCaller(String ID) {
+ super(ID);
+ }
+
+ public int getType() {
+ return ThrottleConstants.DOMAIN_BASE;
+ }
+}
Added: branches/wsas/java/2.1/commons/throttle/modules/core/src/main/java/org/wso2/throttle/impl/domainbase/DomainBaseCallerConfiguration.java
==============================================================================
--- (empty file)
+++ branches/wsas/java/2.1/commons/throttle/modules/core/src/main/java/org/wso2/throttle/impl/domainbase/DomainBaseCallerConfiguration.java Tue Nov 13 05:16:10 2007
@@ -0,0 +1,46 @@
+/*
+* 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.wso2.throttle.impl.domainbase;
+
+import org.wso2.throttle.CallerConfiguration;
+import org.wso2.throttle.ThrottleConstants;
+
+import java.io.Serializable;
+
+/**
+ *
+ */
+
+public class DomainBaseCallerConfiguration extends CallerConfiguration implements Serializable {
+
+ private String id;
+ private static final long serialVersionUID = -4010227726740405404L;
+
+ public String getID() {
+ return this.id;
+ }
+
+ public void setID(String ID) {
+ this.id = ID;
+ }
+
+ public int getType() {
+ return ThrottleConstants.DOMAIN_BASE;
+ }
+}
Added: branches/wsas/java/2.1/commons/throttle/modules/core/src/main/java/org/wso2/throttle/impl/domainbase/DomainBaseThrottleConfiguration.java
==============================================================================
--- (empty file)
+++ branches/wsas/java/2.1/commons/throttle/modules/core/src/main/java/org/wso2/throttle/impl/domainbase/DomainBaseThrottleConfiguration.java Tue Nov 13 05:16:10 2007
@@ -0,0 +1,136 @@
+/*
+* 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.wso2.throttle.impl.domainbase;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.wso2.throttle.CallerConfiguration;
+import org.wso2.throttle.ThrottleConfiguration;
+import org.wso2.throttle.ThrottleConstants;
+
+import java.io.Serializable;
+import java.util.TreeMap;
+
+/**
+ *
+ */
+
+public class DomainBaseThrottleConfiguration implements Serializable, ThrottleConfiguration {
+
+ /**
+ * Log for this class
+ */
+ private static Log log = LogFactory.getLog(DomainBaseThrottleConfiguration.class.getName());
+ /**
+ * The key for Other configuration
+ */
+ private String keyOfOther;
+ /**
+ * The default configuration for a throttle and this will apply to callersMap that have not a custom configuration
+ */
+ private CallerConfiguration defaultCallerConfiguration;
+ /**
+ * To hold configurations
+ */
+ private TreeMap configurationsMap;
+ private static final long serialVersionUID = -6548042636191873581L;
+
+ /**
+ * The Deault Constructor for Throttle Bean
+ */
+ public DomainBaseThrottleConfiguration() {
+ this.configurationsMap = new TreeMap();
+ }
+
+ /**
+ * To get a DomainBaseCallerConfiguration
+ *
+ * @param ID
+ * @return DomainBaseCallerConfiguration
+ */
+ public CallerConfiguration getCallerConfiguration(Object ID) {
+
+ if (ID.equals(ThrottleConstants.KEY_OF_DEFAULT_CONFIGURATION_FOR_OTHER)) {
+ return defaultCallerConfiguration;
+ } else {
+ String key = (String) getConfigurationKeyOfCaller(ID);
+ if (key != null) {
+ if (key.equals(ThrottleConstants.KEY_OF_DEFAULT_CONFIGURATION_FOR_OTHER)) {
+ return defaultCallerConfiguration;
+ }
+ return (CallerConfiguration) configurationsMap.get(key);
+ }
+ }
+ return null;
+ }
+
+ /**
+ * To get map of configurations
+ *
+ * @return TreeMap
+ */
+ public TreeMap getConfigurations() {
+ return this.configurationsMap;
+ }
+
+
+ /**
+ * To set map of configurations
+ *
+ * @param map
+ */
+ public void setConfigurations(TreeMap map) {
+ this.configurationsMap = map;
+ }
+
+ /**
+ * To add a DomainBaseCallerConfiguration
+ *
+ * @param configuration
+ */
+ public void addCallerConfiguration(CallerConfiguration configuration) {
+ String key = configuration.getID();
+ if (key.equals(ThrottleConstants.KEY_OF_DEFAULT_CONFIGURATION_FOR_OTHER)) {
+ keyOfOther = ThrottleConstants.KEY_OF_DEFAULT_CONFIGURATION_FOR_OTHER;
+ defaultCallerConfiguration = configuration;
+ } else {
+ configurationsMap.put(key, configuration);
+ }
+ }
+
+ /**
+ * To get key for access configuration
+ *
+ * @param callerID
+ * @return Object-String representation of corrected epr-key for get configuration
+ */
+ public Object getConfigurationKeyOfCaller(Object callerID) {
+ String domain = (String) callerID;
+ //if there is a unique Domain
+ if (configurationsMap.containsKey(domain)) {
+ return domain;
+ }
+ return keyOfOther;
+ }
+
+ public int getType() {
+ return ThrottleConstants.DOMAIN_BASE;
+ }
+
+}
Added: branches/wsas/java/2.1/commons/throttle/modules/core/src/main/java/org/wso2/throttle/impl/domainbase/DomainBaseThrottleContext.java
==============================================================================
--- (empty file)
+++ branches/wsas/java/2.1/commons/throttle/modules/core/src/main/java/org/wso2/throttle/impl/domainbase/DomainBaseThrottleContext.java Tue Nov 13 05:16:10 2007
@@ -0,0 +1,246 @@
+/*
+* 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.wso2.throttle.impl.domainbase;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.wso2.throttle.*;
+
+import java.io.Serializable;
+import java.util.*;
+
+/**
+ *
+ */
+
+public class DomainBaseThrottleContext implements ThrottleContext, Serializable {
+
+ /**
+ * Log for this class
+ */
+ private static Log log = LogFactory.getLog(DomainBaseThrottleContext.class.getName());
+ /**
+ * The callersMap that have registered for a particular throttle
+ */
+ private TreeMap callersMap;
+ /**
+ * For mapping epr to timeStamp
+ */
+ private HashMap keyToTimeStampMap;
+ /**
+ * The Time which next cleaning for this trottle will have to take place
+ */
+ private long nextCleanTime;
+ /**
+ * The Object for used to lock in synchronizing
+ */
+ private final transient Object lock = new Object();
+ /**
+ * The configuration of a throtle
+ */
+ private ThrottleConfiguration throttleConfiguration;
+
+ private static final long serialVersionUID = -7765899274408287393L;
+
+ /**
+ * To create a ThrottleContext need to provide a ThrottleConfiguration
+ *
+ * @param throttleConfiguration
+ */
+ public DomainBaseThrottleContext(ThrottleConfiguration throttleConfiguration) {
+ this.keyToTimeStampMap = new HashMap();
+ this.callersMap = new TreeMap();
+ this.nextCleanTime = 0;
+ this.throttleConfiguration = throttleConfiguration;
+ }
+
+ /**
+ * To get the ThrottleConfiguration
+ *
+ * @return ThrottleConfiguration
+ */
+ public ThrottleConfiguration getThrottleConfiguration() {
+ return throttleConfiguration;
+ }
+
+ /**
+ * To get a CallerDomain Object
+ *
+ * @param ID
+ * @return CallerDomain
+ */
+ public Caller getCaller(Object ID) {
+ String keyOfConfiguration = (String) throttleConfiguration.getConfigurationKeyOfCaller(ID);
+ if (keyOfConfiguration != null) {
+ synchronized (lock) {
+ Long timeKeyOfCallerContext = (Long) keyToTimeStampMap.get(keyOfConfiguration);
+ if (timeKeyOfCallerContext != null) {
+ Object callerObject = callersMap.get(timeKeyOfCallerContext);
+ if (callerObject != null) {
+ if (callerObject instanceof Caller) {
+ return (Caller) callerObject;
+ } else if (callerObject instanceof LinkedList) {
+ LinkedList callersWithSameTimeStampList = (LinkedList) callerObject;
+ for (Iterator iterator = callersWithSameTimeStampList.iterator(); iterator.hasNext();)
+ {
+ Caller caller = (Caller) iterator.next();
+ if (keyOfConfiguration.equals(throttleConfiguration.getConfigurationKeyOfCaller(caller.getID()))) {
+ return caller;
+ }
+ }
+
+ }
+ }
+ }
+
+ }
+ }
+ return null;
+ }
+
+ /**
+ * To set the ThrottleConfiguration
+ *
+ * @param throttleConfiguration
+ */
+ public void setThrottleConfiguration(ThrottleConfiguration throttleConfiguration) {
+ this.throttleConfiguration = throttleConfiguration;
+ }
+
+ /**
+ * setting callerContext - put callersMap Against time and put time Against epr
+ *
+ * @param caller
+ */
+ public void addCaller(Caller caller) {
+
+ String keyOfConfiguration = (String) throttleConfiguration.getConfigurationKeyOfCaller(caller.getID());
+ if (keyOfConfiguration != null) {
+ setCaller(caller, keyOfConfiguration);
+ }
+ }
+
+ /**
+ * helper method to set a CallerDomain Object
+ *
+ * @param caller
+ * @param keyOfConfiguration
+ */
+ private void setCaller(Caller caller, String keyOfConfiguration) {
+ Long time = new Long(caller.getNextTimeWindow());
+ synchronized (lock) {
+ if (!callersMap.containsKey(time)) {
+ callersMap.put(time, caller);
+ } else {
+ //if there are callersMap with same timewindow ,then use linkedList to hold those
+ LinkedList callersWithSameTimeStampList = null;
+ Object callerObject = callersMap.get(time);
+ if (callerObject != null) {
+ if (callerObject instanceof Caller) {
+ callersWithSameTimeStampList = new LinkedList();
+ callersWithSameTimeStampList.add(callerObject);
+ callersWithSameTimeStampList.add(caller);
+ callersMap.remove(time);
+ } else if (callerObject instanceof LinkedList) {
+ callersWithSameTimeStampList = (LinkedList) callerObject;
+ callersWithSameTimeStampList.add(caller);
+ }
+ if (callersWithSameTimeStampList != null) {
+ callersMap.put(time, callersWithSameTimeStampList);
+ }
+ }
+ }
+ //set Time Vs key
+ keyToTimeStampMap.put(keyOfConfiguration, time);
+
+ }
+ }
+
+ /**
+ * removing caller by providing ID of the caller
+ *
+ * @param ID
+ */
+ public void removeCaller(Object ID) {
+ if (ID instanceof Long) {
+ removeCaller((Long) ID);
+ } else {
+ String keyOfConfiguration = (String) throttleConfiguration.getConfigurationKeyOfCaller(ID);
+ Long time;
+ if (keyOfConfiguration != null) {
+ time = (Long) keyToTimeStampMap.get(keyOfConfiguration);
+ removeCaller(time);
+ }
+
+ }
+ }
+
+ /**
+ * To remove Caller by providing time key
+ *
+ * @param key
+ */
+ private void removeCaller(Long key) {
+ synchronized (lock) {
+ callersMap.remove(key);
+ }
+ }
+
+ /**
+ * processing cleaning list- only process callerContexts which unit time already had overed
+ *
+ * @param currentTime - Current System Time
+ * @throws org.wso2.throttle.ThrottleException
+ */
+
+ public void processCleanList(long currentTime) throws ThrottleException {
+ synchronized (lock) {
+ if (currentTime > nextCleanTime) {
+ SortedMap sortedMap = callersMap.headMap(new Long(currentTime));
+ if (sortedMap != null && sortedMap.size() > 0) {
+ for (Iterator callersIterator = sortedMap.values().iterator(); callersIterator.hasNext();)
+ {
+ Object callerObject = callersIterator.next();
+ if (callerObject != null) {
+ if (callerObject instanceof Caller) { // In the case nextAccessTime is unique for the caller
+ Caller caller = ((Caller) callerObject);
+ caller.canAccessIfUnitTimeOver(this.getThrottleConfiguration().getCallerConfiguration(caller.getID()), this, currentTime);
+ } else
+ if (callerObject instanceof LinkedList) { //In the case nextAccessTime of multDomainle callers are same
+ LinkedList callersWithSameTimeStampList = (LinkedList) callerObject;
+ for (Iterator iterator = callersWithSameTimeStampList.iterator(); iterator.hasNext();)
+ {
+ Caller caller = (Caller) iterator.next();
+ if (caller != null) {
+ caller.canAccessIfUnitTimeOver(this.getThrottleConfiguration().getCallerConfiguration(caller.getID()), this, currentTime);
+ }
+ }
+ }
+ }
+ }
+ }
+ nextCleanTime = currentTime + ThrottleConstants.DEFAULT_THROTTLE_CLEAN_PERIOD;
+ }
+ }
+ }
+
+ public int getType() {
+ return ThrottleConstants.DOMAIN_BASE;
+ }
+}
Added: branches/wsas/java/2.1/commons/throttle/modules/core/src/main/java/org/wso2/throttle/impl/ipbase/IPBaseCaller.java
==============================================================================
--- (empty file)
+++ branches/wsas/java/2.1/commons/throttle/modules/core/src/main/java/org/wso2/throttle/impl/ipbase/IPBaseCaller.java Tue Nov 13 05:16:10 2007
@@ -0,0 +1,50 @@
+/*
+* Copyright 2005,2006 WSO2, Inc. http://wso2.com
+*
+* Licensed 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.wso2.throttle.impl.ipbase;
+
+import org.wso2.throttle.Caller;
+import org.wso2.throttle.ThrottleConstants;
+
+import java.io.Serializable;
+
+/**
+ *
+ *
+ */
+
+public class IPBaseCaller extends Caller implements Serializable {
+
+ private static final long serialVersionUID = 635051645003581667L;
+
+ /**
+ * The constructor
+ *
+ * @param ID
+ */
+ public IPBaseCaller(String ID) {
+ super(ID);
+ }
+
+ /**
+ * @return int value that indicate type of throttle
+ */
+ public int getType() {
+ return ThrottleConstants.IP_BASE;
+ }
+}
\ No newline at end of file
Added: branches/wsas/java/2.1/commons/throttle/modules/core/src/main/java/org/wso2/throttle/impl/ipbase/IPBaseCallerConfiguration.java
==============================================================================
--- (empty file)
+++ branches/wsas/java/2.1/commons/throttle/modules/core/src/main/java/org/wso2/throttle/impl/ipbase/IPBaseCallerConfiguration.java Tue Nov 13 05:16:10 2007
@@ -0,0 +1,139 @@
+/*
+* Copyright 2005,2006 WSO2, Inc. http://wso2.com
+*
+* Licensed 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.wso2.throttle.impl.ipbase;
+
+
+import org.wso2.throttle.CallerConfiguration;
+import org.wso2.throttle.ThrottleConstants;
+
+import java.io.Serializable;
+
+/**
+ * Metadata for configure a throttle - static data -The data have built form processing policy
+ */
+public class IPBaseCallerConfiguration extends CallerConfiguration implements Serializable {
+
+ /**
+ * The ID of CallerConfiguration
+ */
+ private String ID;
+ /**
+ * The First part of the iprange -group ip
+ */
+ private String firstPartOfIPRange;
+ /**
+ * The second part of the iprange - group ip
+ */
+ private String secondPartOfIPRange;
+ private static final long serialVersionUID = -4818099134411420267L;
+
+ /**
+ * The Default Constructor
+ */
+ public IPBaseCallerConfiguration() {
+ super();
+
+ }
+
+ /**
+ * The Constructor with all configuration data
+ *
+ * @param unitTime - long value which represents Unit Time Window
+ * @param maximumRequest - int value which represents Maximum Request
+ * @param prohibitTimePeriod - long value which represents Prohibit Time after Max request came
+ * @param ipRange - String value which represents IP Range
+ */
+ public IPBaseCallerConfiguration(long unitTime, int maximumRequest,
+ long prohibitTimePeriod, String ipRange) {
+ super(unitTime, maximumRequest, prohibitTimePeriod, ipRange);
+ }
+
+ /**
+ * To get IP Range - Group IP
+ *
+ * @return String value of IP Range
+ */
+ public String getID() {
+ return ID;
+ }
+
+ /**
+ * To get First Part of IP Range
+ *
+ * @return String value of First Part Of Ip Range
+ */
+ public String getFirstPartOfIPRange() {
+ return firstPartOfIPRange;
+ }
+
+ /**
+ * To get Second Part of IP Range
+ *
+ * @return String value of Second Part Of IP Range
+ */
+ public String getSecondPartOfIPRange() {
+ return secondPartOfIPRange;
+ }
+
+
+ /**
+ * To set IP Range
+ *
+ * @param iprange
+ */
+ public void setID(String iprange) {
+ String ipParts[] = iprange.trim().split("-");
+ if (ipParts != null) {
+ // if IP Range is unique one IP
+ if (ipParts.length == 1) {
+ this.firstPartOfIPRange = ipParts[0];
+ }
+ // else if IP Range is group IP
+ else if (ipParts.length == 2) {
+ this.firstPartOfIPRange = ipParts[0];
+ this.secondPartOfIPRange = ipParts[1];
+ }
+ }
+ this.ID = iprange;
+ }
+
+ public int getType() {
+ return ThrottleConstants.IP_BASE;
+ }
+
+ /**
+ * To set First Part of IP Range
+ *
+ * @param firstPartOfIPRange - String value
+ */
+ public void setFirstPartOfIPRange(String firstPartOfIPRange) {
+ this.firstPartOfIPRange = firstPartOfIPRange;
+ }
+
+ /**
+ * To set Second Part Of IP Range
+ *
+ * @param secondPartOfIPRange - String value
+ */
+ public void setSecondPartOfIPRange(String secondPartOfIPRange) {
+ this.secondPartOfIPRange = secondPartOfIPRange;
+ }
+
+
+}
Added: branches/wsas/java/2.1/commons/throttle/modules/core/src/main/java/org/wso2/throttle/impl/ipbase/IPBaseThrottleConfiguration.java
==============================================================================
--- (empty file)
+++ branches/wsas/java/2.1/commons/throttle/modules/core/src/main/java/org/wso2/throttle/impl/ipbase/IPBaseThrottleConfiguration.java Tue Nov 13 05:16:10 2007
@@ -0,0 +1,148 @@
+/*
+* Copyright 2005,2006 WSO2, Inc. http://www.wso2.org
+*
+* Licensed 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.wso2.throttle.impl.ipbase;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.wso2.throttle.CallerConfiguration;
+import org.wso2.throttle.ThrottleConfiguration;
+import org.wso2.throttle.ThrottleConstants;
+
+import java.io.Serializable;
+import java.util.Iterator;
+import java.util.SortedMap;
+import java.util.TreeMap;
+
+public class IPBaseThrottleConfiguration implements Serializable, ThrottleConfiguration {
+
+ /**
+ * Log for this class
+ */
+ private static Log log = LogFactory.getLog(IPBaseThrottleConfiguration.class.getName());
+ /**
+ * The key for Other configuration
+ */
+ private String keyOfOther;
+ /**
+ * The default configuration for a throttle and this will apply to callersMap that have not a custom configuration
+ */
+ private CallerConfiguration defaultCallerConfiguration;
+ /**
+ * To hold configurations
+ */
+ private TreeMap configurationsMap;
+
+ private static final long serialVersionUID = -8660015469066052414L;
+
+ /**
+ * The Deault Constructor for Throttle Bean
+ */
+ public IPBaseThrottleConfiguration() {
+ this.configurationsMap = new TreeMap();
+ }
+
+ /**
+ * To get a IPBaseCallerConfiguration
+ *
+ * @param ID
+ * @return IPBaseCallerConfiguration
+ */
+ public CallerConfiguration getCallerConfiguration(Object ID) {
+
+ if (ID.equals(ThrottleConstants.KEY_OF_DEFAULT_CONFIGURATION_FOR_OTHER)) {
+ return defaultCallerConfiguration;
+ } else {
+ String key = (String) getConfigurationKeyOfCaller(ID);
+ if (key != null) {
+ if (key.equals(ThrottleConstants.KEY_OF_DEFAULT_CONFIGURATION_FOR_OTHER)) {
+ return defaultCallerConfiguration;
+ }
+ return (IPBaseCallerConfiguration) configurationsMap.get(key);
+ }
+ }
+ return null;
+ }
+
+ /**
+ * To get map of configurations
+ *
+ * @return TreeMap
+ */
+ public TreeMap getConfigurations() {
+ return this.configurationsMap;
+ }
+
+
+ /**
+ * To set map of configurations
+ *
+ * @param map
+ */
+ public void setConfigurations(TreeMap map) {
+ this.configurationsMap = map;
+ }
+
+ /**
+ * To add a IPBaseCallerConfiguration
+ *
+ * @param configuration
+ */
+ public void addCallerConfiguration(CallerConfiguration configuration) {
+ //TODO need to enable overlapping of ip with FirstPartOfIPRange
+ IPBaseCallerConfiguration ipBaseCallerConfiguration = (IPBaseCallerConfiguration) configuration;
+ String key = ipBaseCallerConfiguration.getFirstPartOfIPRange();
+ if (key.equals(ThrottleConstants.KEY_OF_DEFAULT_CONFIGURATION_FOR_OTHER)) {
+ keyOfOther = ThrottleConstants.KEY_OF_DEFAULT_CONFIGURATION_FOR_OTHER;
+ defaultCallerConfiguration = ipBaseCallerConfiguration;
+ } else {
+ configurationsMap.put(key, ipBaseCallerConfiguration);
+ }
+ }
+
+ /**
+ * To get key for access configuration
+ *
+ * @param callerID
+ * @return Object-String representation of corrected epr-key for get configuration
+ */
+ public Object getConfigurationKeyOfCaller(Object callerID) {
+ String ip = (String) callerID;
+ //if there is a unique IP
+ if (configurationsMap.containsKey(ip)) {
+ return ip;
+ } else {
+ //if ip contains in group iprange
+ SortedMap map = configurationsMap.headMap(ip);
+ if (map != null && map.size() > 0) {
+ for (Iterator iterator = map.values().iterator(); iterator.hasNext();) {
+ IPBaseCallerConfiguration ipBaseCallerConfiguration = (IPBaseCallerConfiguration) iterator.next();
+ if (ipBaseCallerConfiguration.getSecondPartOfIPRange() != null && ipBaseCallerConfiguration.getSecondPartOfIPRange().compareTo(ip) > 0)
+ return ipBaseCallerConfiguration.getFirstPartOfIPRange();
+ }
+
+ }
+ }
+ return keyOfOther;
+ }
+
+ public int getType() {
+ return ThrottleConstants.IP_BASE;
+ }
+
+}
Added: branches/wsas/java/2.1/commons/throttle/modules/core/src/main/java/org/wso2/throttle/impl/ipbase/IPBaseThrottleContext.java
==============================================================================
--- (empty file)
+++ branches/wsas/java/2.1/commons/throttle/modules/core/src/main/java/org/wso2/throttle/impl/ipbase/IPBaseThrottleContext.java Tue Nov 13 05:16:10 2007
@@ -0,0 +1,246 @@
+/*
+* Copyright 2005,2006 WSO2, Inc. http://wso2.com
+*
+* Licensed 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.wso2.throttle.impl.ipbase;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.wso2.throttle.*;
+
+import java.io.Serializable;
+import java.util.*;
+
+/**
+ *
+ *
+ */
+
+public class IPBaseThrottleContext implements ThrottleContext, Serializable {
+
+ /**
+ * Log for this class
+ */
+ private static Log log = LogFactory.getLog(IPBaseThrottleContext.class.getName());
+ /**
+ * The callersMap that have registered for a particular throttle
+ */
+ private TreeMap callersMap;
+ /**
+ * For mapping epr to timeStamp
+ */
+ private HashMap keyToTimeStampMap;
+ /**
+ * The Time which next cleaning for this trottle will have to take place
+ */
+ private long nextCleanTime;
+ /**
+ * The Object for used to lock in synchronizing
+ */
+ private final transient Object lock = new Object();
+ /**
+ * The configuration of a throtle
+ */
+ private ThrottleConfiguration throttleConfiguration;
+ private static final long serialVersionUID = 691325464023192016L;
+
+ /**
+ * To create a ThrottleContext need to provide a ThrottleConfiguration
+ *
+ * @param throttleConfiguration
+ */
+ public IPBaseThrottleContext(ThrottleConfiguration throttleConfiguration) {
+ this.keyToTimeStampMap = new HashMap();
+ this.callersMap = new TreeMap();
+ this.nextCleanTime = 0;
+ this.throttleConfiguration = throttleConfiguration;
+ }
+
+ /**
+ * To get the ThrottleConfiguration
+ *
+ * @return ThrottleConfiguration
+ */
+ public ThrottleConfiguration getThrottleConfiguration() {
+ return throttleConfiguration;
+ }
+
+ /**
+ * To get a CallerIP Object
+ *
+ * @param ID
+ * @return CallerIP
+ */
+ public Caller getCaller(Object ID) {
+ String keyOfConfiguration = (String) throttleConfiguration.getConfigurationKeyOfCaller(ID);
+ if (keyOfConfiguration != null) {
+ synchronized (lock) {
+ Long timeKeyOfCallerContext = (Long) keyToTimeStampMap.get(keyOfConfiguration);
+ if (timeKeyOfCallerContext != null) {
+ Object callerObject = callersMap.get(timeKeyOfCallerContext);
+ if (callerObject != null) {
+ if (callerObject instanceof Caller) {
+ return (Caller) callerObject;
+ } else if (callerObject instanceof LinkedList) {
+ LinkedList callersWithSameTimeStampList = (LinkedList) callerObject;
+ for (Iterator iterator = callersWithSameTimeStampList.iterator(); iterator.hasNext();)
+ {
+ Caller caller = (Caller) iterator.next();
+ if (keyOfConfiguration.equals(throttleConfiguration.getConfigurationKeyOfCaller(caller.getID()))) {
+ return caller;
+ }
+ }
+
+ }
+ }
+ }
+
+ }
+ }
+ return null;
+ }
+
+ /**
+ * To set the ThrottleConfiguration
+ *
+ * @param throttleConfiguration
+ */
+ public void setThrottleConfiguration(ThrottleConfiguration throttleConfiguration) {
+ this.throttleConfiguration = throttleConfiguration;
+ }
+
+ /**
+ * setting callerContext - put callersMap Against time and put time Against epr
+ *
+ * @param caller
+ */
+ public void addCaller(Caller caller) {
+
+ String keyOfConfiguration = (String) throttleConfiguration.getConfigurationKeyOfCaller(caller.getID());
+ if (keyOfConfiguration != null) {
+ setCaller(caller, keyOfConfiguration);
+ }
+ }
+
+ /**
+ * helper method to set a CallerIP Object
+ *
+ * @param caller
+ * @param keyOfConfiguration
+ */
+ private void setCaller(Caller caller, String keyOfConfiguration) {
+ Long time = new Long(caller.getNextTimeWindow());
+ synchronized (lock) {
+ if (!callersMap.containsKey(time)) {
+ callersMap.put(time, caller);
+ } else {
+ //if there are callersMap with same timewindow ,then use linkedList to hold those
+ LinkedList callersWithSameTimeStampList = null;
+ Object callerObject = callersMap.get(time);
+ if (callerObject != null) {
+ if (callerObject instanceof Caller) {
+ callersWithSameTimeStampList = new LinkedList();
+ callersWithSameTimeStampList.add(callerObject);
+ callersWithSameTimeStampList.add(caller);
+ callersMap.remove(time);
+ } else if (callerObject instanceof LinkedList) {
+ callersWithSameTimeStampList = (LinkedList) callerObject;
+ callersWithSameTimeStampList.add(caller);
+ }
+ if (callersWithSameTimeStampList != null) {
+ callersMap.put(time, callersWithSameTimeStampList);
+ }
+ }
+ }
+ //set Time Vs key
+ keyToTimeStampMap.put(keyOfConfiguration, time);
+
+ }
+ }
+
+ /**
+ * removing caller by providing ID of the caller
+ *
+ * @param ID
+ */
+ public void removeCaller(Object ID) {
+ if (ID instanceof Long) {
+ removeCaller((Long) ID);
+ } else {
+ String keyOfConfiguration = (String) throttleConfiguration.getConfigurationKeyOfCaller(ID);
+ Long time;
+ if (keyOfConfiguration != null) {
+ time = (Long) keyToTimeStampMap.get(keyOfConfiguration);
+ removeCaller(time);
+ }
+
+ }
+ }
+
+ /**
+ * To remove Caller by providing time key
+ *
+ * @param key
+ */
+ private void removeCaller(Long key) {
+ synchronized (lock) {
+ callersMap.remove(key);
+ }
+ }
+
+ /**
+ * processing cleaning list- only process callerContexts which unit time already had overed
+ *
+ * @param currentTime - Current System Time
+ * @throws ThrottleException
+ */
+
+ public void processCleanList(long currentTime) throws ThrottleException {
+ synchronized (lock) {
+ if (currentTime > nextCleanTime) {
+ SortedMap sortedMap = callersMap.headMap(new Long(currentTime));
+ if (sortedMap != null && sortedMap.size() > 0) {
+ for (Iterator callersIterator = sortedMap.values().iterator(); callersIterator.hasNext();)
+ {
+ Object callerObject = callersIterator.next();
+ if (callerObject != null) {
+ if (callerObject instanceof Caller) { // In the case nextAccessTime is unique for the caller
+ Caller caller = ((Caller) callerObject);
+ caller.canAccessIfUnitTimeOver(this.getThrottleConfiguration().getCallerConfiguration(caller.getID()), this, currentTime);
+ } else
+ if (callerObject instanceof LinkedList) { //In the case nextAccessTime of multiple callers are same
+ LinkedList callersWithSameTimeStampList = (LinkedList) callerObject;
+ for (Iterator iterator = callersWithSameTimeStampList.iterator(); iterator.hasNext();)
+ {
+ Caller caller = (Caller) iterator.next();
+ if (caller != null) {
+ caller.canAccessIfUnitTimeOver(this.getThrottleConfiguration().getCallerConfiguration(caller.getID()), this, currentTime);
+ }
+ }
+ }
+ }
+ }
+ }
+ nextCleanTime = currentTime + ThrottleConstants.DEFAULT_THROTTLE_CLEAN_PERIOD;
+ }
+ }
+ }
+
+ public int getType() {
+ return ThrottleConstants.IP_BASE;
+ }
+}
Added: branches/wsas/java/2.1/commons/throttle/modules/mar/pom.xml
==============================================================================
--- (empty file)
+++ branches/wsas/java/2.1/commons/throttle/modules/mar/pom.xml Tue Nov 13 05:16:10 2007
@@ -0,0 +1,77 @@
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+
+
+ <parent>
+ <groupId>org.wso2.wso2throttle</groupId>
+ <artifactId>wso2throttle</artifactId>
+ <version>2.1-SNAPSHOT</version>
+ </parent>
+
+ <modelVersion>4.0.0</modelVersion>
+ <artifactId>wso2throttle-mar</artifactId>
+ <packaging>jar</packaging>
+ <version>2.1-SNAPSHOT</version>
+ <name>WSO2 Throttling module - Mar</name>
+
+ <build>
+ <plugins>
+
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-antrun-plugin</artifactId>
+ <version>1.1</version>
+ <executions>
+ <execution>
+ <id>mar</id>
+ <phase>package</phase>
+ <configuration>
+ <tasks>
+ <copy file="target/${pom.artifactId}-${pom.version}.jar"
+ tofile="target/wso2throttle-${pom.version}.mar"/>
+
+ </tasks>
+ </configuration>
+ <goals>
+ <goal>run</goal>
+ </goals>
+ </execution>
+
+ </executions>
+ </plugin>
+ <plugin>
+ <groupId>org.codehaus.mojo</groupId>
+ <artifactId>build-helper-maven-plugin</artifactId>
+ <version>1.0</version>
+ <executions>
+ <execution>
+ <id>mar</id>
+ <phase>package</phase>
+ <goals>
+ <goal>attach-artifact</goal>
+ </goals>
+ <configuration>
+ <artifacts>
+ <artifact>
+ <file>target/wso2throttle-${pom.version}.mar</file>
+ <type>mar</type>
+ </artifact>
+ </artifacts>
+ <finalName>wso2throttle-${pom.version}</finalName>
+ </configuration>
+ </execution>
+ </executions>
+ </plugin>
+ </plugins>
+ </build>
+
+ <dependencies>
+ <dependency>
+ <groupId>org.wso2.wso2throttle</groupId>
+ <artifactId>wso2throttle-core</artifactId>
+ <version>2.1-SNAPSHOT</version>
+ </dependency>
+ </dependencies>
+
+</project>
Added: branches/wsas/java/2.1/commons/throttle/modules/mar/src/main/java/org/wso2/throttle/module/ThrottleModule.java
==============================================================================
--- (empty file)
+++ branches/wsas/java/2.1/commons/throttle/modules/mar/src/main/java/org/wso2/throttle/module/ThrottleModule.java Tue Nov 13 05:16:10 2007
@@ -0,0 +1,211 @@
+/*
+* Copyright 2005,2006 WSO2, Inc. http://wso2.com
+*
+* Licensed 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.wso2.throttle.module;
+
+
+import org.apache.axis2.AxisFault;
+import org.apache.axis2.context.ConfigurationContext;
+import org.apache.axis2.description.*;
+import org.apache.axis2.modules.Module;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.neethi.Assertion;
+import org.apache.neethi.Policy;
+import org.apache.neethi.PolicyEngine;
+import org.wso2.throttle.Throttle;
+import org.wso2.throttle.ThrottleConstants;
+import org.wso2.throttle.ThrottleException;
+import org.wso2.throttle.ThrottlePolicyProcessor;
+
+import java.io.InputStream;
+import java.util.HashMap;
+import java.util.Map;
+
+public class ThrottleModule implements Module {
+
+ private static Log log = LogFactory.getLog(ThrottleModule.class.getName());
+
+ /**
+ * The QName of the module
+ */
+ private String name;
+
+ 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;
+ this.name = module.getName();
+ PolicyInclude policyInclude = module.getPolicyInclude();
+ if (policyInclude != null) {
+ Policy policy = policyInclude.getEffectivePolicy();
+ if (policy != null) {
+ try {
+ throttle = ThrottlePolicyProcessor.processPolicy(policy);
+ }
+ catch (ThrottleException e) {
+
+ log.error("Error was ocuured when initing throttle module " + e.getMessage());
+ log.info("Throttling will occur using default module policy");
+
+ String id = policy.getId();
+ policyInclude.removePolicyElement(id);
+ defaultPolicy.setId(id);
+ policyInclude.addPolicyElement(PolicyInclude.AXIS_MODULE_POLICY, defaultPolicy);
+ throttle = defaultThrottle;
+ }
+ if (throttle != null) {
+ Map throttles = (Map) configctx.getProperty(ThrottleConstants.THROTTLES_MAP);
+ if (throttles == null) {
+ throttles = new HashMap();
+ configctx.setProperty(ThrottleConstants.THROTTLES_MAP, throttles);
+ }
+ if (!throttles.containsKey(ThrottleConstants.GLOBAL_THROTTLE_KEY)) {
+ throttles.put(ThrottleConstants.GLOBAL_THROTTLE_KEY, throttle);
+ }
+ }
+ }
+ }
+
+ }
+
+ public void engageNotify(AxisDescription axisDescription) throws AxisFault {
+
+ String currentServiceName;
+ if (axisDescription instanceof AxisService) {
+ Throttle throttle;
+ AxisService currentService = ((AxisService) axisDescription);
+ AxisModule module = currentService.getAxisConfiguration().getModule(this.name);
+ PolicyInclude policyInclude = currentService.getPolicyInclude();
+ if (policyInclude != null) {
+ try {
+ Policy currentPolicy = policyInclude.getPolicy();
+ if (currentPolicy == null) {
+ currentPolicy = policyInclude.getEffectivePolicy();
+ if (currentPolicy == null) {
+ policyInclude = module.getPolicyInclude();
+ if (policyInclude != null) {
+ currentPolicy = policyInclude.getEffectivePolicy();
+ }
+ }
+ }
+ throttle = ThrottlePolicyProcessor.processPolicy(currentPolicy);
+
+ }
+ catch (ThrottleException e) {
+
+ log.error("Error was ocuured when engaging throttle module for the service :" +
+ currentService.getName() + e.getMessage());
+ log.info("Throttling will occur using default module policy");
+ throttle = defaultThrottle;
+ }
+ if (throttle != null) {
+ Map throttles = (Map) configctx.getProperty(ThrottleConstants.THROTTLES_MAP);
+ if (throttles == null) {
+ throttles = new HashMap();
+ configctx.setProperty(ThrottleConstants.THROTTLES_MAP, throttles);
+ }
+ if (!throttles.containsKey(currentService.getName())) {
+ throttles.put(currentService.getName(), throttle);
+ }
+ }
+ }
+ } else if (axisDescription instanceof AxisOperation) {
+
+ Throttle throttle;
+ AxisOperation currentOperation = ((AxisOperation) axisDescription);
+ currentServiceName = ((AxisService) currentOperation.getParent()).getName();
+ AxisModule module = currentOperation.getAxisConfiguration().getModule(this.name);
+ PolicyInclude policyInclude = currentOperation.getPolicyInclude();
+ if (policyInclude != null) {
+ try {
+ Policy currentPolicy = policyInclude.getPolicy();
+ if (currentPolicy == null) {
+ currentPolicy = policyInclude.getEffectivePolicy();
+ if (currentPolicy == null) {
+ policyInclude = module.getPolicyInclude();
+ if (policyInclude != null) {
+ currentPolicy = policyInclude.getEffectivePolicy();
+ }
+ }
+ }
+ throttle = ThrottlePolicyProcessor.processPolicy(currentPolicy);
+ }
+
+ catch (ThrottleException e) {
+ log.error("Error was ocuured when engaging th