[wsas-java-dev] svn commit r9770 - in
branches/wsas/java/2.1/commons/throttle/modules:
core/src/main/java/org/wso2/throttle
core/src/main/java/org/wso2/throttle/impl/domainbase
core/src/main/java/org/wso2/throttle/impl/ipbase
mar/src/main/java/org/wso2/throttle/module
mar/src/main/java/org/wso2/throttle/module/handler
svn at wso2.org
svn at wso2.org
Wed Nov 14 09:23:51 PST 2007
Author: indika
Date: Wed Nov 14 09:19:38 2007
New Revision: 9770
Modified:
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/ThrottlePolicyProcessor.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/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/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/ThrottleHandler.java
Log:
improve logs .. fixed possible NPE
Modified: 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/AccessController.java (original)
+++ branches/wsas/java/2.1/commons/throttle/modules/core/src/main/java/org/wso2/throttle/AccessController.java Wed Nov 14 09:19:38 2007
@@ -35,8 +35,10 @@
* Log for this class
*/
private static Log log = LogFactory.getLog(AccessController.class.getName());
- private static final String ACCESS_DENIED =
+ private static final String ACCESS_DENIED_TEMPORALLY =
"You cannot access this service since you have exceeded the allocated quota.";
+ private static final String ACCESS_DENIED =
+ "You cannot access this service since I have prohibited permanently.";
/**
* The Object for used to lock in synchronizing
*/
@@ -57,6 +59,7 @@
boolean debugOn = log.isDebugEnabled(); //is debug enable
String type = ThrottleConstants.IP_BASE == callerType ? "IP address" : "domain";
+
ThrottleConfiguration throttleConfigurationBean = throttleContext.getThrottleConfiguration();
if (throttleConfigurationBean == null) {
@@ -110,7 +113,7 @@
if (!caller.canAccess(throttleContext,configuration, currentTime)) {
//if current caller cannot access , then perform cleaning
- log.info(ACCESS_DENIED);
+ log.info(ACCESS_DENIED_TEMPORALLY);
throttleContext.processCleanList(currentTime);
return false;
} else {
Modified: 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/Caller.java (original)
+++ branches/wsas/java/2.1/commons/throttle/modules/core/src/main/java/org/wso2/throttle/Caller.java Wed Nov 14 09:19:38 2007
@@ -98,8 +98,12 @@
*/
public boolean canAccessIfUnitTimeNotOver(CallerConfiguration configurationIPBased, ThrottleContext throttleContext, long currentTime) throws ThrottleException {
boolean canAcess = false;
- if (!(configurationIPBased.getMaximumRequestPerUnitTime() == 0)) {
- if (count <= configurationIPBased.getMaximumRequestPerUnitTime() - 1) {
+ int maxRequest =configurationIPBased.getMaximumRequestPerUnitTime();
+ if (!(maxRequest == 0)) {
+ if (count <= maxRequest - 1) {
+ if (log.isDebugEnabled()) {
+ log.debug("Access allowed :: " + (maxRequest - count) + " of available of " + maxRequest + " connections");
+ }
canAcess = true;
count++;
// can complete access
@@ -108,19 +112,19 @@
//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) {
+ long prohititTime = configurationIPBased.getProhibitTimePeriod();
+ if (prohititTime == 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();
+ this.nextAccessTime = currentTime +prohititTime;
}
if (log.isDebugEnabled()) {
String type = ThrottleConstants.IP_BASE == configurationIPBased.getType() ? "IP address" : "domain";
- log.debug("Maximum Number of requests are reached for caller with " + type + "-" + ID);
+ log.debug("Maximum Number of requests are reached for caller with " + type + " - " + ID);
}
} else {
@@ -128,6 +132,9 @@
// time period has already overed
if (this.nextAccessTime
<= currentTime) {
+ if (log.isDebugEnabled()) {
+ log.debug("Access allowed :: " + (maxRequest) + " of available of " + maxRequest + " connections");
+ }
this.nextAccessTime = 0;
canAcess = true;
count = 1;
@@ -139,7 +146,7 @@
} else {
if (log.isDebugEnabled()) {
String type = ThrottleConstants.IP_BASE == configurationIPBased.getType() ? "IP address" : "domain";
- log.debug("Prohibit period is not yet over for caller with " + type + "- " + ID);
+ log.debug("Prohibit period is not yet over for caller with " + type + " - " + ID);
}
}
}
@@ -177,6 +184,9 @@
// if caller in prohabit session and prohabit period has just overed
if ((this.nextAccessTime == 0) ||
(this.nextAccessTime <= currentTime)) {
+ if (log.isDebugEnabled()) {
+ log.debug("Access allowed :: " + (maxRequest) + " of available of " + maxRequest + " connections");
+ }
this.nextAccessTime = 0;
canAcess = true;
count = 1;// can access the system and this is same as fristAccess
@@ -190,7 +200,7 @@
// if caller in prohabit session and prohabit period has not overed
if (log.isDebugEnabled()) {
String type = ThrottleConstants.IP_BASE == configurationIPBased.getType() ? "IP address" : "domain";
- log.debug("Even unit time has overed , Caller in prohibit state :" + type + " -" + ID);
+ log.debug("Even unit time has overed , Caller in prohibit state :" + type + " - " + ID);
}
}
}
@@ -210,7 +220,7 @@
public boolean canAccess(ThrottleContext throttleContext, CallerConfiguration callerConfiguration, long currentTime) throws ThrottleException {
boolean canAcess = false;
if (callerConfiguration == null) {
- return false;
+ return true;
}
if (callerConfiguration.getMaximumRequestPerUnitTime() < 0 || callerConfiguration.getUnitTimeInMiliSecond() <= 0 || callerConfiguration.getProhibitTimePeriod() < 0) {
throw new ThrottleException("Invalid Throttle Configuration");
Modified: 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/ThrottlePolicyProcessor.java (original)
+++ branches/wsas/java/2.1/commons/throttle/modules/core/src/main/java/org/wso2/throttle/ThrottlePolicyProcessor.java Wed Nov 14 09:19:38 2007
@@ -77,6 +77,9 @@
getPolicy(topLevelXmlPrimtiveAssertion.getValue());
List assertionList = throtlePolicy.getPolicyComponents();
+ if (assertionList == null || (assertionList != null && assertionList.isEmpty())) {
+ handleException("Empty the policy components as ThrottleAssertion's children");
+ }
for (Iterator assertionsIterator = assertionList.iterator(); assertionsIterator.hasNext();)
{
Object topLevelPolicy = assertionsIterator.next();
@@ -186,12 +189,24 @@
if (name.equals(ThrottleConstants.MAXIMUM_COUNT_PARAMETER_NAME)) {
isFoundMaxCount = true;
- configuration.setMaximumRequestPerUnitTime(Integer.parseInt(value.trim()));
-
+ try {
+ configuration.setMaximumRequestPerUnitTime(Integer.parseInt(value.trim()));
+ } catch (NumberFormatException ignored) {
+ log.error("Error occurred - Invalid number for maximum request number ", ignored);
+ if (log.isDebugEnabled()) {
+ log.debug("Access will be fully allowed");
+ }
+ configuration.setAccessState(ThrottleConstants.ACCESS_ALLOWED);
+ }
} else
if (name.equals(ThrottleConstants.UNIT_TIME_PARAMETER_NAME)) {
//TODO need to verify that value is milisecond
- long timeInMiliSec = Long.parseLong(value.trim());
+ long timeInMiliSec = 0;
+ try {
+ timeInMiliSec = Long.parseLong(value.trim());
+ } catch (NumberFormatException ignored) {
+ log.error("Error occurred - Invalid number for unit time ", ignored);
+ }
if (timeInMiliSec == 0) {
handleException("Unit Time cannot find - invalid throttle policy configuration");
}
@@ -200,8 +215,11 @@
} else
if (name.equals(ThrottleConstants.PROHIBIT_TIME_PERIOD_PARAMETER_NAME)) {
- configuration.setProhibitTimePeriod(Long.parseLong(value.trim()));
-
+ try {
+ configuration.setProhibitTimePeriod(Long.parseLong(value.trim()));
+ } catch (NumberFormatException ignored) {
+ log.error("Error occurred - Invalid number for prohibit time ", ignored);
+ }
} else {
handleException("Undefined Policy property for Throttle Policy");
}
@@ -273,7 +291,12 @@
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());
+ int intvalue = 0;
+ try {
+ intvalue = Integer.parseInt(value.trim());
+ } catch (NumberFormatException ignored) {
+ log.error("Error occurred - Invalid number for maximum concurrent access ", ignored);
+ }
if (intvalue > 0) {
throttle.setConcurrentAccessController(new ConcurrentAccessController(intvalue));
}
Modified: 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/domainbase/DomainBaseThrottleContext.java (original)
+++ branches/wsas/java/2.1/commons/throttle/modules/core/src/main/java/org/wso2/throttle/impl/domainbase/DomainBaseThrottleContext.java Wed Nov 14 09:19:38 2007
@@ -64,6 +64,10 @@
* @param throttleConfiguration
*/
public DomainBaseThrottleContext(ThrottleConfiguration throttleConfiguration) {
+ if (throttleConfiguration == null) {
+ throw new InstantiationError("Couldn't create throttle context " +
+ "from null throttle configuration");
+ }
this.keyToTimeStampMap = new HashMap();
this.callersMap = new TreeMap();
this.nextCleanTime = 0;
@@ -100,7 +104,7 @@
for (Iterator iterator = callersWithSameTimeStampList.iterator(); iterator.hasNext();)
{
Caller caller = (Caller) iterator.next();
- if (keyOfConfiguration.equals(throttleConfiguration.getConfigurationKeyOfCaller(caller.getID()))) {
+ if (caller != null && keyOfConfiguration.equals(throttleConfiguration.getConfigurationKeyOfCaller(caller.getID()))) {
return caller;
}
}
@@ -129,11 +133,12 @@
* @param caller
*/
public void addCaller(Caller caller) {
-
+ if(caller != null){
String keyOfConfiguration = (String) throttleConfiguration.getConfigurationKeyOfCaller(caller.getID());
if (keyOfConfiguration != null) {
setCaller(caller, keyOfConfiguration);
}
+ }
}
/**
Modified: 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/IPBaseThrottleConfiguration.java (original)
+++ branches/wsas/java/2.1/commons/throttle/modules/core/src/main/java/org/wso2/throttle/impl/ipbase/IPBaseThrottleConfiguration.java Wed Nov 14 09:19:38 2007
@@ -132,8 +132,12 @@
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();
+ if (ipBaseCallerConfiguration != null) {
+ String secondPart = ipBaseCallerConfiguration.getSecondPartOfIPRange();
+ if (secondPart != null && secondPart.compareTo(ip) > 0) {
+ return ipBaseCallerConfiguration.getFirstPartOfIPRange();
+ }
+ }
}
}
Modified: 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/core/src/main/java/org/wso2/throttle/impl/ipbase/IPBaseThrottleContext.java (original)
+++ branches/wsas/java/2.1/commons/throttle/modules/core/src/main/java/org/wso2/throttle/impl/ipbase/IPBaseThrottleContext.java Wed Nov 14 09:19:38 2007
@@ -100,11 +100,10 @@
for (Iterator iterator = callersWithSameTimeStampList.iterator(); iterator.hasNext();)
{
Caller caller = (Caller) iterator.next();
- if (keyOfConfiguration.equals(throttleConfiguration.getConfigurationKeyOfCaller(caller.getID()))) {
+ if (caller != null && keyOfConfiguration.equals(throttleConfiguration.getConfigurationKeyOfCaller(caller.getID()))) {
return caller;
}
}
-
}
}
}
Modified: 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/ThrottleModule.java (original)
+++ branches/wsas/java/2.1/commons/throttle/modules/mar/src/main/java/org/wso2/throttle/module/ThrottleModule.java Wed Nov 14 09:19:38 2007
@@ -32,6 +32,7 @@
import org.wso2.throttle.ThrottleException;
import org.wso2.throttle.ThrottlePolicyProcessor;
+import javax.xml.namespace.QName;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
@@ -138,41 +139,49 @@
Throttle throttle = null;
AxisOperation currentOperation = ((AxisOperation) axisDescription);
- currentServiceName = ((AxisService) currentOperation.getParent()).getName();
+ AxisService axisService = (AxisService) currentOperation.getParent();
+ if (axisService != null) {
+ currentServiceName = axisService.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();
+ 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();
// }
// }
- }
- if (currentPolicy != null) {
- throttle = ThrottlePolicyProcessor.processPolicy(currentPolicy);
- }
- }
-
- catch (ThrottleException e) {
- log.error("Error was ocuured when engaging throttle module for operation : " +
- currentOperation.getName() + " in the service :" + currentServiceName + 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(currentServiceName + currentOperation.getName())) {
- throttles.put(currentServiceName + currentOperation.getName(), throttle);
+ }
+ if (currentPolicy != null) {
+ throttle = ThrottlePolicyProcessor.processPolicy(currentPolicy);
+ }
+ }
+
+ catch (ThrottleException e) {
+ log.error("Error was ocuured when engaging throttle module for operation : " +
+ currentOperation.getName() + " in the service :" + currentServiceName + 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);
+ }
+ QName opQName = currentOperation.getName();
+ if (opQName != null) {
+ String opName = opQName.getLocalPart();
+ String key = currentServiceName + opName;
+ if (!throttles.containsKey(key)) {
+ throttles.put(key, throttle);
+ }
+ }
}
}
}
@@ -197,19 +206,25 @@
InputStream inputStream = this.getClass().getResourceAsStream("/resources/policy/default_module_policy.xml");
if (inputStream != null) {
defaultPolicy = PolicyEngine.getPolicy(inputStream);
+ } else {
+ throw new AxisFault("Couldn't load the default throttle policy .the module is invalid ");
}
}
private void initDefaultThrottle() throws AxisFault {
- InputStream inputStream = this.getClass().getResourceAsStream("/resources/policy/default_module_policy.xml");
- if (inputStream != null) {
- try {
+ try {
+ if (defaultPolicy != null) {
defaultThrottle = ThrottlePolicyProcessor.processPolicy(defaultPolicy);
- } catch (ThrottleException e) {
- String msg = "Error during processing default throttle policy + system will not works" + e.getMessage();
- log.error(msg);
- throw new AxisFault(msg);
+ if (defaultThrottle == null) {
+ throw new AxisFault("Couldn't create the default throttle .the module is invalid ");
+ }
+ } else {
+ throw new AxisFault("Couldn't find the default throttle policy .the module is invalid ");
}
+ } catch (ThrottleException e) {
+ String msg = "Error during processing default throttle policy + system will not works" + e.getMessage();
+ log.error(msg);
+ throw new AxisFault(msg);
}
}
}
\ No newline at end of file
Modified: 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/java/org/wso2/throttle/module/handler/ThrottleHandler.java (original)
+++ branches/wsas/java/2.1/commons/throttle/modules/mar/src/main/java/org/wso2/throttle/module/handler/ThrottleHandler.java Wed Nov 14 09:19:38 2007
@@ -33,6 +33,7 @@
import org.wso2.throttle.*;
import javax.servlet.http.HttpServletRequest;
+import javax.xml.namespace.QName;
import java.util.Map;
@@ -66,7 +67,7 @@
public Throttle loadThrottle(MessageContext messageContext,
int throttleType) throws AxisFault, ThrottleException {
- Throttle throttle = null;
+ Throttle throttle = null ;
ConfigurationContext configContext = messageContext.getConfigurationContext();
//the Parameter which hold throttle ipbase object
// to get thottles map from the configuration context
@@ -87,11 +88,17 @@
case ThrottleConstants.OPERATION_BASED_THROTTLE: {
AxisOperation axisOperation = messageContext.getAxisOperation();
if (axisOperation != null) {
- String currentServiceName = ((AxisService) axisOperation.getParent()).getName();
- throttle =
- (Throttle) throttles.
- get(currentServiceName +
- axisOperation.getName().getLocalPart());
+ QName opName = axisOperation.getName();
+ if (opName != null) {
+ AxisService service = (AxisService) axisOperation.getParent();
+ if (service != null) {
+ String currentServiceName = service.getName();
+ if (currentServiceName != null) {
+ throttle =
+ (Throttle) throttles.get(currentServiceName + opName.getLocalPart());
+ }
+ }
+ }
} else {
if (log.isDebugEnabled()) {
log.debug("Couldn't find axis operation ");
More information about the Wsas-java-dev
mailing list