Skip to content

Commit d5e57d9

Browse files
committed
feat: made EntityModelManager a non-static class
1 parent b578bac commit d5e57d9

File tree

13 files changed

+335
-170
lines changed

13 files changed

+335
-170
lines changed

src/main/java/io/neonbee/NeonBee.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import io.neonbee.config.NeonBeeConfig;
4141
import io.neonbee.config.ServerConfig;
4242
import io.neonbee.data.DataQuery;
43+
import io.neonbee.entity.EntityModelManager;
4344
import io.neonbee.entity.EntityWrapper;
4445
import io.neonbee.hook.HookRegistry;
4546
import io.neonbee.hook.HookType;
@@ -164,6 +165,8 @@ public class NeonBee {
164165

165166
private final Set<String> localConsumers = new ConcurrentHashSet<>();
166167

168+
private final EntityModelManager modelManager;
169+
167170
private final CompositeMeterRegistry compositeMeterRegistry;
168171

169172
/**
@@ -516,6 +519,8 @@ private Future<Void> deployModules() {
516519
NeonBee(Vertx vertx, NeonBeeOptions options, CompositeMeterRegistry compositeMeterRegistry) {
517520
this.vertx = vertx;
518521
this.options = options;
522+
523+
this.modelManager = new EntityModelManager(this);
519524
this.compositeMeterRegistry = compositeMeterRegistry;
520525

521526
// to be able to retrieve the NeonBee instance from any point you have a Vert.x instance add it to a global map
@@ -644,6 +649,15 @@ public ServerConfig getServerConfig() {
644649
return new ServerConfig((JsonObject) getLocalMap().get(ServerVerticle.SERVER_CONFIG_KEY));
645650
}
646651

652+
/**
653+
* Get the {@link EntityModelManager}.
654+
*
655+
* @return the {@link EntityModelManager}
656+
*/
657+
public EntityModelManager getModelManager() {
658+
return modelManager;
659+
}
660+
647661
/**
648662
* Get the {@link CompositeMeterRegistry}.
649663
*

src/main/java/io/neonbee/endpoint/odatav4/ODataV4Endpoint.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import static com.google.common.base.Strings.nullToEmpty;
66
import static io.neonbee.endpoint.odatav4.ODataV4Endpoint.UriConversion.STRICT;
77
import static io.neonbee.entity.EntityModelManager.EVENT_BUS_MODELS_LOADED_ADDRESS;
8-
import static io.neonbee.entity.EntityModelManager.getSharedModels;
98
import static io.neonbee.internal.helper.FunctionalHelper.entryConsumer;
109
import static io.neonbee.internal.helper.FunctionalHelper.entryFunction;
1110
import static io.neonbee.internal.helper.StringHelper.EMPTY;
@@ -231,7 +230,7 @@ public Router createEndpointRouter(Vertx vertx, String basePath, JsonObject conf
231230

232231
private static Future<Void> refreshRouter(Vertx vertx, Router router, String basePath, UriConversion uriConversion,
233232
RegexBlockList exposedEntities, AtomicReference<Map<String, EntityModel>> currentModels) {
234-
return getSharedModels(NeonBee.get(vertx)).compose(models -> {
233+
return NeonBee.get(vertx).getModelManager().getSharedModels().compose(models -> {
235234
if (models == currentModels.get()) {
236235
return succeededFuture(); // no update needed
237236
} else {

src/main/java/io/neonbee/entity/EntityModelLoader.java

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import static java.nio.charset.StandardCharsets.UTF_8;
99

1010
import java.io.ByteArrayInputStream;
11-
import java.io.File;
1211
import java.io.InputStream;
1312
import java.io.InputStreamReader;
1413
import java.io.Reader;
@@ -20,7 +19,6 @@
2019
import java.util.HashMap;
2120
import java.util.List;
2221
import java.util.Map;
23-
import java.util.Optional;
2422
import java.util.function.Function;
2523
import java.util.stream.Collectors;
2624

@@ -77,23 +75,23 @@ class EntityModelLoader {
7775
}
7876

7977
/**
80-
* Load models from model directory and classpath and returns a future to an unmodifiable map of all loaded models.
78+
* Load models from model directory and class path and return a future to a map of all loaded models.
8179
*
82-
* @return a unmodifiable map of all loaded models
80+
* @return a map of all loaded models
8381
*/
8482
public static Future<Map<String, EntityModel>> load(Vertx vertx) {
85-
return new EntityModelLoader(vertx).loadModelsFromModelDirectoryAndClasspath()
83+
return new EntityModelLoader(vertx).loadModelsFromModelDirectoryAndClassPath()
8684
.map(EntityModelLoader::getModels);
8785
}
8886

8987
/**
90-
* Load models from model directory and classpath, as well as from the maps provided and returns a future to an
91-
* unmodifiable map of all loaded models.
88+
* Load models from model directory and class path, as well as from the maps provided and return a future to a map
89+
* of all loaded models.
9290
*
93-
* @return a unmodifiable map of all loaded models
91+
* @return a map of all loaded models
9492
*/
9593
public static Future<Map<String, EntityModel>> load(Vertx vertx, Collection<EntityModelDefinition> definitions) {
96-
return new EntityModelLoader(vertx).loadModelsFromModelDirectoryAndClasspath().compose(loader -> {
94+
return new EntityModelLoader(vertx).loadModelsFromModelDirectoryAndClassPath().compose(loader -> {
9795
return CompositeFuture
9896
.all(definitions.stream().map(loader::loadModelsFromDefinition).collect(Collectors.toList()))
9997
.map(loader);
@@ -103,18 +101,18 @@ public static Future<Map<String, EntityModel>> load(Vertx vertx, Collection<Enti
103101
/**
104102
* Returns a map of all loaded models.
105103
*
106-
* @return a unmodifiable map of all loaded models
104+
* @return a map of all loaded models
107105
*/
108106
public Map<String, EntityModel> getModels() {
109-
return Collections.unmodifiableMap(models);
107+
return models;
110108
}
111109

112110
/**
113-
* Load models from model directory and classpath.
111+
* Load models from model directory and class path.
114112
*
115113
* @return a future to the {@link EntityModelLoader} instance
116114
*/
117-
public Future<EntityModelLoader> loadModelsFromModelDirectoryAndClasspath() {
115+
public Future<EntityModelLoader> loadModelsFromModelDirectoryAndClassPath() {
118116
NeonBeeOptions options = NeonBee.get(vertx).getOptions();
119117
return CompositeFuture.all(scanDir(options.getModelsDirectory()),
120118
options.shouldIgnoreClassPath() ? succeededFuture() : scanClassPath()).map(this);
@@ -165,7 +163,7 @@ Future<Void> scanClassPath() {
165163
Future<List<String>> modelFiles = scanner.scanManifestFiles(vertx, NEONBEE_MODELS);
166164

167165
return CompositeFuture.all(csnFiles, modelFiles).compose(scanResult -> CompositeFuture
168-
// Use distinct because models mentioned in the manifest could also exists as file.
166+
// use distinct because models mentioned in the manifest could also exists as file.
169167
.all(Streams.concat(csnFiles.result().stream(), modelFiles.result().stream()).distinct()
170168
.map(name -> loadModel(Path.of(name)).otherwise(throwable -> {
171169
// models loaded from the class path are non-vital for NeonBee so continue anyways
@@ -180,25 +178,26 @@ Future<Void> loadModel(Path csnFile) {
180178
return succeededFuture();
181179
}
182180

183-
return readCsnModel(csnFile)
184-
.compose(
185-
cdsModel -> CompositeFuture
186-
.all(EntityModelDefinition.resolveEdmxPaths(csnFile, cdsModel).stream()
187-
.map(this::loadEdmxModel).collect(Collectors.toList()))
188-
.onSuccess(compositeFuture -> {
189-
buildModelMap(cdsModel, compositeFuture.<ServiceMetadata>list());
190-
}))
191-
.mapEmpty();
181+
return readCsnModel(csnFile).compose(cdsModel -> {
182+
return CompositeFuture.all(EntityModelDefinition.resolveEdmxPaths(csnFile, cdsModel).stream()
183+
.map(this::loadEdmxModel).collect(Collectors.toList())).onSuccess(compositeFuture -> {
184+
buildModelMap(cdsModel, compositeFuture.<ServiceMetadata>list());
185+
});
186+
}).mapEmpty();
192187
}
193188

194-
Future<Void> parseModel(String csnFile, byte[] csnPayload, Map<String, byte[]> extensionModels) {
195-
return parseCsnModel(csnPayload).compose(cdsModel -> CompositeFuture.all(EntityModelDefinition
196-
.resolveEdmxPaths(Path.of(csnFile), cdsModel).stream().map(Path::toString).map(path -> {
197-
return Optional.ofNullable(extensionModels.get(path)).orElse(extensionModels
198-
.get(path.replace(File.separatorChar, File.separatorChar == '/' ? '\\' : '/')));
199-
}).map(this::parseEdmxModel).collect(Collectors.toList())).onSuccess(compositeFuture -> {
200-
buildModelMap(cdsModel, compositeFuture.<ServiceMetadata>list());
201-
})).mapEmpty();
189+
Future<Void> parseModel(String csnFile, byte[] csnPayload, Map<String, byte[]> associatedModels) {
190+
return parseCsnModel(csnPayload)
191+
.compose(cdsModel -> CompositeFuture
192+
.all(EntityModelDefinition.resolveEdmxPaths(Path.of(csnFile), cdsModel).stream()
193+
.map(Path::toString).map(path -> {
194+
// we do not know if the path uses windows / unix path separators, try both!
195+
return FileSystemHelper.getPathFromMap(associatedModels, path);
196+
}).map(this::parseEdmxModel).collect(Collectors.toList()))
197+
.onSuccess(compositeFuture -> {
198+
buildModelMap(cdsModel, compositeFuture.<ServiceMetadata>list());
199+
}))
200+
.mapEmpty();
202201
}
203202

204203
private void buildModelMap(CdsModel cdsModel, List<ServiceMetadata> edmxModels) {

0 commit comments

Comments
 (0)