Skip to content

Commit

Permalink
Allow RuntimeBeanBuildItem to pick static or runtime init
Browse files Browse the repository at this point in the history
  • Loading branch information
stuartwdouglas committed Dec 4, 2019
1 parent ecb043a commit 2c1c471
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ public final class RuntimeBeanBuildItem extends MultiBuildItem {
final RuntimeValue<?> runtimeValue;
final NavigableMap<String, NavigableMap<String, Object>> qualifiers;
final boolean removable;
final boolean staticInit;

RuntimeBeanBuildItem(String scope, String type, Supplier<Object> supplier,
NavigableMap<String, NavigableMap<String, Object>> qualifiers, boolean removable,
RuntimeValue<?> runtimeValue) {
RuntimeValue<?> runtimeValue, boolean staticInit) {
this.staticInit = staticInit;
if (supplier != null && runtimeValue != null) {
throw new IllegalArgumentException("It is not possible to specify both - a supplier and a runtime value");
}
Expand All @@ -37,6 +39,10 @@ public final class RuntimeBeanBuildItem extends MultiBuildItem {
this.runtimeValue = runtimeValue;
}

public boolean isStaticInit() {
return staticInit;
}

public String getScope() {
return scope;
}
Expand Down Expand Up @@ -78,6 +84,7 @@ public static class Builder {
Supplier<Object> supplier;
RuntimeValue<?> value;
final NavigableMap<String, NavigableMap<String, Object>> qualifiers = new TreeMap<>();
boolean staticInit = true;

public Builder(String type) {
this.type = type;
Expand Down Expand Up @@ -126,8 +133,17 @@ public Builder setRuntimeValue(RuntimeValue<?> runtimeValue) {
return this;
}

public boolean isStaticInit() {
return staticInit;
}

public Builder setStaticInit(boolean staticInit) {
this.staticInit = staticInit;
return this;
}

public RuntimeBeanBuildItem build() {
return new RuntimeBeanBuildItem(scope, type, supplier, qualifiers, removable, value);
return new RuntimeBeanBuildItem(scope, type, supplier, qualifiers, removable, value, staticInit);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import io.quarkus.arc.processor.BeanInfo;
import io.quarkus.arc.runtime.ArcRecorder;
import io.quarkus.builder.item.SimpleBuildItem;
import io.quarkus.deployment.annotations.BuildProducer;
import io.quarkus.deployment.annotations.BuildStep;
import io.quarkus.deployment.annotations.ExecutionTime;
Expand All @@ -29,7 +30,7 @@ public class RuntimeBeanProcessor {

@BuildStep
@Record(ExecutionTime.STATIC_INIT)
void build(List<RuntimeBeanBuildItem> beans,
BeanInfoBuildItem build(List<RuntimeBeanBuildItem> beans,
BuildProducer<GeneratedBeanBuildItem> generatedBean,
ArcRecorder recorder,
BuildProducer<UnremovableBeanBuildItem> unremovableBeans) {
Expand All @@ -43,8 +44,10 @@ public void write(String name, byte[] data) {
}, beanName, null, Object.class.getName());

classCreator.addAnnotation(ApplicationScoped.class);
Map<String, Supplier<Object>> map = new HashMap<>();
Map<String, Supplier<Object>> staticBeans = new HashMap<>();
Map<String, Supplier<Object>> runtimeBeans = new HashMap<>();
for (RuntimeBeanBuildItem bean : beans) {
Map<String, Supplier<Object>> map = bean.isStaticInit() ? staticBeans : runtimeBeans;
//deterministic name
//as we know the maps are sorted this will result in the same hash for the same bean
String name = bean.type.replace(".", "_") + "_" + HashUtil.sha1(bean.qualifiers.toString());
Expand Down Expand Up @@ -82,6 +85,30 @@ public boolean test(BeanInfo bean) {
producer.returnValue(result);
}
classCreator.close();
recorder.initSupplierBeans(map);
return new BeanInfoBuildItem(staticBeans, runtimeBeans);
}

@BuildStep
@Record(ExecutionTime.STATIC_INIT)
void initStatic(BeanInfoBuildItem infoBuildItem,
ArcRecorder recorder) {
recorder.initStaticSupplierBeans(infoBuildItem.staticBeans);
}

@BuildStep
@Record(ExecutionTime.RUNTIME_INIT)
void initRuntime(BeanInfoBuildItem infoBuildItem,
ArcRecorder recorder) {
recorder.initRuntimeSupplierBeans(infoBuildItem.runtimeBeans);
}

static final class BeanInfoBuildItem extends SimpleBuildItem {
final Map<String, Supplier<Object>> staticBeans;
final Map<String, Supplier<Object>> runtimeBeans;

BeanInfoBuildItem(Map<String, Supplier<Object>> staticBeans, Map<String, Supplier<Object>> runtimeBeans) {
this.staticBeans = staticBeans;
this.runtimeBeans = runtimeBeans;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.function.Supplier;

Expand Down Expand Up @@ -45,8 +46,12 @@ public void initExecutor(ExecutorService executor) {
Arc.setExecutor(executor);
}

public void initSupplierBeans(Map<String, Supplier<Object>> beans) {
supplierMap = beans;
public void initStaticSupplierBeans(Map<String, Supplier<Object>> beans) {
supplierMap = new ConcurrentHashMap<>(beans);
}

public void initRuntimeSupplierBeans(Map<String, Supplier<Object>> beans) {
supplierMap.putAll(beans);
}

public BeanContainer initBeanContainer(ArcContainer container, List<BeanContainerListener> listeners,
Expand Down

0 comments on commit 2c1c471

Please sign in to comment.