1010import com .github .azahnen .dagger .annotations .AutoBind ;
1111import com .google .common .collect .ImmutableList ;
1212import com .google .common .collect .ImmutableMap ;
13- import de .ii .xtraplatform .base .domain .AppConfiguration ;
14- import de .ii .xtraplatform .base .domain .AppContext ;
13+ import de .ii .xtraplatform .base .domain .AppLifeCycle ;
14+ import de .ii .xtraplatform .base .domain .LogContext ;
15+ import de .ii .xtraplatform .store .domain .BlobStore ;
16+ import java .io .IOException ;
1517import java .nio .file .Path ;
16- import java .nio .file .Paths ;
1718import java .util .List ;
1819import java .util .Map ;
20+ import java .util .Objects ;
21+ import java .util .Optional ;
1922import javax .inject .Inject ;
2023import javax .inject .Singleton ;
24+ import org .slf4j .Logger ;
25+ import org .slf4j .LoggerFactory ;
2126
2227@ Singleton
2328@ AutoBind
24- public class ProjLoaderImpl implements ProjLoader {
29+ public class ProjLoaderImpl implements ProjLoader , AppLifeCycle {
2530
26- private final Path dataDirectory ;
31+ private static final Logger LOGGER = LoggerFactory .getLogger (ProjLoaderImpl .class );
32+ private static final String RESOURCES_PATH = "proj" ;
33+
34+ private final BlobStore blobStore ;
35+ private Optional <Path > dataDirectory ;
2736
2837 @ Inject
29- public ProjLoaderImpl (AppContext appContext ) {
30- this .dataDirectory = getDataDirectory (appContext .getDataDir (), appContext .getConfiguration ());
38+ public ProjLoaderImpl (BlobStore blobStore ) {
39+ this .blobStore = blobStore .with (RESOURCES_PATH );
40+ this .dataDirectory = Optional .empty ();
3141 }
3242
3343 // for unit tests only
3444 ProjLoaderImpl (Path dataDirectory ) {
35- this .dataDirectory = dataDirectory ;
45+ this .dataDirectory = Optional .ofNullable (dataDirectory );
46+ this .blobStore = null ;
47+ }
48+
49+ @ Override
50+ public int getPriority () {
51+ return 900 ;
52+ }
53+
54+ @ Override
55+ public void onStart () {
56+ if (dataDirectory .isEmpty () && Objects .nonNull (blobStore )) {
57+ try {
58+ this .dataDirectory = blobStore .asLocalPath (Path .of ("" ), true );
59+ } catch (IOException e ) {
60+ LogContext .error (LOGGER , e , "Could not initialize PROJ data directory" );
61+ }
62+ }
3663 }
3764
3865 @ Override
@@ -56,8 +83,12 @@ public List<String> getLibraries() {
5683
5784 @ Override
5885 public Map <Path , List <String >> getResources () {
86+ if (dataDirectory .isEmpty ()) {
87+ return ImmutableMap .of ();
88+ }
89+
5990 return ImmutableMap .of (
60- dataDirectory ,
91+ dataDirectory . get () ,
6192 ImmutableList .of (
6293 "CH" ,
6394 "deformation_model.schema.json" ,
@@ -76,22 +107,8 @@ public Map<Path, List<String>> getResources() {
76107 "world" ));
77108 }
78109
79- @ Override
80- public void preload () {
81- // sqLiteLoader.load();
82- }
83-
84110 @ Override
85111 public Path getDataDirectory () {
86- return dataDirectory ;
87- }
88-
89- private Path getDataDirectory (Path dataDir , AppConfiguration configuration ) {
90- String projLocation = configuration .proj .location ;
91- if (Paths .get (projLocation ).isAbsolute ()) {
92- return Paths .get (projLocation );
93- }
94-
95- return dataDir .resolve (projLocation );
112+ return dataDirectory .orElseThrow ();
96113 }
97114}
0 commit comments