Skip to content

Commit

Permalink
Merge pull request #22113 from mkouba/arc-container-remove-require-ru…
Browse files Browse the repository at this point in the history
…nning

Remove the ArcContainerImpl.requireRunning() check
  • Loading branch information
Sanne authored Dec 10, 2021
2 parents 480337a + 911aa73 commit c605c9a
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,33 @@

import io.quarkus.arc.impl.ArcContainerImpl;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;

/**
*
* @author Martin Kouba
* Provides access to the ArC container.
*/
public final class Arc {

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

/**
*
* @return the initialized container
*/
public static ArcContainer initialize() {
if (INITIALIZED.compareAndSet(false, true)) {
try {
ArcContainerImpl container = new ArcContainerImpl();
INSTANCE.set(container);
container.init();
return container;
} catch (Throwable t) {
INITIALIZED.set(false);
throw new RuntimeException("Failed to initialize Arc", t);
ArcContainerImpl container = INSTANCE.get();
if (container == null) {
synchronized (INSTANCE) {
container = INSTANCE.get();
if (container == null) {
container = new ArcContainerImpl();
// Set the container instance first because Arc.container() can be used within ArcContainerImpl.init()
INSTANCE.set(container);
container.init();
}
}
}
return container();
return container;
}

public static void setExecutor(ExecutorService executor) {
Expand All @@ -42,13 +44,13 @@ public static ArcContainer container() {
}

public static void shutdown() {
if (INSTANCE.get() != null) {
ArcContainerImpl container = INSTANCE.get();
if (container != null) {
synchronized (INSTANCE) {
ArcContainerImpl container = INSTANCE.get();
container = INSTANCE.get();
if (container != null) {
container.shutdown();
INSTANCE.set(null);
INITIALIZED.set(false);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public interface ArcContainer {
* Returns true if Arc container is running.
* This can be used as a quick check to determine CDI availability in Quarkus.
*
* @return true is {@link ArcContainer} is running, false otherwise
* @return true if {@link ArcContainer} is running, false otherwise
*/
boolean isRunning();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,6 @@ private static void addBuiltInBeans(List<InjectableBean<?>> beans) {
}

public void init() {
requireRunning();
// Fire an event with qualifier @Initialized(ApplicationScoped.class)
Set<Annotation> qualifiers = Set.of(Initialized.Literal.APPLICATION, Any.Literal.INSTANCE);
EventImpl.createNotifier(Object.class, Object.class, qualifiers, this, false)
Expand All @@ -193,7 +192,6 @@ public void init() {

@Override
public InjectableContext getActiveContext(Class<? extends Annotation> scopeType) {
requireRunning();
// Application/Singleton context is always active
if (ApplicationScoped.class.equals(scopeType)) {
return applicationContext;
Expand All @@ -218,7 +216,6 @@ public InjectableContext getActiveContext(Class<? extends Annotation> scopeType)

@Override
public List<InjectableContext> getContexts(Class<? extends Annotation> scopeType) {
requireRunning();
return contexts.getOrDefault(scopeType, Collections.emptyList());
}

Expand All @@ -229,27 +226,22 @@ public Set<Class<? extends Annotation>> getScopes() {

@Override
public <T> InstanceHandle<T> instance(Class<T> type, Annotation... qualifiers) {
requireRunning();
return instanceHandle(type, qualifiers);
}

@Override
public <T> InstanceHandle<T> instance(TypeLiteral<T> type, Annotation... qualifiers) {
requireRunning();
return instanceHandle(type.getType(), qualifiers);
}

@Override
public <X> InstanceHandle<X> instance(Type type, Annotation... qualifiers) {
requireRunning();
return instanceHandle(type, qualifiers);
}

@SuppressWarnings("unchecked")
@Override
public <T> Supplier<InstanceHandle<T>> instanceSupplier(Class<T> type, Annotation... qualifiers) {
requireRunning();

if (qualifiers == null || qualifiers.length == 0) {
qualifiers = new Annotation[] { Default.Literal.INSTANCE };
}
Expand Down Expand Up @@ -282,7 +274,6 @@ public InstanceHandle<T> get() {
@Override
public <T> InstanceHandle<T> instance(InjectableBean<T> bean) {
Objects.requireNonNull(bean);
requireRunning();
return beanInstanceHandle(bean, null);
}

Expand All @@ -305,15 +296,13 @@ public boolean isRunning() {
@Override
public <T> InjectableBean<T> bean(String beanIdentifier) {
Objects.requireNonNull(beanIdentifier);
requireRunning();
return (InjectableBean<T>) beansById.getValue(beanIdentifier);
}

@SuppressWarnings("unchecked")
@Override
public <T> InstanceHandle<T> instance(String name) {
Objects.requireNonNull(name);
requireRunning();
Set<InjectableBean<?>> resolvedBeans = beansByName.getValue(name);
return resolvedBeans.size() != 1 ? EagerInstanceHandle.unavailable()
: (InstanceHandle<T>) beanInstanceHandle(resolvedBeans.iterator()
Expand Down Expand Up @@ -839,12 +828,6 @@ public static ArcContainerImpl instance() {
return unwrap(Arc.container());
}

private void requireRunning() {
if (!running.get()) {
throw new IllegalStateException("Container not running: " + this);
}
}

private static final class Resolvable {

private static final Annotation[] ANY_QUALIFIER = { Any.Literal.INSTANCE };
Expand Down

0 comments on commit c605c9a

Please sign in to comment.