package org.w3c.jigsaw.filters ;
import java.io.* ;
import java.util.Hashtable ;
import org.w3c.tools.resources.*;
import org.w3c.www.http.*;
import org.w3c.jigsaw.http.* ;
import org.w3c.jigsaw.resources.* ;
public class AccessLimitFilter extends ResourceFilter {
protected static int ATTR_LIMIT = -1 ;
protected static int ATTR_TIMEOUT = -1 ;
static {
Attribute a = null ;
Class cls = null ;
try {
cls = Class.forName("org.w3c.jigsaw.filters.AccessLimitFilter");
} catch (Exception ex) {
ex.printStackTrace() ;
System.exit(1) ;
}
a = new IntegerAttribute("limit"
, new Integer(1)
, Attribute.EDITABLE) ;
ATTR_LIMIT = AttributeRegistry.registerAttribute(cls, a) ;
a = new IntegerAttribute("timeout"
, new Integer(60000)
, Attribute.EDITABLE) ;
ATTR_TIMEOUT = AttributeRegistry.registerAttribute(cls, a) ;
}
protected int count = 0 ;
public int getLimit() {
return getInt(ATTR_LIMIT, 1) ;
}
public int getTimeout() {
return getInt(ATTR_TIMEOUT, -1);
}
@param request
public synchronized
ReplyInterface ingoingFilter (RequestInterface req)
throws HTTPException
{
Request request = (Request) req;
int limit = getLimit() ;
int timeout = getTimeout() ;
if ( limit < 0 )
return null;
while ( count >= limit ) {
if ( timeout > 0 ) {
try {
wait((long) timeout, 0) ;
} catch (InterruptedException ex) {
}
if ( count >= limit ) {
String msg = "Simultaneous number of access to this page "
+ "is limited to " + limit + " you was not able to "
+ "get in." ;
Reply error = request.makeReply(HTTP.SERVICE_UNAVAILABLE);
error.setContent (msg) ;
throw new HTTPException (error) ;
}
} else {
try {
wait() ;
} catch (InterruptedException ex) {
}
}
}
count++ ;
return null;
}
@param request@param reply
public synchronized
ReplyInterface outgoingFilter (RequestInterface req,
ReplyInterface rep)
throws HTTPException
{
count-- ;
notifyAll() ;
return null ;
}
}