Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvement/logging issue regarding osgi #510

Merged
merged 2 commits into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions src/main/java/com/hivemq/HiveMQServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,25 +66,28 @@ public class HiveMQServer {
private final @NotNull SystemInformation systemInformation;
private final @NotNull MetricRegistry metricRegistry;
private final boolean migrate;
private final boolean enableLoggingBootstrap;

private @Nullable Injector injector;
private @Nullable FullConfigurationService configService;

public HiveMQServer() {
this(new SystemInformationImpl(true), new MetricRegistry(), null, true);
this(new SystemInformationImpl(true), new MetricRegistry(), null, true, true);
}

public HiveMQServer(
final @NotNull SystemInformation systemInformation,
final @Nullable MetricRegistry metricRegistry,
final @Nullable FullConfigurationService configService,
final boolean enableLoggingBootstrap,
final boolean migrate) {
hivemqId = new HivemqId();
lifecycleModule = new LifecycleModule();
dataLock = new DataLock();
this.systemInformation = systemInformation;
this.metricRegistry = metricRegistry;
this.configService = configService;
this.enableLoggingBootstrap = enableLoggingBootstrap;
this.migrate = migrate;
}

Expand All @@ -105,7 +108,9 @@ public void bootstrap() throws Exception {

metricRegistry.addListener(new MetricRegistryLogger());

LoggingBootstrap.prepareLogging();
if (enableLoggingBootstrap) {
LoggingBootstrap.prepareLogging();
}

log.info("Starting HiveMQ Community Edition Server");

Expand All @@ -116,8 +121,11 @@ public void bootstrap() throws Exception {
systemInformation.init();
}

log.trace("Initializing Logging");
LoggingBootstrap.initLogging(systemInformation.getConfigFolder());
if (enableLoggingBootstrap) {
log.trace("Initializing Logging");

LoggingBootstrap.initLogging(systemInformation.getConfigFolder());
}

log.trace("Initializing Exception handlers");
HiveMQExceptionHandlerBootstrap.addUnrecoverableExceptionHandler();
Expand Down Expand Up @@ -216,7 +224,10 @@ public void startInstance(final @Nullable EmbeddedExtension embeddedExtension) t

/* It's important that we are modifying the log levels after Guice is initialized,
otherwise this somehow interferes with Singleton creation */
LoggingBootstrap.addLoglevelModifiers();
if (enableLoggingBootstrap) {
LoggingBootstrap.addLoglevelModifiers();
}

instance.start(embeddedExtension);
}

Expand Down Expand Up @@ -262,7 +273,9 @@ public void stop() {
if (configService.persistenceConfigurationService().getMode() == PersistenceMode.FILE) {
dataLock.unlock();
}
LoggingBootstrap.resetLogging();
if (enableLoggingBootstrap) {
LoggingBootstrap.resetLogging();
}
}

public @Nullable Injector getInjector() {
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/com/hivemq/embedded/EmbeddedHiveMQBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,14 @@ public interface EmbeddedHiveMQBuilder {
*/
@NotNull EmbeddedHiveMQBuilder withEmbeddedExtension(@Nullable EmbeddedExtension embeddedExtension);

/**
* Disables the internal logging bootstrap, for the case any logging is already provided by other frameworks,
* such as OSGi or Springboot.
*
* @return this builder.
*/
@NotNull EmbeddedHiveMQBuilder withoutLoggingBootstrap();

/**
* Concludes the EmbeddedHiveMQ build process.
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public class EmbeddedHiveMQBuilderImpl implements EmbeddedHiveMQBuilder {
private @Nullable Path dataFolder = null;
private @Nullable Path extensionsFolder = null;
private @Nullable EmbeddedExtension embeddedExtension = null;
private boolean enableLoggingBootstrap = true;

@Override
public @NotNull EmbeddedHiveMQBuilder withConfigurationFolder(final @Nullable Path configFolder) {
Expand All @@ -60,14 +61,20 @@ public class EmbeddedHiveMQBuilderImpl implements EmbeddedHiveMQBuilder {
return this;
}

@Override
public @NotNull EmbeddedHiveMQBuilder withoutLoggingBootstrap() {
this.enableLoggingBootstrap = false;
return this;
}

@Override
public @NotNull EmbeddedHiveMQ build() {
// Shim for the old API
final File confFile = configFolder == null ? null : configFolder.toFile();
final File dataFile = dataFolder == null ? null : dataFolder.toFile();
final File extensionsFile = extensionsFolder == null ? null : extensionsFolder.toFile();

return new EmbeddedHiveMQImpl(confFile, dataFile, extensionsFile, embeddedExtension);
return new EmbeddedHiveMQImpl(confFile, dataFile, extensionsFile, embeddedExtension, enableLoggingBootstrap);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,21 @@ class EmbeddedHiveMQImpl implements EmbeddedHiveMQ {
private @NotNull LinkedList<CompletableFuture<Void>> stopFutures = new LinkedList<>();
private @Nullable Future<?> shutDownFuture;

private final boolean enableLoggingBootstrap;

EmbeddedHiveMQImpl(
final @Nullable File conf, final @Nullable File data, final @Nullable File extensions) {
this(conf, data, extensions, null);
this(conf, data, extensions, null, true);
}

EmbeddedHiveMQImpl(
final @Nullable File conf,
final @Nullable File data,
final @Nullable File extensions,
final @Nullable EmbeddedExtension embeddedExtension) {
final @Nullable EmbeddedExtension embeddedExtension,
final boolean enableLoggingBootstrap) {
this.embeddedExtension = embeddedExtension;
this.enableLoggingBootstrap = enableLoggingBootstrap;

log.info("Setting default authentication behavior to ALLOW ALL");
InternalConfigurations.AUTH_DENY_UNAUTHENTICATED_CONNECTIONS.set(false);
Expand Down Expand Up @@ -146,7 +150,7 @@ private void stateChange() {
systemInformation.init();
configurationService = ConfigurationBootstrap.bootstrapConfig(systemInformation);

hiveMQServer = new HiveMQServer(systemInformation, metricRegistry, configurationService, false);
hiveMQServer = new HiveMQServer(systemInformation, metricRegistry, configurationService, enableLoggingBootstrap, false);
hiveMQServer.bootstrap();
hiveMQServer.startInstance(embeddedExtension);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,8 @@ public void test_hivemq_uses_embedded_extension_with_normal() throws Exception {
final EmbeddedExtensionImpl extension =
new EmbeddedExtensionImpl("id", "name", "123", "luke_skywalker", 0, 1000, embeddedMain);

final EmbeddedHiveMQImpl embeddedHiveMQ = new EmbeddedHiveMQImpl(conf, data, extensions, extension);
final EmbeddedHiveMQImpl embeddedHiveMQ =
new EmbeddedHiveMQImpl(conf, data, extensions, extension, true);
embeddedHiveMQ.start().get();

assertTrue(embeddedMain.running.get());
Expand Down