Skip to content

Commit 9624446

Browse files
author
Ubuntu
committed
Refine binary storage conditional beans
1 parent d5318d2 commit 9624446

File tree

2 files changed

+14
-49
lines changed

2 files changed

+14
-49
lines changed

src/main/java/ca/uhn/fhir/jpa/starter/common/FhirServerConfigCommon.java

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package ca.uhn.fhir.jpa.starter.common;
22

33
import ca.uhn.fhir.jpa.api.config.JpaStorageSettings;
4-
import ca.uhn.fhir.jpa.binary.api.IBinaryStorageSvc;
54
import ca.uhn.fhir.jpa.binstore.DatabaseBinaryContentStorageSvcImpl;
65
import ca.uhn.fhir.jpa.binstore.FilesystemBinaryStorageSvcImpl;
76
import ca.uhn.fhir.jpa.config.HibernatePropertiesProvider;
@@ -20,7 +19,7 @@
2019
import org.hl7.fhir.r4.model.Bundle.BundleType;
2120
import org.slf4j.Logger;
2221
import org.slf4j.LoggerFactory;
23-
import org.springframework.beans.factory.ObjectProvider;
22+
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
2423
import org.springframework.boot.env.YamlPropertySourceLoader;
2524
import org.springframework.context.annotation.*;
2625
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
@@ -344,21 +343,8 @@ public HibernatePropertiesProvider jpaStarterDialectProvider(
344343
return new JpaHibernatePropertiesProvider(myEntityManagerFactory);
345344
}
346345

347-
@Lazy
348-
@Primary
349-
@Bean
350-
public IBinaryStorageSvc binaryStorageSvc(
351-
AppProperties appProperties,
352-
ObjectProvider<DatabaseBinaryContentStorageSvcImpl> databaseBinaryStorageSvcProvider,
353-
ObjectProvider<FilesystemBinaryStorageSvcImpl> filesystemBinaryStorageSvcProvider) {
354-
if (appProperties.getBinary_storage_mode() == AppProperties.BinaryStorageMode.FILESYSTEM) {
355-
return filesystemBinaryStorageSvcProvider.getObject();
356-
}
357-
return databaseBinaryStorageSvcProvider.getObject();
358-
}
359-
360-
@Lazy
361346
@Bean
347+
@ConditionalOnProperty(prefix = "hapi.fhir", name = "binary_storage_mode", havingValue = "FILESYSTEM")
362348
public FilesystemBinaryStorageSvcImpl filesystemBinaryStorageSvc(AppProperties appProperties) {
363349
String baseDirectory = appProperties.getBinary_storage_filesystem_base_directory();
364350
Assert.hasText(
@@ -379,8 +365,12 @@ public FilesystemBinaryStorageSvcImpl filesystemBinaryStorageSvc(AppProperties a
379365
return filesystemSvc;
380366
}
381367

382-
@Lazy
383368
@Bean
369+
@ConditionalOnProperty(
370+
prefix = "hapi.fhir",
371+
name = "binary_storage_mode",
372+
havingValue = "DATABASE",
373+
matchIfMissing = true)
384374
public DatabaseBinaryContentStorageSvcImpl databaseBinaryStorageSvc(AppProperties appProperties) {
385375
DatabaseBinaryContentStorageSvcImpl databaseSvc = new DatabaseBinaryContentStorageSvcImpl();
386376
Integer maxBinarySize = appProperties.getMax_binary_size();

src/test/java/ca/uhn/fhir/jpa/starter/common/FhirServerConfigCommonBinaryStorageTest.java

Lines changed: 7 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@
22

33
import ca.uhn.fhir.jpa.binstore.DatabaseBinaryContentStorageSvcImpl;
44
import ca.uhn.fhir.jpa.binstore.FilesystemBinaryStorageSvcImpl;
5+
import ca.uhn.fhir.jpa.binary.api.IBinaryStorageSvc;
56
import ca.uhn.fhir.jpa.starter.AppProperties;
67
import org.junit.jupiter.api.Test;
78
import org.junit.jupiter.api.io.TempDir;
8-
import org.springframework.beans.factory.ObjectProvider;
99

1010
import java.nio.file.Files;
1111
import java.nio.file.Path;
12-
import java.util.function.Supplier;
1312

1413
import static org.assertj.core.api.Assertions.assertThat;
1514
import static org.assertj.core.api.Assertions.assertThatThrownBy;
@@ -27,7 +26,7 @@ private FhirServerConfigCommon newConfig() {
2726
void defaultsToDatabaseImplementation() {
2827
AppProperties props = new AppProperties();
2928

30-
Object svc = binaryStorageSvc(props);
29+
IBinaryStorageSvc svc = binaryStorageSvc(props);
3130

3231
assertThat(svc).isInstanceOf(DatabaseBinaryContentStorageSvcImpl.class);
3332
}
@@ -83,39 +82,15 @@ void filesystemModeRequiresBaseDirectory() {
8382
.hasMessageContaining("binary_storage_filesystem_base_directory");
8483
}
8584

86-
private Object binaryStorageSvc(AppProperties props) {
85+
private IBinaryStorageSvc binaryStorageSvc(AppProperties props) {
8786
FhirServerConfigCommon config = newConfig();
88-
return config.binaryStorageSvc(
89-
props,
90-
provider(() -> config.databaseBinaryStorageSvc(props)),
91-
provider(() -> config.filesystemBinaryStorageSvc(props)));
87+
if (props.getBinary_storage_mode() == AppProperties.BinaryStorageMode.FILESYSTEM) {
88+
return config.filesystemBinaryStorageSvc(props);
89+
}
90+
return config.databaseBinaryStorageSvc(props);
9291
}
9392

9493
private FilesystemBinaryStorageSvcImpl filesystemBinaryStorageSvc(AppProperties props) {
9594
return (FilesystemBinaryStorageSvcImpl) binaryStorageSvc(props);
9695
}
97-
98-
private static <T> ObjectProvider<T> provider(Supplier<T> supplier) {
99-
return new ObjectProvider<>() {
100-
@Override
101-
public T getObject(Object... args) {
102-
return supplier.get();
103-
}
104-
105-
@Override
106-
public T getObject() {
107-
return supplier.get();
108-
}
109-
110-
@Override
111-
public T getIfAvailable() {
112-
return supplier.get();
113-
}
114-
115-
@Override
116-
public T getIfUnique() {
117-
return supplier.get();
118-
}
119-
};
120-
}
12196
}

0 commit comments

Comments
 (0)