Skip to content

Commit

Permalink
Arc - introduce ArcInitConfig object along with a builder, replace ex…
Browse files Browse the repository at this point in the history
…isting usages, deprecate former init method
  • Loading branch information
manovotn committed Apr 5, 2023
1 parent 3e86a56 commit 39db8fd
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import io.quarkus.arc.Arc;
import io.quarkus.arc.ArcContainer;
import io.quarkus.arc.ArcInitConfig;
import io.quarkus.arc.CurrentContextFactory;
import io.quarkus.arc.InjectableBean;
import io.quarkus.arc.InjectableBean.Kind;
Expand Down Expand Up @@ -43,8 +44,9 @@ public class ArcRecorder {
public ArcContainer initContainer(ShutdownContext shutdown, RuntimeValue<CurrentContextFactory> currentContextFactory,
boolean strictCompatibility)
throws Exception {
ArcContainer container = Arc.initialize(currentContextFactory != null ? currentContextFactory.getValue() : null,
strictCompatibility);
ArcContainer container = Arc.initialize(ArcInitConfig.builder()
.setCurrentContextFactory(currentContextFactory != null ? currentContextFactory.getValue() : null)
.setStrictCompatibility(strictCompatibility).build());
shutdown.addShutdownTask(new Runnable() {
@Override
public void run() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,37 @@ public final class Arc {

private static final AtomicReference<ArcContainerImpl> INSTANCE = new AtomicReference<>();

/**
* Initializes {@link ArcContainer} with default settings.
* This is equal to using {@code Arc#initialize(ArcInitConfig.INSTANCE)}
*
* @return {@link ArcContainer} instance with default configuration
*/
public static ArcContainer initialize() {
return initialize(null);
return initialize(ArcInitConfig.INSTANCE);
}

/**
* Deprecated, will be removed in future iterations.
* Users are encouraged to use {@link #initialize(ArcInitConfig)} instead.
*
* @param currentContextFactory
* @return the initialized container
*/
@Deprecated(since = "3.0")
public static ArcContainer initialize(CurrentContextFactory currentContextFactory) {
return initialize(currentContextFactory, false);
return initialize(ArcInitConfig.builder().setCurrentContextFactory(currentContextFactory).build());
}

public static ArcContainer initialize(CurrentContextFactory currentContextFactory, boolean strictMode) {
public static ArcContainer initialize(ArcInitConfig arcInitConfig) {
ArcContainerImpl container = INSTANCE.get();
if (container == null) {
synchronized (INSTANCE) {
container = INSTANCE.get();
if (container == null) {
// Set the container instance first because Arc.container() can be used within ArcContainerImpl.init()
container = new ArcContainerImpl(currentContextFactory, strictMode);
container = new ArcContainerImpl(arcInitConfig.getCurrentContextFactory(),
arcInitConfig.isStrictCompatibility());
INSTANCE.set(container);
container.init();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package io.quarkus.arc;

/**
* A configuration object used while initializing Arc, see {@link Arc#initialize()} methods.
* Consolidates all configuration objects needed for Arc to initialize, values are initialized to their defaults.
*
*/
public final class ArcInitConfig {

/**
* Basic instance without any configuration, all values are default
*/
public static final ArcInitConfig INSTANCE = builder().build();

/**
* Obtains a builder for {@link ArcInitConfig}
*
* @return new instance of the builder
*/
public static Builder builder() {
return new Builder();
}

private ArcInitConfig(Builder builder) {
this.currentContextFactory = builder.currentContextFactory;
this.strictCompatibility = builder.strictCompatibility;
}

private final boolean strictCompatibility;
private final CurrentContextFactory currentContextFactory;

public boolean isStrictCompatibility() {
return strictCompatibility;
}

public CurrentContextFactory getCurrentContextFactory() {
return currentContextFactory;
}

public static class Builder {
private boolean strictCompatibility;
private CurrentContextFactory currentContextFactory;

private Builder() {
// init all values with their defaults
this.strictCompatibility = false;
this.currentContextFactory = null;
}

public Builder setStrictCompatibility(boolean strictCompatibility) {
this.strictCompatibility = strictCompatibility;
return this;
}

public Builder setCurrentContextFactory(CurrentContextFactory currentContextFactory) {
this.currentContextFactory = currentContextFactory;
return this;
}

public ArcInitConfig build() {
return new ArcInitConfig(this);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import io.quarkus.arc.Arc;
import io.quarkus.arc.ArcContainer;
import io.quarkus.arc.ArcInitConfig;
import io.quarkus.arc.InjectableInstance;
import io.quarkus.arc.InstanceHandle;
import io.quarkus.arc.arquillian.utils.ClassLoading;
Expand Down Expand Up @@ -82,7 +83,7 @@ public ProtocolMetaData deploy(Archive<?> archive) throws DeploymentException {
Thread.currentThread().setContextClassLoader(deploymentClassLoader);

// passing strict mode here allows it to be visible in runtime
ArcContainer arcContainer = Arc.initialize(null, true);
ArcContainer arcContainer = Arc.initialize(ArcInitConfig.builder().setStrictCompatibility(true).build());
runningArc.set(arcContainer);
arcContainer.beanManager().getEvent().fire(new Startup());

Expand Down

0 comments on commit 39db8fd

Please sign in to comment.