Skip to content

Commit

Permalink
Log version and capabilities when starting up
Browse files Browse the repository at this point in the history
- also make it possible to inject optional build item
- improve ServiceStartBuildItem javadoc
- resolves #283
  • Loading branch information
mkouba committed Jan 9, 2019
1 parent 4de295b commit 98156d8
Show file tree
Hide file tree
Showing 21 changed files with 140 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@
import org.jboss.builder.BuildContext;
import org.jboss.builder.BuildProvider;
import org.jboss.builder.BuildStepBuilder;
import org.jboss.builder.ConsumeFlag;
import org.jboss.builder.ConsumeFlags;
import org.jboss.builder.ProduceFlag;
import org.jboss.builder.item.BuildItem;
import org.jboss.builder.item.MultiBuildItem;
Expand Down Expand Up @@ -615,7 +617,14 @@ private String registerBuildStep(List<InjectedBuildResource> fieldList, String p
//register fields
for (InjectedBuildResource field : fieldList) {
if (field.consumedTypeName != null) {
mc.invokeVirtualMethod(ofMethod(BuildStepBuilder.class, "consumes", BuildStepBuilder.class, Class.class), builder, mc.loadClass(field.consumedTypeName));
if (field.injectionType == InjectionType.OPTIONAL) {
ResultHandle consumeFlags = mc.invokeStaticMethod(ofMethod(ConsumeFlags.class, "of", ConsumeFlags.class, ConsumeFlag.class),
mc.load(ConsumeFlag.OPTIONAL));
mc.invokeVirtualMethod(ofMethod(BuildStepBuilder.class, "consumes", BuildStepBuilder.class, Class.class, ConsumeFlags.class), builder,
mc.loadClass(field.consumedTypeName), consumeFlags);
} else {
mc.invokeVirtualMethod(ofMethod(BuildStepBuilder.class, "consumes", BuildStepBuilder.class, Class.class), builder, mc.loadClass(field.consumedTypeName));
}
}
if (field.producedTypeName != null) {
mc.invokeVirtualMethod(ofMethod(BuildStepBuilder.class, "produces", BuildStepBuilder.class, Class.class), builder, mc.loadClass(field.producedTypeName));
Expand All @@ -640,7 +649,14 @@ private String registerBuildStep(List<InjectedBuildResource> fieldList, String p
for (InjectedBuildResource injection : methodInjection) {
if (injection.injectionType != InjectionType.TEMPLATE && injection.injectionType != InjectionType.RECORDER_CONTEXT && injection.injectionType != InjectionType.EXECUTOR) {
if (injection.consumedTypeName != null) {
mc.invokeVirtualMethod(ofMethod(BuildStepBuilder.class, "consumes", BuildStepBuilder.class, Class.class), builder, mc.loadClass(injection.consumedTypeName));
if (injection.injectionType == InjectionType.OPTIONAL) {
ResultHandle consumeFlags = mc.invokeStaticMethod(ofMethod(ConsumeFlags.class, "of", ConsumeFlags.class, ConsumeFlag.class),
mc.load(ConsumeFlag.OPTIONAL));
mc.invokeVirtualMethod(ofMethod(BuildStepBuilder.class, "consumes", BuildStepBuilder.class, Class.class, ConsumeFlags.class), builder,
mc.loadClass(injection.consumedTypeName), consumeFlags);
} else {
mc.invokeVirtualMethod(ofMethod(BuildStepBuilder.class, "consumes", BuildStepBuilder.class, Class.class), builder, mc.loadClass(injection.consumedTypeName));
}
}
if (injection.producedTypeName != null) {
mc.invokeVirtualMethod(ofMethod(BuildStepBuilder.class, "produces", BuildStepBuilder.class, Class.class), builder, mc.loadClass(injection.producedTypeName));
Expand Down Expand Up @@ -670,6 +686,9 @@ private ResultHandle generateMethodInjection(MethodCreator buildStepMc, ResultHa
val = buildStepMc.invokeVirtualMethod(ofMethod(BuildContext.class, "consume", SimpleBuildItem.class, Class.class), buildStepMc.getMethodParam(0), buildStepMc.loadClass(i.consumedTypeName));
} else if (i.injectionType == InjectionType.LIST) {
val = buildStepMc.invokeVirtualMethod(ofMethod(BuildContext.class, "consumeMulti", List.class, Class.class), buildStepMc.getMethodParam(0), buildStepMc.loadClass(i.consumedTypeName));
} else if (i.injectionType == InjectionType.OPTIONAL) {
ResultHandle tempVal = buildStepMc.invokeVirtualMethod(ofMethod(BuildContext.class, "consume", SimpleBuildItem.class, Class.class), buildStepMc.getMethodParam(0), buildStepMc.loadClass(i.consumedTypeName));
val = buildStepMc.invokeStaticMethod(ofMethod(Optional.class, "ofNullable", Optional.class, Object.class), tempVal);
} else {
val = buildStepMc.newInstance(ofConstructor(BUILD_PRODUCER, Class.class, BuildContext.class), buildStepMc.loadClass(i.producedTypeName), buildStepMc.getMethodParam(0));
}
Expand All @@ -683,6 +702,10 @@ private void generateFieldInjection(String processorClassName, MethodCreator bui
} else if (field.injectionType == InjectionType.LIST) {
ResultHandle val = buildStepMc.invokeVirtualMethod(ofMethod(BuildContext.class, "consumeMulti", List.class, Class.class), buildStepMc.getMethodParam(0), buildStepMc.loadClass(field.consumedTypeName));
buildStepMc.writeInstanceField(FieldDescriptor.of(processorClassName, field.element.getSimpleName().toString(), List.class), p, val);
} else if (field.injectionType == InjectionType.OPTIONAL) {
ResultHandle val = buildStepMc.invokeVirtualMethod(ofMethod(BuildContext.class, "consume", SimpleBuildItem.class, Class.class), buildStepMc.getMethodParam(0), buildStepMc.loadClass(field.consumedTypeName));
ResultHandle optional = buildStepMc.invokeStaticMethod(ofMethod(Optional.class, "ofNullable", Optional.class, Object.class), val);
buildStepMc.writeInstanceField(FieldDescriptor.of(processorClassName, field.element.getSimpleName().toString(), Optional.class), p, optional);
} else if (field.injectionType == InjectionType.EXECUTOR) {
ResultHandle val = buildStepMc.invokeVirtualMethod(ofMethod(BuildContext.class, "getExecutor", Executor.class), buildStepMc.getMethodParam(0));
buildStepMc.writeInstanceField(FieldDescriptor.of(processorClassName, field.element.getSimpleName().toString(), Executor.class), p, val);
Expand Down Expand Up @@ -747,6 +770,16 @@ private InjectedBuildResource createInjectionResource(Element element, DeclaredT
verifyType(typeMirror, MultiBuildItem.class);
consumedTypeName = processingEnv.getElementUtils().getBinaryName(((TypeElement) ((DeclaredType) typeMirror).asElement())).toString();

} else if (simpleType.equals(Optional.class.getName())) {
ft = InjectionType.OPTIONAL;
if (type.getTypeArguments().size() != 1) {
throw new RuntimeException("Cannot use @BuildResource on an optional that does not include a generic type");
}
TypeMirror typeMirror = type.getTypeArguments().get(0);

verifyType(typeMirror, SimpleBuildItem.class);
consumedTypeName = processingEnv.getElementUtils().getBinaryName(((TypeElement) ((DeclaredType) typeMirror).asElement())).toString();

} else if (simpleType.equals(BuildProducer.class.getName())) {
ft = InjectionType.PRODUCER;
if (type.getTypeArguments().size() != 1) {
Expand Down Expand Up @@ -792,7 +825,8 @@ enum InjectionType {
PRODUCER,
TEMPLATE,
RECORDER_CONTEXT,
EXECUTOR
EXECUTOR,
OPTIONAL
}

static class ConfigInjectionInfo {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,21 @@
*/
public final class Capabilities extends SimpleBuildItem {

public static final String CDI_ARC = "org.jboss.shamrock.cdi.arc";
public static final String CDI_ARC = "org.jboss.shamrock.cdi";
public static final String JAX_RS = "org.jboss.shamrock.jax-rs";
public static final String TRANSACTIONS = "org.jboss.shamrock.transactions";

public static final String WEBSOCKET = "org.jboss.shamrock.websocket";
public static final String MP_HEALTH = "org.jboss.shamrock.microprofile.health";
public static final String MP_OPENAPI = "org.jboss.shamrock.microprofile.openapi";
public static final String MP_METRICS = "org.jboss.shamrock.microprofile.metrics";
public static final String MP_FAULT_TOLERANCE = "org.jboss.shamrock.microprofile.fault-tolerance";
public static final String MP_OPENTRACING = "org.jboss.shamrock.microprofile.opentracing";
public static final String MP_REST_CLIENT = "org.jboss.shamrock.microprofile.rest-client";
public static final String MP_REACTIVE_OPERATORS = "org.jboss.shamrock.microprofile.reactive-streams-operators";
public static final String BEAN_VALIDATION = "org.jboss.shamrock.bean-validation";
public static final String JPA = "org.jboss.shamrock.jpa";
public static final String SCHEDULER = "org.jboss.shamrock.scheduler";
public static final String VERTX = "org.jboss.shamrock.vertx";

private final Set<String> capabilities;

Expand All @@ -40,4 +52,8 @@ public Capabilities(Set<String> capabilities) {
this.capabilities = capabilities;
}

public Set<String> getCapabilities() {
return capabilities;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package org.jboss.shamrock.deployment.builditem;

import org.jboss.builder.item.SimpleBuildItem;

public final class HttpServerBuiltItem extends SimpleBuildItem {

private final String host;

private final Integer port;

public HttpServerBuiltItem(String host, Integer port) {
this.host = host;
this.port = port;
}

public String getHost() {
return host;
}

public Integer getPort() {
return port;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@
package org.jboss.shamrock.deployment.builditem;

import org.jboss.builder.item.MultiBuildItem;
import org.jboss.shamrock.runtime.StartupEvent;

/**
* A symbolic class that represents a service start.
* <p>
* {@link StartupEvent} is fired after all services are started.
*/
public final class ServiceStartBuildItem extends MultiBuildItem {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,17 @@
import static org.jboss.protean.gizmo.MethodDescriptor.ofConstructor;
import static org.jboss.protean.gizmo.MethodDescriptor.ofMethod;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Modifier;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.Optional;
import java.util.Properties;
import java.util.stream.Collectors;

import javax.inject.Inject;

import org.jboss.builder.Version;
import org.jboss.protean.gizmo.CatchBlockCreator;
import org.jboss.protean.gizmo.ClassCreator;
import org.jboss.protean.gizmo.FieldCreator;
Expand All @@ -31,8 +38,10 @@
import org.jboss.protean.gizmo.ResultHandle;
import org.jboss.protean.gizmo.TryBlock;
import org.jboss.shamrock.annotations.BuildStep;
import org.jboss.shamrock.deployment.Capabilities;
import org.jboss.shamrock.deployment.ClassOutput;
import org.jboss.shamrock.deployment.builditem.ClassOutputBuildItem;
import org.jboss.shamrock.deployment.builditem.HttpServerBuiltItem;
import org.jboss.shamrock.deployment.builditem.MainBytecodeRecorderBuildItem;
import org.jboss.shamrock.deployment.builditem.MainClassBuildItem;
import org.jboss.shamrock.deployment.builditem.StaticBytecodeRecorderBuildItem;
Expand All @@ -44,15 +53,18 @@

class MainClassBuildStep {

private static final AtomicInteger COUNT = new AtomicInteger();
private static final String APP_CLASS = "org.jboss.shamrock.runner.ApplicationImpl";
private static final String MAIN_CLASS = "org.jboss.shamrock.runner.GeneratedMain";
private static final String STARTUP_CONTEXT = "STARTUP_CONTEXT";


@Inject
Capabilities capabilities;

@BuildStep
MainClassBuildItem build(List<StaticBytecodeRecorderBuildItem> staticInitTasks,
List<MainBytecodeRecorderBuildItem> mainMethod,
List<SystemPropertyBuildItem> properties,
Optional<HttpServerBuiltItem> httpServer,
ClassOutputBuildItem classOutput) {

// Application class
Expand Down Expand Up @@ -110,7 +122,16 @@ MainClassBuildItem build(List<StaticBytecodeRecorderBuildItem> staticInitTasks,
tryBlock.invokeInterfaceMethod(ofMethod(StartupTask.class, "deploy", void.class, StartupContext.class), dup, startupContext);
}
}
tryBlock.invokeStaticMethod(ofMethod(Timing.class, "printStartupTime", void.class));

// Startup log messages
ResultHandle capabilitiesHandle = tryBlock.load(capabilities.getCapabilities()
.stream()
.map(c -> c.contains(".") ? c.substring(c.lastIndexOf(".") + 1) : c)
.collect(Collectors.joining(", ")));
ResultHandle serverHandle = httpServer.isPresent() ? tryBlock.load("Listening on: http://" + httpServer.get()
.getHost() + ":" + httpServer.get().getPort()) : tryBlock.load("");
tryBlock.invokeStaticMethod(ofMethod(Timing.class, "printStartupTime", void.class, String.class, String.class, String.class),
tryBlock.load(Version.getVersion()), capabilitiesHandle, serverHandle);

cb = tryBlock.addCatch(Throwable.class);
cb.invokeVirtualMethod(ofMethod(Throwable.class, "printStackTrace", void.class), cb.getCaughtException());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,10 @@ public static void restart() {
bootStartTime = System.nanoTime();
}

public static void printStartupTime() {
public static void printStartupTime(String version, String capabilities, String httpServer) {
final long time = System.nanoTime() - bootStartTime + 500;
Logger.getLogger("org.jboss.shamrock").infof("Shamrock started in %d.%03dms", Long.valueOf(time / 1_000_000), Long.valueOf(time % 1_000_000 / 1_000));
Logger.getLogger("org.jboss.shamrock").infof("Shamrock %s started in %d.%03dms. %s", version, Long.valueOf(time / 1_000_000), Long.valueOf(time % 1_000_000 / 1_000), httpServer);
Logger.getLogger("org.jboss.shamrock").infof("Installed capabilities: [%s]", capabilities);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import org.jboss.shamrock.beanvalidation.runtime.ValidatorProvider;
import org.jboss.shamrock.beanvalidation.runtime.ValidatorTemplate;
import org.jboss.shamrock.beanvalidation.runtime.interceptor.MethodValidationInterceptor;
import org.jboss.shamrock.deployment.Capabilities;
import org.jboss.shamrock.deployment.builditem.AdditionalBeanBuildItem;
import org.jboss.shamrock.deployment.builditem.CombinedIndexBuildItem;
import org.jboss.shamrock.deployment.builditem.HotDeploymentConfigFileBuildItem;
Expand Down Expand Up @@ -83,7 +84,7 @@ void registerAdditionalBeans(BuildProducer<AdditionalBeanBuildItem> additionalBe
}
}

@BuildStep
@BuildStep(providesCapabilities = Capabilities.BEAN_VALIDATION)
@Record(STATIC_INIT)
public void build(ValidatorTemplate template, RecorderContext recorder,
BuildProducer<ReflectiveFieldBuildItem> reflectiveFields,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.jboss.shamrock.annotations.BuildProducer;
import org.jboss.shamrock.annotations.BuildStep;
import org.jboss.shamrock.arc.deployment.AnnotationsTransformerBuildItem;
import org.jboss.shamrock.deployment.Capabilities;
import org.jboss.shamrock.deployment.builditem.AdditionalBeanBuildItem;
import org.jboss.shamrock.deployment.builditem.CombinedIndexBuildItem;
import org.jboss.shamrock.deployment.builditem.substrate.ReflectiveClassBuildItem;
Expand Down Expand Up @@ -76,7 +77,7 @@ SubstrateSystemPropertyBuildItem disableJmx() {
return new SubstrateSystemPropertyBuildItem("archaius.dynamicPropertyFactory.registerConfigWithJMX", "false");
}

@BuildStep
@BuildStep(providesCapabilities = Capabilities.MP_FAULT_TOLERANCE)
public void build(BuildProducer<AnnotationsTransformerBuildItem> annotationsTransformer) throws Exception {

IndexView index = combinedIndexBuildItem.getIndex();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import org.eclipse.microprofile.config.inject.ConfigProperty;
import org.jboss.shamrock.annotations.BuildStep;
import org.jboss.shamrock.deployment.Capabilities;
import org.jboss.shamrock.deployment.builditem.AdditionalBeanBuildItem;
import org.jboss.shamrock.health.runtime.HealthServlet;
import org.jboss.shamrock.undertow.ServletBuildItem;
Expand All @@ -44,7 +45,7 @@ ServletBuildItem produceServlet() {
return servletBuildItem;
}

@BuildStep
@BuildStep(providesCapabilities = Capabilities.MP_HEALTH)
List<AdditionalBeanBuildItem> produceCdi() {
return Arrays.asList(
new AdditionalBeanBuildItem(SmallRyeHealthReporter.class),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
import org.jboss.shamrock.annotations.BuildProducer;
import org.jboss.shamrock.annotations.BuildStep;
import org.jboss.shamrock.annotations.Record;
import org.jboss.shamrock.deployment.Capabilities;
import org.jboss.shamrock.deployment.builditem.BeanContainerBuildItem;
import org.jboss.shamrock.deployment.builditem.CombinedIndexBuildItem;
import org.jboss.shamrock.deployment.builditem.ProxyUnwrapperBuildItem;
Expand Down Expand Up @@ -368,7 +369,7 @@ void registerProviders(BuildProducer<ReflectiveClassBuildItem> reflectiveClass,
}

@Record(STATIC_INIT)
@BuildStep
@BuildStep(providesCapabilities = Capabilities.JAX_RS)
void integrate(JaxrsTemplate template, BeanContainerBuildItem beanContainerBuildItem, List<ProxyUnwrapperBuildItem> proxyUnwrappers) {
List<Function<Object, Object>> unwrappers = new ArrayList<>();
for (ProxyUnwrapperBuildItem i : proxyUnwrappers) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ void handleNativeImageImportSql(BuildProducer<SubstrateResourceBuildItem> resour
}


@BuildStep
@BuildStep(providesCapabilities = Capabilities.JPA)
@Record(STATIC_INIT)
public BeanContainerListenerBuildItem build(RecorderContext recorder, JPADeploymentTemplate template,
List<PersistenceUnitDescriptorBuildItem> descItems, CombinedIndexBuildItem index,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.jboss.shamrock.annotations.BuildProducer;
import org.jboss.shamrock.annotations.BuildStep;
import org.jboss.shamrock.annotations.Record;
import org.jboss.shamrock.deployment.Capabilities;
import org.jboss.shamrock.deployment.builditem.AdditionalBeanBuildItem;
import org.jboss.shamrock.deployment.builditem.BeanArchiveIndexBuildItem;
import org.jboss.shamrock.deployment.builditem.BeanContainerBuildItem;
Expand Down Expand Up @@ -81,7 +82,7 @@ void beans( BuildProducer<AdditionalBeanBuildItem> additionalBeans) {
MetricsServlet.class));
}

@BuildStep
@BuildStep(providesCapabilities = Capabilities.MP_METRICS)
@Record(STATIC_INIT)
public void build(BeanContainerBuildItem beanContainerBuildItem,
MetricsDeploymentTemplate metrics,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.jboss.jandex.IndexView;
import org.jboss.shamrock.annotations.BuildStep;
import org.jboss.shamrock.annotations.Record;
import org.jboss.shamrock.deployment.Capabilities;
import org.jboss.shamrock.deployment.builditem.AdditionalBeanBuildItem;
import org.jboss.shamrock.deployment.builditem.ApplicationArchivesBuildItem;
import org.jboss.shamrock.deployment.builditem.CombinedIndexBuildItem;
Expand Down Expand Up @@ -86,7 +87,7 @@ List<AdditionalBeanBuildItem> beans() {
new AdditionalBeanBuildItem(OpenApiDocumentProducer.class));
}

@BuildStep
@BuildStep(providesCapabilities = Capabilities.MP_OPENAPI)
@Record(STATIC_INIT)
public BeanContainerListenerBuildItem build(OpenApiDeploymentTemplate template,
ApplicationArchivesBuildItem archivesBuildItem,
Expand Down
Loading

0 comments on commit 98156d8

Please sign in to comment.