diff --git a/src/main/java/org/gcube/accounting/aggregator/RegexRulesAggregator.java b/src/main/java/org/gcube/accounting/aggregator/RegexRulesAggregator.java index d4ae8b6..41926a5 100644 --- a/src/main/java/org/gcube/accounting/aggregator/RegexRulesAggregator.java +++ b/src/main/java/org/gcube/accounting/aggregator/RegexRulesAggregator.java @@ -2,12 +2,14 @@ package org.gcube.accounting.aggregator; import java.util.ArrayList; import java.util.List; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledFuture; +import java.util.concurrent.ThreadFactory; import java.util.concurrent.TimeUnit; import org.gcube.accounting.datamodel.validations.validators.RegexReplace; import org.gcube.accounting.persistence.AccountingPersistenceConfiguration; -import org.gcube.documentstore.persistence.ExecutorUtils; import org.gcube.documentstore.records.DSMapper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -19,6 +21,24 @@ public class RegexRulesAggregator implements Runnable { private static final Logger logger = LoggerFactory.getLogger(RegexRulesAggregator.class); + private static final ScheduledExecutorService REGEX_REDISCOVERY_POOL; + + static { + + REGEX_REDISCOVERY_POOL = Executors.newScheduledThreadPool(50, new ThreadFactory() { + + private int counter = 0; + private static final String prefix = "RegexRediscoveryThread"; + + public Thread newThread(Runnable r) { + return new Thread(r, prefix + "-" + counter++); + } + }); + + } + + + protected TimeUnit timeUnit = TimeUnit.MINUTES; protected long delay = TimeUnit.MINUTES.toMinutes(15); @@ -46,8 +66,10 @@ public class RegexRulesAggregator implements Runnable { readConfiguration(); } - public List getRegexReplacelist() { - return regexReplaceList; + public List getRegexReplaceList() { + synchronized(regexReplaceList) { + return regexReplaceList; + } } public RegexReplace addRegexReplace(String serviceClass, String serviceName, String regex, String replace) { @@ -99,7 +121,7 @@ public class RegexRulesAggregator implements Runnable { public void start() { if(reloadAggregatorRules == null) { - reloadAggregatorRules = ExecutorUtils.scheduler.scheduleAtFixedRate(this, delay, delay, timeUnit); + reloadAggregatorRules = REGEX_REDISCOVERY_POOL.scheduleAtFixedRate(this, delay, delay, timeUnit); } } diff --git a/src/main/java/org/gcube/accounting/datamodel/validations/validators/CalledMethodRegexReplaceValidator.java b/src/main/java/org/gcube/accounting/datamodel/validations/validators/CalledMethodRegexReplaceValidator.java index 9633adc..59c2d8d 100644 --- a/src/main/java/org/gcube/accounting/datamodel/validations/validators/CalledMethodRegexReplaceValidator.java +++ b/src/main/java/org/gcube/accounting/datamodel/validations/validators/CalledMethodRegexReplaceValidator.java @@ -19,21 +19,6 @@ public class CalledMethodRegexReplaceValidator implements FieldAction { private static Logger logger = LoggerFactory.getLogger(CalledMethodRegexReplaceValidator.class); - @Deprecated - public static List getRegexReplacelist() { - return RegexRulesAggregator.getInstance().getRegexReplacelist(); - } - - @Deprecated - public static RegexReplace addRegexReplace(String serviceClass, String serviceName, String regex, String replace) { - return RegexRulesAggregator.getInstance().addRegexReplace(serviceClass, serviceName, regex, replace); - } - - @Deprecated - public static RegexReplace addRegexReplace(RegexReplace regexReplace) { - return RegexRulesAggregator.getInstance().addRegexReplace(regexReplace); - } - /** * {@inheritDoc} */ @@ -64,7 +49,9 @@ public class CalledMethodRegexReplaceValidator implements FieldAction { return value; } - for(RegexReplace regexReplace : getRegexReplacelist()) { + List regexReplaceList = RegexRulesAggregator.getInstance().getRegexReplaceList(); + + for(RegexReplace regexReplace : regexReplaceList) { if(serviceClass.compareTo(regexReplace.getServiceClass())==0 && serviceName.compareTo(regexReplace.getServiceName())==0) { Matcher matcher = regexReplace.regexPattern.matcher(stringValue); if(matcher.matches()) { diff --git a/src/main/java/org/gcube/accounting/persistence/AccountingPersistenceConfiguration.java b/src/main/java/org/gcube/accounting/persistence/AccountingPersistenceConfiguration.java index 6067928..52dc468 100644 --- a/src/main/java/org/gcube/accounting/persistence/AccountingPersistenceConfiguration.java +++ b/src/main/java/org/gcube/accounting/persistence/AccountingPersistenceConfiguration.java @@ -17,12 +17,16 @@ import org.gcube.documentstore.persistence.PersistenceBackendConfiguration; import org.gcube.resources.discovery.client.api.DiscoveryClient; import org.gcube.resources.discovery.client.queries.api.SimpleQuery; import org.gcube.resources.discovery.icclient.ICFactory; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * @author Luca Frosini (ISTI - CNR) */ public class AccountingPersistenceConfiguration extends PersistenceBackendConfiguration { + private static final Logger logger = LoggerFactory.getLogger(AccountingPersistenceConfiguration.class); + protected static final String TARGET_SCOPE = "targetScope"; protected static final String SERVICE_ENDPOINT_CATEGORY = "Accounting"; @@ -38,8 +42,13 @@ public class AccountingPersistenceConfiguration extends PersistenceBackendConfig public AccountingPersistenceConfiguration(Class clz) throws Exception { super(); - ServiceEndpoint serviceEndpoint = getServiceEndpoint(SERVICE_ENDPOINT_CATEGORY, SERVICE_ENDPOINT_NAME, clz); - setValues(serviceEndpoint, clz); + try { + ServiceEndpoint serviceEndpoint = getServiceEndpoint(SERVICE_ENDPOINT_CATEGORY, SERVICE_ENDPOINT_NAME, clz); + setValues(serviceEndpoint, clz); + }catch (Exception e) { + logger.error("Error while instancing {} in context {}", this.getClass().getSimpleName(), + BasicUsageRecord.getContextFromToken(), e); + } } protected ServiceEndpoint getServiceEndpoint(String serviceEndpointCategory, String serviceEndpointName, Class clz){ diff --git a/src/main/java/org/gcube/accounting/persistence/AccountingPersistenceFactory.java b/src/main/java/org/gcube/accounting/persistence/AccountingPersistenceFactory.java index e1ad6eb..a474f2b 100644 --- a/src/main/java/org/gcube/accounting/persistence/AccountingPersistenceFactory.java +++ b/src/main/java/org/gcube/accounting/persistence/AccountingPersistenceFactory.java @@ -63,6 +63,8 @@ public class AccountingPersistenceFactory { //flush all and shutdown connection and thread PersistenceBackendFactory.flushAll(); PersistenceBackendFactory.shutdown(); + + } /** diff --git a/src/test/java/org/gcube/accounting/aggregator/RegexRulesAggregatorTest.java b/src/test/java/org/gcube/accounting/aggregator/RegexRulesAggregatorTest.java index a3187ef..cc25301 100644 --- a/src/test/java/org/gcube/accounting/aggregator/RegexRulesAggregatorTest.java +++ b/src/test/java/org/gcube/accounting/aggregator/RegexRulesAggregatorTest.java @@ -15,7 +15,7 @@ public class RegexRulesAggregatorTest extends ScopedTest { @Test public void test() { RegexRulesAggregator regexRulesAggregator = RegexRulesAggregator.getInstance(); - List list = regexRulesAggregator.getRegexReplacelist(); + List list = regexRulesAggregator.getRegexReplaceList(); for(RegexReplace regexReplace : list) { logger.debug("{} {} {} {}", regexReplace.getServiceClass(), regexReplace.getServiceName(), regexReplace.getRegex(), regexReplace.getReplace()); } diff --git a/src/test/java/org/gcube/accounting/datamodel/usagerecords/ServiceUsageRecordTest.java b/src/test/java/org/gcube/accounting/datamodel/usagerecords/ServiceUsageRecordTest.java index 05d7b3f..77efc0a 100644 --- a/src/test/java/org/gcube/accounting/datamodel/usagerecords/ServiceUsageRecordTest.java +++ b/src/test/java/org/gcube/accounting/datamodel/usagerecords/ServiceUsageRecordTest.java @@ -174,7 +174,7 @@ public class ServiceUsageRecordTest extends ScopedTest { logger.debug("{}", usageRecord); RegexRulesAggregator regexRulesAggregator = RegexRulesAggregator.getInstance(); - List list = regexRulesAggregator.getRegexReplacelist(); + List list = regexRulesAggregator.getRegexReplaceList(); for(RegexReplace regexReplace : list) { logger.debug("{} {} {} {}", regexReplace.getServiceClass(), regexReplace.getServiceName(), regexReplace.getRegex(), regexReplace.getReplace()); }