added reset parameters

git-svn-id: http://svn.d4science-ii.research-infrastructures.eu/gcube/trunk/portlets/user/uri-resolver-manager@100816 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Francesco Mangiacrapa 2014-10-21 10:43:00 +00:00
parent 982b70f25a
commit 025ac2c19b
2 changed files with 104 additions and 3 deletions

View File

@ -3,10 +3,11 @@
*/
package org.gcube.portlets.user.uriresolvermanager;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Timer;
import java.util.TimerTask;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.portlets.user.uriresolvermanager.entity.ServiceParameter;
@ -27,12 +28,35 @@ import org.slf4j.LoggerFactory;
public class UriResolverManager {
/**
*
*/
public static int RESET_DELAY = 10*60*1000; //10 MINUTES
public static int RESET_TIME = RESET_DELAY; //10 MINUTES
private UriResolverMapReader uriResolverMapReader;
private Map<String, String> applicationTypes;
private String scope;
private String applicationType;
private RuntimeResourceReader reader;
/**
* A lock to prevent reader = null;
*/
private int usingReader = 0;
public synchronized void lockReader() {
usingReader++;
}
public synchronized void releaseReader() {
usingReader--;
}
public synchronized int countReaders() {
return usingReader;
}
public static final Logger logger = LoggerFactory.getLogger(UriResolverManager.class);
/**
@ -62,7 +86,7 @@ public class UriResolverManager {
}
this.applicationType = applicationType;
this.reloadRuntimeResourceParameter(RESET_DELAY, RESET_TIME);
}
@ -85,6 +109,8 @@ public class UriResolverManager {
try {
lockReader();
if(reader==null){
logger.info("Runtime Resource Reader is null, istancing...");
ScopeProvider.instance.set(this.scope);
@ -104,6 +130,8 @@ public class UriResolverManager {
String baseURI = reader.getServiceBaseURI();
releaseReader();
String params = UrlEncoderUtil.encodeQuery(parameters);
link = baseURI+"?"+params;
logger.info("Created HTTP URI request (link): "+link);
@ -142,4 +170,26 @@ public class UriResolverManager {
public Map<String, String> getCapabilities(){
return this.applicationTypes;
}
/**
*
*/
public void reloadRuntimeResourceParameter(long delay, long period) {
Timer timer = new Timer(true);
timer.schedule(new TimerTask() {
@Override
public void run() {
logger.info("Timer Reset Runtime Resource running..");
int counters = countReaders();
if(counters==0){
logger.info("Reader not locked, resetting");
reader = null;
}else
logger.info("Reader locked, counters is/are:"+counters+", skipping");
}
}, delay, period);
}
}

View File

@ -18,7 +18,7 @@ import org.junit.Test;
*/
public class UriResolverTest {
@Test
// @Test
public void test() {
try {
@ -37,5 +37,56 @@ public class UriResolverTest {
e.printStackTrace();
}
}
/**
* Thread safe
*/
// @Test
public void test2(){
//create thread to print counter value
Thread t = new Thread(new Runnable() {
@Override
public void run() {
while (true) {
try {
ScopeProvider.instance.set("/gcube/devsec/devVRE");
UriResolverManager resolver;
resolver = new UriResolverManager("GIS");
Map<String, String> params = new HashMap<String, String>();
params.put("gis-UUID", "eb1a1b63-f324-47ee-9522-b8f5803e19ec");
params.put("scope", "/gcube/devsec/devVRE");
String shortLink = resolver.getLink(params, true);
System.out.println(shortLink); //true, link is shorted otherwise none
System.out.println("Thread "+Thread.currentThread().getId() +"reading counter is: " + resolver.countReaders());
Thread.sleep(1000);
} catch (InterruptedException ex) {
ex.printStackTrace();
}catch (UriResolverMapException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
t.start();
try {
Thread.sleep(500000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}