Skip to content
This repository has been archived by the owner on Apr 8, 2019. It is now read-only.

Commit

Permalink
GTNPORTAL-2961: Token Service: Configurable the delay time to run cle…
Browse files Browse the repository at this point in the history
…ar expired token

Conflicts:

	component/web/security/src/test/resources/conf/tokenservice-configuration.xml
  • Loading branch information
haint authored and trongtt committed May 3, 2013
1 parent 79c9ed5 commit b998162
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 17 deletions.
Expand Up @@ -27,6 +27,7 @@

import org.exoplatform.container.PortalContainer;
import org.exoplatform.container.xml.InitParams;
import org.exoplatform.container.xml.ValueParam;
import org.exoplatform.management.annotations.Impact;
import org.exoplatform.management.annotations.ImpactType;
import org.exoplatform.management.annotations.Managed;
Expand Down Expand Up @@ -61,7 +62,7 @@ public abstract class AbstractTokenService<T extends Token, K> implements Starta

protected static final String SERVICE_CONFIG = "service.configuration";

protected static final int DELAY_TIME = 600;
protected static final String CLEANUP_PERIOD_TIME = "cleanup.period.time";

/**
* See {@link #tokenByteLength}. 8 bytes (64 bits) would be enough, but we want to get padding-less Byte64 representation,
Expand All @@ -80,6 +81,8 @@ public abstract class AbstractTokenService<T extends Token, K> implements Starta

protected long validityMillis;

protected int delay_time = 600;

private ScheduledExecutorService executor;

@SuppressWarnings("unchecked")
Expand All @@ -89,26 +92,32 @@ public AbstractTokenService(InitParams initParams) throws TokenServiceInitializa
long configValue = new Long(params.get(1));
this.validityMillis = TimeoutEnum.valueOf(params.get(2)).toMilisecond(configValue);
this.tokenByteLength = DEFAULT_TOKEN_BYTE_LENGTH;
ValueParam delayParam = initParams.getValueParam(CLEANUP_PERIOD_TIME);
if(delayParam != null) {
delay_time = Integer.parseInt(delayParam.getValue());
}
}

public void start() {

// start a thread, garbage expired cookie token every [DELAY_TIME]
executor = Executors.newSingleThreadScheduledExecutor();
executor.scheduleWithFixedDelay(new Runnable() {
public void run() {
try {
AbstractTokenService.this.cleanExpiredTokens();
} catch (Throwable t) {
log.warn("Failed to clean expired tokens", t);
if(delay_time > 0) {
// start a thread, garbage expired cookie token every [DELAY_TIME]
executor = Executors.newSingleThreadScheduledExecutor();
executor.scheduleWithFixedDelay(new Runnable() {
public void run() {
try {
AbstractTokenService.this.cleanExpiredTokens();
} catch (Throwable t) {
log.warn("Failed to clean expired tokens", t);
}
}
}
}, 0, DELAY_TIME, TimeUnit.SECONDS);

}, 0, delay_time, TimeUnit.SECONDS);
}
}

public void stop() {
executor.shutdown();
if(executor != null) {
executor.shutdown();
}
}

public static <T extends AbstractTokenService<?, ?>> T getInstance(Class<T> classType) {
Expand Down Expand Up @@ -160,7 +169,7 @@ public long getValidityTime() {
@Managed
@ManagedDescription("The expiration daemon period time in seconds")
public long getPeriodTime() {
return DELAY_TIME;
return delay_time;
}

@Managed
Expand Down
Expand Up @@ -44,6 +44,10 @@
<name>gatein.conf.dir</name>
<value>jar:/conf</value>
</value-param>
<value-param>
<name>cleanup.period.time</name>
<value>0</value>
</value-param>
<values-param>
<name>service.configuration</name>
<value>jcr-token</value>
Expand All @@ -63,6 +67,10 @@
<name>gatein.conf.dir</name>
<value>jar:/conf</value>
</value-param>
<value-param>
<name>cleanup.period.time</name>
<value>0</value>
</value-param>
<values-param>
<name>service.configuration</name>
<value>jcr-token</value>
Expand All @@ -86,6 +94,10 @@
<name>gatein.conf.dir</name>
<value>jar:/conf</value>
</value-param>
<value-param>
<name>cleanup.period.time</name>
<value>0</value>
</value-param>
<values-param>
<name>service.configuration</name>
<value>jcr-token</value>
Expand All @@ -101,6 +113,10 @@
<key>org.exoplatform.web.security.security.TransientTokenService</key>
<type>org.exoplatform.web.security.security.TransientTokenService</type>
<init-params>
<value-param>
<name>cleanup.period.time</name>
<value>0</value>
</value-param>
<values-param>
<name>service.configuration</name>
<value>memory-token</value>
Expand Down
Expand Up @@ -137,14 +137,14 @@ public void testCleanExpiredTokens() throws Exception {
tokenIndex.setUserId("root" + i);
service.createToken(tokenIndex, tokenInfo);

assertEquals(service.size(), 2);
assertEquals(2, service.size());
Thread.sleep(1500);
service.cleanExpiredTokens();
/*
* one of the two tokens should have been cleaned at this point, i.e. cca 2.5 seconds after the creation of the first
* one
*/
assertEquals(service.size(), 1);
assertEquals(1, service.size());

clearAllTokens();
}
Expand Down
Expand Up @@ -33,6 +33,10 @@
<component>
<type>org.exoplatform.portal.gadget.core.GadgetTokenInfoService</type>
<init-params>
<value-param>
<name>cleanup.period.time</name>
<value>0</value>
</value-param>
<values-param>
<name>service.configuration</name>
<value>gadget-token</value>
Expand Down

0 comments on commit b998162

Please sign in to comment.