Merge pull request #774 from hapifhir/dotasek-fix-baseworkercontext-null-debug-log

Fix null exception for debug messages in BaseWorkerContext
This commit is contained in:
dotasek 2022-03-29 16:34:16 -04:00 committed by GitHub
commit 57b554c6b6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 69 additions and 38 deletions

View File

@ -53,9 +53,7 @@ import org.hl7.fhir.exceptions.FHIRException;
import org.hl7.fhir.exceptions.NoTerminologyServiceException;
import org.hl7.fhir.exceptions.TerminologyServiceException;
import org.hl7.fhir.r5.conformance.ProfileUtilities;
import org.hl7.fhir.r5.context.BaseWorkerContext.ResourceProxy;
import org.hl7.fhir.r5.context.CanonicalResourceManager.CanonicalResourceProxy;
import org.hl7.fhir.r5.context.IWorkerContext.PackageVersion;
import org.hl7.fhir.r5.context.IWorkerContext.ILoggingService.LogCategory;
import org.hl7.fhir.r5.context.TerminologyCache.CacheToken;
import org.hl7.fhir.r5.model.BooleanType;
@ -128,7 +126,7 @@ import org.hl7.fhir.utilities.validation.ValidationOptions.ValueSetMode;
import com.google.gson.JsonObject;
import ca.uhn.fhir.model.valueset.BundleEntrySearchModeEnum;
import javax.annotation.Nonnull;
public abstract class BaseWorkerContext extends I18nBase implements IWorkerContext{
@ -234,7 +232,7 @@ public abstract class BaseWorkerContext extends I18nBase implements IWorkerConte
private boolean canRunWithoutTerminology;
protected boolean noTerminologyServer;
private int expandCodesLimit = 1000;
protected ILoggingService logger;
protected ILoggingService logger = new SystemOutLoggingService();
protected Parameters expParameters;
private TranslationServices translator = new NullTranslator();
@ -244,7 +242,7 @@ public abstract class BaseWorkerContext extends I18nBase implements IWorkerConte
private boolean tlogging = true;
private ICanonicalResourceLocator locator;
protected String userAgent;
protected BaseWorkerContext() throws FileNotFoundException, IOException, FHIRException {
setValidationMessageLanguage(getLocale());
clock = new TimeTracker();
@ -399,7 +397,7 @@ public abstract class BaseWorkerContext extends I18nBase implements IWorkerConte
CanonicalResource m = (CanonicalResource) r;
String url = m.getUrl();
if (!allowLoadingDuplicates && hasResource(r.getClass(), url)) {
// spcial workaround for known problems with existing packages
// special workaround for known problems with existing packages
if (Utilities.existsInList(url, "http://hl7.org/fhir/SearchParameter/example")) {
return;
}
@ -597,19 +595,19 @@ public abstract class BaseWorkerContext extends I18nBase implements IWorkerConte
}
if (txcaps == null) {
try {
log("Terminology server: Check for supported code systems for "+system);
logger.logMessage("Terminology server: Check for supported code systems for "+system);
final TerminologyCapabilities capabilityStatement = txCache.hasTerminologyCapabilities() ? txCache.getTerminologyCapabilities() : txClient.getTerminologyCapabilities();
txCache.cacheTerminologyCapabilities(capabilityStatement);
setTxCaps(capabilityStatement);
} catch (Exception e) {
if (canRunWithoutTerminology) {
noTerminologyServer = true;
log("==============!! Running without terminology server !! ==============");
logger.logMessage("==============!! Running without terminology server !! ==============");
if (txClient!=null) {
log("txServer = "+txClient.getAddress());
log("Error = "+e.getMessage()+"");
logger.logMessage("txServer = "+txClient.getAddress());
logger.logMessage("Error = "+e.getMessage()+"");
}
log("=====================================================================");
logger.logMessage("=====================================================================");
return false;
} else {
e.printStackTrace();
@ -625,22 +623,13 @@ public abstract class BaseWorkerContext extends I18nBase implements IWorkerConte
}
}
private void log(String message) {
if (logger != null) {
logger.logMessage(message);
} else {
System.out.println(message);
}
}
protected void tlog(String msg) {
protected void txLog(String msg) {
if (tlogging ) {
if (logger != null) {
logger.logDebugMessage(LogCategory.TX, msg);
} else {
System.out.println("-tx: "+msg);
}
}
}
@ -692,7 +681,7 @@ public abstract class BaseWorkerContext extends I18nBase implements IWorkerConte
Map<String, String> params = new HashMap<String, String>();
params.put("_limit", Integer.toString(expandCodesLimit ));
params.put("_incomplete", "true");
tlog("$expand on "+txCache.summary(vs));
txLog("$expand on "+txCache.summary(vs));
try {
ValueSet result = txClient.expandValueset(vs, p, params);
res = new ValueSetExpansionOutcome(result).setTxLink(txLog.getLastId());
@ -786,7 +775,7 @@ public abstract class BaseWorkerContext extends I18nBase implements IWorkerConte
Map<String, String> params = new HashMap<String, String>();
params.put("_limit", Integer.toString(expandCodesLimit ));
params.put("_incomplete", "true");
tlog("$expand on "+txCache.summary(vs));
txLog("$expand on "+txCache.summary(vs));
try {
ValueSet result = txClient.expandValueset(vs, p, params);
if (!result.hasUrl()) {
@ -895,7 +884,7 @@ public abstract class BaseWorkerContext extends I18nBase implements IWorkerConte
}
}
if (batch.getEntry().size() > 0) {
tlog("$batch validate for "+batch.getEntry().size()+" codes on systems "+systems.toString());
txLog("$batch validate for "+batch.getEntry().size()+" codes on systems "+systems.toString());
if (txClient == null) {
throw new FHIRException(formatMessage(I18nConstants.ATTEMPT_TO_USE_TERMINOLOGY_SERVER_WHEN_NO_TERMINOLOGY_SERVER_IS_AVAILABLE));
}
@ -983,9 +972,9 @@ public abstract class BaseWorkerContext extends I18nBase implements IWorkerConte
}
String csumm = txCache != null ? txCache.summary(code) : null;
if (txCache != null) {
tlog("$validate "+csumm+" for "+ txCache.summary(vs));
txLog("$validate "+csumm+" for "+ txCache.summary(vs));
} else {
tlog("$validate "+csumm+" before cache exists");
txLog("$validate "+csumm+" before cache exists");
}
try {
Parameters pIn = constructParameters(options, code);
@ -1107,7 +1096,7 @@ public abstract class BaseWorkerContext extends I18nBase implements IWorkerConte
if (noTerminologyServer) {
return new ValidationResult(IssueSeverity.ERROR, "Error validating code: running without terminology services", TerminologyServiceErrorClass.NOSERVICE);
}
tlog("$validate "+txCache.summary(code)+" for "+ txCache.summary(vs));
txLog("$validate "+txCache.summary(code)+" for "+ txCache.summary(vs));
try {
Parameters pIn = constructParameters(options, code);
res = validateOnServer(vs, pIn, options);
@ -1291,7 +1280,7 @@ public abstract class BaseWorkerContext extends I18nBase implements IWorkerConte
this.canRunWithoutTerminology = canRunWithoutTerminology;
}
public void setLogger(ILoggingService logger) {
public void setLogger(@Nonnull ILoggingService logger) {
this.logger = logger;
}

View File

@ -77,6 +77,8 @@ import org.hl7.fhir.utilities.validation.ValidationOptions;
import com.google.gson.JsonSyntaxException;
import javax.annotation.Nonnull;
/**
* This is the standard interface used for access to underlying FHIR
@ -802,7 +804,7 @@ public interface IWorkerContext {
public void logDebugMessage(LogCategory category, String message); // verbose; only when debugging
}
public void setLogger(ILoggingService logger);
public void setLogger(@Nonnull ILoggingService logger);
public ILoggingService getLogger();
public boolean isNoTerminologyServer();

View File

@ -201,6 +201,9 @@ public class SimpleWorkerContext extends BaseWorkerContext implements IWorkerCon
@With
private final boolean allowLoadingDuplicates;
@With
private final IWorkerContext.ILoggingService loggingService;
public SimpleWorkerContextBuilder() {
cacheTerminologyClientErrors = false;
alwaysUseTerminologyServer = false;
@ -209,6 +212,7 @@ public class SimpleWorkerContext extends BaseWorkerContext implements IWorkerCon
locale = null;
userAgent = null;
allowLoadingDuplicates = false;
loggingService = new SystemOutLoggingService();
}
private SimpleWorkerContext getSimpleWorkerContextInstance() throws IOException {
@ -227,6 +231,7 @@ public class SimpleWorkerContext extends BaseWorkerContext implements IWorkerCon
private SimpleWorkerContext build(SimpleWorkerContext context) throws IOException {
context.initTS(terminologyCachePath);
context.setUserAgent(userAgent);
context.setLogger(loggingService);
return context;
}
@ -314,7 +319,7 @@ public class SimpleWorkerContext extends BaseWorkerContext implements IWorkerCon
public String connectToTSServer(TerminologyClient client, String log) {
try {
tlog("Connect to "+client.getAddress());
txLog("Connect to "+client.getAddress());
txClient = client;
if (log != null && log.endsWith(".txt")) {
txLog = new TextClientLogger(log);

View File

@ -0,0 +1,25 @@
package org.hl7.fhir.r5.context;
import lombok.RequiredArgsConstructor;
@RequiredArgsConstructor
public class SystemOutLoggingService implements IWorkerContext.ILoggingService {
private final boolean debug;
public SystemOutLoggingService() {
this(false);
}
@Override
public void logMessage(String message) {
System.out.println(message);
}
@Override
public void logDebugMessage(LogCategory category, String message) {
if (debug) {
System.out.println(" -" + category.name().toLowerCase() + ": " + message);
}
}
}

View File

@ -12,9 +12,11 @@ import org.hl7.fhir.convertors.txClient.TerminologyClientFactory;
import org.hl7.fhir.exceptions.DefinitionException;
import org.hl7.fhir.exceptions.FHIRException;
import org.hl7.fhir.r5.conformance.ProfileUtilities;
import org.hl7.fhir.r5.context.IWorkerContext;
import org.hl7.fhir.r5.context.IWorkerContext.ICanonicalResourceLocator;
import org.hl7.fhir.r5.context.IWorkerContext.PackageVersion;
import org.hl7.fhir.r5.context.SimpleWorkerContext;
import org.hl7.fhir.r5.context.SystemOutLoggingService;
import org.hl7.fhir.r5.elementmodel.Element;
import org.hl7.fhir.r5.elementmodel.Manager;
import org.hl7.fhir.r5.elementmodel.Manager.FhirFormat;
@ -207,6 +209,10 @@ public class ValidationEngine implements IValidatorResourceFetcher, IValidationP
@With
private final boolean canRunWithoutTerminologyServer;
@With
private final IWorkerContext.ILoggingService loggingService;
public ValidationEngineBuilder() {
terminologyCachePath = null;
userAgent = null;
@ -216,9 +222,10 @@ public class ValidationEngine implements IValidatorResourceFetcher, IValidationP
txVersion = null;
timeTracker = null;
canRunWithoutTerminologyServer = false;
loggingService = new SystemOutLoggingService();
}
public ValidationEngineBuilder(String terminologyCachePath, String userAgent, String version, String txServer, String txLog, FhirPublication txVersion, TimeTracker timeTracker, boolean canRunWithoutTerminologyServer) {
public ValidationEngineBuilder(String terminologyCachePath, String userAgent, String version, String txServer, String txLog, FhirPublication txVersion, TimeTracker timeTracker, boolean canRunWithoutTerminologyServer, IWorkerContext.ILoggingService loggingService) {
this.terminologyCachePath = terminologyCachePath;
this.userAgent = userAgent;
this.version = version;
@ -227,15 +234,16 @@ public class ValidationEngine implements IValidatorResourceFetcher, IValidationP
this.txVersion = txVersion;
this.timeTracker = timeTracker;
this.canRunWithoutTerminologyServer = canRunWithoutTerminologyServer;
this.loggingService = loggingService;
}
public ValidationEngineBuilder withTxServer(String txServer, String txLog, FhirPublication txVersion) {
return new ValidationEngineBuilder(terminologyCachePath, userAgent, version, txServer, txLog, txVersion,timeTracker, canRunWithoutTerminologyServer);
return new ValidationEngineBuilder(terminologyCachePath, userAgent, version, txServer, txLog, txVersion,timeTracker, canRunWithoutTerminologyServer, loggingService);
}
public ValidationEngine fromNothing() throws IOException {
ValidationEngine engine = new ValidationEngine();
SimpleWorkerContext.SimpleWorkerContextBuilder contextBuilder = new SimpleWorkerContext.SimpleWorkerContextBuilder();
SimpleWorkerContext.SimpleWorkerContextBuilder contextBuilder = new SimpleWorkerContext.SimpleWorkerContextBuilder().withLoggingService(loggingService);
if (terminologyCachePath != null)
contextBuilder = contextBuilder.withTerminologyCachePath(terminologyCachePath);
engine.setContext(contextBuilder.build());
@ -246,7 +254,7 @@ public class ValidationEngine implements IValidatorResourceFetcher, IValidationP
public ValidationEngine fromSource(String src) throws IOException, URISyntaxException {
ValidationEngine engine = new ValidationEngine();
engine.loadCoreDefinitions(src, false, terminologyCachePath, userAgent, timeTracker);
engine.loadCoreDefinitions(src, false, terminologyCachePath, userAgent, timeTracker, loggingService);
engine.getContext().setCanRunWithoutTerminology(canRunWithoutTerminologyServer);
if (txServer != null) {
@ -258,11 +266,11 @@ public class ValidationEngine implements IValidatorResourceFetcher, IValidationP
}
}
private void loadCoreDefinitions(String src, boolean recursive, String terminologyCachePath, String userAgent, TimeTracker tt) throws FHIRException, IOException {
private void loadCoreDefinitions(String src, boolean recursive, String terminologyCachePath, String userAgent, TimeTracker tt, IWorkerContext.ILoggingService loggingService) throws FHIRException, IOException {
NpmPackage npm = getPcm().loadPackage(src, null);
if (npm != null) {
version = npm.fhirVersion();
SimpleWorkerContext.SimpleWorkerContextBuilder contextBuilder = new SimpleWorkerContext.SimpleWorkerContextBuilder();
SimpleWorkerContext.SimpleWorkerContextBuilder contextBuilder = new SimpleWorkerContext.SimpleWorkerContextBuilder().withLoggingService(loggingService);
if (terminologyCachePath != null)
contextBuilder = contextBuilder.withTerminologyCachePath(terminologyCachePath);
if (userAgent != null) {

View File

@ -2,6 +2,7 @@ package org.hl7.fhir.validation.cli.services;
import org.hl7.fhir.exceptions.FHIRException;
import org.hl7.fhir.r5.context.SimpleWorkerContext;
import org.hl7.fhir.r5.context.SystemOutLoggingService;
import org.hl7.fhir.r5.context.TerminologyCache;
import org.hl7.fhir.r5.elementmodel.Manager;
import org.hl7.fhir.r5.elementmodel.Manager.FhirFormat;
@ -329,6 +330,7 @@ public class ValidationService {
String txver = validator.setTerminologyServer(cliContext.getTxServer(), cliContext.getTxLog(), ver);
System.out.println(" - Version " + txver + " (" + tt.milestone() + ")");
validator.setDebug(cliContext.isDoDebug());
validator.getContext().setLogger(new SystemOutLoggingService(cliContext.isDoDebug()));
for (String src : cliContext.getIgs()) {
igLoader.loadIg(validator.getIgs(), validator.getBinaries(), src, cliContext.isRecursive());
}