com.danielsomerfield.cvecheck.DefaultScanningEngineSourceUpdate.groovy Maven / Gradle / Ivy
package com.danielsomerfield.cvecheck
import com.danielsomerfield.cvecheck.owaspdependencycheck.ScanningEngine
import com.danielsomerfield.util.Maybe
import com.danielsomerfield.util.TimeSource
import org.slf4j.Logger
import org.slf4j.LoggerFactory
class DefaultScanningEngineSourceUpdate implements ScanningEngineSourceUpdate {
private Configuration configuration;
private RuntimeStore runtimeStore;
private TimeSource timeSource;
private final Logger logger = LoggerFactory.getLogger(this.class)
public DefaultScanningEngineSourceUpdate(
final Configuration configuration, final RuntimeStore runtimeStore, TimeSource timeSource) {
this.configuration = configuration
this.runtimeStore = runtimeStore
this.timeSource = timeSource
}
@Override
public void run(ScanningEngine engine) {
if (this.configuration.refreshAfter() <= 0 || refreshTimePast()) {
logger.info("Updating CVE contents.")
engine.updateSource()
runtimeStore.refreshed()
} else {
logger.info("Skipping CVE update.")
}
}
public boolean refreshTimePast(){
Maybe lastRefreshed = runtimeStore.lastRefreshed()
long currentTime = timeSource.currentTimeInMillis()
long refreshAfterInMillis = configuration.refreshAfter() * 1000;
return !lastRefreshed.exists() || (currentTime - lastRefreshed.value > refreshAfterInMillis)
}
}