Module architecture
A module follows a standard organization of packages and directories, which separates responsibilities and makes the code easier to read and maintain.
Packages
| Package | Content | Example |
|---|---|---|
service | Business logic: rules, and access to the databases | UserService.java creates, updates and deletes members |
resource | The REST API. These classes receive HTTP requests, call the services and return the responses. | UserResource.java exposes GET, POST, PUT and DELETE on members |
dto | Data transfer objects: plain classes that carry data between layers, and between the server and the user interface | UserDTO.java |
exception | Custom exceptions, which let the API return clear messages and status codes | UserNotFoundException.java |
config | The configuration class of the module, usually named after the module with the suffix ConfigService, which initializes what resources and services depend on | WorkspaceConfigService.java |
The resources directory, which is not a package, holds the configuration files:
application.properties, application-dev.properties and application-prd.properties.
Injection and singletons
Resource classes are instantiated for each request. Services are singletons, annotated with
@ApplicationScoped.
Calling a service from a resource:
@Inject
private UserService userService;
Declaring the service:
@ApplicationScoped
public class DefaultUserService implements UserService {
}
Not every singleton inherited from Open ENT NG has been migrated to @ApplicationScoped. Those
that have not are initialized either in a Vert.x verticle or in a class
with the ConfigService suffix. Every module has at least one of the two.
Verticles are created by factories defined in the base class
fr.tech.openent.common.vertx.BaseVerticle, from which each verticle inherits.
With the launcher, all verticles run in one Java virtual
machine. To keep singletons from being initialized several times, the setting
openent.launcher.mode.enabled controls whether a module starts the shared services itself.
Singletons initialized in verticles
Singletons shared by all modules are initialized in BaseVerticle. Those specific to a module
are initialized in the module's own verticle.
| Component | Role | Initialized in |
|---|---|---|
I18n | Translations | The module's verticle |
FileResolver | File resolution | The module's verticle |
EventStoreFactory | Factory for the event store | The base verticle |
UserValidationFactory | Factory for member validation | The base verticle |
MongoDb | Document database | The module's verticle |
Neo4j | Graph database | The module's verticle |
They are reached by the name of the singleton followed by getInstance(), for example
I18n.getInstance().
Singletons initialized in the configuration bean
The configuration bean, in the config package, initializes what resources and services depend
on, and publishes a JsonObject bean holding the module's settings. For the document space
module:
@ApplicationScoped
@Startup
public class WorkspaceConfigService extends BaseConfigService {
private JsonObject config;
@Inject
public WorkspaceConfigService() {
super(DefaultModuleName.WORKSPACE_MODULE_NAME);
this.loadConfig();
}
private void loadConfig() {
this.config = ConfigManager.getQuarkusConfig(WORKSPACE_MODULE_NAME);
}
@Produces
@OpenENT(WORKSPACE_MODULE_NAME)
public JsonObject getConfig() {
return config;
}
}
Other classes then inject the settings with:
@OpenENT(DefaultModuleName.WORKSPACE_MODULE_NAME) JsonObject config
The configuration bean also creates singletons. Some must be created differently depending on whether the module runs inside the launcher or on its own as a microservice. This mail sender is created by the module only when it runs on its own; inside the launcher, one sender is created for all modules:
@Produces
@OpenENT("emailSender")
@UnlessBuildProperty(name = "openent.launcher.mode.enabled", stringValue = "true", enableIfMissing = true)
public EmailSender getEmailSender(Vertx vertx, @OpenENT(FEEDER_MODULE_NAME) JsonObject config) {
EmailFactory emailFactory = new EmailFactory(vertx, config);
return emailFactory.getSender();
}
The @OpenENT qualifier
Beans created in a ConfigService class are qualified with @OpenENT. A bean that can be built
with its default constructor, with no particular settings, does not need the qualifier. The
constructor of a resource therefore shows at a glance which beans come from the configuration
bean and which are injected directly by Quarkus:
public WorkspaceResource(Vertx vertx,
@OpenENT(DefaultModuleName.WORKSPACE_MODULE_NAME) JsonObject config,
@OpenENT("storage") Storage storage,
@OpenENT("workspaceService") WorkspaceService workspaceService,
@OpenENT("shareService") final ShareService shareService,
@OpenENT("pdfGenerator") final PdfGenerator aPdfGenerator,
@OpenENT("timelineHelper") final TimelineHelper timelineHelper,
@OpenENT("folderManager") final FolderManager fm,
@OpenENT("eventHelper") final EventHelper eventHelper) {
// Resource initialization
}
Making beans visible to the launcher
The launcher aggregates modules, so the annotated beans of each module (verticles, resources and
others) must be discoverable. Add this file as resources/META-INF/beans.xml:
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
bean-discovery-mode="annotated">
</beans>
Settings
Each module is configured by .properties files, and every property can be overridden by an
environment variable. See Configuration and
Module anatomy.