hapifhir / hapi-fhir

🔥 HAPI FHIR - Java API for HL7 FHIR Clients and Servers
http://hapifhir.io
Apache License 2.0
1.96k stars 1.3k forks source link

Creating HapiWorkerContext crashes: dependency missing: com.github.benmanes.caffeine.cache.Caffeine #2444

Open jingtang10 opened 3 years ago

jingtang10 commented 3 years ago

Describe the bug

Caused by: java.lang.ClassNotFoundException: com.github.benmanes.caffeine.cache.Caffeine

To Reproduce Steps to reproduce the behavior:

Create a new java project using gradle.

Add dependency to hapi structures lib

    implementation 'ca.uhn.hapi.fhir:hapi-fhir-structures-r4:5.3.0'

Create a main class that does the following:

package com.example;

import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.context.support.DefaultProfileValidationSupport;
import org.hl7.fhir.r4.utils.FHIRPathEngine;
import org.hl7.fhir.r4.hapi.ctx.HapiWorkerContext;

public class Main {
  public static void main (String[] args) {
    FhirContext context = FhirContext.forR4();
    DefaultProfileValidationSupport validationSupport = new DefaultProfileValidationSupport(context);
    HapiWorkerContext workerContext = new HapiWorkerContext(context, validationSupport);
    FHIRPathEngine fhirPathEngine = new FHIRPathEngine(workerContext);
  }
}

Expected behavior It should run.

Actual behavior

Caused by: java.lang.ClassNotFoundException: com.github.benmanes.caffeine.cache.Caffeine

Environment (please complete the following information):

Additional info This can be resolved by adding the following explicit dependency to my project:

implementation 'com.github.ben-manes.caffeine:caffeine:2.9.0'

but I shouldn't need to do this. Is this a problem with the pom.xml file?

vitorpamplona commented 1 year ago

Caffeine 3.0+ does not plan to support Android. Caffeine 3.0 already requires a java.lang.System.getLogger from the JEP-264: Platform Logging API to redirect logs to slf4j. Unfortunately, Android has not provided a System.getLogger interface yet. This means that every use of 3.0+ on Android will simply crash on runtime.

Maybe the right way forward is to build a ServiceLoader for a caching service and provide both Caffeine and Guava implementations (maybe LruCache as well?). Then migrate the HapiWorkerContext and others to the new service loader.