Skip to content

Commit d5318d2

Browse files
shusheerUbuntu
authored andcommitted
Refine binary storage bean wiring
1 parent 6f18c79 commit d5318d2

File tree

2 files changed

+81
-35
lines changed

2 files changed

+81
-35
lines changed

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

Lines changed: 38 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.hl7.fhir.r4.model.Bundle.BundleType;
2121
import org.slf4j.Logger;
2222
import org.slf4j.LoggerFactory;
23+
import org.springframework.beans.factory.ObjectProvider;
2324
import org.springframework.boot.env.YamlPropertySourceLoader;
2425
import org.springframework.context.annotation.*;
2526
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
@@ -344,38 +345,49 @@ public HibernatePropertiesProvider jpaStarterDialectProvider(
344345
}
345346

346347
@Lazy
348+
@Primary
347349
@Bean
348-
public IBinaryStorageSvc binaryStorageSvc(AppProperties appProperties) {
349-
IBinaryStorageSvc binaryStorageSvc;
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+
}
350359

351-
Integer maxBinarySize = appProperties.getMax_binary_size();
352-
Integer inlineResourceThreshold = resolveInlineResourceThreshold(appProperties);
360+
@Lazy
361+
@Bean
362+
public FilesystemBinaryStorageSvcImpl filesystemBinaryStorageSvc(AppProperties appProperties) {
363+
String baseDirectory = appProperties.getBinary_storage_filesystem_base_directory();
364+
Assert.hasText(
365+
baseDirectory,
366+
"binary_storage_filesystem_base_directory must be provided when binary_storage_mode=FILESYSTEM");
353367

354-
if (appProperties.getBinary_storage_mode() == AppProperties.BinaryStorageMode.FILESYSTEM) {
355-
String baseDirectory = appProperties.getBinary_storage_filesystem_base_directory();
356-
Assert.hasText(
357-
baseDirectory,
358-
"binary_storage_filesystem_base_directory must be provided when binary_storage_mode=FILESYSTEM");
359-
360-
FilesystemBinaryStorageSvcImpl filesystemSvc = new FilesystemBinaryStorageSvcImpl(baseDirectory);
361-
int minimumBinarySize =
362-
inlineResourceThreshold == null ? DEFAULT_FILESYSTEM_INLINE_THRESHOLD : inlineResourceThreshold;
363-
filesystemSvc.setMinimumBinarySize(minimumBinarySize);
364-
365-
if (maxBinarySize != null) {
366-
filesystemSvc.setMaximumBinarySize(maxBinarySize.longValue());
367-
}
368+
FilesystemBinaryStorageSvcImpl filesystemSvc = new FilesystemBinaryStorageSvcImpl(baseDirectory);
369+
Integer inlineResourceThreshold = resolveInlineResourceThreshold(appProperties);
370+
int minimumBinarySize =
371+
inlineResourceThreshold == null ? DEFAULT_FILESYSTEM_INLINE_THRESHOLD : inlineResourceThreshold;
372+
filesystemSvc.setMinimumBinarySize(minimumBinarySize);
368373

369-
binaryStorageSvc = filesystemSvc;
370-
} else {
371-
DatabaseBinaryContentStorageSvcImpl databaseSvc = new DatabaseBinaryContentStorageSvcImpl();
372-
if (maxBinarySize != null) {
373-
databaseSvc.setMaximumBinarySize(maxBinarySize.longValue());
374-
}
375-
binaryStorageSvc = databaseSvc;
374+
Integer maxBinarySize = appProperties.getMax_binary_size();
375+
if (maxBinarySize != null) {
376+
filesystemSvc.setMaximumBinarySize(maxBinarySize.longValue());
376377
}
377378

378-
return binaryStorageSvc;
379+
return filesystemSvc;
380+
}
381+
382+
@Lazy
383+
@Bean
384+
public DatabaseBinaryContentStorageSvcImpl databaseBinaryStorageSvc(AppProperties appProperties) {
385+
DatabaseBinaryContentStorageSvcImpl databaseSvc = new DatabaseBinaryContentStorageSvcImpl();
386+
Integer maxBinarySize = appProperties.getMax_binary_size();
387+
if (maxBinarySize != null) {
388+
databaseSvc.setMaximumBinarySize(maxBinarySize.longValue());
389+
}
390+
return databaseSvc;
379391
}
380392

381393
private Integer resolveInlineResourceThreshold(AppProperties appProperties) {

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

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
package ca.uhn.fhir.jpa.starter.common;
22

3-
import ca.uhn.fhir.jpa.binary.api.IBinaryStorageSvc;
43
import ca.uhn.fhir.jpa.binstore.DatabaseBinaryContentStorageSvcImpl;
54
import ca.uhn.fhir.jpa.binstore.FilesystemBinaryStorageSvcImpl;
65
import ca.uhn.fhir.jpa.starter.AppProperties;
76
import org.junit.jupiter.api.Test;
87
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;
1213

1314
import static org.assertj.core.api.Assertions.assertThat;
1415
import static org.assertj.core.api.Assertions.assertThatThrownBy;
@@ -26,7 +27,7 @@ private FhirServerConfigCommon newConfig() {
2627
void defaultsToDatabaseImplementation() {
2728
AppProperties props = new AppProperties();
2829

29-
IBinaryStorageSvc svc = newConfig().binaryStorageSvc(props);
30+
Object svc = binaryStorageSvc(props);
3031

3132
assertThat(svc).isInstanceOf(DatabaseBinaryContentStorageSvcImpl.class);
3233
}
@@ -39,8 +40,7 @@ void filesystemModeUsesDefaultMinimumWhenUnspecified() throws Exception {
3940
Files.createDirectories(baseDir);
4041
props.setBinary_storage_filesystem_base_directory(baseDir.toString());
4142

42-
FilesystemBinaryStorageSvcImpl svc =
43-
(FilesystemBinaryStorageSvcImpl) newConfig().binaryStorageSvc(props);
43+
FilesystemBinaryStorageSvcImpl svc = filesystemBinaryStorageSvc(props);
4444

4545
assertThat(svc.getMinimumBinarySize()).isEqualTo(102_400);
4646
}
@@ -54,8 +54,7 @@ void filesystemModeHonoursExplicitMinimum() throws Exception {
5454
Files.createDirectories(baseDir);
5555
props.setBinary_storage_filesystem_base_directory(baseDir.toString());
5656

57-
FilesystemBinaryStorageSvcImpl svc =
58-
(FilesystemBinaryStorageSvcImpl) newConfig().binaryStorageSvc(props);
57+
FilesystemBinaryStorageSvcImpl svc = filesystemBinaryStorageSvc(props);
5958

6059
assertThat(svc.getMinimumBinarySize()).isEqualTo(4096);
6160
}
@@ -69,8 +68,7 @@ void filesystemModeSupportsZeroMinimumWhenExplicit() throws Exception {
6968
Files.createDirectories(baseDir);
7069
props.setBinary_storage_filesystem_base_directory(baseDir.toString());
7170

72-
FilesystemBinaryStorageSvcImpl svc =
73-
(FilesystemBinaryStorageSvcImpl) newConfig().binaryStorageSvc(props);
71+
FilesystemBinaryStorageSvcImpl svc = filesystemBinaryStorageSvc(props);
7472

7573
assertThat(svc.getMinimumBinarySize()).isZero();
7674
}
@@ -80,8 +78,44 @@ void filesystemModeRequiresBaseDirectory() {
8078
AppProperties props = new AppProperties();
8179
props.setBinary_storage_mode(AppProperties.BinaryStorageMode.FILESYSTEM);
8280

83-
assertThatThrownBy(() -> newConfig().binaryStorageSvc(props))
81+
assertThatThrownBy(() -> filesystemBinaryStorageSvc(props))
8482
.isInstanceOf(IllegalArgumentException.class)
8583
.hasMessageContaining("binary_storage_filesystem_base_directory");
8684
}
85+
86+
private Object binaryStorageSvc(AppProperties props) {
87+
FhirServerConfigCommon config = newConfig();
88+
return config.binaryStorageSvc(
89+
props,
90+
provider(() -> config.databaseBinaryStorageSvc(props)),
91+
provider(() -> config.filesystemBinaryStorageSvc(props)));
92+
}
93+
94+
private FilesystemBinaryStorageSvcImpl filesystemBinaryStorageSvc(AppProperties props) {
95+
return (FilesystemBinaryStorageSvcImpl) binaryStorageSvc(props);
96+
}
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+
}
87121
}

0 commit comments

Comments
 (0)